public inbox for [email protected]  
help / color / mirror / Atom feed
From: SATYANARAYANA NARLAPURAM <[email protected]>
To: PostgreSQL Hackers <[email protected]>
Subject: Discarded adjust_relid_set() return values in remove_self_join_rel
Date: Sat, 25 Apr 2026 00:44:01 -0700
Message-ID: <CAHg+QDf+fJsyT_Oj6xDCGkyKWMQ=oEjLdxZ_K4WmKApQWP8hjA@mail.gmail.com> (raw)

Hi hackers,

I found that remove_self_join_rel() in analyzejoins.c discards the
return values of two adjust_relid_set() calls when updating
root->all_result_relids and root->leaf_result_relids:

    adjust_relid_set(root->all_result_relids, toRemove->relid,
toKeep->relid);
    adjust_relid_set(root->leaf_result_relids, toRemove->relid,
toKeep->relid);

adjust_relid_set() does not modify its input in-place.  When it finds
the old relid in the set, it calls bms_copy() to create a new
Bitmapset, performs the substitution on the copy, and returns the new
pointer.  Without capturing the return value, the original sets are
never updated. Every other call site in the file  analyzejoins.c correctly
assigns the return value.

I am not able to craft a query to show it as a problem.
For the current release, these two lines appears to be a no-op?

Attached a patch to assign the returned values.

Thanks,
Satya


Attachments:

  [application/octet-stream] 0001-Fix-discarded-adjust_relid_set-return-values-in-SJE.patch (1.7K, 3-0001-Fix-discarded-adjust_relid_set-return-values-in-SJE.patch)
  download | inline diff:
From 211caf4d6048c332b613022cb968cdcd05f5c503 Mon Sep 17 00:00:00 2001
From: Satya Narlapuram <[email protected]>
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



view thread (2+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Discarded adjust_relid_set() return values in remove_self_join_rel
  In-Reply-To: <CAHg+QDf+fJsyT_Oj6xDCGkyKWMQ=oEjLdxZ_K4WmKApQWP8hjA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox