]> git.ipfire.org Git - thirdparty/postgresql.git/commit
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:46:30 +0000 (15:46 +0200)
commit07b7a964d368e0618e1e1c788f3b998b4e571427
treeaee3ebc32fa9ed5e6e11a8eda5c91d3236197d5c
parente7975f1c06b585dcda645c6047e6f37cdfacce84
Fix self-join removal to update bare Var references in join clauses

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