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

Commit 98f3f1d

Browse files
committed
Further fixes for CREATE TABLE LIKE: cope with self-referential FKs.
Commit 502898192 was too careless about the order of execution of the additional ALTER TABLE operations generated by expandTableLikeClause. It just stuck them all at the end, which seems okay for most purposes. But it falls down in the case where LIKE is importing a primary key or unique index and the outer CREATE TABLE includes a FOREIGN KEY constraint that needs to depend on that index. Weird as that is, it used to work, so we ought to keep it working. To fix, make parse_utilcmd.c insert LIKE clauses between index-creation and FK-creation commands in the transformed list of commands, and change utility.c so that the commands generated by expandTableLikeClause are executed immediately not at the end. One could imagine scenarios where this wouldn't work either; but currently expandTableLikeClause only makes column default expressions, CHECK constraints, and indexes, and this ordering seems fine for those. Per bug #16730 from Sofoklis Papasofokli. Like the previous patch, back-patch to all supported branches. Discussion: https://postgr.es/m/16730-b902f7e6e0276b30@postgresql.org
1 parent 95d3954 commit 98f3f1d

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

src/backend/parser/parse_utilcmd.c

+22-5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ typedef struct
8686
List *ckconstraints; /* CHECK constraints */
8787
List *fkconstraints; /* FOREIGN KEY constraints */
8888
List *ixconstraints; /* index-creating constraints */
89+
List *likeclauses; /* LIKE clauses that need post-processing */
8990
List *extstats; /* cloned extended statistics */
9091
List *blist; /* "before list" of things to do before
9192
* creating the table */
@@ -243,6 +244,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
243244
cxt.ckconstraints = NIL;
244245
cxt.fkconstraints = NIL;
245246
cxt.ixconstraints = NIL;
247+
cxt.likeclauses = NIL;
246248
cxt.extstats = NIL;
247249
cxt.blist = NIL;
248250
cxt.alist = NIL;
@@ -309,6 +311,20 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
309311
*/
310312
transformIndexConstraints(&cxt);
311313

314+
/*
315+
* Re-consideration of LIKE clauses should happen after creation of
316+
* indexes, but before creation of foreign keys. This order is critical
317+
* because a LIKE clause may attempt to create a primary key. If there's
318+
* also a pkey in the main CREATE TABLE list, creation of that will not
319+
* check for a duplicate at runtime (since index_check_primary_key()
320+
* expects that we rejected dups here). Creation of the LIKE-generated
321+
* pkey behaves like ALTER TABLE ADD, so it will check, but obviously that
322+
* only works if it happens second. On the other hand, we want to make
323+
* pkeys before foreign key constraints, in case the user tries to make a
324+
* self-referential FK.
325+
*/
326+
cxt.alist = list_concat(cxt.alist, cxt.likeclauses);
327+
312328
/*
313329
* Postprocess foreign-key constraints.
314330
*/
@@ -920,7 +936,7 @@ transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint)
920936
* Change the LIKE <srctable> portion of a CREATE TABLE statement into
921937
* column definitions that recreate the user defined column portions of
922938
* <srctable>. Also, if there are any LIKE options that we can't fully
923-
* process at this point, add the TableLikeClause to cxt->alist, which
939+
* process at this point, add the TableLikeClause to cxt->likeclauses, which
924940
* will cause utility.c to call expandTableLikeClause() after the new
925941
* table has been created.
926942
*/
@@ -1085,15 +1101,15 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
10851101
* We cannot yet deal with defaults, CHECK constraints, or indexes, since
10861102
* we don't yet know what column numbers the copied columns will have in
10871103
* the finished table. If any of those options are specified, add the
1088-
* LIKE clause to cxt->alist so that expandTableLikeClause will be called
1089-
* after we do know that.
1104+
* LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
1105+
* called after we do know that.
10901106
*/
10911107
if (table_like_clause->options &
10921108
(CREATE_TABLE_LIKE_DEFAULTS |
10931109
CREATE_TABLE_LIKE_GENERATED |
10941110
CREATE_TABLE_LIKE_CONSTRAINTS |
10951111
CREATE_TABLE_LIKE_INDEXES))
1096-
cxt->alist = lappend(cxt->alist, table_like_clause);
1112+
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
10971113

