From bef764029b01b1e193cfa61a8ce6da835b919e6f Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 11 May 2004 13:15:15 +0000
Subject: [PATCH] Fix oversights in processing of LIMIT expressions during
 planning.

---
 src/backend/optimizer/plan/setrefs.c   | 12 ++++++++++--
 src/backend/optimizer/plan/subselect.c | 10 ++++++++--
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index faf4eb29bee..84b2aa82805 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.100 2004/01/06 04:31:01 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.101 2004/05/11 13:15:15 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -171,7 +171,6 @@ set_plan_references(Plan *plan, List *rtable)
 		case T_Sort:
 		case T_Unique:
 		case T_SetOp:
-		case T_Limit:
 
 			/*
 			 * These plan types don't actually bother to evaluate their
@@ -184,6 +183,15 @@ set_plan_references(Plan *plan, List *rtable)
 			 * the plan tree!
 			 */
 			break;
+		case T_Limit:
+			/*
+			 * Like the plan types above, Limit doesn't evaluate its
+			 * tlist or quals.  It does have live expressions for
+			 * limit/offset, however.
+			 */
+			fix_expr_references(plan, ((Limit *) plan)->limitOffset);
+			fix_expr_references(plan, ((Limit *) plan)->limitCount);
+			break;
 		case T_Agg:
 		case T_Group:
 			set_uppernode_references(plan, (Index) 0);
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index 4e7e2fe4390..af33da1184e 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.88 2004/02/03 17:34:03 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.89 2004/05/11 13:15:15 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1063,6 +1063,13 @@ finalize_plan(Plan *plan, List *rtable,
 							  &context);
 			break;
 
+		case T_Limit:
+			finalize_primnode(((Limit *) plan)->limitOffset,
+							  &context);
+			finalize_primnode(((Limit *) plan)->limitCount,
+							  &context);
+			break;
+
 		case T_Hash:
 		case T_Agg:
 		case T_SeqScan:
@@ -1070,7 +1077,6 @@ finalize_plan(Plan *plan, List *rtable,
 		case T_Sort:
 		case T_Unique:
 		case T_SetOp:
-		case T_Limit:
 		case T_Group:
 			break;
 
-- 
GitLab