Wrong results from self-join elimination with different restriction operators
Hi,
After finding the SJE bug involving inheritance scans and ONLY, I
asked an LLM to generate fuzz tests to look for further issues in
self-join elimination.
This uncovered another wrong-result case involving restriction clauses
with different equality operators.
How to reproduce:
...
postgres=# CREATE FUNCTION text_ci_eq(text, text) RETURNS boolean
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE
AS $$
BEGIN
RETURN lower($1) = lower($2);
END $$;
CREATE FUNCTION text_ci_cmp(text, text) RETURNS int
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
AS $$SELECT CASE WHEN lower($1) < lower($2) THEN -1
WHEN lower($1) > lower($2) THEN 1 ELSE 0 END$$;
CREATE OPERATOR === (LEFTARG=text, RIGHTARG=text, PROCEDURE=text_ci_eq,
COMMUTATOR='===', RESTRICT=eqsel, JOIN=eqjoinsel, MERGES);
CREATE OPERATOR FAMILY ci_text_fam USING btree;
CREATE OPERATOR CLASS ci_text_ops FOR TYPE text USING btree FAMILY
ci_text_fam AS
OPERATOR 1 <, OPERATOR 2 <=, OPERATOR 3 ===, OPERATOR 4 >=, OPERATOR 5 >,
FUNCTION 1 text_ci_cmp;
CREATE TABLE ci_t(id int, a text, b int);
CREATE UNIQUE INDEX ci_t_a ON ci_t(a); -- deterministic (standard)
unique index
INSERT INTO ci_t VALUES (1,'a',1),(2,'A',1),(3,'b',2);
ANALYZE ci_t;
SET enable_self_join_elimination = on;
SELECT p1.id, p1.a, p2.id, p2.a
FROM ci_t p1 JOIN ci_t p2 ON p1.b = p2.b
WHERE p1.a === 'a' AND p2.a = 'a'
ORDER BY 1, 3;
id | a | id | a
----+---+----+---
1 | a | 1 | a
(1 row)
SET enable_self_join_elimination = off;
SELECT p1.id, p1.a, p2.id, p2.a
FROM ci_t p1 JOIN ci_t p2 ON p1.b = p2.b
WHERE p1.a === 'a' AND p2.a = 'a'
ORDER BY 1, 3;
id | a | id | a
----+---+----+---
1 | a | 1 | a
2 | A | 1 | a
(2 rows)
...
SJE incorrectly considers the two restrictions equivalent and
eliminates the join.
The unique index and p2.a = 'a' establish uniqueness on the kept side.
match_unique_clauses() then checks for a corresponding restriction on
the removed side, but compares only the operands, ignoring the
operator and input collation.
It therefore treats p1.a === 'a' as matching p2.a = 'a', even though
they select different rows.
The attached patch requires both the operator and input collation to
match, in addition to the existing operand checks.
It includes a regression test for the different-operator case and
verifies that identical restrictions still allow self-join
elimination.
The full reproducer, including the custom operator definitions, is in
the regression test.
--
Thanks,
Tender Wang
Tender Wang <tndrwang@gmail.com> writes:
After finding the SJE bug involving inheritance scans and ONLY, I
asked an LLM to generate fuzz tests to look for further issues in
self-join elimination.
This uncovered another wrong-result case involving restriction clauses
with different equality operators.
If we're going to refuse that, I wonder if we couldn't simplify
match_unique_clauses a great deal: just insist that the two OpExpr
clauses be equal(), after the ChangeVarNodes substitution on the outer
clause. Your proposal already breaks commuted-clause cases, which
seem like the only justification for all the futzing around with left
and right operands. I doubt that detecting that is of any real-world
value anyway.
regards, tom lane
Tom Lane <tgl@sss.pgh.pa.us> 于2026年10月15日周四 04:56写道:
If we're going to refuse that, I wonder if we couldn't simplify
match_unique_clauses a great deal: just insist that the two OpExpr
clauses be equal(), after the ChangeVarNodes substitution on the outer
clause. Your proposal already breaks commuted-clause cases, which
seem like the only justification for all the futzing around with left
and right operands. I doubt that detecting that is of any real-world
value anyway.
Attached is v2, following your suggestion to compare the complete
clauses with equal() after the ChangeVarNodes substitution. I also
removed the test added in v1.
I ran the regression tests and found no plan differences. This
suggests the existing tests don't cover cases where matching commuted
restrictions enables self-join elimination.
--
Thanks,
Tender Wang
Tender Wang <tndrwang@gmail.com> writes:
Tom Lane <tgl@sss.pgh.pa.us> 于2026年10月15日周四 04:56写道:
If we're going to refuse that, I wonder if we couldn't simplify
match_unique_clauses a great deal: just insist that the two OpExpr
clauses be equal(), after the ChangeVarNodes substitution on the outer
clause. Your proposal already breaks commuted-clause cases, which
seem like the only justification for all the futzing around with left
and right operands. I doubt that detecting that is of any real-world
value anyway.
Attached is v2, following your suggestion to compare the complete
clauses with equal() after the ChangeVarNodes substitution. I also
removed the test added in v1.
LGTM, pushed.
I ran the regression tests and found no plan differences. This
suggests the existing tests don't cover cases where matching commuted
restrictions enables self-join elimination.
Yeah, that's evidence in favor of my opinion that this wasn't worth
the complexity. If somebody wanted to put that back, we could try
building a commuted clause (cf. CommuteOpExpr) and applying equal()
with that. But I really doubt it's worth the code and cycles.
regards, tom lane