10981114
/*
10991115
* We may copy extended statistics if requested, since the representation
@@ -2690,7 +2706,7 @@ transformFKConstraints(CreateStmtContext *cxt,
26902706
* Note: the ADD CONSTRAINT command must also execute after any index
26912707
* creation commands. Thus, this should run after
26922708
* transformIndexConstraints, so that the CREATE INDEX commands are
2693-
* already in cxt->alist.
2709+
* already in cxt->alist. See also the handling of cxt->likeclauses.
26942710
*/
26952711
if (!isAddConstraint)
26962712
{
@@ -3203,6 +3219,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
32033219
cxt.ckconstraints = NIL;
32043220
cxt.fkconstraints = NIL;
32053221
cxt.ixconstraints = NIL;
3222+
cxt.likeclauses = NIL;
32063223
cxt.extstats = NIL;
32073224
cxt.blist = NIL;
32083225
cxt.alist = NIL;

src/backend/tcop/utility.c

+13-15
Original file line numberDiff line numberDiff line change
@@ -1136,17 +1136,22 @@ ProcessUtilitySlow(ParseState *pstate,
11361136
case T_CreateForeignTableStmt:
11371137
{
11381138
List *stmts;
1139-
ListCell *l;
11401139
RangeVar *table_rv = NULL;
11411140

11421141
/* Run parse analysis ... */
11431142
stmts = transformCreateStmt((CreateStmt *) parsetree,
11441143
queryString);
11451144

1146-
/* ... and do it */
1147-
foreach(l, stmts)
1145+
/*
1146+
* ... and do it. We can't use foreach() because we may
1147+
* modify the list midway through, so pick off the
1148+
* elements one at a time, the hard way.
1149+
*/
1150+
while (stmts != NIL)
11481151
{
1149-
Node *stmt = (Node *) lfirst(l);
1152+
Node *stmt = (Node *) linitial(stmts);
1153+
1154+
stmts = list_delete_first(stmts);
11501155

11511156
if (IsA(stmt, CreateStmt))
11521157
{
@@ -1212,23 +1217,16 @@ ProcessUtilitySlow(ParseState *pstate,
12121217
/*
12131218
* Do delayed processing of LIKE options. This
12141219
* will result in additional sub-statements for us
1215-
* to process. We can just tack those onto the
1216-
* to-do list.
1220+
* to process. Those should get done before any
1221+
* remaining actions, so prepend them to "stmts".
12171222
*/
12181223
TableLikeClause *like = (TableLikeClause *) stmt;
12191224
List *morestmts;
12201225

12211226
Assert(table_rv != NULL);
12221227

12231228
morestmts = expandTableLikeClause(table_rv, like);
1224-
stmts = list_concat(stmts, morestmts);
1225-
1226-
/*
1227-
* We don't need a CCI now, besides which the "l"
1228-
* list pointer is now possibly invalid, so just
1229-
* skip the CCI test below.
1230-
*/
1231-
continue;
1229+
stmts = list_concat(morestmts, stmts);
12321230
}
12331231
else
12341232
{
@@ -1256,7 +1254,7 @@ ProcessUtilitySlow(ParseState *pstate,
12561254
}
12571255

12581256
/* Need CCI between commands */
1259-
if (lnext(stmts, l) != NULL)
1257+
if (stmts != NIL)
12601258
CommandCounterIncrement();
12611259
}
12621260

src/test/regress/expected/create_table_like.out

+16
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,22 @@ INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail
282282
ERROR: duplicate key value violates unique constraint "inhg_x_key"
283283
DETAIL: Key (x)=(15) already exists.
284284
DROP TABLE inhg;
285+
DROP TABLE inhz;
286+
/* Use primary key imported by LIKE for self-referential FK constraint */
287+
CREATE TABLE inhz (x text REFERENCES inhz, LIKE inhx INCLUDING INDEXES);
288+
\d inhz
289+
Table "public.inhz"
290+
Column | Type | Collation | Nullable | Default
291+
--------+------+-----------+----------+---------
292+
x | text | | |
293+
xx | text | | not null |
294+
Indexes:
295+
"inhz_pkey" PRIMARY KEY, btree (xx)
296+
Foreign-key constraints:
297+
"inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
298+
Referenced by:
299+
TABLE "inhz" CONSTRAINT "inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
300+
285301
DROP TABLE inhz;
286302
-- including storage and comments
287303
CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text);

src/test/regress/sql/create_table_like.sql

+5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail
114114
DROP TABLE inhg;
115115
DROP TABLE inhz;
116116

117+
/* Use primary key imported by LIKE for self-referential FK constraint */
118+
CREATE TABLE inhz (x text REFERENCES inhz, LIKE inhx INCLUDING INDEXES);
119+
\d inhz
120+
DROP TABLE inhz;
121+
117122
-- including storage and comments
118123
CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text);
119124
CREATE INDEX ctlt1_b_key ON ctlt1 (b);

0 commit comments

Comments
 (0)