]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix self-join removal to update bare Var references in join clauses
authorAlexander Korotkov <akorotkov@postgresql.org>
Fri, 20 Mar 2026 13:32:52 +0000 (15:32 +0200)
committerAlexander Korotkov <akorotkov@postgresql.org>
Fri, 20 Mar 2026 13:47:11 +0000 (15:47 +0200)
Self-join removal failed to update Var nodes when the join clause was a
bare Var (e.g., ON t1.bool_col) rather than an expression containing
Vars.  ChangeVarNodesWalkExpression() used expression_tree_walker(),
which descends into child nodes but does not process the top-level node
itself.  When a bare Var referencing the removed relation appeared as
the clause, its varno was left unchanged, leading to "no relation entry
for relid N" errors.

Fix by calling ChangeVarNodes_walker() directly instead of
expression_tree_walker(), so the top-level node is also processed.

Bug: #19435
Reported-by: Hang Ammmkilo <ammmkilo@163.com>
Author: Andrei Lepikhov <lepihov@gmail.com>
Co-authored-by: Tender Wang <tndrwang@gmail.com>
Co-authored-by: Alexander Korotkov <aekorotkov@gmail.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/19435-3cc1a87f291129f1%40postgresql.org
Backpatch-through: 18

src/backend/rewrite/rewriteManip.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index 0fcd1fbd14ef140c1aecb025b55538fef8071d0e..0049e6e440549e85e2fcf37f5dc6c975bf77a063 100644 (file)
@@ -743,9 +743,7 @@ ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
 bool
 ChangeVarNodesWalkExpression(Node *node, ChangeVarNodes_context *context)
 {
-       return expression_tree_walker(node,
-                                                                 ChangeVarNodes_walker,
-                                                                 (void *) context);
+       return ChangeVarNodes_walker(node, context);
 }
 
 /*
index 75db7ebea752c75efd259aa19424b3a1c4e14a41..0eb3f6930c3afa26ae16a6d8d76cc67c9b68ff7e 100644 (file)
@@ -7896,6 +7896,22 @@ WHERE q0.a = 1;
    ->  Seq Scan on sj n1
 (7 rows)
 
+-- Do not forget to replace relid in bare Var join clause (bug #19435)
+ALTER TABLE sl ADD COLUMN bool_col boolean;
+EXPLAIN (COSTS OFF)
+SELECT 1 AS c1 FROM sl sl1 LEFT JOIN (sl AS sl2 NATURAL JOIN sl AS sl3)
+  ON sl2.bool_col LEFT JOIN sl AS sl4 ON sl2.bool_col;
+                                                       QUERY PLAN                                                        
+-------------------------------------------------------------------------------------------------------------------------
+ Nested Loop Left Join
+   ->  Seq Scan on sl sl1
+   ->  Nested Loop Left Join
+         Join Filter: sl3.bool_col
+         ->  Seq Scan on sl sl3
+               Filter: (bool_col AND (a IS NOT NULL) AND (b IS NOT NULL) AND (c IS NOT NULL) AND (bool_col IS NOT NULL))
+         ->  Seq Scan on sl sl4
+(7 rows)
+
 -- Check optimization disabling if it will violate special join conditions.
 -- Two identical joined relations satisfies self join removal conditions but
 -- stay in different special join infos.
index 8ef99af22c97628f8e5a90cac41c414334a97c60..58b1c89e40aa99d0fb8a8e071cdaa93b00479507 100644 (file)
@@ -3078,6 +3078,12 @@ SELECT * FROM
 (SELECT n2.a FROM sj n1, sj n2 WHERE n1.a <> n2.a) q0, sl
 WHERE q0.a = 1;
 
+-- Do not forget to replace relid in bare Var join clause (bug #19435)
+ALTER TABLE sl ADD COLUMN bool_col boolean;
+EXPLAIN (COSTS OFF)
+SELECT 1 AS c1 FROM sl sl1 LEFT JOIN (sl AS sl2 NATURAL JOIN sl AS sl3)
+  ON sl2.bool_col LEFT JOIN sl AS sl4 ON sl2.bool_col;
+
 -- Check optimization disabling if it will violate special join conditions.
 -- Two identical joined relations satisfies self join removal conditions but
 -- stay in different special join infos.