mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-27 08:39:28 +08:00
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>
This commit is contained in:
parent
a846e6d023
commit
daa9fe8a52
@ -8162,150 +8162,77 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
|
|
||||||
resetPQExpBuffer(q);
|
resetPQExpBuffer(q);
|
||||||
|
|
||||||
|
appendPQExpBuffer(q,
|
||||||
|
"SELECT\n"
|
||||||
|
"a.attnum,\n"
|
||||||
|
"a.attname,\n"
|
||||||
|
"a.atttypmod,\n"
|
||||||
|
"a.attstattarget,\n"
|
||||||
|
"a.attstorage,\n"
|
||||||
|
"t.typstorage,\n"
|
||||||
|
"a.attnotnull,\n"
|
||||||
|
"a.atthasdef,\n"
|
||||||
|
"a.attisdropped,\n"
|
||||||
|
"a.attlen,\n"
|
||||||
|
"a.attalign,\n"
|
||||||
|
"a.attislocal,\n"
|
||||||
|
"pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n");
|
||||||
|
|
||||||
if (fout->remoteVersion >= 110000)
|
if (fout->remoteVersion >= 110000)
|
||||||
{
|
appendPQExpBuffer(q,
|
||||||
/* atthasmissing and attmissingval are new in 11 */
|
|
||||||
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
|
|
||||||
"a.attstattarget, a.attstorage, t.typstorage, "
|
|
||||||
"a.attnotnull, a.atthasdef, a.attisdropped, "
|
|
||||||
"a.attlen, a.attalign, a.attislocal, "
|
|
||||||
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
|
|
||||||
"array_to_string(a.attoptions, ', ') AS attoptions, "
|
|
||||||
"CASE WHEN a.attcollation <> t.typcollation "
|
|
||||||
"THEN a.attcollation ELSE 0 END AS attcollation, "
|
|
||||||
"a.attidentity, "
|
|
||||||
"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 ,"
|
|
||||||
"CASE WHEN a.atthasmissing AND NOT a.attisdropped "
|
"CASE WHEN a.atthasmissing AND NOT a.attisdropped "
|
||||||
"THEN a.attmissingval ELSE null END AS attmissingval "
|
"THEN a.attmissingval ELSE null END AS attmissingval,\n");
|
||||||
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
|
|
||||||
"ON a.atttypid = t.oid "
|
|
||||||
"WHERE a.attrelid = '%u'::pg_catalog.oid "
|
|
||||||
"AND a.attnum > 0::pg_catalog.int2 "
|
|
||||||
"ORDER BY a.attnum",
|
|
||||||
tbinfo->dobj.catId.oid);
|
|
||||||
}
|
|
||||||
else if (fout->remoteVersion >= 100000)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* attidentity is new in version 10.
|
|
||||||
*/
|
|
||||||
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
|
|
||||||
"a.attstattarget, a.attstorage, t.typstorage, "
|
|
||||||
"a.attnotnull, a.atthasdef, a.attisdropped, "
|
|
||||||
"a.attlen, a.attalign, a.attislocal, "
|
|
||||||
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
|
|
||||||
"array_to_string(a.attoptions, ', ') AS attoptions, "
|
|
||||||
"CASE WHEN a.attcollation <> t.typcollation "
|
|
||||||
"THEN a.attcollation ELSE 0 END AS attcollation, "
|
|
||||||
"a.attidentity, "
|
|
||||||
"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 ,"
|
|
||||||
"NULL as attmissingval "
|
|
||||||
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
|
|
||||||
"ON a.atttypid = t.oid "
|
|
||||||
"WHERE a.attrelid = '%u'::pg_catalog.oid "
|
|
||||||
"AND a.attnum > 0::pg_catalog.int2 "
|
|
||||||
"ORDER BY a.attnum",
|
|
||||||
tbinfo->dobj.catId.oid);
|
|
||||||
}
|
|
||||||
else if (fout->remoteVersion >= 90200)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* attfdwoptions is new in 9.2.
|
|
||||||
*/
|
|
||||||
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
|
|
||||||
"a.attstattarget, a.attstorage, t.typstorage, "
|
|
||||||
"a.attnotnull, a.atthasdef, a.attisdropped, "
|
|
||||||
"a.attlen, a.attalign, a.attislocal, "
|
|
||||||
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
|
|
||||||
"array_to_string(a.attoptions, ', ') AS attoptions, "
|
|
||||||
"CASE WHEN a.attcollation <> t.typcollation "
|
|
||||||
"THEN a.attcollation ELSE 0 END AS attcollation, "
|
|
||||||
"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, "
|
|
||||||
"NULL as attmissingval "
|
|
||||||
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
|
|
||||||
"ON a.atttypid = t.oid "
|
|
||||||
"WHERE a.attrelid = '%u'::pg_catalog.oid "
|
|
||||||
"AND a.attnum > 0::pg_catalog.int2 "
|
|
||||||
"ORDER BY a.attnum",
|
|
||||||
tbinfo->dobj.catId.oid);
|
|
||||||
}
|
|
||||||
else if (fout->remoteVersion >= 90100)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* attcollation is new in 9.1. Since we only want to dump COLLATE
|
|
||||||
* clauses for attributes whose collation is different from their
|
|
||||||
* type's default, we use a CASE here to suppress uninteresting
|
|
||||||
* attcollations cheaply.
|
|
||||||
*/
|
|
||||||
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
|
|
||||||
"a.attstattarget, a.attstorage, t.typstorage, "
|
|
||||||
"a.attnotnull, a.atthasdef, a.attisdropped, "
|
|
||||||
"a.attlen, a.attalign, a.attislocal, "
|
|
||||||
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
|
|
||||||
"array_to_string(a.attoptions, ', ') AS attoptions, "
|
|
||||||
"CASE WHEN a.attcollation <> t.typcollation "
|
|
||||||
"THEN a.attcollation ELSE 0 END AS attcollation, "
|
|
||||||
"NULL AS attfdwoptions, "
|
|
||||||
"NULL as attmissingval "
|
|
||||||
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
|
|
||||||
"ON a.atttypid = t.oid "
|
|
||||||
"WHERE a.attrelid = '%u'::pg_catalog.oid "
|
|
||||||
"AND a.attnum > 0::pg_catalog.int2 "
|
|
||||||
"ORDER BY a.attnum",
|
|
||||||
tbinfo->dobj.catId.oid);
|
|
||||||
}
|
|
||||||
else if (fout->remoteVersion >= 90000)
|
|
||||||
{
|
|
||||||
/* attoptions is new in 9.0 */
|
|
||||||
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
|
|
||||||
"a.attstattarget, a.attstorage, t.typstorage, "
|
|
||||||
"a.attnotnull, a.atthasdef, a.attisdropped, "
|
|
||||||
"a.attlen, a.attalign, a.attislocal, "
|
|
||||||
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
|
|
||||||
"array_to_string(a.attoptions, ', ') AS attoptions, "
|
|
||||||
"0 AS attcollation, "
|
|
||||||
"NULL AS attfdwoptions, "
|
|
||||||
"NULL as attmissingval "
|
|
||||||
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
|
|
||||||
"ON a.atttypid = t.oid "
|
|
||||||
"WHERE a.attrelid = '%u'::pg_catalog.oid "
|
|
||||||
"AND a.attnum > 0::pg_catalog.int2 "
|
|
||||||
"ORDER BY a.attnum",
|
|
||||||
tbinfo->dobj.catId.oid);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
appendPQExpBuffer(q,
|
||||||
/* need left join here to not fail on dropped columns ... */
|
"NULL AS attmissingval,\n");
|
||||||
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
|
|
||||||
"a.attstattarget, a.attstorage, t.typstorage, "
|
if (fout->remoteVersion >= 100000)
|
||||||
"a.attnotnull, a.atthasdef, a.attisdropped, "
|
appendPQExpBuffer(q,
|
||||||
"a.attlen, a.attalign, a.attislocal, "
|
"a.attidentity,\n");
|
||||||
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
|
else
|
||||||
"'' AS attoptions, 0 AS attcollation, "
|
appendPQExpBuffer(q,
|
||||||
"NULL AS attfdwoptions, "
|
"'' AS attidentity,\n");
|
||||||
"NULL as attmissingval "
|
|
||||||
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
|
if (fout->remoteVersion >= 90200)
|
||||||
"ON a.atttypid = t.oid "
|
appendPQExpBuffer(q,
|
||||||
"WHERE a.attrelid = '%u'::pg_catalog.oid "
|
"pg_catalog.array_to_string(ARRAY("
|
||||||
"AND a.attnum > 0::pg_catalog.int2 "
|
"SELECT pg_catalog.quote_ident(option_name) || "
|
||||||
"ORDER BY a.attnum",
|
"' ' || pg_catalog.quote_literal(option_value) "
|
||||||
tbinfo->dobj.catId.oid);
|
"FROM pg_catalog.pg_options_to_table(attfdwoptions) "
|
||||||
}
|
"ORDER BY option_name"
|
||||||
|
"), E',\n ') AS attfdwoptions,\n");
|
||||||
|
else
|
||||||
|
appendPQExpBuffer(q,
|
||||||
|
"'' AS attfdwoptions,\n");
|
||||||
|
|
||||||
|
if (fout->remoteVersion >= 90100)
|
||||||
|
/*
|
||||||
|
* Since we only want to dump COLLATE clauses for attributes whose
|
||||||
|
* collation is different from their type's default, we use a CASE
|
||||||
|
* here to suppress uninteresting attcollations cheaply.
|
||||||
|
*/
|
||||||
|
appendPQExpBuffer(q,
|
||||||
|
"CASE WHEN a.attcollation <> t.typcollation "
|
||||||
|
"THEN a.attcollation ELSE 0 END AS attcollation,\n");
|
||||||
|
else
|
||||||
|
appendPQExpBuffer(q,
|
||||||
|
"0 AS attcollation,\n");
|
||||||
|
|
||||||
|
if (fout->remoteVersion >= 90000)
|
||||||
|
appendPQExpBuffer(q,
|
||||||
|
"array_to_string(a.attoptions, ', ') AS attoptions\n");
|
||||||
|
else
|
||||||
|
appendPQExpBuffer(q,
|
||||||
|
"'' AS attoptions\n");
|
||||||
|
|
||||||
|
appendPQExpBuffer(q,
|
||||||
|
/* need left join here to not fail on dropped columns ... */
|
||||||
|
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
|
||||||
|
"ON a.atttypid = t.oid\n"
|
||||||
|
"WHERE a.attrelid = '%u'::pg_catalog.oid "
|
||||||
|
"AND a.attnum > 0::pg_catalog.int2\n"
|
||||||
|
"ORDER BY a.attnum",
|
||||||
|
tbinfo->dobj.catId.oid);
|
||||||
|
|
||||||
res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
|
res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
|
||||||
|
|
||||||
@ -8363,7 +8290,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
|
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
|
||||||
tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
|
tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
|
||||||
tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
|
tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
|
||||||
tbinfo->attidentity[j] = (i_attidentity >= 0 ? *(PQgetvalue(res, j, i_attidentity)) : '\0');
|
tbinfo->attidentity[j] = *(PQgetvalue(res, j, i_attidentity));
|
||||||
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, i_attisdropped)[0] == 't');
|
||||||
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
|
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
|
||||||
@ -16023,7 +15950,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
/*
|
/*
|
||||||
* Dump per-column attributes.
|
* Dump per-column attributes.
|
||||||
*/
|
*/
|
||||||
if (tbinfo->attoptions[j] && tbinfo->attoptions[j][0] != '\0')
|
if (tbinfo->attoptions[j][0] != '\0')
|
||||||
{
|
{
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
|
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
|
||||||
qualrelname);
|
qualrelname);
|
||||||
@ -16037,7 +15964,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
* Dump per-column fdw options.
|
* Dump per-column fdw options.
|
||||||
*/
|
*/
|
||||||
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
|
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
|
||||||
tbinfo->attfdwoptions[j] &&
|
|
||||||
tbinfo->attfdwoptions[j][0] != '\0')
|
tbinfo->attfdwoptions[j][0] != '\0')
|
||||||
{
|
{
|
||||||
appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",
|
appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",
|
||||||
|
Loading…
Reference in New Issue
Block a user