From 211caf4d6048c332b613022cb968cdcd05f5c503 Mon Sep 17 00:00:00 2001
From: Satya Narlapuram <satyanarlapuram@gmail.com>
Date: Sat, 25 Apr 2026 07:30:13 +0000
Subject: [PATCH] Fix discarded adjust_relid_set() return values in SJE

remove_self_join_rel() calls adjust_relid_set() to update
root->all_result_relids and root->leaf_result_relids after
eliminating a self-joined relation, but discards the return values.

adjust_relid_set() allocates a new Bitmapset via bms_copy() when
it finds the old relid in the set, so the caller must capture the
returned pointer.  Every other call site in the file (20+ instances)
correctly assigns the return value.

---
 src/backend/optimizer/plan/analyzejoins.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c
index 03056bdf..4fa3abc7 100644
--- a/src/backend/optimizer/plan/analyzejoins.c
+++ b/src/backend/optimizer/plan/analyzejoins.c
@@ -1994,8 +1994,12 @@ remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark,
 	ChangeVarNodesExtended((Node *) root->processed_tlist, toRemove->relid,
 						   toKeep->relid, 0, replace_relid_callback);
 
-	adjust_relid_set(root->all_result_relids, toRemove->relid, toKeep->relid);
-	adjust_relid_set(root->leaf_result_relids, toRemove->relid, toKeep->relid);
+	root->all_result_relids = adjust_relid_set(root->all_result_relids,
+											   toRemove->relid,
+											   toKeep->relid);
+	root->leaf_result_relids = adjust_relid_set(root->leaf_result_relids,
+												toRemove->relid,
+												toKeep->relid);
 
 	/*
 	 * There may be references to the rel in root->fkey_list, but if so,
-- 
2.43.0

