mirror of
https://github.com/postgres/postgres.git
synced 2025-05-29 16:21:20 +03:00
pg_dump: Add FOREIGN to ALTER statements, if appropriate
Author: Luis Carril Reviewed-by: Tomas Vondra, Daniel Gustafsson, Álvaro Herrera Discussion: https://postgr.es/m/LEJPR01MB0185A19B2E7C98E5E2A031F5E7F20@LEJPR01MB0185.DEUPRD01.PROD.OUTLOOK.DE
This commit is contained in:
parent
71c2fd0c04
commit
4e62091341
@ -15655,6 +15655,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
{
|
{
|
||||||
char *ftoptions = NULL;
|
char *ftoptions = NULL;
|
||||||
char *srvname = NULL;
|
char *srvname = NULL;
|
||||||
|
char *foreign = "";
|
||||||
|
|
||||||
switch (tbinfo->relkind)
|
switch (tbinfo->relkind)
|
||||||
{
|
{
|
||||||
@ -15688,6 +15689,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
|
ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
destroyPQExpBuffer(query);
|
destroyPQExpBuffer(query);
|
||||||
|
|
||||||
|
foreign = "FOREIGN ";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RELKIND_MATVIEW:
|
case RELKIND_MATVIEW:
|
||||||
@ -16037,11 +16040,10 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraint.\n");
|
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraint.\n");
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
|
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ADD CONSTRAINT %s %s;\n",
|
||||||
qualrelname);
|
foreign, qualrelname,
|
||||||
appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
|
fmtId(constr->dobj.name),
|
||||||
fmtId(constr->dobj.name));
|
constr->condef);
|
||||||
appendPQExpBuffer(q, "%s;\n", constr->condef);
|
|
||||||
appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
|
appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
|
||||||
"SET conislocal = false\n"
|
"SET conislocal = false\n"
|
||||||
"WHERE contype = 'c' AND conname = ");
|
"WHERE contype = 'c' AND conname = ");
|
||||||
@ -16058,7 +16060,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
{
|
{
|
||||||
TableInfo *parentRel = parents[k];
|
TableInfo *parentRel = parents[k];
|
||||||
|
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT %s;\n",
|
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s INHERIT %s;\n", foreign,
|
||||||
qualrelname,
|
qualrelname,
|
||||||
fmtQualifiedDumpable(parentRel));
|
fmtQualifiedDumpable(parentRel));
|
||||||
}
|
}
|
||||||
@ -16163,12 +16165,10 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
*/
|
*/
|
||||||
if (!shouldPrintColumn(dopt, tbinfo, j) &&
|
if (!shouldPrintColumn(dopt, tbinfo, j) &&
|
||||||
tbinfo->notnull[j] && !tbinfo->inhNotNull[j])
|
tbinfo->notnull[j] && !tbinfo->inhNotNull[j])
|
||||||
{
|
appendPQExpBuffer(q,
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
|
"ALTER %sTABLE ONLY %s ALTER COLUMN %s SET NOT NULL;\n",
|
||||||
qualrelname);
|
foreign, qualrelname,
|
||||||
appendPQExpBuffer(q, "ALTER COLUMN %s SET NOT NULL;\n",
|
|
||||||
fmtId(tbinfo->attnames[j]));
|
fmtId(tbinfo->attnames[j]));
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Dump per-column statistics information. We only issue an ALTER
|
* Dump per-column statistics information. We only issue an ALTER
|
||||||
@ -16176,14 +16176,10 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
* non-negative (i.e. it's not the default value)
|
* non-negative (i.e. it's not the default value)
|
||||||
*/
|
*/
|
||||||
if (tbinfo->attstattarget[j] >= 0)
|
if (tbinfo->attstattarget[j] >= 0)
|
||||||
{
|
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STATISTICS %d;\n",
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
|
foreign, qualrelname,
|
||||||
qualrelname);
|
fmtId(tbinfo->attnames[j]),
|
||||||
appendPQExpBuffer(q, "ALTER COLUMN %s ",
|
|
||||||
fmtId(tbinfo->attnames[j]));
|
|
||||||
appendPQExpBuffer(q, "SET STATISTICS %d;\n",
|
|
||||||
tbinfo->attstattarget[j]);
|
tbinfo->attstattarget[j]);
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Dump per-column storage information. The statement is only
|
* Dump per-column storage information. The statement is only
|
||||||
@ -16213,42 +16209,33 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
|
|||||||
* Only dump the statement if it's a storage type we recognize
|
* Only dump the statement if it's a storage type we recognize
|
||||||
*/
|
*/
|
||||||
if (storage != NULL)
|
if (storage != NULL)
|
||||||
{
|
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STORAGE %s;\n",
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
|
foreign, qualrelname,
|
||||||
qualrelname);
|
fmtId(tbinfo->attnames[j]),
|
||||||
appendPQExpBuffer(q, "ALTER COLUMN %s ",
|
|
||||||
fmtId(tbinfo->attnames[j]));
|
|
||||||
appendPQExpBuffer(q, "SET STORAGE %s;\n",
|
|
||||||
storage);
|
storage);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Dump per-column attributes.
|
* Dump per-column attributes.
|
||||||
*/
|
*/
|
||||||
if (tbinfo->attoptions[j][0] != '\0')
|
if (tbinfo->attoptions[j][0] != '\0')
|
||||||
{
|
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n",
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
|
foreign, qualrelname,
|
||||||
qualrelname);
|
fmtId(tbinfo->attnames[j]),
|
||||||
appendPQExpBuffer(q, "ALTER COLUMN %s ",
|
|
||||||
fmtId(tbinfo->attnames[j]));
|
|
||||||
appendPQExpBuffer(q, "SET (%s);\n",
|
|
||||||
tbinfo->attoptions[j]);
|
tbinfo->attoptions[j]);
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 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][0] != '\0')
|
tbinfo->attfdwoptions[j][0] != '\0')
|
||||||
{
|
appendPQExpBuffer(q,
|
||||||
appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",
|
"ALTER FOREIGN TABLE %s ALTER COLUMN %s OPTIONS (\n"
|
||||||
qualrelname);
|
" %s\n"
|
||||||
appendPQExpBuffer(q, "ALTER COLUMN %s ",
|
");\n",
|
||||||
fmtId(tbinfo->attnames[j]));
|
qualrelname,
|
||||||
appendPQExpBuffer(q, "OPTIONS (\n %s\n);\n",
|
fmtId(tbinfo->attnames[j]),
|
||||||
tbinfo->attfdwoptions[j]);
|
tbinfo->attfdwoptions[j]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftoptions)
|
if (ftoptions)
|
||||||
@ -16351,6 +16338,7 @@ dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
|
|||||||
PQExpBuffer delq;
|
PQExpBuffer delq;
|
||||||
char *qualrelname;
|
char *qualrelname;
|
||||||
char *tag;
|
char *tag;
|
||||||
|
char *foreign;
|
||||||
|
|
||||||
/* Skip if table definition not to be dumped */
|
/* Skip if table definition not to be dumped */
|
||||||
if (!tbinfo->dobj.dump || dopt->dataOnly)
|
if (!tbinfo->dobj.dump || dopt->dataOnly)
|
||||||
@ -16365,15 +16353,15 @@ dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
|
|||||||
|
|
||||||
qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
|
qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
|
||||||
|
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
|
foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
|
||||||
qualrelname);
|
|
||||||
appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
|
appendPQExpBuffer(q,
|
||||||
fmtId(tbinfo->attnames[adnum - 1]),
|
"ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
|
||||||
|
foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
|
||||||
adinfo->adef_expr);
|
adinfo->adef_expr);
|
||||||
|
|
||||||
appendPQExpBuffer(delq, "ALTER TABLE %s ",
|
appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
|
||||||
qualrelname);
|
foreign, qualrelname,
|
||||||
appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
|
|
||||||
fmtId(tbinfo->attnames[adnum - 1]));
|
fmtId(tbinfo->attnames[adnum - 1]));
|
||||||
|
|
||||||
tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
|
tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
|
||||||
@ -16683,6 +16671,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
|
|||||||
PQExpBuffer q;
|
PQExpBuffer q;
|
||||||
PQExpBuffer delq;
|
PQExpBuffer delq;
|
||||||
char *tag = NULL;
|
char *tag = NULL;
|
||||||
|
char *foreign;
|
||||||
|
|
||||||
/* Skip if not to be dumped */
|
/* Skip if not to be dumped */
|
||||||
if (!coninfo->dobj.dump || dopt->dataOnly)
|
if (!coninfo->dobj.dump || dopt->dataOnly)
|
||||||
@ -16691,6 +16680,9 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
|
|||||||
q = createPQExpBuffer();
|
q = createPQExpBuffer();
|
||||||
delq = createPQExpBuffer();
|
delq = createPQExpBuffer();
|
||||||
|
|
||||||
|
foreign = tbinfo &&
|
||||||
|
tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
|
||||||
|
|
||||||
if (coninfo->contype == 'p' ||
|
if (coninfo->contype == 'p' ||
|
||||||
coninfo->contype == 'u' ||
|
coninfo->contype == 'u' ||
|
||||||
coninfo->contype == 'x')
|
coninfo->contype == 'x')
|
||||||
@ -16709,7 +16701,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
|
|||||||
binary_upgrade_set_pg_class_oids(fout, q,
|
binary_upgrade_set_pg_class_oids(fout, q,
|
||||||
indxinfo->dobj.catId.oid, true);
|
indxinfo->dobj.catId.oid, true);
|
||||||
|
|
||||||
appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
|
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
|
||||||
fmtQualifiedDumpable(tbinfo));
|
fmtQualifiedDumpable(tbinfo));
|
||||||
appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
|
appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
|
||||||
fmtId(coninfo->dobj.name));
|
fmtId(coninfo->dobj.name));
|
||||||
@ -16804,7 +16796,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
|
|||||||
"pg_catalog.pg_class", "INDEX",
|
"pg_catalog.pg_class", "INDEX",
|
||||||
fmtQualifiedDumpable(indxinfo));
|
fmtQualifiedDumpable(indxinfo));
|
||||||
|
|
||||||
appendPQExpBuffer(delq, "ALTER TABLE ONLY %s ",
|
appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
|
||||||
fmtQualifiedDumpable(tbinfo));
|
fmtQualifiedDumpable(tbinfo));
|
||||||
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
|
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
|
||||||
fmtId(coninfo->dobj.name));
|
fmtId(coninfo->dobj.name));
|
||||||
@ -16838,13 +16830,13 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
|
|||||||
* XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
|
* XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
|
||||||
* current table data is not processed
|
* current table data is not processed
|
||||||
*/
|
*/
|
||||||
appendPQExpBuffer(q, "ALTER TABLE %s%s\n",
|
appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
|
||||||
only, fmtQualifiedDumpable(tbinfo));
|
only, fmtQualifiedDumpable(tbinfo));
|
||||||
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
|
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
|
||||||
fmtId(coninfo->dobj.name),
|
fmtId(coninfo->dobj.name),
|
||||||
coninfo->condef);
|
coninfo->condef);
|
||||||
|
|
||||||
appendPQExpBuffer(delq, "ALTER TABLE %s%s ",
|
appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
|
||||||
only, fmtQualifiedDumpable(tbinfo));
|
only, fmtQualifiedDumpable(tbinfo));
|
||||||
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
|
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
|
||||||
fmtId(coninfo->dobj.name));
|
fmtId(coninfo->dobj.name));
|
||||||
@ -16869,13 +16861,13 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
|
|||||||
if (coninfo->separate && coninfo->conislocal)
|
if (coninfo->separate && coninfo->conislocal)
|
||||||
{
|
{
|
||||||
/* not ONLY since we want it to propagate to children */
|
/* not ONLY since we want it to propagate to children */
|
||||||
appendPQExpBuffer(q, "ALTER TABLE %s\n",
|
appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
|
||||||
fmtQualifiedDumpable(tbinfo));
|
fmtQualifiedDumpable(tbinfo));
|
||||||
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
|
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
|
||||||
fmtId(coninfo->dobj.name),
|
fmtId(coninfo->dobj.name),
|
||||||
coninfo->condef);
|
coninfo->condef);
|
||||||
|
|
||||||
appendPQExpBuffer(delq, "ALTER TABLE %s ",
|
appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
|
||||||
fmtQualifiedDumpable(tbinfo));
|
fmtQualifiedDumpable(tbinfo));
|
||||||
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
|
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
|
||||||
fmtId(coninfo->dobj.name));
|
fmtId(coninfo->dobj.name));
|
||||||
@ -17474,7 +17466,8 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
|
|||||||
|
|
||||||
if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
|
if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
|
||||||
{
|
{
|
||||||
appendPQExpBuffer(query, "\nALTER TABLE %s ",
|
appendPQExpBuffer(query, "\nALTER %sTABLE %s ", foreign,
|
||||||
|
tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
|
||||||
fmtQualifiedDumpable(tbinfo));
|
fmtQualifiedDumpable(tbinfo));
|
||||||
switch (tginfo->tgenabled)
|
switch (tginfo->tgenabled)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user