Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4e62091

Browse files
committed
pg_dump: Add FOREIGN to ALTER statements, if appropriate
Author: Luis CarrilReviewed-by: Tomas Vondra, Daniel Gustafsson, Álvaro HerreraDiscussion:https://postgr.es/m/LEJPR01MB0185A19B2E7C98E5E2A031F5E7F20@LEJPR01MB0185.DEUPRD01.PROD.OUTLOOK.DE
1 parent71c2fd0 commit4e62091

File tree

1 file changed

+46
-53
lines changed

1 file changed

+46
-53
lines changed

‎src/bin/pg_dump/pg_dump.c

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15655,6 +15655,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1565515655
{
1565615656
char *ftoptions = NULL;
1565715657
char *srvname = NULL;
15658+
char *foreign = "";
1565815659

1565915660
switch (tbinfo->relkind)
1566015661
{
@@ -15688,6 +15689,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1568815689
ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
1568915690
PQclear(res);
1569015691
destroyPQExpBuffer(query);
15692+
15693+
foreign = "FOREIGN ";
1569115694
break;
1569215695
}
1569315696
case RELKIND_MATVIEW:
@@ -16037,11 +16040,10 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1603716040
continue;
1603816041

1603916042
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraint.\n");
16040-
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16041-
qualrelname);
16042-
appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
16043-
fmtId(constr->dobj.name));
16044-
appendPQExpBuffer(q, "%s;\n", constr->condef);
16043+
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ADD CONSTRAINT %s %s;\n",
16044+
foreign, qualrelname,
16045+
fmtId(constr->dobj.name),
16046+
constr->condef);
1604516047
appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
1604616048
"SET conislocal = false\n"
1604716049
"WHERE contype = 'c' AND conname = ");
@@ -16058,7 +16060,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1605816060
{
1605916061
TableInfo *parentRel = parents[k];
1606016062

16061-
appendPQExpBuffer(q, "ALTERTABLE ONLY %s INHERIT %s;\n",
16063+
appendPQExpBuffer(q, "ALTER%sTABLE ONLY %s INHERIT %s;\n", foreign,
1606216064
qualrelname,
1606316065
fmtQualifiedDumpable(parentRel));
1606416066
}
@@ -16163,27 +16165,21 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1616316165
*/
1616416166
if (!shouldPrintColumn(dopt, tbinfo, j) &&
1616516167
tbinfo->notnull[j] && !tbinfo->inhNotNull[j])
16166-
{
16167-
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16168-
qualrelname);
16169-
appendPQExpBuffer(q, "ALTER COLUMN %s SET NOT NULL;\n",
16168+
appendPQExpBuffer(q,
16169+
"ALTER %sTABLE ONLY %s ALTER COLUMN %s SET NOT NULL;\n",
16170+
foreign, qualrelname,
1617016171
fmtId(tbinfo->attnames[j]));
16171-
}
1617216172

1617316173
/*
1617416174
* Dump per-column statistics information. We only issue an ALTER
1617516175
* TABLE statement if the attstattarget entry for this column is
1617616176
* non-negative (i.e. it's not the default value)
1617716177
*/
1617816178
if (tbinfo->attstattarget[j] >= 0)
16179-
{
16180-
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16181-
qualrelname);
16182-
appendPQExpBuffer(q, "ALTER COLUMN %s ",
16183-
fmtId(tbinfo->attnames[j]));
16184-
appendPQExpBuffer(q, "SET STATISTICS %d;\n",
16179+
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STATISTICS %d;\n",
16180+
foreign, qualrelname,
16181+
fmtId(tbinfo->attnames[j]),
1618516182
tbinfo->attstattarget[j]);
16186-
}
1618716183

1618816184
/*
1618916185
* Dump per-column storage information. The statement is only
@@ -16213,42 +16209,33 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1621316209
* Only dump the statement if it's a storage type we recognize
1621416210
*/
1621516211
if (storage != NULL)
16216-
{
16217-
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16218-
qualrelname);
16219-
appendPQExpBuffer(q, "ALTER COLUMN %s ",
16220-
fmtId(tbinfo->attnames[j]));
16221-
appendPQExpBuffer(q, "SET STORAGE %s;\n",
16212+
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STORAGE %s;\n",
16213+
foreign, qualrelname,
16214+
fmtId(tbinfo->attnames[j]),
1622216215
storage);
16223-
}
1622416216
}
1622516217

1622616218
/*
1622716219
* Dump per-column attributes.
1622816220
*/
1622916221
if (tbinfo->attoptions[j][0] != '\0')
16230-
{
16231-
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16232-
qualrelname);
16233-
appendPQExpBuffer(q, "ALTER COLUMN %s ",
16234-
fmtId(tbinfo->attnames[j]));
16235-
appendPQExpBuffer(q, "SET (%s);\n",
16222+
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n",
16223+
foreign, qualrelname,
16224+
fmtId(tbinfo->attnames[j]),
1623616225
tbinfo->attoptions[j]);
16237-
}
1623816226

