Skip to content

Commit c4ebf7b

Browse files
committed
Parser was dropping foreign-key constraints on the floor if present in
an ALTER TABLE ADD COLUMN command. Per bug #896.
1 parent f4a72f7 commit c4ebf7b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/backend/parser/analyze.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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/13 20:45:21 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.264 2003/02/13 22:50:01 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -111,7 +111,8 @@ static void transformTableConstraint(ParseState *pstate,
111111
static void transformIndexConstraints(ParseState *pstate,
112112
CreateStmtContext *cxt);
113113
static void transformFKConstraints(ParseState *pstate,
114-
CreateStmtContext *cxt);
114+
CreateStmtContext *cxt,
115+
bool isAddConstraint);
115116
static void applyColumnNames(List *dst, List *src);
116117
static List *getSetColTypes(ParseState *pstate, Node *node);
117118
static 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

12891290
static void
1290-
transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt)
1291+
transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt,
1292+
bool isAddConstraint)
12911293
{
12921294
if (cxt->fkconstraints == NIL)
12931295
return;
@@ -1296,16 +1298,16 @@ transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt)
12961298
cxt->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 basic CREATE 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 basic command 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
{
13101312
AlterTableStmt *alterstmt = makeNode(AlterTableStmt);
13111313
List *fkclist;
@@ -2257,7 +2259,7 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt,
22572259
(ColumnDef *) stmt->def);
22582260

22592261
transformIndexConstraints(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,
22942296
elog(ERROR, "Unexpected node type in ALTER TABLE ADD CONSTRAINT");
22952297

22962298
transformIndexConstraints(pstate, &cxt);
2297-
transformFKConstraints(pstate, &cxt);
2299+
transformFKConstraints(pstate, &cxt, true);
22982300

22992301
Assert(cxt.columns == NIL);
2302+
/* fkconstraints should be put into my own stmt in this case */
23002303
stmt->def = (Node *) nconc(cxt.ckconstraints, cxt.fkconstraints);
23012304
*extras_before = nconc(*extras_before, cxt.blist);
23022305
*extras_after = nconc(cxt.alist, *extras_after);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy