From e59334994eee14d8f934d57c1a21c25c8d6cbae8 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon, 26 Nov 2001 00:29:15 +0000
Subject: [PATCH] Repair problem with listing rules that have a WHERE condition
 and have an INSERT...SELECT as the first or only action.  Per bug report from
 Sergio Pili.

---
 src/backend/utils/adt/ruleutils.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 03ea936b62e..421c6a828ce 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3,7 +3,7 @@
  *				back to source text
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.87 2001/11/19 19:51:20 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.88 2001/11/26 00:29:15 tgl Exp $
  *
  *	  This software is copyrighted by Jan Wieck - Hamburg.
  *
@@ -178,9 +178,9 @@ pg_get_ruledef(PG_FUNCTION_ARGS)
 		elog(ERROR, "get_ruledef: cannot connect to SPI manager");
 
 	/*
-	 * On the first call prepare the plan to lookup pg_proc. We read
-	 * pg_proc over the SPI manager instead of using the syscache to be
-	 * checked for read access on pg_proc.
+	 * On the first call prepare the plan to lookup pg_rewrite. We read
+	 * pg_rewrite over the SPI manager instead of using the syscache to be
+	 * checked for read access on pg_rewrite.
 	 */
 	if (plan_getrule == NULL)
 	{
@@ -271,9 +271,9 @@ pg_get_viewdef(PG_FUNCTION_ARGS)
 		elog(ERROR, "get_viewdef: cannot connect to SPI manager");
 
 	/*
-	 * On the first call prepare the plan to lookup pg_proc. We read
-	 * pg_proc over the SPI manager instead of using the syscache to be
-	 * checked for read access on pg_proc.
+	 * On the first call prepare the plan to lookup pg_rewrite. We read
+	 * pg_rewrite over the SPI manager instead of using the syscache to be
+	 * checked for read access on pg_rewrite.
 	 */
 	if (plan_getview == NULL)
 	{
@@ -769,8 +769,21 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc)
 		appendStringInfo(buf, " WHERE ");
 
 		qual = stringToNode(ev_qual);
+
+		/*
+		 * We need to make a context for recognizing any Vars in the qual
+		 * (which can only be references to OLD and NEW).  Use the rtable
+		 * of the first query in the action list for this purpose.
+		 */
 		query = (Query *) lfirst(actions);
 
+		/*
+		 * If the action is INSERT...SELECT, OLD/NEW have been pushed
+		 * down into the SELECT, and that's what we need to look at.
+		 * (Ugly kluge ... try to fix this when we redesign querytrees.)
+		 */
+		query = getInsertSelectQuery(query, NULL);
+
 		context.buf = buf;
 		context.namespaces = makeList1(&dpns);
 		context.varprefix = (length(query->rtable) != 1);
-- 
GitLab