@ -86,6 +86,7 @@ typedef struct
List * ckconstraints ; /* CHECK constraints */
List * ckconstraints ; /* CHECK constraints */
List * fkconstraints ; /* FOREIGN KEY constraints */
List * fkconstraints ; /* FOREIGN KEY constraints */
List * ixconstraints ; /* index-creating constraints */
List * ixconstraints ; /* index-creating constraints */
List * likeclauses ; /* LIKE clauses that need post-processing */
List * extstats ; /* cloned extended statistics */
List * extstats ; /* cloned extended statistics */
List * blist ; /* "before list" of things to do before
List * blist ; /* "before list" of things to do before
* creating the table */
* creating the table */
@ -243,6 +244,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
cxt . ckconstraints = NIL ;
cxt . ckconstraints = NIL ;
cxt . fkconstraints = NIL ;
cxt . fkconstraints = NIL ;
cxt . ixconstraints = NIL ;
cxt . ixconstraints = NIL ;
cxt . likeclauses = NIL ;
cxt . extstats = NIL ;
cxt . extstats = NIL ;
cxt . blist = NIL ;
cxt . blist = NIL ;
cxt . alist = NIL ;
cxt . alist = NIL ;
@ -309,6 +311,20 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
*/
*/
transformIndexConstraints ( & cxt ) ;
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 .
* Postprocess foreign - key constraints .
*/
*/
@ -923,7 +939,7 @@ transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint)
* Change the LIKE < srctable > portion of a CREATE TABLE statement into
* Change the LIKE < srctable > portion of a CREATE TABLE statement into
* column definitions that recreate the user defined column portions of
* column definitions that recreate the user defined column portions of
* < srctable > . Also , if there are any LIKE options that we can ' t fully
* < 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
* will cause utility . c to call expandTableLikeClause ( ) after the new
* table has been created .
* table has been created .
*/
*/
@ -1088,15 +1104,15 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
* We cannot yet deal with defaults , CHECK constraints , or indexes , since
* We cannot yet deal with defaults , CHECK constraints , or indexes , since
* we don ' t yet know what column numbers the copied columns will have in
* 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
* the finished table . If any of those options are specified , add the
* LIKE clause to cxt - > alist so that expandTableLikeClause will be called
* LIKE clause to cxt - > likeclauses so that expandTableLikeClause will be
* after we do know that .
* called after we do know that .
*/
*/
if ( table_like_clause - > options &
if ( table_like_clause - > options &
( CREATE_TABLE_LIKE_DEFAULTS |
( CREATE_TABLE_LIKE_DEFAULTS |
CREATE_TABLE_LIKE_GENERATED |
CREATE_TABLE_LIKE_GENERATED |
CREATE_TABLE_LIKE_CONSTRAINTS |
CREATE_TABLE_LIKE_CONSTRAINTS |
CREATE_TABLE_LIKE_INDEXES ) )
CREATE_TABLE_LIKE_INDEXES ) )
cxt - > alist = lappend ( cxt - > alist , table_like_clause ) ;
cxt - > likeclauses = lappend ( cxt - > likeclauses , table_like_clause ) ;
/*
/*
* We may copy extended statistics if requested , since the representation
* We may copy extended statistics if requested , since the representation
@ -2692,7 +2708,7 @@ transformFKConstraints(CreateStmtContext *cxt,
* Note : the ADD CONSTRAINT command must also execute after any index
* Note : the ADD CONSTRAINT command must also execute after any index
* creation commands . Thus , this should run after
* creation commands . Thus , this should run after
* transformIndexConstraints , so that the CREATE INDEX commands are
* 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 )
if ( ! isAddConstraint )
{
{
@ -3205,6 +3221,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
cxt . ckconstraints = NIL ;
cxt . ckconstraints = NIL ;
cxt . fkconstraints = NIL ;
cxt . fkconstraints = NIL ;
cxt . ixconstraints = NIL ;
cxt . ixconstraints = NIL ;
cxt . likeclauses = NIL ;
cxt . extstats = NIL ;
cxt . extstats = NIL ;
cxt . blist = NIL ;
cxt . blist = NIL ;
cxt . alist = NIL ;
cxt . alist = NIL ;