mirror of
https://github.com/postgres/postgres.git
synced 2025-05-05 09:19:17 +03:00
pg_dump: Further reorganize getTableAttrs()
After further discussion after daa9fe8a5264a3f192efa5ddee8fb011ad9da365, 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
This commit is contained in:
parent
a5cd7047e7
commit
991c444e7a
@ -8419,36 +8419,15 @@ void
|
|||||||
getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
||||||
{
|
{
|
||||||
DumpOptions *dopt = fout->dopt;
|
DumpOptions *dopt = fout->dopt;
|
||||||
int i,
|
|
||||||
j;
|
|
||||||
PQExpBuffer q = createPQExpBuffer();
|
PQExpBuffer q = createPQExpBuffer();
|
||||||
int i_attnum;
|
|
||||||
int i_attname;
|
for (int i = 0; i < numTables; i++)
|
||||||
int i_atttypname;
|
{
|
||||||
int i_atttypmod;
|
TableInfo *tbinfo = &tblinfo[i];
|
||||||
int i_attstattarget;
|
|
||||||
int i_attstorage;
|
|
||||||
int i_typstorage;
|
|
||||||
int i_attnotnull;
|
|
||||||
int i_atthasdef;
|
|
||||||
int i_attidentity;
|
|
||||||
int i_attgenerated;
|
|
||||||
int i_attisdropped;
|
|
||||||
int i_attlen;
|
|
||||||
int i_attalign;
|
|
||||||
int i_attislocal;
|
|
||||||
int i_attoptions;
|
|
||||||
int i_attcollation;
|
|
||||||
int i_attfdwoptions;
|
|
||||||
int i_attmissingval;
|
|
||||||
PGresult *res;
|
PGresult *res;
|
||||||
int ntups;
|
int ntups;
|
||||||
bool hasdefaults;
|
bool hasdefaults;
|
||||||
|
|
||||||
for (i = 0; i < numTables; i++)
|
|
||||||
{
|
|
||||||
TableInfo *tbinfo = &tblinfo[i];
|
|
||||||
|
|
||||||
/* Don't bother to collect info for sequences */
|
/* Don't bother to collect info for sequences */
|
||||||
if (tbinfo->relkind == RELKIND_SEQUENCE)
|
if (tbinfo->relkind == RELKIND_SEQUENCE)
|
||||||
continue;
|
continue;
|
||||||
@ -8485,39 +8464,12 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
"a.attislocal,\n"
|
"a.attislocal,\n"
|
||||||
"pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n");
|
"pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n");
|
||||||
|
|
||||||
if (fout->remoteVersion >= 120000)
|
if (fout->remoteVersion >= 90000)
|
||||||
appendPQExpBufferStr(q,
|
appendPQExpBufferStr(q,
|
||||||
"a.attgenerated,\n");
|
"array_to_string(a.attoptions, ', ') AS attoptions,\n");
|
||||||
else
|
else
|
||||||
appendPQExpBufferStr(q,
|
appendPQExpBufferStr(q,
|
||||||
"'' AS attgenerated,\n");
|
"'' AS attoptions,\n");
|
||||||
|
|
||||||
if (fout->remoteVersion >= 110000)
|
|
||||||
appendPQExpBufferStr(q,
|
|
||||||
"CASE WHEN a.atthasmissing AND NOT a.attisdropped "
|
|
||||||
"THEN a.attmissingval ELSE null END AS attmissingval,\n");
|
|
||||||
else
|
|
||||||
appendPQExpBufferStr(q,
|
|
||||||
"NULL AS attmissingval,\n");
|
|
||||||
|
|
||||||
if (fout->remoteVersion >= 100000)
|
|
||||||
appendPQExpBufferStr(q,
|
|
||||||
"a.attidentity,\n");
|
|
||||||
else
|
|
||||||
appendPQExpBufferStr(q,
|
|
||||||
"'' AS attidentity,\n");
|
|
||||||
|
|
||||||
if (fout->remoteVersion >= 90200)
|
|
||||||
appendPQExpBufferStr(q,
|
|
||||||
"pg_catalog.array_to_string(ARRAY("
|
|
||||||
"SELECT pg_catalog.quote_ident(option_name) || "
|
|
||||||
"' ' || pg_catalog.quote_literal(option_value) "
|
|
||||||
"FROM pg_catalog.pg_options_to_table(attfdwoptions) "
|
|
||||||
"ORDER BY option_name"
|
|
||||||
"), E',\n ') AS attfdwoptions,\n");
|
|
||||||
else
|
|
||||||
appendPQExpBufferStr(q,
|
|
||||||
"'' AS attfdwoptions,\n");
|
|
||||||
|
|
||||||
if (fout->remoteVersion >= 90100)
|
if (fout->remoteVersion >= 90100)
|
||||||
{
|
{
|
||||||
@ -8534,12 +8486,39 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
appendPQExpBufferStr(q,
|
appendPQExpBufferStr(q,
|
||||||
"0 AS attcollation,\n");
|
"0 AS attcollation,\n");
|
||||||
|
|
||||||
if (fout->remoteVersion >= 90000)
|
if (fout->remoteVersion >= 90200)
|
||||||
appendPQExpBufferStr(q,
|
appendPQExpBufferStr(q,
|
||||||
"array_to_string(a.attoptions, ', ') AS attoptions\n");
|
"pg_catalog.array_to_string(ARRAY("
|
||||||
|
"SELECT pg_catalog.quote_ident(option_name) || "
|
||||||
|
"' ' || pg_catalog.quote_literal(option_value) "
|
||||||
|
"FROM pg_catalog.pg_options_to_table(attfdwoptions) "
|
||||||
|
"ORDER BY option_name"
|
||||||
|
"), E',\n ') AS attfdwoptions,\n");
|
||||||
else
|
else
|
||||||
appendPQExpBufferStr(q,
|
appendPQExpBufferStr(q,
|
||||||
"'' AS attoptions\n");
|
"'' AS attfdwoptions,\n");
|
||||||
|
|
||||||
|
if (fout->remoteVersion >= 100000)
|
||||||
|
appendPQExpBufferStr(q,
|
||||||
|
"a.attidentity,\n");
|
||||||
|
else
|
||||||
|
appendPQExpBufferStr(q,
|
||||||
|
"'' AS attidentity,\n");
|
||||||
|
|
||||||
|
if (fout->remoteVersion >= 110000)
|
||||||
|
appendPQExpBufferStr(q,
|
||||||
|
"CASE WHEN a.atthasmissing AND NOT a.attisdropped "
|
||||||
|
"THEN a.attmissingval ELSE null END AS attmissingval,\n");
|
||||||
|
else
|
||||||
|
appendPQExpBufferStr(q,
|
||||||
|
"NULL AS attmissingval,\n");
|
||||||
|
|
||||||
|
if (fout->remoteVersion >= 120000)
|
||||||
|
appendPQExpBufferStr(q,
|
||||||
|
"a.attgenerated\n");
|
||||||
|
else
|
||||||
|
appendPQExpBufferStr(q,
|
||||||
|
"'' AS attgenerated\n");
|
||||||
|
|
||||||
/* need left join here to not fail on dropped columns ... */
|
/* need left join here to not fail on dropped columns ... */
|
||||||
appendPQExpBuffer(q,
|
appendPQExpBuffer(q,
|
||||||
@ -8554,26 +8533,6 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
|
|
||||||
ntups = PQntuples(res);
|
ntups = PQntuples(res);
|
||||||
|
|
||||||
i_attnum = PQfnumber(res, "attnum");
|
|
||||||
i_attname = PQfnumber(res, "attname");
|
|
||||||
i_atttypname = PQfnumber(res, "atttypname");
|
|
||||||
i_atttypmod = PQfnumber(res, "atttypmod");
|
|
||||||
i_attstattarget = PQfnumber(res, "attstattarget");
|
|
||||||
i_attstorage = PQfnumber(res, "attstorage");
|
|
||||||
i_typstorage = PQfnumber(res, "typstorage");
|
|
||||||
i_attnotnull = PQfnumber(res, "attnotnull");
|
|
||||||
i_atthasdef = PQfnumber(res, "atthasdef");
|
|
||||||
i_attidentity = PQfnumber(res, "attidentity");
|
|
||||||
i_attgenerated = PQfnumber(res, "attgenerated");
|
|
||||||
i_attisdropped = PQfnumber(res, "attisdropped");
|
|
||||||
i_attlen = PQfnumber(res, "attlen");
|
|
||||||
i_attalign = PQfnumber(res, "attalign");
|
|
||||||
i_attislocal = PQfnumber(res, "attislocal");
|
|
||||||
i_attoptions = PQfnumber(res, "attoptions");
|
|
||||||
i_attcollation = PQfnumber(res, "attcollation");
|
|
||||||
i_attfdwoptions = PQfnumber(res, "attfdwoptions");
|
|
||||||
i_attmissingval = PQfnumber(res, "attmissingval");
|
|
||||||
|
|
||||||
tbinfo->numatts = ntups;
|
tbinfo->numatts = ntups;
|
||||||
tbinfo->attnames = (char **) pg_malloc(ntups * sizeof(char *));
|
tbinfo->attnames = (char **) pg_malloc(ntups * sizeof(char *));
|
||||||
tbinfo->atttypnames = (char **) pg_malloc(ntups * sizeof(char *));
|
tbinfo->atttypnames = (char **) pg_malloc(ntups * sizeof(char *));
|
||||||
@ -8596,31 +8555,31 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
|
tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
|
||||||
hasdefaults = false;
|
hasdefaults = false;
|
||||||
|
|
||||||
for (j = 0; j < ntups; j++)
|
for (int j = 0; j < ntups; j++)
|
||||||
{
|
{
|
||||||
if (j + 1 != atoi(PQgetvalue(res, j, i_attnum)))
|
if (j + 1 != atoi(PQgetvalue(res, j, PQfnumber(res, "attnum"))))
|
||||||
fatal("invalid column numbering in table \"%s\"",
|
fatal("invalid column numbering in table \"%s\"",
|
||||||
tbinfo->dobj.name);
|
tbinfo->dobj.name);
|
||||||
tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, i_attname));
|
tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attname")));
|
||||||
tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, i_atttypname));
|
tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "atttypname")));
|
||||||
tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
|
tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "atttypmod")));
|
||||||
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
|
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "attstattarget")));
|
||||||
tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
|
tbinfo->attstorage[j] = *(PQgetvalue(res, j, PQfnumber(res, "attstorage")));
|
||||||
tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
|
tbinfo->typstorage[j] = *(PQgetvalue(res, j, PQfnumber(res, "typstorage")));
|
||||||
tbinfo->attidentity[j] = *(PQgetvalue(res, j, i_attidentity));
|
tbinfo->attidentity[j] = *(PQgetvalue(res, j, PQfnumber(res, "attidentity")));
|
||||||
tbinfo->attgenerated[j] = *(PQgetvalue(res, j, i_attgenerated));
|
tbinfo->attgenerated[j] = *(PQgetvalue(res, j, PQfnumber(res, "attgenerated")));
|
||||||
tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
|
tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
|
||||||
tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
|
tbinfo->attisdropped[j] = (PQgetvalue(res, j, PQfnumber(res, "attisdropped"))[0] == 't');
|
||||||
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
|
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, PQfnumber(res, "attlen")));
|
||||||
tbinfo->attalign[j] = *(PQgetvalue(res, j, i_attalign));
|
tbinfo->attalign[j] = *(PQgetvalue(res, j, PQfnumber(res, "attalign")));
|
||||||
tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
|
tbinfo->attislocal[j] = (PQgetvalue(res, j, PQfnumber(res, "attislocal"))[0] == 't');
|
||||||
tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
|
tbinfo->notnull[j] = (PQgetvalue(res, j, PQfnumber(res, "attnotnull"))[0] == 't');
|
||||||
tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, i_attoptions));
|
tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attoptions")));
|
||||||
tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, i_attcollation));
|
tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, PQfnumber(res, "attcollation")));
|
||||||
tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, i_attfdwoptions));
|
tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attfdwoptions")));
|
||||||
tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, i_attmissingval));
|
tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attmissingval")));
|
||||||
tbinfo->attrdefs[j] = NULL; /* fix below */
|
tbinfo->attrdefs[j] = NULL; /* fix below */
|
||||||
if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
|
if (PQgetvalue(res, j, PQfnumber(res, "atthasdef"))[0] == 't')
|
||||||
hasdefaults = true;
|
hasdefaults = true;
|
||||||
/* these flags will be set in flagInhAttrs() */
|
/* these flags will be set in flagInhAttrs() */
|
||||||
tbinfo->inhNotNull[j] = false;
|
tbinfo->inhNotNull[j] = false;
|
||||||
@ -8651,7 +8610,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
numDefaults = PQntuples(res);
|
numDefaults = PQntuples(res);
|
||||||
attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
|
attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
|
||||||
|
|
||||||
for (j = 0; j < numDefaults; j++)
|
for (int j = 0; j < numDefaults; j++)
|
||||||
{
|
{
|
||||||
int adnum;
|
int adnum;
|
||||||
|
|
||||||
@ -8783,7 +8742,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
constrs = (ConstraintInfo *) pg_malloc(numConstrs * sizeof(ConstraintInfo));
|
constrs = (ConstraintInfo *) pg_malloc(numConstrs * sizeof(ConstraintInfo));
|
||||||
tbinfo->checkexprs = constrs;
|
tbinfo->checkexprs = constrs;
|
||||||
|
|
||||||
for (j = 0; j < numConstrs; j++)
|
for (int j = 0; j < numConstrs; j++)
|
||||||
{
|
{
|
||||||
bool validated = PQgetvalue(res, j, 5)[0] == 't';
|
bool validated = PQgetvalue(res, j, 5)[0] == 't';
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user