pgsql: Convert 'x IN (VALUES ...)' to 'x = ANY ...' then appropriate

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Convert 'x IN (VALUES ...)' to 'x = ANY ...' then appropriate
Date: 2025-04-04 13:17:00
Message-ID: E1u0guq-002dS9-1m@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

Convert 'x IN (VALUES ...)' to 'x = ANY ...' then appropriate

This commit implements the automatic conversion of 'x IN (VALUES ...)' into
ScalarArrayOpExpr. That simplifies the query tree, eliminating the appearance
of an unnecessary join.

Since VALUES describes a relational table, and the value of such a list is
a table row, the optimizer will likely face an underestimation problem due to
the inability to estimate cardinality through MCV statistics. The cardinality
evaluation mechanism can work with the array inclusion check operation.
If the array is small enough (< 100 elements), it will perform a statistical
evaluation element by element.

We perform the transformation in the convert_ANY_sublink_to_join() if VALUES
RTE is proper and the transformation is convertible. The conversion is only
possible for operations on scalar values, not rows. Also, we currently
support the transformation only when it ends up with a constant array.
Otherwise, the evaluation of non-hashed SAOP might be slower than the
corresponding Hash Join with VALUES.

Discussion: https://postgr.es/m/0184212d-1248-4f1f-a42d-f5cb1c1976d2%40tantorlabs.com
Author: Alena Rybakina <a(dot)rybakina(at)postgrespro(dot)ru>
Author: Andrei Lepikhov <lepihov(at)gmail(dot)com>
Reviewed-by: Ivan Kush <ivan(dot)kush(at)tantorlabs(dot)com>
Reviewed-by: Alexander Korotkov <aekorotkov(at)gmail(dot)com>

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/c0962a113d1f2f94cb7222a7ca025a67e9ce3860

Modified Files
--------------
src/backend/optimizer/plan/subselect.c | 80 ++++++++
src/backend/optimizer/prep/prepjointree.c | 12 ++
src/backend/optimizer/util/clauses.c | 14 +-
src/include/optimizer/subselect.h | 3 +
src/test/regress/expected/subselect.out | 308 ++++++++++++++++++++++++++++++
src/test/regress/sql/subselect.sql | 100 ++++++++++
6 files changed, 512 insertions(+), 5 deletions(-)

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Nathan Bossart 2025-04-04 14:38:47 pgsql: Re-pgindent pg_largeobject.c after commit 0d6c477664.
Previous Message Peter Eisentraut 2025-04-04 12:46:20 pgsql: Fix crash/valgrind error

Browse pgsql-hackers by date

  From Date Subject
Next Message Shlok Kyal 2025-04-04 13:34:36 Re: Proposal: Filter irrelevant change before reassemble transactions during logical decoding
Previous Message Peter Eisentraut 2025-04-04 12:46:59 Re: Index AM API cleanup