Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fd41ba9

Browse files
committed
Dump not-null constraints on inherited columns correctly
With not-null constraints defined in child tables for columns that are coming from their parent tables, we were printing ALTER TABLE SET NOT NULL commands that were missing the constraint name, so the original constraint name was being lost, which is bogus. Fix by instead adding a table-constraint constraint declaration with the correct constraint name in the CREATE TABLE instead. Oversight in commit 14e87ff. We could have fixed it by changing the ALTER TABLE SET NOT NULL to ALTER TABLE ADD CONSTRAINT, but I'm not sure that's any better. A potential problem here might be that if sent to a non-Postgres server, the new pg_dump output would fail because the "CONSTRAINT foo NOT NULL colname" syntax isn't SQL-conforming. However, Postgres' implementation of inheritance is already non-SQL-conforming, so that'd likely fail anyway. This problem was only noticed by Ashutosh's proposed test framework for pg_dump, https://postgr.es/m/CAExHW5uF5V=Cjecx3_Z=7xfh4rg2Wf61PT+hfquzjBqouRzQJQ@mail.gmail.com Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> Reported-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/CAExHW5tbdgAKDfqjDJ-7Fk6PJtHg8D4zUF6FQ4H2Pq8zK38Nyw@mail.gmail.com
1 parent a0ff56e commit fd41ba9

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16220,6 +16220,38 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
1622016220
fmtQualifiedDumpable(coll));
1622116221
}
1622216222
}
16223+
16224+
/*
16225+
* On the other hand, if we choose not to print a column
16226+
* (likely because it is created by inheritance), but the
16227+
* column has a locally-defined not-null constraint, we need
16228+
* to dump the constraint as a standalone object.
16229+
*
16230+
* This syntax isn't SQL-conforming, but if you wanted
16231+
* standard output you wouldn't be creating non-standard
16232+
* objects to begin with.
16233+
*/
16234+
if (!shouldPrintColumn(dopt, tbinfo, j) &&
16235+
!tbinfo->attisdropped[j] &&
16236+
tbinfo->notnull_constrs[j] != NULL &&
16237+
tbinfo->notnull_islocal[j])
16238+
{
16239+
/* Format properly if not first attr */
16240+
if (actual_atts == 0)
16241+
appendPQExpBufferStr(q, " (");
16242+
else
16243+
appendPQExpBufferChar(q, ',');
16244+
appendPQExpBufferStr(q, "\n ");
16245+
actual_atts++;
16246+
16247+
if (tbinfo->notnull_constrs[j][0] == '\0')
16248+
appendPQExpBuffer(q, "NOT NULL %s",
16249+
fmtId(tbinfo->attnames[j]));
16250+
else
16251+
appendPQExpBuffer(q, "CONSTRAINT %s NOT NULL %s",
16252+
tbinfo->notnull_constrs[j],
16253+
fmtId(tbinfo->attnames[j]));
16254+
}
1622316255
}
1622416256

1622516257
/*
@@ -16661,29 +16693,6 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
1666116693
if (tbinfo->attisdropped[j])
1666216694
continue;
1666316695

16664-
/*
16665-
* If we didn't dump the column definition explicitly above, and
16666-
* it is not-null and did not inherit that property from a parent,
16667-
* we have to mark it separately.
16668-
*/
16669-
if (!shouldPrintColumn(dopt, tbinfo, j) &&
16670-
tbinfo->notnull_constrs[j] != NULL &&
16671-
(tbinfo->notnull_islocal[j] && !tbinfo->ispartition && !dopt->binary_upgrade))
16672-
{
16673-
/* No constraint name desired? */
16674-
if (tbinfo->notnull_constrs[j][0] == '\0')
16675-
appendPQExpBuffer(q,
16676-
"ALTER %sTABLE ONLY %s ALTER COLUMN %s SET NOT NULL;\n",
16677-
foreign, qualrelname,
16678-
fmtId(tbinfo->attnames[j]));
16679-
else
16680-
appendPQExpBuffer(q,
16681-
"ALTER %sTABLE ONLY %s ADD CONSTRAINT %s NOT NULL %s;\n",
16682-
foreign, qualrelname,
16683-
tbinfo->notnull_constrs[j],
16684-
fmtId(tbinfo->attnames[j]));
16685-
}
16686-
1668716696
/*
1668816697
* Dump per-column statistics information. We only issue an ALTER
1668916698
* TABLE statement if the attstattarget entry for this column is

0 commit comments

Comments
 (0)