Skip to content

Commit 991c444

Browse files
committed
pg_dump: Further reorganize getTableAttrs()
After further discussion after daa9fe8, reorder the version-specific sections from oldest to newest. Also, remove the variable assignments from PQfnumber() to reduce vertical space. Reviewed-by: Fabien COELHO <coelho@cri.ensmp.fr> Discussion: https://www.postgresql.org/message-id/flat/6594334b-40fd-14f1-6bc5-877afa3feed5@2ndquadrant.com
1 parent a5cd704 commit 991c444

File tree

1 file changed

+54
-95
lines changed

1 file changed

+54
-95
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 54 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -8419,35 +8419,14 @@ void
84198419
getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
84208420
{
84218421
DumpOptions *dopt = fout->dopt;
8422-
int i,
8423-
j;
84248422
PQExpBuffer q = createPQExpBuffer();
8425-
int i_attnum;
8426-
int i_attname;
8427-
int i_atttypname;
8428-
int i_atttypmod;
8429-
int i_attstattarget;
8430-
int i_attstorage;
8431-
int i_typstorage;
8432-
int i_attnotnull;
8433-
int i_atthasdef;
8434-
int i_attidentity;
8435-
int i_attgenerated;
8436-
int i_attisdropped;
8437-
int i_attlen;
8438-
int i_attalign;
8439-
int i_attislocal;
8440-
int i_attoptions;
8441-
int i_attcollation;
8442-
int i_attfdwoptions;
8443-
int i_attmissingval;
8444-
PGresult *res;
8445-
int ntups;
8446-
bool hasdefaults;
84478423

8448-
for (i = 0; i < numTables; i++)
8424+
for (int i = 0; i < numTables; i++)
84498425
{
84508426
TableInfo *tbinfo = &tblinfo[i];
8427+
PGresult *res;
8428+
int ntups;
8429+
bool hasdefaults;
84518430

84528431
/* Don't bother to collect info for sequences */
84538432
if (tbinfo->relkind == RELKIND_SEQUENCE)
@@ -8485,27 +8464,27 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
84858464
"a.attislocal,\n"
84868465
"pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n");
84878466

8488-
if (fout->remoteVersion >= 120000)
8489-
appendPQExpBufferStr(q,
8490-
"a.attgenerated,\n");
8491-
else
8492-
appendPQExpBufferStr(q,
8493-
"'' AS attgenerated,\n");
8494-
8495-
if (fout->remoteVersion >= 110000)
8467+
if (fout->remoteVersion >= 90000)
84968468
appendPQExpBufferStr(q,
8497-
"CASE WHEN a.atthasmissing AND NOT a.attisdropped "
8498-
"THEN a.attmissingval ELSE null END AS attmissingval,\n");
8469+
"array_to_string(a.attoptions, ', ') AS attoptions,\n");
84998470
else
85008471
appendPQExpBufferStr(q,
8501-
"NULL AS attmissingval,\n");
8472+
"'' AS attoptions,\n");
85028473

8503-
if (fout->remoteVersion >= 100000)
8474+
if (fout->remoteVersion >= 90100)
8475+
{
8476+
/*
8477+
* Since we only want to dump COLLATE clauses for attributes whose
8478+
* collation is different from their type's default, we use a CASE
8479+
* here to suppress uninteresting attcollations cheaply.
8480+
*/
85048481
appendPQExpBufferStr(q,
8505-
"a.attidentity,\n");
8482+
"CASE WHEN a.attcollation <> t.typcollation "
8483+
"THEN a.attcollation ELSE 0 END AS attcollation,\n");
8484+
}
85068485
else
85078486
appendPQExpBufferStr(q,
8508-
"'' AS attidentity,\n");
8487+
"0 AS attcollation,\n");
85098488

85108489
if (fout->remoteVersion >= 90200)
85118490
appendPQExpBufferStr(q,
@@ -8519,27 +8498,27 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
85198498
appendPQExpBufferStr(q,
85208499
"'' AS attfdwoptions,\n");
85218500

8522-
if (fout->remoteVersion >= 90100)
8523-
{
8524-
/*
8525-
* Since we only want to dump COLLATE clauses for attributes whose
8526-
* collation is different from their type's default, we use a CASE
8527-
* here to suppress uninteresting attcollations cheaply.
8528-
*/
8501+
if (fout->remoteVersion >= 100000)
85298502
appendPQExpBufferStr(q,
8530-
"CASE WHEN a.attcollation <> t.typcollation "
8531-
"THEN a.attcollation ELSE 0 END AS attcollation,\n");
8532-
}
8503+
"a.attidentity,\n");
85338504
else
85348505
appendPQExpBufferStr(q,
8535-
"0 AS attcollation,\n");
8506+
"'' AS attidentity,\n");
85368507

8537-
if (fout->remoteVersion >= 90000)
8508+
if (fout->remoteVersion >= 110000)
85388509
appendPQExpBufferStr(q,
8539-
"array_to_string(a.attoptions, ', ') AS attoptions\n");
8510+
"CASE WHEN a.atthasmissing AND NOT a.attisdropped "
8511+
"THEN a.attmissingval ELSE null END AS attmissingval,\n");
85408512
else
85418513
appendPQExpBufferStr(q,
8542-
"'' AS attoptions\n");
8514+
"NULL AS attmissingval,\n");
8515+
8516+
if (fout->remoteVersion >= 120000)
8517+
appendPQExpBufferStr(q,
8518+
"a.attgenerated\n");
8519+
else
8520+
appendPQExpBufferStr(q,
8521+
"'' AS attgenerated\n");
85438522

