From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Manu <manuelreyesbravo@gmail.com>
Date: Sun, 06 Dec 2026 00:35:59 -0300
Subject: [PATCH v4] Keep volatile duplicate quals in self-join elimination

When self-join elimination merges two relations, fixup_selfjoin_quals()
drops a restriction clause that the Var substitution has made equal to
one already kept.  Two volatile clauses written once per side, such as
f(a.id) and f(b.id), become identical that way, and one of them was
dropped.  Keep the clause if it contains volatile functions.

Reported-by: Wren <idrissaajalloh@gmail.com>
Discussion: https://postgr.es/m/19861-db2245414c5e093b@postgresql.org
---
 src/backend/optimizer/plan/analyzejoins.c | 10 +++++++---
 src/test/regress/expected/join.out        | 20 ++++++++++++++++++++
 src/test/regress/sql/join.sql             |  9 +++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c
index 1d541bdae5f..c5817d68b2d 100644
--- a/src/backend/optimizer/plan/analyzejoins.c
+++ b/src/backend/optimizer/plan/analyzejoins.c
@@ -1501,7 +1501,10 @@ fixup_selfjoin_jointree(PlannerInfo *root, Node *jtnode, int relid,
  *    clause, again leading to bad selectivity estimates.
  * Second, conditions that were written against the two relations separately
  * may now be identical, and we don't want to apply the same condition twice
- * (much less double-count its selectivity).
+ * (much less double-count its selectivity).  That does not hold for
+ * conditions containing volatile functions, which stay separate conditions
+ * even when they look alike, so we keep those; replace_selfjoin_qual()
+ * likewise leaves volatile clauses alone.
  *
  * We only touch the top-level conjuncts of the list.  There, turning a NULL
  * result into FALSE makes no difference, whereas below a NOT it would,
@@ -1528,8 +1531,9 @@ fixup_selfjoin_quals(PlannerInfo *root, List *quals, int relid)
 		if (bms_is_member(relid, pull_varnos(root, qual)))
 		{
 			qual = replace_selfjoin_qual(qual);
-			/* Drop it if the substitution has made it a duplicate */
-			if (list_member(result, qual))
+			/* Drop it if the substitution made it a non-volatile duplicate */
+			if (list_member(result, qual) &&
+				!contain_volatile_functions(qual))
 				continue;
 		}
 		result = lappend(result, qual);
diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out
index 6aaf3736e85..b11267f1947 100644
--- a/src/test/regress/expected/join.out
+++ b/src/test/regress/expected/join.out
@@ -10216,6 +10216,26 @@ WHERE j1.b = j2.b
  3 | 1 | 3 | 3 | 1 | 3
 (1 row)
 
+-- Identical volatile restrictions on both sides must both be kept
+EXPLAIN (COSTS OFF)
+SELECT * FROM sj j1, sj j2
+WHERE j1.a = j2.a AND random() < j1.b AND random() < j2.b;
+                                                QUERY PLAN                                                 
+-----------------------------------------------------------------------------------------------------------
+ Seq Scan on sj j2
+   Filter: ((a IS NOT NULL) AND (random() < (b)::double precision) AND (random() < (b)::double precision))
+(2 rows)
+
+-- ... but identical non-volatile ones are still merged
+EXPLAIN (COSTS OFF)
+SELECT * FROM sj j1, sj j2
+WHERE j1.a = j2.a AND abs(j1.b) < 10 AND abs(j2.b) < 10;
+                  QUERY PLAN                   
+-----------------------------------------------
+ Seq Scan on sj j2
+   Filter: ((a IS NOT NULL) AND (abs(b) < 10))
+(2 rows)
+
 -- Multiple filters
 CREATE UNIQUE INDEX sj_temp_idx1 ON sj(a,b,c);
 -- Remove SJ
diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql
index 25fc4548688..c2cc295aefd 100644
--- a/src/test/regress/sql/join.sql
+++ b/src/test/regress/sql/join.sql
@@ -3932,6 +3932,15 @@ WHERE j1.b = j2.b
   AND (j1.a*j1.c/3) = (random()/3 + 3)::int
   AND (random()/3 + 3)::int = (j2.a*j2.c/3);
 
+-- Identical volatile restrictions on both sides must both be kept
+EXPLAIN (COSTS OFF)
+SELECT * FROM sj j1, sj j2
+WHERE j1.a = j2.a AND random() < j1.b AND random() < j2.b;
+-- ... but identical non-volatile ones are still merged
+EXPLAIN (COSTS OFF)
+SELECT * FROM sj j1, sj j2
+WHERE j1.a = j2.a AND abs(j1.b) < 10 AND abs(j2.b) < 10;
+
 -- Multiple filters
 CREATE UNIQUE INDEX sj_temp_idx1 ON sj(a,b,c);
 
-- 
2.56.1
