Skip to content

Commit 94e4670

Browse files
committed
Fix pg_dump to ensure that a comment on a table CHECK constraint cannot
be emitted too soon. The previous code got this right in the case where the CHECK was emitted as a separate ALTER TABLE command, but not in the case where the CHECK is emitted right in CREATE TABLE. Per report from Slawomir Sudnik. Note: this code is pretty ugly; it'd perhaps be better to treat comments as independently sortable dump objects. That'd be much too invasive a change for RC time though.
1 parent af80de1 commit 94e4670

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.394 2004/12/03 18:48:19 tgl Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.395 2004/12/14 21:35:20 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -142,6 +142,7 @@ static void dumpAttrDef(Archive *fout, AttrDefInfo *adinfo);
142142
static void dumpSequence(Archive *fout, TableInfo *tbinfo);
143143
static void dumpIndex(Archive *fout, IndxInfo *indxinfo);
144144
static void dumpConstraint(Archive *fout, ConstraintInfo *coninfo);
145+
static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo);
145146

146147
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
147148
const char *type, const char *name,
@@ -3939,6 +3940,12 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
39393940
constrs[j].conindex = 0;
39403941
constrs[j].coninherited = false;
39413942
constrs[j].separate = false;
3943+
/*
3944+
* Mark the constraint as needing to appear before the
3945+
* table --- this is so that any other dependencies of
3946+
* the constraint will be emitted before we try to create
3947+
* the table.
3948+
*/
39423949
addObjectDependency(&tbinfo->dobj,
39433950
constrs[j].dobj.dumpId);
39443951

@@ -4005,6 +4012,13 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
40054012
* plus catalog ID and subid which are the lookup key for pg_description,
40064013
* plus the dump ID for the object (for setting a dependency).
40074014
* If a matching pg_description entry is found, it is dumped.
4015+
*
4016+
* Note: although this routine takes a dumpId for dependency purposes,
4017+
* that purpose is just to mark the dependency in the emitted dump file
4018+
* for possible future use by pg_restore. We do NOT use it for determining
4019+
* ordering of the comment in the dump file, because this routine is called
4020+
* after dependency sorting occurs. This routine should be called just after
4021+
* calling ArchiveEntry() for the specified object.
40084022
*/
40094023
static void
40104024
dumpComment(Archive *fout, const char *target,
@@ -6725,6 +6739,17 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
67256739
/* Dump Table Comments */
67266740
dumpTableComment(fout, tbinfo, reltypename);
67276741

6742+
/* Dump comments on inlined table constraints */
6743+
for (j = 0; j < tbinfo->ncheck; j++)
6744+
{
6745+
ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
6746+
6747+
if (constr->coninherited || constr->separate)
6748+
continue;
6749+
6750+
dumpTableConstraintComment(fout, constr);
6751+
}
6752+
67286753
destroyPQExpBuffer(query);
67296754
destroyPQExpBuffer(q);
67306755
destroyPQExpBuffer(delq);
@@ -6836,7 +6861,8 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
68366861

68376862
/*
68386863
* If there's an associated constraint, don't dump the index per se,
6839-
* but do dump any comment for it.
6864+
* but do dump any comment for it. (This is safe because dependency
6865+
* ordering will have ensured the constraint is emitted first.)
68406866
*/
68416867
if (indxinfo->indexconstraint == 0)
68426868
{
@@ -7078,28 +7104,43 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
70787104
}
70797105

70807106
/* Dump Constraint Comments --- only works for table constraints */
7081-
if (tbinfo)
7082-
{
7083-
resetPQExpBuffer(q);
7084-
appendPQExpBuffer(q, "CONSTRAINT %s ",
7085-
fmtId(coninfo->dobj.name));
7086-
appendPQExpBuffer(q, "ON %s",
7087-
fmtId(tbinfo->dobj.name));
7088-
dumpComment(fout, q->data,
7089-
tbinfo->dobj.namespace->dobj.name,
7090-
tbinfo->usename,
7091-
coninfo->dobj.catId, 0, coninfo->dobj.dumpId);
7092-
}
7107+
if (tbinfo && coninfo->separate)
7108+
dumpTableConstraintComment(fout, coninfo);
70937109

70947110
destroyPQExpBuffer(q);
70957111
destroyPQExpBuffer(delq);
70967112
}
70977113

7114+
/*
7115+
* dumpTableConstraintComment --- dump a constraint's comment if any
7116+
*
7117+
* This is split out because we need the function in two different places
7118+
* depending on whether the constraint is dumped as part of CREATE TABLE
7119+
* or as a separate ALTER command.
7120+
*/
7121+
static void
7122+
dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
7123+
{
7124+
TableInfo *tbinfo = coninfo->contable;
7125+
PQExpBuffer q = createPQExpBuffer();
7126+
7127+
appendPQExpBuffer(q, "CONSTRAINT %s ",
7128+
fmtId(coninfo->dobj.name));
7129+
appendPQExpBuffer(q, "ON %s",
7130+
fmtId(tbinfo->dobj.name));
7131+
dumpComment(fout, q->data,
7132+
tbinfo->dobj.namespace->dobj.name,
7133+
tbinfo->usename,
7134+
coninfo->dobj.catId, 0,
7135+
coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
7136+
7137+
destroyPQExpBuffer(q);
7138+
}
7139+
70987140
/*
70997141
* setMaxOid -
71007142
* find the maximum oid and generate a COPY statement to set it
7101-
*/
7102-
7143+
*/
71037144
static void
71047145
setMaxOid(Archive *fout)
71057146
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy