Skip to content

Commit 2dc53fe

Browse files
committed
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 aa769f8 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.
1 parent 537ca68 commit 2dc53fe

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

contrib/postgres_fdw/postgres_fdw.c

+23-27
Original file line numberDiff line numberDiff line change
@@ -5287,45 +5287,41 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
52875287
"SELECT relname, "
52885288
" attname, "
52895289
" format_type(atttypid, atttypmod), "
5290-
" attnotnull, ");
5290+
" attnotnull, "
5291+
" pg_get_expr(adbin, adrelid), ");
52915292

52925293
/* Generated columns are supported since Postgres 12 */
52935294
if (PQserverVersion(conn) >= 120000)
52945295
appendStringInfoString(&buf,
5295-
" attgenerated, "
5296-
" pg_get_expr(adbin, adrelid), ");
5296+
" attgenerated, ");
52975297
else
52985298
appendStringInfoString(&buf,
5299-
" NULL, "
5300-
" pg_get_expr(adbin, adrelid), ");
5299+
" NULL, ");
53015300

53025301
if (import_collate)
53035302
appendStringInfoString(&buf,
53045303
" collname, "
5305-
" collnsp.nspname "
5306-
"FROM pg_class c "
5307-
" JOIN pg_namespace n ON "
5308-
" relnamespace = n.oid "
5309-
" LEFT JOIN pg_attribute a ON "
5310-
" attrelid = c.oid AND attnum > 0 "
5311-
" AND NOT attisdropped "
5312-
" LEFT JOIN pg_attrdef ad ON "
5313-
" adrelid = c.oid AND adnum = attnum "
5304+
" collnsp.nspname ");
5305+
else
5306+
appendStringInfoString(&buf,
5307+
" NULL, NULL ");
5308+
5309+
appendStringInfoString(&buf,
5310+
"FROM pg_class c "
5311+
" JOIN pg_namespace n ON "
5312+
" relnamespace = n.oid "
5313+
" LEFT JOIN pg_attribute a ON "
5314+
" attrelid = c.oid AND attnum > 0 "
5315+
" AND NOT attisdropped "
5316+
" LEFT JOIN pg_attrdef ad ON "
5317+
" adrelid = c.oid AND adnum = attnum ");
5318+
5319+
if (import_collate)
5320+
appendStringInfoString(&buf,
53145321
" LEFT JOIN pg_collation coll ON "
53155322
" coll.oid = attcollation "
53165323
" LEFT JOIN pg_namespace collnsp ON "
53175324
" collnsp.oid = collnamespace ");
5318-
else
5319-
appendStringInfoString(&buf,
5320-
" NULL, NULL "
5321-
"FROM pg_class c "
5322-
" JOIN pg_namespace n ON "
5323-
" relnamespace = n.oid "
5324-
" LEFT JOIN pg_attribute a ON "
5325-
" attrelid = c.oid AND attnum > 0 "
5326-
" AND NOT attisdropped "
5327-
" LEFT JOIN pg_attrdef ad ON "
5328-
" adrelid = c.oid AND adnum = attnum ");
53295325

53305326
appendStringInfoString(&buf,
53315327
"WHERE c.relkind IN ("
@@ -5405,9 +5401,9 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
54055401
attname = PQgetvalue(res, i, 1);
54065402
typename = PQgetvalue(res, i, 2);
54075403
attnotnull = PQgetvalue(res, i, 3);
5408-
attgenerated = PQgetisnull(res, i, 4) ? (char *) NULL :
5404+
attdefault = PQgetisnull(res, i, 4) ? (char *) NULL :
54095405
PQgetvalue(res, i, 4);
5410-
attdefault = PQgetisnull(res, i, 5) ? (char *) NULL :
5406+
attgenerated = PQgetisnull(res, i, 5) ? (char *) NULL :
54115407
PQgetvalue(res, i, 5);
54125408
collname = PQgetisnull(res, i, 6) ? (char *) NULL :
54135409
PQgetvalue(res, i, 6);

0 commit comments

Comments
 (0)