From f1af32816635254773386630b634835bd26d1227 Mon Sep 17 00:00:00 2001
From: amitlan <amitlangote09@gmail.com>
Date: Fri, 2 Dec 2022 19:32:14 +0900
Subject: [PATCH v26 2/2] Add root_parent_relids to PartitionPruneResult

It's same as the corresponding PartitionPruneInfo's root_parent_relids.
Like PartitionPruneInfo.root_parent_relids, it's there for
cross-checking a PartitionPruneResult found at a given plan node's
part_prune_index actually matches the plan node.
---
 src/backend/executor/execMain.c      |  2 ++
 src/backend/executor/execPartition.c | 13 +++++++++++--
 src/include/nodes/plannodes.h        |  7 +++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index 7a4db80104..1e84e47d46 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -145,6 +145,8 @@ ExecutorDoInitialPruning(PlannedStmt *plannedstmt, ParamListInfo params,
 		PartitionPruneInfo *pruneinfo = lfirst(lc);
 		PartitionPruneResult *pruneresult = makeNode(PartitionPruneResult);
 
+		pruneresult->root_parent_relids =
+			bms_copy(pruneinfo->root_parent_relids);
 		pruneresult->valid_subplan_offs =
 			ExecPartitionDoInitialPruning(plannedstmt, params, pruneinfo,
 										  scan_leafpart_rtis);
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 13e450c0fa..eda14d6241 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -1852,8 +1852,17 @@ ExecInitPartitionPruning(PlanState *planstate,
 	 */
 	if (estate->es_part_prune_results)
 	{
-		pruneresult = list_nth(estate->es_part_prune_results, part_prune_index);
-		Assert(IsA(pruneresult, PartitionPruneResult));
+		pruneresult = list_nth_node(PartitionPruneResult,
+									estate->es_part_prune_results,
+									part_prune_index);
+		if (!bms_equal(root_parent_relids, pruneinfo->root_parent_relids))
+			ereport(ERROR,
+					errcode(ERRCODE_INTERNAL_ERROR),
+					errmsg_internal("mismatching PartitionPruneInfo and PartitionPruneResult at part_prune_index %d",
+									part_prune_index),
+					errdetail_internal("prunresult relids %s, pruneinfo relids %s",
+									   bmsToString(pruneresult->root_parent_relids),
+									   bmsToString(pruneinfo->root_parent_relids)));
 	}
 
 	if (pruneresult == NULL || pruneinfo->needs_exec_pruning)
diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 0cab6958d7..30f51414e9 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -1580,6 +1580,12 @@ typedef struct PartitionPruneStepCombine
  * The result of performing ExecPartitionDoInitialPruning() on a given
  * PartitionPruneInfo.
  *
+ * root_parent_relids is same as PartitionPruneInfo.root_parent_relids.  It's
+ * there for cross-checking in ExecInitPartitionPruning() that the
+ * PartitionPruneResult and the PartitionPruneInfo at a given index in
+ * EState.es_part_prune_results and EState.es_part_prune_infos, respectively,
+ * belong to the same parent plan node.
+ *
  * valid_subplans_offs contains the indexes of subplans remaining after
  * performing initial pruning by calling ExecFindMatchingSubPlans() on the
  * PartitionPruneInfo.
@@ -1597,6 +1603,7 @@ typedef struct PartitionPruneResult
 {
 	NodeTag		type;
 
+	Bitmapset	   *root_parent_relids;
 	Bitmapset	   *valid_subplan_offs;
 } PartitionPruneResult;
 
-- 
2.35.3

