From 04f156396309f8c34a853ce1ad4e293fe4e2c4a2 Mon Sep 17 00:00:00 2001
From: amitlan <amitlangote09@gmail.com>
Date: Fri, 2 Dec 2022 19:32:14 +0900
Subject: [PATCH v28 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 | 10 ++++++++++
 src/include/nodes/plannodes.h        |  7 +++++++
 3 files changed, 19 insertions(+)

diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index f15265716a..554623751b 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -147,6 +147,8 @@ ExecutorDoInitialPruning(PlannedStmt *plannedstmt, ParamListInfo params,
 		PartitionPruneInfo *pruneinfo = lfirst_node(PartitionPruneInfo, 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 bc8331a222..2eadc30ec8 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -1842,9 +1842,19 @@ ExecInitPartitionPruning(PlanState *planstate,
 	 * is set.
 	 */
 	if (estate->es_part_prune_results)
+	{
 		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 714e2cf2c7..ed664c5469 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