1623916227
/*
1624016228
* Dump per-column fdw options.
1624116229
*/
1624216230
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
1624316231
tbinfo->attfdwoptions[j][0] != '\0')
16244-
{
16245-
appendPQExpBuffer(q,"ALTER FOREIGN TABLE %s",
16246-
qualrelname);
16247-
appendPQExpBuffer(q, "ALTER COLUMN %s",
16248-
fmtId(tbinfo->attnames[j]));
16249-
appendPQExpBuffer(q, "OPTIONS (\n %s\n);\n",
16232+
appendPQExpBuffer(q,
16233+
"ALTER FOREIGN TABLE %sALTER COLUMN %s OPTIONS (\n"
16234+
" %s\n"
16235+
");\n",
16236+
qualrelname,
16237+
fmtId(tbinfo->attnames[j]),
1625016238
tbinfo->attfdwoptions[j]);
16251-
}
1625216239
}
1625316240

1625416241
if (ftoptions)
@@ -16351,6 +16338,7 @@ dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
1635116338
PQExpBuffer delq;
1635216339
char *qualrelname;
1635316340
char *tag;
16341+
char *foreign;
1635416342

1635516343
/* Skip if table definition not to be dumped */
1635616344
if (!tbinfo->dobj.dump || dopt->dataOnly)
@@ -16365,15 +16353,15 @@ dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
1636516353

1636616354
qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
1636716355

16368-
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16369-
qualrelname);
16370-
appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
16371-
fmtId(tbinfo->attnames[adnum - 1]),
16356+
foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16357+
16358+
appendPQExpBuffer(q,
16359+
"ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16360+
foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
1637216361
adinfo->adef_expr);
1637316362

16374-
appendPQExpBuffer(delq, "ALTER TABLE %s ",
16375-
qualrelname);
16376-
appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
16363+
appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16364+
foreign, qualrelname,
1637716365
fmtId(tbinfo->attnames[adnum - 1]));
1637816366

1637916367
tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
@@ -16683,6 +16671,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
1668316671
PQExpBuffer q;
1668416672
PQExpBuffer delq;
1668516673
char *tag = NULL;
16674+
char *foreign;
1668616675

1668716676
/* Skip if not to be dumped */
1668816677
if (!coninfo->dobj.dump || dopt->dataOnly)
@@ -16691,6 +16680,9 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
1669116680
q = createPQExpBuffer();
1669216681
delq = createPQExpBuffer();
1669316682

16683+
foreign = tbinfo &&
16684+
tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16685+
1669416686
if (coninfo->contype == 'p' ||
1669516687
coninfo->contype == 'u' ||
1669616688
coninfo->contype == 'x')
@@ -16709,7 +16701,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
1670916701
binary_upgrade_set_pg_class_oids(fout, q,
1671016702
indxinfo->dobj.catId.oid, true);
1671116703

16712-
appendPQExpBuffer(q, "ALTERTABLE ONLY %s\n",
16704+
appendPQExpBuffer(q, "ALTER%sTABLE ONLY %s\n", foreign,
1671316705
fmtQualifiedDumpable(tbinfo));
1671416706
appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
1671516707
fmtId(coninfo->dobj.name));
@@ -16804,7 +16796,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
1680416796
"pg_catalog.pg_class", "INDEX",
1680516797
fmtQualifiedDumpable(indxinfo));
1680616798

16807-
appendPQExpBuffer(delq, "ALTERTABLE ONLY %s ",
16799+
appendPQExpBuffer(delq, "ALTER%sTABLE ONLY %s ", foreign,
1680816800
fmtQualifiedDumpable(tbinfo));
1680916801
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
1681016802
fmtId(coninfo->dobj.name));
@@ -16838,13 +16830,13 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
1683816830
* XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
1683916831
* current table data is not processed
1684016832
*/
16841-
appendPQExpBuffer(q, "ALTERTABLE %s%s\n",
16833+
appendPQExpBuffer(q, "ALTER%sTABLE %s%s\n", foreign,
1684216834
only, fmtQualifiedDumpable(tbinfo));
1684316835
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
1684416836
fmtId(coninfo->dobj.name),
1684516837
coninfo->condef);
1684616838

16847-
appendPQExpBuffer(delq, "ALTERTABLE %s%s ",
16839+
appendPQExpBuffer(delq, "ALTER%sTABLE %s%s ", foreign,
1684816840
only, fmtQualifiedDumpable(tbinfo));
1684916841
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
1685016842
fmtId(coninfo->dobj.name));
@@ -16869,13 +16861,13 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
1686916861
if (coninfo->separate && coninfo->conislocal)
1687016862
{
1687116863
/* not ONLY since we want it to propagate to children */
16872-
appendPQExpBuffer(q, "ALTERTABLE %s\n",
16864+
appendPQExpBuffer(q, "ALTER%sTABLE %s\n", foreign,
1687316865
fmtQualifiedDumpable(tbinfo));
1687416866
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
1687516867
fmtId(coninfo->dobj.name),
1687616868
coninfo->condef);
1687716869

16878-
appendPQExpBuffer(delq, "ALTERTABLE %s ",
16870+
appendPQExpBuffer(delq, "ALTER%sTABLE %s ", foreign,
1687916871
fmtQualifiedDumpable(tbinfo));
1688016872
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
1688116873
fmtId(coninfo->dobj.name));
@@ -17474,7 +17466,8 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
1747417466

1747517467
if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
1747617468
{
17477-
appendPQExpBuffer(query, "\nALTER TABLE %s ",
17469+
appendPQExpBuffer(query, "\nALTER %sTABLE %s ", foreign,
17470+
tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
1747817471
fmtQualifiedDumpable(tbinfo));
1747917472
switch (tginfo->tgenabled)
1748017473
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp