Hello, pg_dump creates plain ALTER TABLE statements even if the table is a foreign table, which for someone reading the dump is confusing. This also made a difference when applying the dump if there is any plugin installed that hooks on ProcessUtility, because the plugin could react differently to ALTER TABLE than to ALTER FOREIGN TABLE. Opinions?
An unrelated question: if I apply pgindent to a file (in this case pg_dump.c) and get a bunch of changes on the indentation that are not related to my patch, which is the accepted policy? A different patch first with only the indentation? Maybe, am I using pgindent wrong? Cheers Luis M Carril
From a5b439b7dfdc1f9bab8d2cf1fc95a8eaedb1d83e Mon Sep 17 00:00:00 2001 From: Luis Carril <luis.car...@swarm64.com> Date: Fri, 12 Jul 2019 12:51:17 +0200 Subject: [PATCH] Add FOREIGN to ALTER statements --- src/bin/pg_dump/pg_dump.c | 40 ++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 806fc78f04..72951f9a0a 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -15535,6 +15535,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) { char *ftoptions = NULL; char *srvname = NULL; + char *foreign = ""; switch (tbinfo->relkind) { @@ -15568,6 +15569,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions)); PQclear(res); destroyPQExpBuffer(query); + + foreign = "FOREIGN"; break; } case RELKIND_MATVIEW: @@ -15909,7 +15912,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) continue; appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraint.\n"); - appendPQExpBuffer(q, "ALTER TABLE ONLY %s ", + appendPQExpBuffer(q, "ALTER %s TABLE ONLY %s ", foreign, qualrelname); appendPQExpBuffer(q, " ADD CONSTRAINT %s ", fmtId(constr->dobj.name)); @@ -15930,7 +15933,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) { TableInfo *parentRel = parents[k]; - appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT %s;\n", + appendPQExpBuffer(q, "ALTER %s TABLE ONLY %s INHERIT %s;\n", foreign, qualrelname, fmtQualifiedDumpable(parentRel)); } @@ -16036,7 +16039,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) if (!shouldPrintColumn(dopt, tbinfo, j) && tbinfo->notnull[j] && !tbinfo->inhNotNull[j]) { - appendPQExpBuffer(q, "ALTER TABLE ONLY %s ", + appendPQExpBuffer(q, "ALTER %s TABLE ONLY %s ", foreign, qualrelname); appendPQExpBuffer(q, "ALTER COLUMN %s SET NOT NULL;\n", fmtId(tbinfo->attnames[j])); @@ -16049,7 +16052,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) */ if (tbinfo->attstattarget[j] >= 0) { - appendPQExpBuffer(q, "ALTER TABLE ONLY %s ", + appendPQExpBuffer(q, "ALTER %s TABLE ONLY %s ", foreign, qualrelname); appendPQExpBuffer(q, "ALTER COLUMN %s ", fmtId(tbinfo->attnames[j])); @@ -16086,7 +16089,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) */ if (storage != NULL) { - appendPQExpBuffer(q, "ALTER TABLE ONLY %s ", + appendPQExpBuffer(q, "ALTER %s TABLE ONLY %s ", foreign, qualrelname); appendPQExpBuffer(q, "ALTER COLUMN %s ", fmtId(tbinfo->attnames[j])); @@ -16100,7 +16103,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) */ if (tbinfo->attoptions[j][0] != '\0') { - appendPQExpBuffer(q, "ALTER TABLE ONLY %s ", + appendPQExpBuffer(q, "ALTER %s TABLE ONLY %s ", foreign, qualrelname); appendPQExpBuffer(q, "ALTER COLUMN %s ", fmtId(tbinfo->attnames[j])); @@ -16223,6 +16226,7 @@ dumpAttrDef(Archive *fout, AttrDefInfo *adinfo) PQExpBuffer delq; char *qualrelname; char *tag; + char *foreign; /* Skip if table definition not to be dumped */ if (!tbinfo->dobj.dump || dopt->dataOnly) @@ -16237,13 +16241,15 @@ dumpAttrDef(Archive *fout, AttrDefInfo *adinfo) qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo)); - appendPQExpBuffer(q, "ALTER TABLE ONLY %s ", + foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN" : ""; + + appendPQExpBuffer(q, "ALTER %s TABLE ONLY %s ", foreign, qualrelname); appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n", fmtId(tbinfo->attnames[adnum - 1]), adinfo->adef_expr); - appendPQExpBuffer(delq, "ALTER TABLE %s ", + appendPQExpBuffer(delq, "ALTER %s TABLE %s ", foreign, qualrelname); appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n", fmtId(tbinfo->attnames[adnum - 1])); @@ -16536,6 +16542,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo) PQExpBuffer q; PQExpBuffer delq; char *tag = NULL; + char *foreign = NULL; /* Skip if not to be dumped */ if (!coninfo->dobj.dump || dopt->dataOnly) @@ -16544,6 +16551,8 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo) q = createPQExpBuffer(); delq = createPQExpBuffer(); + foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN" : ""; + if (coninfo->contype == 'p' || coninfo->contype == 'u' || coninfo->contype == 'x') @@ -16562,7 +16571,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo) binary_upgrade_set_pg_class_oids(fout, q, indxinfo->dobj.catId.oid, true); - appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n", + appendPQExpBuffer(q, "ALTER %s TABLE ONLY %s\n", foreign, fmtQualifiedDumpable(tbinfo)); appendPQExpBuffer(q, " ADD CONSTRAINT %s ", fmtId(coninfo->dobj.name)); @@ -16652,7 +16661,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo) fmtId(indxinfo->dobj.name)); } - appendPQExpBuffer(delq, "ALTER TABLE ONLY %s ", + appendPQExpBuffer(delq, "ALTER %s TABLE ONLY %s ", foreign, fmtQualifiedDumpable(tbinfo)); appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", fmtId(coninfo->dobj.name)); @@ -16686,13 +16695,13 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo) * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the * current table data is not processed */ - appendPQExpBuffer(q, "ALTER TABLE %s%s\n", + appendPQExpBuffer(q, "ALTER %s TABLE %s%s\n", foreign, only, fmtQualifiedDumpable(tbinfo)); appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n", fmtId(coninfo->dobj.name), coninfo->condef); - appendPQExpBuffer(delq, "ALTER TABLE %s%s ", + appendPQExpBuffer(delq, "ALTER %s TABLE %s%s ", foreign, only, fmtQualifiedDumpable(tbinfo)); appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", fmtId(coninfo->dobj.name)); @@ -16717,13 +16726,13 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo) if (coninfo->separate && coninfo->conislocal) { /* not ONLY since we want it to propagate to children */ - appendPQExpBuffer(q, "ALTER TABLE %s\n", + appendPQExpBuffer(q, "ALTER %s TABLE %s\n", foreign, fmtQualifiedDumpable(tbinfo)); appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n", fmtId(coninfo->dobj.name), coninfo->condef); - appendPQExpBuffer(delq, "ALTER TABLE %s ", + appendPQExpBuffer(delq, "ALTER %s TABLE %s ", foreign, fmtQualifiedDumpable(tbinfo)); appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", fmtId(coninfo->dobj.name)); @@ -17315,7 +17324,8 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo) if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O') { - appendPQExpBuffer(query, "\nALTER TABLE %s ", + char * foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE? "FOREIGN" : ""; + appendPQExpBuffer(query, "\nALTER %s TABLE %s ", foreign, fmtQualifiedDumpable(tbinfo)); switch (tginfo->tgenabled) { -- 2.20.1