diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c
index 132cda732b250c958dc4c9c57550cb0e5cf2af3c..e9f27cd50982e538ef65419f27e3da1058ca49db 100644
--- a/src/backend/optimizer/plan/planmain.c
+++ b/src/backend/optimizer/plan/planmain.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.20 1998/02/26 04:32:50 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.21 1998/03/31 23:30:49 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -187,7 +187,9 @@ query_planner(Query *root,
 	 */
 	if (constant_qual)
 	{
-		subplan = (Plan *) make_result((!root->hasAggs && !root->groupClause)
+		subplan = (Plan *) make_result((!root->hasAggs &&
+									    !root->groupClause &&
+									    !root->havingQual)
 									   ? tlist : subplan->targetlist,
 									   (Node *) constant_qual,
 									   subplan);
@@ -195,7 +197,7 @@ query_planner(Query *root,
 		/*
 		 * Change all varno's of the Result's node target list.
 		 */
-		if (!root->hasAggs && !root->groupClause)
+		if (!root->hasAggs && !root->groupClause && !root->havingQual)
 			set_tlist_references(subplan);
 
 		return subplan;
@@ -216,7 +218,7 @@ query_planner(Query *root,
 	 */
 	else
 	{
-		if (!root->hasAggs && !root->groupClause)
+		if (!root->hasAggs && !root->groupClause && !root->havingQual)
 			subplan->targetlist = flatten_tlist_vars(tlist,
 													 subplan->targetlist);
 		return subplan;
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 35c19d6c44f845ce274853fb20ff7ab99eddd7ca..260bf230cf94982cd772fd342ee76f8688a06916 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.24 1998/03/30 16:36:04 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.25 1998/03/31 23:30:51 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -292,11 +292,10 @@ union_planner(Query *parse)
 		((Agg *) result_plan)->aggs =
 			set_agg_tlist_references((Agg *) result_plan); 
 
-		/***S*H***/
-		if(parse->havingQual!=NULL) {
+		if(parse->havingQual != NULL) {
 		  List	   *clause;
 
-		  /***S*H***/ /* set qpqual of having clause */
+		  /* set qpqual of having clause */
 		  ((Agg *) result_plan)->plan.qual=cnfify((Expr *)parse->havingQual,true);
 
 		  foreach(clause, ((Agg *) result_plan)->plan.qual)
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index e1c620956a447c88e94e76d00d3c255e9361ad1c..5cede30f48792fb29b6caea781b70b45dff63ad1 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.22 1998/03/31 04:43:49 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.23 1998/03/31 23:30:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -280,6 +280,8 @@ plan_inherit_query(List *relids,
 		new_root->uniqueFlag = NULL;
 		new_root->sortClause = NULL;
 		new_root->groupClause = NULL;
+		new_root->havingQual = NULL;
+		
 		if (new_root->hasAggs)
 		{
 			new_root->hasAggs = false;
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index 4ec2f1853a17a3664e3dff29864e07a174a8070e..7d107e49c3943341557c15f67dba926408b991bd 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.73 1998/03/30 16:47:02 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.74 1998/03/31 23:31:10 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -319,6 +319,9 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
 
 	/* fix where clause */
 	qry->qual = transformWhereClause(pstate, stmt->whereClause);
+
+	qry->havingQual = transformWhereClause(pstate, stmt->havingClause);
+
 	qry->hasSubLinks = pstate->p_hasSubLinks;
 
 	/* now the range table will not change */
@@ -784,7 +787,6 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
 
 	qry->qual = transformWhereClause(pstate, stmt->whereClause);
 
-        /***S*H***/
 	qry->havingQual = transformWhereClause(pstate, stmt->havingClause);
 
 	qry->hasSubLinks = pstate->p_hasSubLinks;