Commit c4ebf7b1 authored by Tom Lane's avatar Tom Lane

Parser was dropping foreign-key constraints on the floor if present in

an ALTER TABLE ADD COLUMN command.  Per bug #896.
parent f4a72f77
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.263 2003/02/13 20:45:21 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.264 2003/02/13 22:50:01 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -111,7 +111,8 @@ static void transformTableConstraint(ParseState *pstate, ...@@ -111,7 +111,8 @@ static void transformTableConstraint(ParseState *pstate,
static void transformIndexConstraints(ParseState *pstate, static void transformIndexConstraints(ParseState *pstate,
CreateStmtContext *cxt); CreateStmtContext *cxt);
static void transformFKConstraints(ParseState *pstate, static void transformFKConstraints(ParseState *pstate,
CreateStmtContext *cxt); CreateStmtContext *cxt,
bool isAddConstraint);
static void applyColumnNames(List *dst, List *src); static void applyColumnNames(List *dst, List *src);
static List *getSetColTypes(ParseState *pstate, Node *node); static List *getSetColTypes(ParseState *pstate, Node *node);
static void transformForUpdate(Query *qry, List *forUpdate); static void transformForUpdate(Query *qry, List *forUpdate);
...@@ -777,7 +778,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt, ...@@ -777,7 +778,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt,
/* /*
* Postprocess foreign-key constraints. * Postprocess foreign-key constraints.
*/ */
transformFKConstraints(pstate, &cxt); transformFKConstraints(pstate, &cxt, false);
/* /*
* Output results. * Output results.
...@@ -1287,7 +1288,8 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt) ...@@ -1287,7 +1288,8 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
} }
static void static void
transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt) transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt,
bool isAddConstraint)
{ {
if (cxt->fkconstraints == NIL) if (cxt->fkconstraints == NIL)
return; return;
...@@ -1296,16 +1298,16 @@ transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt) ...@@ -1296,16 +1298,16 @@ transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt)
cxt->stmtType); cxt->stmtType);
/* /*
* For ALTER TABLE ADD CONSTRAINT, nothing to do. For CREATE TABLE, * For ALTER TABLE ADD CONSTRAINT, nothing to do. For CREATE TABLE or
* gin up an ALTER TABLE ADD CONSTRAINT command to execute after * ALTER TABLE ADD COLUMN, gin up an ALTER TABLE ADD CONSTRAINT command
* the basic CREATE TABLE is complete. * to execute after the basic command is complete.
* *
* 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.
*/ */
if (strcmp(cxt->stmtType, "CREATE TABLE") == 0) if (!isAddConstraint)
{ {
AlterTableStmt *alterstmt = makeNode(AlterTableStmt); AlterTableStmt *alterstmt = makeNode(AlterTableStmt);
List *fkclist; List *fkclist;
...@@ -2257,7 +2259,7 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt, ...@@ -2257,7 +2259,7 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt,
(ColumnDef *) stmt->def); (ColumnDef *) stmt->def);
transformIndexConstraints(pstate, &cxt); transformIndexConstraints(pstate, &cxt);
transformFKConstraints(pstate, &cxt); transformFKConstraints(pstate, &cxt, false);
((ColumnDef *) stmt->def)->constraints = cxt.ckconstraints; ((ColumnDef *) stmt->def)->constraints = cxt.ckconstraints;
*extras_before = nconc(*extras_before, cxt.blist); *extras_before = nconc(*extras_before, cxt.blist);
...@@ -2294,9 +2296,10 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt, ...@@ -2294,9 +2296,10 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt,
elog(ERROR, "Unexpected node type in ALTER TABLE ADD CONSTRAINT"); elog(ERROR, "Unexpected node type in ALTER TABLE ADD CONSTRAINT");
transformIndexConstraints(pstate, &cxt); transformIndexConstraints(pstate, &cxt);
transformFKConstraints(pstate, &cxt); transformFKConstraints(pstate, &cxt, true);
Assert(cxt.columns == NIL); Assert(cxt.columns == NIL);
/* fkconstraints should be put into my own stmt in this case */
stmt->def = (Node *) nconc(cxt.ckconstraints, cxt.fkconstraints); stmt->def = (Node *) nconc(cxt.ckconstraints, cxt.fkconstraints);
*extras_before = nconc(*extras_before, cxt.blist); *extras_before = nconc(*extras_before, cxt.blist);
*extras_after = nconc(cxt.alist, *extras_after); *extras_after = nconc(cxt.alist, *extras_after);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment