Skip to content

Commit d782d59

Browse files
Avoid invoking PQfnumber in loop constructs
When looping over the resultset from a SQL query, extracting the field number before the loop body to avoid repeated calls to PQfnumber is an established pattern. On very wide tables this can have a performance impact, but it wont be noticeable in the common case. This fixes a few queries which were extracting the field number in the loop body. Author: Hou Zhijie <houzj.fnst@fujitsu.com> Reviewed-by: Nathan Bossart <bossartn@amazon.com> Discussion: https://postgr.es/m/OS0PR01MB57164C392783F29F6D0ECA0B94139@OS0PR01MB5716.jpnprd01.prod.outlook.com
1 parent abc0910 commit d782d59

File tree

1 file changed

+71
-23
lines changed

1 file changed

+71
-23
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8696,6 +8696,26 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
86968696
{
86978697
DumpOptions *dopt = fout->dopt;
86988698
PQExpBuffer q = createPQExpBuffer();
8699+
int i_attnum;
8700+
int i_attname;
8701+
int i_atttypname;
8702+
int i_atttypmod;
8703+
int i_attstattarget;
8704+
int i_attstorage;
8705+
int i_typstorage;
8706+
int i_attidentity;
8707+
int i_attgenerated;
8708+
int i_attisdropped;
8709+
int i_attlen;
8710+
int i_attalign;
8711+
int i_attislocal;
8712+
int i_attnotnull;
8713+
int i_attoptions;
8714+
int i_attcollation;
8715+
int i_attcompression;
8716+
int i_attfdwoptions;
8717+
int i_attmissingval;
8718+
int i_atthasdef;
86998719

87008720
for (int i = 0; i < numTables; i++)
87018721
{
@@ -8839,32 +8859,53 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
88398859
tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
88408860
hasdefaults = false;
88418861

8862+
i_attnum = PQfnumber(res, "attnum");
8863+
i_attname = PQfnumber(res, "attname");
8864+
i_atttypname = PQfnumber(res, "atttypname");
8865+
i_atttypmod = PQfnumber(res, "atttypmod");
8866+
i_attstattarget = PQfnumber(res, "attstattarget");
8867+
i_attstorage = PQfnumber(res, "attstorage");
8868+
i_typstorage = PQfnumber(res, "typstorage");
8869+
i_attidentity = PQfnumber(res, "attidentity");
8870+
i_attgenerated = PQfnumber(res, "attgenerated");
8871+
i_attisdropped = PQfnumber(res, "attisdropped");
8872+
i_attlen = PQfnumber(res, "attlen");
8873+
i_attalign = PQfnumber(res, "attalign");
8874+
i_attislocal = PQfnumber(res, "attislocal");
8875+
i_attnotnull = PQfnumber(res, "attnotnull");
8876+
i_attoptions = PQfnumber(res, "attoptions");
8877+
i_attcollation = PQfnumber(res, "attcollation");
8878+
i_attcompression = PQfnumber(res, "attcompression");
8879+
i_attfdwoptions = PQfnumber(res, "attfdwoptions");
8880+
i_attmissingval = PQfnumber(res, "attmissingval");
8881+
i_atthasdef = PQfnumber(res, "atthasdef");
8882+
88428883
for (int j = 0; j < ntups; j++)
88438884
{
8844-
if (j + 1 != atoi(PQgetvalue(res, j, PQfnumber(res, "attnum"))))
8885+
if (j + 1 != atoi(PQgetvalue(res, j, i_attnum)))
88458886
fatal("invalid column numbering in table \"%s\"",
88468887
tbinfo->dobj.name);
8847-
tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attname")));
8848-
tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "atttypname")));
8849-
tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "atttypmod")));
8850-
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "attstattarget")));
8851-
tbinfo->attstorage[j] = *(PQgetvalue(res, j, PQfnumber(res, "attstorage")));
8852-
tbinfo->typstorage[j] = *(PQgetvalue(res, j, PQfnumber(res, "typstorage")));
8853-
tbinfo->attidentity[j] = *(PQgetvalue(res, j, PQfnumber(res, "attidentity")));
8854-
tbinfo->attgenerated[j] = *(PQgetvalue(res, j, PQfnumber(res, "attgenerated")));
8888+
tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, i_attname));
8889+
tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, i_atttypname));
8890+
tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
8891+
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
8892+
tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
8893+
tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
8894+
tbinfo->attidentity[j] = *(PQgetvalue(res, j, i_attidentity));
8895+
tbinfo->attgenerated[j] = *(PQgetvalue(res, j, i_attgenerated));
88558896
tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
8856-
tbinfo->attisdropped[j] = (PQgetvalue(res, j, PQfnumber(res, "attisdropped"))[0] == 't');
8857-
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "attlen")));
8858-
tbinfo->attalign[j] = *(PQgetvalue(res, j, PQfnumber(res, "attalign")));
8859-
tbinfo->attislocal[j] = (PQgetvalue(res, j, PQfnumber(res, "attislocal"))[0] == 't');
8860-
tbinfo->notnull[j] = (PQgetvalue(res, j, PQfnumber(res, "attnotnull"))[0] == 't');
8861-
tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attoptions")));
8862-
tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, PQfnumber(res, "attcollation")));
8863-
tbinfo->attcompression[j] = *(PQgetvalue(res, j, PQfnumber(res, "attcompression")));
8864-
tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attfdwoptions")));
8865-
tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attmissingval")));
8897+
tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
8898+
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
8899+
tbinfo->attalign[j] = *(PQgetvalue(res, j, i_attalign));
8900+
tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
8901+
tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
8902+
tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, i_attoptions));
8903+
tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, i_attcollation));
8904+
tbinfo->attcompression[j] = *(PQgetvalue(res, j, i_attcompression));
8905+
tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, i_attfdwoptions));
8906+
tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, i_attmissingval));
88668907
tbinfo->attrdefs[j] = NULL; /* fix below */
8867-
if (PQgetvalue(res, j, PQfnumber(res, "atthasdef"))[0] == 't')
8908+
if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
88688909
hasdefaults = true;
88698910
/* these flags will be set in flagInhAttrs() */
88708911
tbinfo->inhNotNull[j] = false;
@@ -10712,6 +10753,8 @@ dumpEnumType(Archive *fout, const TypeInfo *tyinfo)
1071210753
char *qtypname;
1071310754
char *qualtypname;
1071410755
char *label;
10756+
int i_enumlabel;
10757+
int i_oid;
1071510758

1071610759
if (fout->remoteVersion >= 90100)
1071710760
appendPQExpBuffer(query, "SELECT oid, enumlabel "
@@ -10749,10 +10792,12 @@ dumpEnumType(Archive *fout, const TypeInfo *tyinfo)
1074910792

1075010793
if (!dopt->binary_upgrade)
1075110794
{
10795+
i_enumlabel = PQfnumber(res, "enumlabel");
10796+
1075210797
/* Labels with server-assigned oids */
1075310798
for (i = 0; i < num; i++)
1075410799
{
10755-
label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
10800+
label = PQgetvalue(res, i, i_enumlabel);
1075610801
if (i > 0)
1075710802
appendPQExpBufferChar(q, ',');
1075810803
appendPQExpBufferStr(q, "\n ");
@@ -10764,11 +10809,14 @@ dumpEnumType(Archive *fout, const TypeInfo *tyinfo)
1076410809

1076510810
if (dopt->binary_upgrade)
1076610811
{
10812+
i_oid = PQfnumber(res, "oid");
10813+
i_enumlabel = PQfnumber(res, "enumlabel");
10814+
1076710815
/* Labels with dump-assigned (preserved) oids */
1076810816
for (i = 0; i < num; i++)
1076910817
{
10770-
enum_oid = atooid(PQgetvalue(res, i, PQfnumber(res, "oid")));
10771-
label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
10818+
enum_oid = atooid(PQgetvalue(res, i, i_oid));
10819+
label = PQgetvalue(res, i, i_enumlabel);
1077210820

1077310821
if (i == 0)
1077410822
appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");

0 commit comments

Comments
 (0)