From 32fcfcdbd66368c51c5561329d2a003eaff1fd64 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed, 19 Oct 2005 17:31:20 +0000
Subject: [PATCH] Fix oversight in recent changes to enable the 'physical
 tlist' optimization for subquery and function scan nodes: we can't just do it
 unconditionally, we still have to check whether there is any need for a
 whole-row Var.  I had been thinking that these node types couldn't have any
 system columns, which is true, but that loop is also checking for attno zero,
 ie, whole-row Var.  Fix comment to not be so misleading. Per test case from
 Richard Huxton.

---
 src/backend/optimizer/plan/createplan.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index f0dd6548711..fc093fdf180 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.201 2005/10/15 02:49:20 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.202 2005/10/19 17:31:20 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -308,17 +308,13 @@ use_physical_tlist(RelOptInfo *rel)
 	int			i;
 
 	/*
-	 * OK for subquery and function scans; otherwise, can't do it for anything
-	 * except real relations.
+	 * We can do this for real relation scans, subquery scans, and function
+	 * scans (but not for, eg, joins).
 	 */
-	if (rel->rtekind != RTE_RELATION)
-	{
-		if (rel->rtekind == RTE_SUBQUERY)
-			return true;
-		if (rel->rtekind == RTE_FUNCTION)
-			return true;
+	if (rel->rtekind != RTE_RELATION &&
+		rel->rtekind != RTE_SUBQUERY &&
+		rel->rtekind != RTE_FUNCTION)
 		return false;
-	}
 
 	/*
 	 * Can't do it with inheritance cases either (mainly because Append
@@ -328,15 +324,16 @@ use_physical_tlist(RelOptInfo *rel)
 		return false;
 
 	/*
-	 * Can't do it if any system columns are requested, either.  (This could
-	 * possibly be fixed but would take some fragile assumptions in setrefs.c,
-	 * I think.)
+	 * Can't do it if any system columns or whole-row Vars are requested,
+	 * either.  (This could possibly be fixed but would take some fragile
+	 * assumptions in setrefs.c, I think.)
 	 */
 	for (i = rel->min_attr; i <= 0; i++)
 	{
 		if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
 			return false;
 	}
+
 	return true;
 }
 
-- 
GitLab