66 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
9- *$Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.263 2003/02/1320:45:21 tgl Exp $
9+ *$Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.264 2003/02/1322:50:01 tgl Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -111,7 +111,8 @@ static void transformTableConstraint(ParseState *pstate,
111111static void transformIndexConstraints (ParseState * pstate ,
112112CreateStmtContext * cxt );
113113static void transformFKConstraints (ParseState * pstate ,
114- CreateStmtContext * cxt );
114+ CreateStmtContext * cxt ,
115+ bool isAddConstraint );
115116static void applyColumnNames (List * dst ,List * src );
116117static List * getSetColTypes (ParseState * pstate ,Node * node );
117118static void transformForUpdate (Query * qry ,List * forUpdate );
@@ -777,7 +778,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt,
777778/*
778779 * Postprocess foreign-key constraints.
779780 */
780- transformFKConstraints (pstate ,& cxt );
781+ transformFKConstraints (pstate ,& cxt , false );
781782
782783/*
783784 * Output results.
@@ -1287,7 +1288,8 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
12871288}
12881289
12891290static void
1290- transformFKConstraints (ParseState * pstate ,CreateStmtContext * cxt )
1291+ transformFKConstraints (ParseState * pstate ,CreateStmtContext * cxt ,
1292+ bool isAddConstraint )
12911293{
12921294if (cxt -> fkconstraints == NIL )
12931295return ;
@@ -1296,16 +1298,16 @@ transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt)
12961298cxt -> stmtType );
12971299
12981300/*
1299- * For ALTER TABLE ADD CONSTRAINT, nothing to do. For CREATE TABLE,
1300- * gin up an ALTER TABLE ADD CONSTRAINT command to execute after
1301- * the basicCREATE TABLE is complete.
1301+ * For ALTER TABLE ADD CONSTRAINT, nothing to do. For CREATE TABLE or
1302+ *ALTER TABLE ADD COLUMN, gin up an ALTER TABLE ADD CONSTRAINT command
1303+ *to execute after the basiccommand is complete.
13021304 *
13031305 * Note: the ADD CONSTRAINT command must also execute after any index
13041306 * creation commands. Thus, this should run after
13051307 * transformIndexConstraints, so that the CREATE INDEX commands are
13061308 * already in cxt->alist.
13071309 */
1308- if (strcmp ( cxt -> stmtType , "CREATE TABLE" ) == 0 )
1310+ if (! isAddConstraint )
13091311{
13101312AlterTableStmt * alterstmt = makeNode (AlterTableStmt );
13111313List * fkclist ;
@@ -2257,7 +2259,7 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt,
22572259 (ColumnDef * )stmt -> def );
22582260
22592261transformIndexConstraints (pstate ,& cxt );
2260- transformFKConstraints (pstate ,& cxt );
2262+ transformFKConstraints (pstate ,& cxt , false );
22612263
22622264((ColumnDef * )stmt -> def )-> constraints = cxt .ckconstraints ;
22632265* extras_before = nconc (* extras_before ,cxt .blist );
@@ -2294,9 +2296,10 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt,
22942296elog (ERROR ,"Unexpected node type in ALTER TABLE ADD CONSTRAINT" );
22952297
22962298transformIndexConstraints (pstate ,& cxt );
2297- transformFKConstraints (pstate ,& cxt );
2299+ transformFKConstraints (pstate ,& cxt , true );
22982300
22992301Assert (cxt .columns == NIL );
2302+ /* fkconstraints should be put into my own stmt in this case */
23002303stmt -> def = (Node * )nconc (cxt .ckconstraints ,cxt .fkconstraints );
23012304* extras_before = nconc (* extras_before ,cxt .blist );
23022305* extras_after = nconc (cxt .alist ,* extras_after );