List *ckconstraints; /* CHECK constraints */
List *fkconstraints; /* FOREIGN KEY constraints */
List *ixconstraints; /* index-creating constraints */
+ List *likeclauses; /* LIKE clauses that need post-processing */
List *blist; /* "before list" of things to do before
* creating the table */
List *alist; /* "after list" of things to do after creating
cxt.ckconstraints = NIL;
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
+ cxt.likeclauses = NIL;
cxt.blist = NIL;
cxt.alist = NIL;
cxt.pkey = NULL;
*/
transformIndexConstraints(&cxt);
+ /*
+ * Re-consideration of LIKE clauses should happen after creation of
+ * indexes, but before creation of foreign keys. This order is critical
+ * because a LIKE clause may attempt to create a primary key. If there's
+ * also a pkey in the main CREATE TABLE list, creation of that will not
+ * check for a duplicate at runtime (since index_check_primary_key()
+ * expects that we rejected dups here). Creation of the LIKE-generated
+ * pkey behaves like ALTER TABLE ADD, so it will check, but obviously that
+ * only works if it happens second. On the other hand, we want to make
+ * pkeys before foreign key constraints, in case the user tries to make a
+ * self-referential FK.
+ */
+ cxt.alist = list_concat(cxt.alist, cxt.likeclauses);
+
/*
* Postprocess foreign-key constraints.
*/
* Change the LIKE <srctable> portion of a CREATE TABLE statement into
* column definitions that recreate the user defined column portions of
* <srctable>. Also, if there are any LIKE options that we can't fully
- * process at this point, add the TableLikeClause to cxt->alist, which
+ * process at this point, add the TableLikeClause to cxt->likeclauses, which
* will cause utility.c to call expandTableLikeClause() after the new
* table has been created.
*/
* We cannot yet deal with CHECK constraints or indexes, since we don't
* yet know what column numbers the copied columns will have in the
* finished table. If any of those options are specified, add the LIKE
- * clause to cxt->alist so that expandTableLikeClause will be called after
- * we do know that.
+ * clause to cxt->likeclauses so that expandTableLikeClause will be called
+ * after we do know that.
*/
if (table_like_clause->options &
(CREATE_TABLE_LIKE_CONSTRAINTS |
CREATE_TABLE_LIKE_INDEXES))
- cxt->alist = lappend(cxt->alist, table_like_clause);
+ cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
/*
* Close the parent rel, but keep our AccessShareLock on it until xact
* Note: the ADD CONSTRAINT command must also execute after any index
* creation commands. Thus, this should run after
* transformIndexConstraints, so that the CREATE INDEX commands are
- * already in cxt->alist.
+ * already in cxt->alist. See also the handling of cxt->likeclauses.
*/
if (!isAddConstraint)
{
cxt.ckconstraints = NIL;
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
+ cxt.likeclauses = NIL;
cxt.blist = NIL;
cxt.alist = NIL;
cxt.pkey = NULL;
case T_CreateForeignTableStmt:
{
List *stmts;
- ListCell *l;
RangeVar *table_rv = NULL;
/* Run parse analysis ... */
stmts = transformCreateStmt((CreateStmt *) parsetree,
queryString);
- /* ... and do it */
- foreach(l, stmts)
+ /*
+ * ... and do it. We can't use foreach() because we may
+ * modify the list midway through, so pick off the
+ * elements one at a time, the hard way.
+ */
+ while (stmts != NIL)
{
- Node *stmt = (Node *) lfirst(l);
+ Node *stmt = (Node *) linitial(stmts);
+
+ stmts = list_delete_first(stmts);
if (IsA(stmt, CreateStmt))
{
/*
* Do delayed processing of LIKE options. This
* will result in additional sub-statements for us
- * to process. We can just tack those onto the
- * to-do list.
+ * to process. Those should get done before any
+ * remaining actions, so prepend them to "stmts".
*/
TableLikeClause *like = (TableLikeClause *) stmt;
List *morestmts;
Assert(table_rv != NULL);
morestmts = expandTableLikeClause(table_rv, like);
- stmts = list_concat(stmts, morestmts);
-
- /*
- * We don't need a CCI now, besides which the "l"
- * list pointer is now possibly invalid, so just
- * skip the CCI test below.
- */
- continue;
+ stmts = list_concat(morestmts, stmts);
}
else
{
}
/* Need CCI between commands */
- if (lnext(l) != NULL)
+ if (stmts != NIL)
CommandCounterIncrement();
}
ERROR: duplicate key value violates unique constraint "inhg_x_key"
DETAIL: Key (x)=(15) already exists.
DROP TABLE inhg;
+DROP TABLE inhz;
+/* Use primary key imported by LIKE for self-referential FK constraint */
+CREATE TABLE inhz (x text REFERENCES inhz, LIKE inhx INCLUDING INDEXES);
+\d inhz
+ Table "public.inhz"
+ Column | Type | Modifiers
+--------+------+-----------
+ x | text |
+ xx | text | not null
+Indexes:
+ "inhz_pkey" PRIMARY KEY, btree (xx)
+Foreign-key constraints:
+ "inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
+Referenced by:
+ TABLE "inhz" CONSTRAINT "inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
+
DROP TABLE inhz;
-- including storage and comments
CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text);