From 0c822e5e038bd9505724df2222433aa8ebb05173 Mon Sep 17 00:00:00 2001
From: amitlan <amitlangote09@gmail.com>
Date: Sun, 11 Dec 2022 17:57:17 +0900
Subject: [PATCH v1 2/2] Fix build_simple_rel() to correctly set childrel
 userid

For child relations of a subquery parent baserels, build_simple_rel()
should look up RTEPermissionInfo instead of copying the parent rel's
userid, because the latter would be 0 give that it's a subquery rel.
---
 src/backend/optimizer/util/relnode.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
index 7085cf3c41..f7fc8079e7 100644
--- a/src/backend/optimizer/util/relnode.c
+++ b/src/backend/optimizer/util/relnode.c
@@ -229,9 +229,10 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
 		/*
 		 * Get the userid from the relation's RTEPermissionInfo, though only
 		 * the tables mentioned in query are assigned RTEPermissionInfos.
-		 * Child relations (otherrels) simply use the parent's value.
+		 * Child relations (otherrels) simply use the parent's value, unless
+		 * the parent is a subquery base rel.
 		 */
-		if (parent == NULL)
+		if (parent == NULL || parent->rtekind != RTE_RELATION)
 		{
 			RTEPermissionInfo *perminfo;
 
-- 
2.35.3

