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

Commit 87ab464

Browse files
committed
Further fixes for CREATE TABLE LIKE: cope with self-referential FKs.
Commit 5028981 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 4097a79 commit 87ab464

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

src/backend/parser/parse_utilcmd.c

Lines changed: 22 additions & 5 deletions
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
*/
@@ -910,7 +926,7 @@ transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint)
910926
* Change the LIKE <srctable> portion of a CREATE TABLE statement into
911927
* column definitions that recreate the user defined column portions of
912928
* <srctable>. Also, if there are any LIKE options that we can't fully
913-
* process at this point, add the TableLikeClause to cxt->alist, which
929+
* process at this point, add the TableLikeClause to cxt->likeclauses, which
914930
* will cause utility.c to call expandTableLikeClause() after the new
915931
* table has been created.
916932
*/
@@ -1074,15 +1090,15 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
10741090
* We cannot yet deal with defaults, CHECK constraints, or indexes, since
10751091
* we don't yet know what column numbers the copied columns will have in
10761092
* the finished table. If any of those options are specified, add the
1077-
* LIKE clause to cxt->alist so that expandTableLikeClause will be called
1078-
* after we do know that.
1093+
* LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
1094+
* called after we do know that.
10791095
*/
10801096
if (table_like_clause->options &
10811097
(CREATE_TABLE_LIKE_DEFAULTS |
10821098
CREATE_TABLE_LIKE_GENERATED |
10831099
CREATE_TABLE_LIKE_CONSTRAINTS |
10841100
CREATE_TABLE_LIKE_INDEXES))
1085-
cxt->alist = lappend(cxt->alist, table_like_clause);
1101+
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
10861102

10871103
/*
10881104
* We may copy extended statistics if requested, since the representation
@@ -2664,7 +2680,7 @@ transformFKConstraints(CreateStmtContext *cxt,
26642680
* Note: the ADD CONSTRAINT command must also execute after any index
26652681
* creation commands. Thus, this should run after
26662682
* transformIndexConstraints, so that the CREATE INDEX commands are
2667-
* already in cxt->alist.
2683+
* already in cxt->alist. See also the handling of cxt->likeclauses.
26682684
*/
26692685
if (!isAddConstraint)
26702686
{
@@ -3175,6 +3191,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
31753191
cxt.ckconstraints = NIL;
31763192
cxt.fkconstraints = NIL;
31773193
cxt.ixconstraints = NIL;
3194+
cxt.likeclauses = NIL;
31783195
cxt.extstats = NIL;
31793196
cxt.blist = NIL;
31803197
cxt.alist = NIL;

src/backend/tcop/utility.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -994,17 +994,22 @@ ProcessUtilitySlow(ParseState *pstate,
994994
case T_CreateForeignTableStmt:
995995
{
996996
List *stmts;
997-
ListCell *l;
998997
RangeVar *table_rv = NULL;
999998

1000999
/* Run parse analysis ... */
10011000
stmts = transformCreateStmt((CreateStmt *) parsetree,
10021001
queryString);
10031002

1004-
/* ... and do it */
1005-
foreach(l, stmts)
1003+
/*
1004+
* ... and do it. We can't use foreach() because we may
1005+
* modify the list midway through, so pick off the
1006+
* elements one at a time, the hard way.
1007+
*/
1008+
while (stmts != NIL)
10061009
{
1007-
Node *stmt = (Node *) lfirst(l);
1010+
Node *stmt = (Node *) linitial(stmts);
1011+
1012+
stmts = list_delete_first(stmts);
10081013

10091014
if (IsA(stmt, CreateStmt))
10101015
{
@@ -1070,23 +1075,16 @@ ProcessUtilitySlow(ParseState *pstate,
10701075
/*
10711076
* Do delayed processing of LIKE options. This
10721077
* will result in additional sub-statements for us
1073-
* to process. We can just tack those onto the
1074-
* to-do list.
1078+
* to process. Those should get done before any
1079+
* remaining actions, so prepend them to "stmts".
10751080
*/
10761081
TableLikeClause *like = (TableLikeClause *) stmt;
10771082
List *morestmts;
10781083

10791084
Assert(table_rv != NULL);
10801085

10811086
morestmts = expandTableLikeClause(table_rv, like);
1082-
stmts = list_concat(stmts, morestmts);
1083-
1084-
/*
1085-
* We don't need a CCI now, besides which the "l"
1086-
* list pointer is now possibly invalid, so just
1087-
* skip the CCI test below.
1088-
*/
1089-
continue;
1087+
stmts = list_concat(morestmts, stmts);
10901088
}
10911089
else
10921090
{
@@ -1114,7 +1112,7 @@ ProcessUtilitySlow(ParseState *pstate,
11141112
}
11151113

11161114
/* Need CCI between commands */
1117-
if (lnext(l) != NULL)
1115+
if (stmts != NIL)
11181116
CommandCounterIncrement();
11191117
}
11201118

src/test/regress/expected/create_table_like.out

Lines changed: 16 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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)