Skip to content

Commit 159b677

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 d726e44 commit 159b677

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
@@ -81,6 +81,7 @@ typedef struct
8181
List *ckconstraints; /* CHECK constraints */
8282
List *fkconstraints; /* FOREIGN KEY constraints */
8383
List *ixconstraints; /* index-creating constraints */
84+
List *likeclauses; /* LIKE clauses that need post-processing */
8485
List *blist; /* "before list" of things to do before
8586
* creating the table */
8687
List *alist; /* "after list" of things to do after creating
@@ -228,6 +229,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
228229
cxt.ckconstraints = NIL;
229230
cxt.fkconstraints = NIL;
230231
cxt.ixconstraints = NIL;
232+
cxt.likeclauses = NIL;
231233
cxt.blist = NIL;
232234
cxt.alist = NIL;
233235
cxt.pkey = NULL;
@@ -307,6 +309,20 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
307309
*/
308310
transformIndexConstraints(&cxt);
309311

312+
/*
313+
* Re-consideration of LIKE clauses should happen after creation of
314+
* indexes, but before creation of foreign keys. This order is critical
315+
* because a LIKE clause may attempt to create a primary key. If there's
316+
* also a pkey in the main CREATE TABLE list, creation of that will not
317+
* check for a duplicate at runtime (since index_check_primary_key()
318+
* expects that we rejected dups here). Creation of the LIKE-generated
319+
* pkey behaves like ALTER TABLE ADD, so it will check, but obviously that
320+
* only works if it happens second. On the other hand, we want to make
321+
* pkeys before foreign key constraints, in case the user tries to make a
322+
* self-referential FK.
323+
*/
324+
cxt.alist = list_concat(cxt.alist, cxt.likeclauses);
325+
310326
/*
311327
* Postprocess foreign-key constraints.
312328
*/
@@ -722,7 +738,7 @@ transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint)
722738
* Change the LIKE <srctable> portion of a CREATE TABLE statement into
723739
* column definitions that recreate the user defined column portions of
724740
* <srctable>. Also, if there are any LIKE options that we can't fully
725-
* process at this point, add the TableLikeClause to cxt->alist, which
741+
* process at this point, add the TableLikeClause to cxt->likeclauses, which
726742
* will cause utility.c to call expandTableLikeClause() after the new
727743
* table has been created.
728744
*/
@@ -892,13 +908,13 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
892908
* We cannot yet deal with CHECK constraints or indexes, since we don't
893909
* yet know what column numbers the copied columns will have in the
894910
* finished table. If any of those options are specified, add the LIKE
895-
* clause to cxt->alist so that expandTableLikeClause will be called after
896-
* we do know that.
911+
* clause to cxt->likeclauses so that expandTableLikeClause will be called
912+
* after we do know that.
897913
*/
898914
if (table_like_clause->options &
899915
(CREATE_TABLE_LIKE_CONSTRAINTS |
900916
CREATE_TABLE_LIKE_INDEXES))
901-
cxt->alist = lappend(cxt->alist, table_like_clause);
917+
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
902918

