Skip to content

Commit daa9fe8

Browse files
committed
pg_dump: Reorganize getTableAttrs()
Instead of repeating the almost same large query in each version branch, use one query and add a few columns to the SELECT list depending on the version. This saves a lot of duplication. Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
1 parent a846e6d commit daa9fe8

File tree

1 file changed

+62
-136
lines changed

1 file changed

+62
-136
lines changed

src/bin/pg_dump/pg_dump.c

+62-136
Original file line numberDiff line numberDiff line change
@@ -8162,150 +8162,77 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
81628162

81638163
resetPQExpBuffer(q);
81648164

8165+
appendPQExpBuffer(q,
8166+
"SELECT\n"
8167+
"a.attnum,\n"
8168+
"a.attname,\n"
8169+
"a.atttypmod,\n"
8170+
"a.attstattarget,\n"
8171+
"a.attstorage,\n"
8172+
"t.typstorage,\n"
8173+
"a.attnotnull,\n"
8174+
"a.atthasdef,\n"
8175+
"a.attisdropped,\n"
8176+
"a.attlen,\n"
8177+
"a.attalign,\n"
8178+
"a.attislocal,\n"
8179+
"pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n");
8180+
81658181
if (fout->remoteVersion >= 110000)
8166-
{
8167-
/* atthasmissing and attmissingval are new in 11 */
8168-
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8169-
"a.attstattarget, a.attstorage, t.typstorage, "
8170-
"a.attnotnull, a.atthasdef, a.attisdropped, "
8171-
"a.attlen, a.attalign, a.attislocal, "
8172-
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8173-
"array_to_string(a.attoptions, ', ') AS attoptions, "
8174-
"CASE WHEN a.attcollation <> t.typcollation "
8175-
"THEN a.attcollation ELSE 0 END AS attcollation, "
8176-
"a.attidentity, "
8177-
"pg_catalog.array_to_string(ARRAY("
8178-
"SELECT pg_catalog.quote_ident(option_name) || "
8179-
"' ' || pg_catalog.quote_literal(option_value) "
8180-
"FROM pg_catalog.pg_options_to_table(attfdwoptions) "
8181-
"ORDER BY option_name"
8182-
"), E',\n ') AS attfdwoptions ,"
8182+
appendPQExpBuffer(q,
81838183
"CASE WHEN a.atthasmissing AND NOT a.attisdropped "
8184-
"THEN a.attmissingval ELSE null END AS attmissingval "
8185-
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8186-
"ON a.atttypid = t.oid "
8187-
"WHERE a.attrelid = '%u'::pg_catalog.oid "
8188-
"AND a.attnum > 0::pg_catalog.int2 "
8189-
"ORDER BY a.attnum",
8190-
tbinfo->dobj.catId.oid);
8191-
}
8192-
else if (fout->remoteVersion >= 100000)
8193-
{
8194-
/*
8195-
* attidentity is new in version 10.
8196-
*/
8197-
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8198-
"a.attstattarget, a.attstorage, t.typstorage, "
8199-
"a.attnotnull, a.atthasdef, a.attisdropped, "
8200-
"a.attlen, a.attalign, a.attislocal, "
8201-
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8202-
"array_to_string(a.attoptions, ', ') AS attoptions, "
8203-
"CASE WHEN a.attcollation <> t.typcollation "
8204-
"THEN a.attcollation ELSE 0 END AS attcollation, "
8205-
"a.attidentity, "
8206-
"pg_catalog.array_to_string(ARRAY("
8207-
"SELECT pg_catalog.quote_ident(option_name) || "
8208-
"' ' || pg_catalog.quote_literal(option_value) "
8209-
"FROM pg_catalog.pg_options_to_table(attfdwoptions) "
8210-
"ORDER BY option_name"
8211-
"), E',\n ') AS attfdwoptions ,"
8212-
"NULL as attmissingval "
8213-
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8214-
"ON a.atttypid = t.oid "
8215-
"WHERE a.attrelid = '%u'::pg_catalog.oid "
8216-
"AND a.attnum > 0::pg_catalog.int2 "
8217-
"ORDER BY a.attnum",
8218-
tbinfo->dobj.catId.oid);
8219-
}
8220-
else if (fout->remoteVersion >= 90200)
8221-
{
8222-
/*
8223-
* attfdwoptions is new in 9.2.
8224-
*/
8225-
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8226-
"a.attstattarget, a.attstorage, t.typstorage, "
8227-
"a.attnotnull, a.atthasdef, a.attisdropped, "
8228-
"a.attlen, a.attalign, a.attislocal, "
8229-
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8230-
"array_to_string(a.attoptions, ', ') AS attoptions, "
8231-
"CASE WHEN a.attcollation <> t.typcollation "
8232-
"THEN a.attcollation ELSE 0 END AS attcollation, "
8184+
"THEN a.attmissingval ELSE null END AS attmissingval,\n");
8185+
else
8186+
appendPQExpBuffer(q,
8187+
"NULL AS attmissingval,\n");
8188+
8189+
if (fout->remoteVersion >= 100000)
8190+
appendPQExpBuffer(q,
8191+
"a.attidentity,\n");
8192+
else
8193+
appendPQExpBuffer(q,
8194+
"'' AS attidentity,\n");
8195+
8196+
if (fout->remoteVersion >= 90200)
8197+
appendPQExpBuffer(q,
82338198
"pg_catalog.array_to_string(ARRAY("
82348199
"SELECT pg_catalog.quote_ident(option_name) || "
82358200
"' ' || pg_catalog.quote_literal(option_value) "
82368201
"FROM pg_catalog.pg_options_to_table(attfdwoptions) "
82378202
"ORDER BY option_name"
8238-
"), E',\n ') AS attfdwoptions, "
8239-
"NULL as attmissingval "
8240-
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8241-
"ON a.atttypid = t.oid "
8242-
"WHERE a.attrelid = '%u'::pg_catalog.oid "
8243-
"AND a.attnum > 0::pg_catalog.int2 "
8244-
"ORDER BY a.attnum",
8245-
tbinfo->dobj.catId.oid);
8246-
}
8247-
else if (fout->remoteVersion >= 90100)
8248-
{
8203+
"), E',\n ') AS attfdwoptions,\n");
8204+
else
8205+
appendPQExpBuffer(q,
8206+
"'' AS attfdwoptions,\n");
8207+
8208+
if (fout->remoteVersion >= 90100)
82498209
/*
8250-
* attcollation is new in 9.1. Since we only want to dump COLLATE
8251-
* clauses for attributes whose collation is different from their
8252-
* type's default, we use a CASE here to suppress uninteresting
8253-
* attcollations cheaply.
8210+
* Since we only want to dump COLLATE clauses for attributes whose
8211+
* collation is different from their type's default, we use a CASE
8212+
* here to suppress uninteresting attcollations cheaply.
82548213
*/
8255-
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8256-
"a.attstattarget, a.attstorage, t.typstorage, "
8257-
"a.attnotnull, a.atthasdef, a.attisdropped, "
8258-
"a.attlen, a.attalign, a.attislocal, "
8259-
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8260-
"array_to_string(a.attoptions, ', ') AS attoptions, "
8214+
appendPQExpBuffer(q,
82618215
"CASE WHEN a.attcollation <> t.typcollation "
8262-
"THEN a.attcollation ELSE 0 END AS attcollation, "
8263-
"NULL AS attfdwoptions, "
8264-
"NULL as attmissingval "
8265-
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8266-
"ON a.atttypid = t.oid "
8267-
"WHERE a.attrelid = '%u'::pg_catalog.oid "
8268-
"AND a.attnum > 0::pg_catalog.int2 "
8269-
"ORDER BY a.attnum",
8270-
tbinfo->dobj.catId.oid);
8271-
}
8272-
else if (fout->remoteVersion >= 90000)
8273-
{
8274-
/* attoptions is new in 9.0 */
8275-
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8276-
"a.attstattarget, a.attstorage, t.typstorage, "
8277-
"a.attnotnull, a.atthasdef, a.attisdropped, "
8278-
"a.attlen, a.attalign, a.attislocal, "
8279-
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8280-
"array_to_string(a.attoptions, ', ') AS attoptions, "
8281-
"0 AS attcollation, "
8282-
"NULL AS attfdwoptions, "
8283-
"NULL as attmissingval "
8284-
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8285-
"ON a.atttypid = t.oid "
8286-
"WHERE a.attrelid = '%u'::pg_catalog.oid "
8287-
"AND a.attnum > 0::pg_catalog.int2 "
8288-
"ORDER BY a.attnum",
8289-
tbinfo->dobj.catId.oid);
8290-
}
8216+
"THEN a.attcollation ELSE 0 END AS attcollation,\n");
82918217
else
8292-
{
8293-
/* need left join here to not fail on dropped columns ... */
8294-
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8295-
"a.attstattarget, a.attstorage, t.typstorage, "
8296-
"a.attnotnull, a.atthasdef, a.attisdropped, "
8297-
"a.attlen, a.attalign, a.attislocal, "
8298-
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8299-
"'' AS attoptions, 0 AS attcollation, "
8300-
"NULL AS attfdwoptions, "
8301-
"NULL as attmissingval "
8302-
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8303-
"ON a.atttypid = t.oid "
8304-
"WHERE a.attrelid = '%u'::pg_catalog.oid "
8305-
"AND a.attnum > 0::pg_catalog.int2 "
8306-
"ORDER BY a.attnum",
8307-
tbinfo->dobj.catId.oid);
8308-
}
8218+
appendPQExpBuffer(q,
8219+
"0 AS attcollation,\n");
8220+
8221+
if (fout->remoteVersion >= 90000)
8222+
appendPQExpBuffer(q,
8223+
"array_to_string(a.attoptions, ', ') AS attoptions\n");
8224+
else
8225+
appendPQExpBuffer(q,
8226+
"'' AS attoptions\n");
8227+
8228+
appendPQExpBuffer(q,
8229+
/* need left join here to not fail on dropped columns ... */
8230+
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8231+
"ON a.atttypid = t.oid\n"
8232+
"WHERE a.attrelid = '%u'::pg_catalog.oid "
8233+
"AND a.attnum > 0::pg_catalog.int2\n"
8234+
"ORDER BY a.attnum",
8235+
tbinfo->dobj.catId.oid);
83098236

83108237
res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
83118238

@@ -8363,7 +8290,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
83638290
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
83648291
tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
83658292
tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
8366-
tbinfo->attidentity[j] = (i_attidentity >= 0 ? *(PQgetvalue(res, j, i_attidentity)) : '\0');
8293+
tbinfo->attidentity[j] = *(PQgetvalue(res, j, i_attidentity));
83678294
tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
83688295
tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
83698296
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
@@ -16023,7 +15950,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1602315950
/*
1602415951
* Dump per-column attributes.
1602515952
*/
16026-
if (tbinfo->attoptions[j] && tbinfo->attoptions[j][0] != '\0')
15953+
if (tbinfo->attoptions[j][0] != '\0')
1602715954
{
1602815955
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
1602915956
qualrelname);
@@ -16037,7 +15964,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1603715964
* Dump per-column fdw options.
1603815965
*/
1603915966
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
16040-
tbinfo->attfdwoptions[j] &&
1604115967
tbinfo->attfdwoptions[j][0] != '\0')
1604215968
{
1604315969
appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",

0 commit comments

Comments
 (0)