From c4ebf7b150efdcfe3798790f806cf1ec6a1612b7 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 13 Feb 2003 22:50:01 +0000
Subject: [PATCH] Parser was dropping foreign-key constraints on the floor if
 present in an ALTER TABLE ADD COLUMN command.  Per bug #896.

---
 src/backend/parser/analyze.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index 0736572f0f9..2089008bbf5 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * 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,
 static void transformIndexConstraints(ParseState *pstate,
 						  CreateStmtContext *cxt);
 static void transformFKConstraints(ParseState *pstate,
-					   CreateStmtContext *cxt);
+								   CreateStmtContext *cxt,
+								   bool isAddConstraint);
 static void applyColumnNames(List *dst, List *src);
 static List *getSetColTypes(ParseState *pstate, Node *node);
 static void transformForUpdate(Query *qry, List *forUpdate);
@@ -777,7 +778,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt,
 	/*
 	 * Postprocess foreign-key constraints.
 	 */
-	transformFKConstraints(pstate, &cxt);
+	transformFKConstraints(pstate, &cxt, false);
 
 	/*
 	 * Output results.
@@ -1287,7 +1288,8 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
 }
 
 static void
-transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt)
+transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt,
+					   bool isAddConstraint)
 {
 	if (cxt->fkconstraints == NIL)
 		return;
@@ -1296,16 +1298,16 @@ transformFKConstraints(ParseState *pstate, CreateStmtContext *cxt)
 		 cxt->stmtType);
 
 	/*
-	 * For ALTER TABLE ADD CONSTRAINT, nothing to do.  For CREATE TABLE,
-	 * gin up an ALTER TABLE ADD CONSTRAINT command to execute after
-	 * the basic CREATE TABLE is complete.
+	 * For ALTER TABLE ADD CONSTRAINT, nothing to do.  For CREATE TABLE or
+	 * ALTER TABLE ADD COLUMN, gin up an ALTER TABLE ADD CONSTRAINT command
+	 * to execute after the basic command is complete.
 	 *
 	 * 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.
 	 */
-	if (strcmp(cxt->stmtType, "CREATE TABLE") == 0)
+	if (!isAddConstraint)
 	{
 		AlterTableStmt *alterstmt = makeNode(AlterTableStmt);
 		List	   *fkclist;
@@ -2257,7 +2259,7 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt,
 									  (ColumnDef *) stmt->def);
 
 			transformIndexConstraints(pstate, &cxt);
-			transformFKConstraints(pstate, &cxt);
+			transformFKConstraints(pstate, &cxt, false);
 
 			((ColumnDef *) stmt->def)->constraints = cxt.ckconstraints;
 			*extras_before = nconc(*extras_before, cxt.blist);
@@ -2294,9 +2296,10 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt,
 				elog(ERROR, "Unexpected node type in ALTER TABLE ADD CONSTRAINT");
 
 			transformIndexConstraints(pstate, &cxt);
-			transformFKConstraints(pstate, &cxt);
+			transformFKConstraints(pstate, &cxt, true);
 
 			Assert(cxt.columns == NIL);
+			/* fkconstraints should be put into my own stmt in this case */
 			stmt->def = (Node *) nconc(cxt.ckconstraints, cxt.fkconstraints);
 			*extras_before = nconc(*extras_before, cxt.blist);
 			*extras_after = nconc(cxt.alist, *extras_after);
-- 
GitLab