903919
/*
904920
* Close the parent rel, but keep our AccessShareLock on it until xact
@@ -2065,7 +2081,7 @@ transformFKConstraints(CreateStmtContext *cxt,
20652081
* Note: the ADD CONSTRAINT command must also execute after any index
20662082
* creation commands. Thus, this should run after
20672083
* transformIndexConstraints, so that the CREATE INDEX commands are
2068-
* already in cxt->alist.
2084+
* already in cxt->alist. See also the handling of cxt->likeclauses.
20692085
*/
20702086
if (!isAddConstraint)
20712087
{
@@ -2574,6 +2590,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
25742590
cxt.ckconstraints = NIL;
25752591
cxt.fkconstraints = NIL;
25762592
cxt.ixconstraints = NIL;
2593+
cxt.likeclauses = NIL;
25772594
cxt.blist = NIL;
25782595
cxt.alist = NIL;
25792596
cxt.pkey = NULL;

src/backend/tcop/utility.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -963,17 +963,22 @@ ProcessUtilitySlow(Node *parsetree,
963963
case T_CreateForeignTableStmt:
964964
{
965965
List *stmts;
966-
ListCell *l;
967966
RangeVar *table_rv = NULL;
968967

969968
/* Run parse analysis ... */
970969
stmts = transformCreateStmt((CreateStmt *) parsetree,
971970
queryString);
972971

973-
/* ... and do it */
974-
foreach(l, stmts)
972+
/*
973+
* ... and do it. We can't use foreach() because we may
974+
* modify the list midway through, so pick off the
975+
* elements one at a time, the hard way.
976+
*/
977+
while (stmts != NIL)
975978
{
976-
Node *stmt = (Node *) lfirst(l);
979+
Node *stmt = (Node *) linitial(stmts);
980+
981+
stmts = list_delete_first(stmts);
977982

978983
if (IsA(stmt, CreateStmt))
979984
{
@@ -1037,23 +1042,16 @@ ProcessUtilitySlow(Node *parsetree,
10371042
/*
10381043
* Do delayed processing of LIKE options. This
10391044
* will result in additional sub-statements for us
1040-
* to process. We can just tack those onto the
1041-
* to-do list.
1045+
* to process. Those should get done before any
1046+
* remaining actions, so prepend them to "stmts".
10421047
*/
10431048
TableLikeClause *like = (TableLikeClause *) stmt;
10441049
List *morestmts;
10451050

10461051
Assert(table_rv != NULL);
10471052

10481053
morestmts = expandTableLikeClause(table_rv, like);
1049-
stmts = list_concat(stmts, morestmts);
1050-
1051-
/*
1052-
* We don't need a CCI now, besides which the "l"
1053-
* list pointer is now possibly invalid, so just
1054-
* skip the CCI test below.
1055-
*/
1056-
continue;
1054+
stmts = list_concat(morestmts, stmts);
10571055
}
10581056
else
10591057
{
@@ -1071,7 +1069,7 @@ ProcessUtilitySlow(Node *parsetree,
10711069
}
10721070

10731071
/* Need CCI between commands */
1074-
if (lnext(l) != NULL)
1072+
if (stmts != NIL)
10751073
CommandCounterIncrement();
10761074
}
10771075

src/test/regress/expected/create_table_like.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail
114114
ERROR: duplicate key value violates unique constraint "inhg_x_key"
115115
DETAIL: Key (x)=(15) already exists.
116116
DROP TABLE inhg;
117+
DROP TABLE inhz;
118+
/* Use primary key imported by LIKE for self-referential FK constraint */
119+
CREATE TABLE inhz (x text REFERENCES inhz, LIKE inhx INCLUDING INDEXES);
120+
\d inhz
121+
Table "public.inhz"
122+
Column | Type | Modifiers
123+
--------+------+-----------
124+
x | text |
125+
xx | text | not null
126+
Indexes:
127+
"inhz_pkey" PRIMARY KEY, btree (xx)
128+
Foreign-key constraints:
129+
"inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
130+
Referenced by:
131+
TABLE "inhz" CONSTRAINT "inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
132+
117133
DROP TABLE inhz;
118134
-- including storage and comments
119135
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
@@ -66,6 +66,11 @@ INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail
6666
DROP TABLE inhg;
6767
DROP TABLE inhz;
6868

69+
/* Use primary key imported by LIKE for self-referential FK constraint */
70+
CREATE TABLE inhz (x text REFERENCES inhz, LIKE inhx INCLUDING INDEXES);
71+
\d inhz
72+
DROP TABLE inhz;
73+
6974
-- including storage and comments
7075
CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text);
7176
CREATE INDEX ctlt1_b_key ON ctlt1 (b);

0 commit comments

Comments
 (0)