85448523
/* need left join here to not fail on dropped columns ... */
85458524
appendPQExpBuffer(q,
@@ -8554,26 +8533,6 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
85548533

85558534
ntups = PQntuples(res);
85568535

8557-
i_attnum = PQfnumber(res, "attnum");
8558-
i_attname = PQfnumber(res, "attname");
8559-
i_atttypname = PQfnumber(res, "atttypname");
8560-
i_atttypmod = PQfnumber(res, "atttypmod");
8561-
i_attstattarget = PQfnumber(res, "attstattarget");
8562-
i_attstorage = PQfnumber(res, "attstorage");
8563-
i_typstorage = PQfnumber(res, "typstorage");
8564-
i_attnotnull = PQfnumber(res, "attnotnull");
8565-
i_atthasdef = PQfnumber(res, "atthasdef");
8566-
i_attidentity = PQfnumber(res, "attidentity");
8567-
i_attgenerated = PQfnumber(res, "attgenerated");
8568-
i_attisdropped = PQfnumber(res, "attisdropped");
8569-
i_attlen = PQfnumber(res, "attlen");
8570-
i_attalign = PQfnumber(res, "attalign");
8571-
i_attislocal = PQfnumber(res, "attislocal");
8572-
i_attoptions = PQfnumber(res, "attoptions");
8573-
i_attcollation = PQfnumber(res, "attcollation");
8574-
i_attfdwoptions = PQfnumber(res, "attfdwoptions");
8575-
i_attmissingval = PQfnumber(res, "attmissingval");
8576-
85778536
tbinfo->numatts = ntups;
85788537
tbinfo->attnames = (char **) pg_malloc(ntups * sizeof(char *));
85798538
tbinfo->atttypnames = (char **) pg_malloc(ntups * sizeof(char *));
@@ -8596,31 +8555,31 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
85968555
tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
85978556
hasdefaults = false;
85988557

8599-
for (j = 0; j < ntups; j++)
8558+
for (int j = 0; j < ntups; j++)
86008559
{
8601-
if (j + 1 != atoi(PQgetvalue(res, j, i_attnum)))
8560+
if (j + 1 != atoi(PQgetvalue(res, j, PQfnumber(res, "attnum"))))
86028561
fatal("invalid column numbering in table \"%s\"",
86038562
tbinfo->dobj.name);
8604-
tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, i_attname));
8605-
tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, i_atttypname));
8606-
tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
8607-
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
8608-
tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
8609-
tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
8610-
tbinfo->attidentity[j] = *(PQgetvalue(res, j, i_attidentity));
8611-
tbinfo->attgenerated[j] = *(PQgetvalue(res, j, i_attgenerated));
8563+
tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attname")));
8564+
tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "atttypname")));
8565+
tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "atttypmod")));
8566+
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "attstattarget")));
8567+
tbinfo->attstorage[j] = *(PQgetvalue(res, j, PQfnumber(res, "attstorage")));
8568+
tbinfo->typstorage[j] = *(PQgetvalue(res, j, PQfnumber(res, "typstorage")));
8569+
tbinfo->attidentity[j] = *(PQgetvalue(res, j, PQfnumber(res, "attidentity")));
8570+
tbinfo->attgenerated[j] = *(PQgetvalue(res, j, PQfnumber(res, "attgenerated")));
86128571
tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
8613-
tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
8614-
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
8615-
tbinfo->attalign[j] = *(PQgetvalue(res, j, i_attalign));
8616-
tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
8617-
tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
8618-
tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, i_attoptions));
8619-
tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, i_attcollation));
8620-
tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, i_attfdwoptions));
8621-
tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, i_attmissingval));
8572+
tbinfo->attisdropped[j] = (PQgetvalue(res, j, PQfnumber(res, "attisdropped"))[0] == 't');
8573+
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "attlen")));
8574+
tbinfo->attalign[j] = *(PQgetvalue(res, j, PQfnumber(res, "attalign")));
8575+
tbinfo->attislocal[j] = (PQgetvalue(res, j, PQfnumber(res, "attislocal"))[0] == 't');
8576+
tbinfo->notnull[j] = (PQgetvalue(res, j, PQfnumber(res, "attnotnull"))[0] == 't');
8577+
tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attoptions")));
8578+
tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, PQfnumber(res, "attcollation")));
8579+
tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attfdwoptions")));
8580+
tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attmissingval")));
86228581
tbinfo->attrdefs[j] = NULL; /* fix below */
8623-
if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
8582+
if (PQgetvalue(res, j, PQfnumber(res, "atthasdef"))[0] == 't')
86248583
hasdefaults = true;
86258584
/* these flags will be set in flagInhAttrs() */
86268585
tbinfo->inhNotNull[j] = false;
@@ -8651,7 +8610,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
86518610
numDefaults = PQntuples(res);
86528611
attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
86538612

8654-
for (j = 0; j < numDefaults; j++)
8613+
for (int j = 0; j < numDefaults; j++)
86558614
{
86568615
int adnum;
86578616

@@ -8783,7 +8742,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
87838742
constrs = (ConstraintInfo *) pg_malloc(numConstrs * sizeof(ConstraintInfo));
87848743
tbinfo->checkexprs = constrs;
87858744

8786-
for (j = 0; j < numConstrs; j++)
8745+
for (int j = 0; j < numConstrs; j++)
87878746
{
87888747
bool validated = PQgetvalue(res, j, 5)[0] == 't';
87898748

0 commit comments

Comments
 (0)