Skip to content

Commit 6ebeeae

Browse files
Cache typarray for fast lookups in binary upgrade mode
When upgrading a large schema it adds significant overhead to perform individual catalog lookups per relation in order to retrieve Oid for preserving Oid calls. This instead adds the typarray to the TypeInfo cache which then allows for fast lookups using the existing API. A 35% reduction of pg_dump runtime in binary upgrade mode was observed with this change. Reviewed-by: Nathan Bossart <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent 4d5111b commit 6ebeeae

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/bin/pg_dump/pg_dump.c

+8-12
Original file line numberDiff line numberDiff line change
@@ -5412,23 +5412,15 @@ binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
54125412
Oid pg_type_array_oid;
54135413
Oid pg_type_multirange_oid;
54145414
Oid pg_type_multirange_array_oid;
5415+
TypeInfo *tinfo;
54155416

54165417
appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
54175418
appendPQExpBuffer(upgrade_buffer,
54185419
"SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
54195420
pg_type_oid);
54205421

5421-
appendPQExpBuffer(upgrade_query,
5422-
"SELECT typarray "
5423-
"FROM pg_catalog.pg_type "
5424-
"WHERE oid = '%u'::pg_catalog.oid;",
5425-
pg_type_oid);
5426-
5427-
res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5428-
5429-
pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5430-
5431-
PQclear(res);
5422+
tinfo = findTypeByOid(pg_type_oid);
5423+
pg_type_array_oid = tinfo->typarray;
54325424

54335425
if (!OidIsValid(pg_type_array_oid) && force_array_type)
54345426
pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
@@ -5910,6 +5902,7 @@ getTypes(Archive *fout)
59105902
int i_typtype;
59115903
int i_typisdefined;
59125904
int i_isarray;
5905+
int i_typarray;
59135906

59145907
/*
59155908
* we include even the built-in types because those may be used as array
@@ -5930,7 +5923,7 @@ getTypes(Archive *fout)
59305923
"typnamespace, typacl, "
59315924
"acldefault('T', typowner) AS acldefault, "
59325925
"typowner, "
5933-
"typelem, typrelid, "
5926+
"typelem, typrelid, typarray, "
59345927
"CASE WHEN typrelid = 0 THEN ' '::\"char\" "
59355928
"ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
59365929
"typtype, typisdefined, "
@@ -5957,6 +5950,7 @@ getTypes(Archive *fout)
59575950
i_typtype = PQfnumber(res, "typtype");
59585951
i_typisdefined = PQfnumber(res, "typisdefined");
59595952
i_isarray = PQfnumber(res, "isarray");
5953+
i_typarray = PQfnumber(res, "typarray");
59605954

59615955
for (i = 0; i < ntups; i++)
59625956
{
@@ -5989,6 +5983,8 @@ getTypes(Archive *fout)
59895983
else
59905984
tyinfo[i].isArray = false;
59915985

5986+
tyinfo[i].typarray = atooid(PQgetvalue(res, i, i_typarray));
5987+
59925988
if (tyinfo[i].typtype == TYPTYPE_MULTIRANGE)
59935989
tyinfo[i].isMultirange = true;
59945990
else

src/bin/pg_dump/pg_dump.h

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ typedef struct _typeInfo
200200
const char *rolname;
201201
Oid typelem;
202202
Oid typrelid;
203+
Oid typarray;
203204
char typrelkind; /* 'r', 'v', 'c', etc */
204205
char typtype; /* 'b', 'c', etc */
205206
bool isArray; /* true if auto-generated array type */

0 commit comments

Comments
 (0)