pgsql: Check the validity of commutators for merge/hash clauses

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Check the validity of commutators for merge/hash clauses
Date: 2024-09-04 03:20:48
Message-ID: E1slgZb-0003Uq-CE@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Check the validity of commutators for merge/hash clauses

When creating merge or hash join plans in createplan.c, the merge or
hash clauses may need to get commuted to ensure that the outer var is
on the left and the inner var is on the right if they are not already
in the expected form. This requires that their operators have
commutators. Failing to find a commutator at this stage would result
in 'ERROR: could not find commutator for operator xxx', with no
opportunity to select an alternative plan.

Typically, this is not an issue because mergejoinable or hashable
operators are expected to always have valid commutators. But in some
artificial cases this assumption may not hold true. Therefore, here
in this patch we check the validity of commutators for clauses in the
form "inner op outer" when selecting mergejoin/hash clauses, and
consider a clause unusable for the current pair of outer and inner
relations if it lacks a commutator.

There are not (and should not be) any such operators built into
Postgres that are mergejoinable or hashable but have no commutators;
so we leverage the alias type 'int8alias1' created in equivclass.sql
to build the test case. This is why the test case is included in
equivclass.sql rather than in join.sql.

Although this is arguably a bug fix, it cannot be reproduced without
installing an incomplete opclass, which is unlikely to happen in
practice, so no back-patch.

Reported-by: Alexander Pyhalov
Author: Richard Guo
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/c59ec04a2fef94d9ffc35a9b17dfc081@postgrespro.ru

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/4f1124548f6f3c7dea639ccd3d3f234cf70faf0a

Modified Files
--------------
src/backend/optimizer/path/joinpath.c | 32 +++++++++++++++++++++
src/test/regress/expected/equivclass.out | 48 ++++++++++++++++++++++++++++++++
src/test/regress/sql/equivclass.sql | 34 ++++++++++++++++++++++
3 files changed, 114 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Amit Kapila 2024-09-04 03:37:58 pgsql: Collect statistics about conflicts in logical replication.
Previous Message Michael Paquier 2024-09-04 01:23:04 pgsql: Fix inconsistent LWLock tranche name "CommitTsSLRU"