1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-05 23:56:58 +03:00

Refactor postgresImportForeignSchema to avoid code duplication.

Avoid repeating fragments of the query we're building, along the
same lines as recent cleanup in pg_dump.  I got annoyed about this
because aa769f80e broke my pending patch to change postgres_fdw's
collation handling, due to each of us having incompletely done
this same refactoring.  Let's finish that job in hopes of having
a more stable base.
This commit is contained in:
Tom Lane 2021-09-01 16:21:13 -04:00
parent 537ca68dbb
commit 2dc53fe2a7

View File

@ -5287,45 +5287,41 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
"SELECT relname, " "SELECT relname, "
" attname, " " attname, "
" format_type(atttypid, atttypmod), " " format_type(atttypid, atttypmod), "
" attnotnull, "); " attnotnull, "
" pg_get_expr(adbin, adrelid), ");
/* Generated columns are supported since Postgres 12 */ /* Generated columns are supported since Postgres 12 */
if (PQserverVersion(conn) >= 120000) if (PQserverVersion(conn) >= 120000)
appendStringInfoString(&buf, appendStringInfoString(&buf,
" attgenerated, " " attgenerated, ");
" pg_get_expr(adbin, adrelid), ");
else else
appendStringInfoString(&buf, appendStringInfoString(&buf,
" NULL, " " NULL, ");
" pg_get_expr(adbin, adrelid), ");
if (import_collate) if (import_collate)
appendStringInfoString(&buf, appendStringInfoString(&buf,
" collname, " " collname, "
" collnsp.nspname " " collnsp.nspname ");
"FROM pg_class c " else
" JOIN pg_namespace n ON " appendStringInfoString(&buf,
" relnamespace = n.oid " " NULL, NULL ");
" LEFT JOIN pg_attribute a ON "
" attrelid = c.oid AND attnum > 0 " appendStringInfoString(&buf,
" AND NOT attisdropped " "FROM pg_class c "
" LEFT JOIN pg_attrdef ad ON " " JOIN pg_namespace n ON "
" adrelid = c.oid AND adnum = attnum " " relnamespace = n.oid "
" LEFT JOIN pg_attribute a ON "
" attrelid = c.oid AND attnum > 0 "
" AND NOT attisdropped "
" LEFT JOIN pg_attrdef ad ON "
" adrelid = c.oid AND adnum = attnum ");
if (import_collate)
appendStringInfoString(&buf,
" LEFT JOIN pg_collation coll ON " " LEFT JOIN pg_collation coll ON "
" coll.oid = attcollation " " coll.oid = attcollation "
" LEFT JOIN pg_namespace collnsp ON " " LEFT JOIN pg_namespace collnsp ON "
" collnsp.oid = collnamespace "); " collnsp.oid = collnamespace ");
else
appendStringInfoString(&buf,
" NULL, NULL "
"FROM pg_class c "
" JOIN pg_namespace n ON "
" relnamespace = n.oid "
" LEFT JOIN pg_attribute a ON "
" attrelid = c.oid AND attnum > 0 "
" AND NOT attisdropped "
" LEFT JOIN pg_attrdef ad ON "
" adrelid = c.oid AND adnum = attnum ");
appendStringInfoString(&buf, appendStringInfoString(&buf,
"WHERE c.relkind IN (" "WHERE c.relkind IN ("
@ -5405,9 +5401,9 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
attname = PQgetvalue(res, i, 1); attname = PQgetvalue(res, i, 1);
typename = PQgetvalue(res, i, 2); typename = PQgetvalue(res, i, 2);
attnotnull = PQgetvalue(res, i, 3); attnotnull = PQgetvalue(res, i, 3);
attgenerated = PQgetisnull(res, i, 4) ? (char *) NULL : attdefault = PQgetisnull(res, i, 4) ? (char *) NULL :
PQgetvalue(res, i, 4); PQgetvalue(res, i, 4);
attdefault = PQgetisnull(res, i, 5) ? (char *) NULL : attgenerated = PQgetisnull(res, i, 5) ? (char *) NULL :
PQgetvalue(res, i, 5); PQgetvalue(res, i, 5);
collname = PQgetisnull(res, i, 6) ? (char *) NULL : collname = PQgetisnull(res, i, 6) ? (char *) NULL :
PQgetvalue(res, i, 6); PQgetvalue(res, i, 6);