pgsql: Correctly identify which EC members are computable at a plan nod

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Correctly identify which EC members are computable at a plan nod
Date: 2024-10-12 18:56:21
Message-ID: E1szhHp-000Tdz-Uv@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Correctly identify which EC members are computable at a plan node.

find_computable_ec_member() had the wrong mental model of what
its primary caller prepare_sort_from_pathkeys() would do with
the selected EquivalenceClass member expression. We will not
compute the EC expression in a plan node atop the one returning
the passed-in targetlist; rather, the EC expression will be
computed as an additional column of that targetlist. So any
Var or quasi-Var used in the given tlist is also available to the
EC expression. In simple cases this makes no difference because
the given tlist is just a list of Vars or quasi-Vars --- but if
we are considering an appendrel member produced by flattening
a UNION ALL, the tlist may contain expressions, resulting in
failure to match and a "could not find pathkey item to sort"
error.

To fix, we can flatten both the tlist and the EC members with
pull_var_clause(), and then just check for subset-ness, so
that the code is actually shorter than before.

While this bug is quite old, the present patch only works back to
v13. We could possibly make it work in v12 by back-patching parts
of 375398244. On the whole though I don't like the risk/reward
ratio of that idea. v12's final release is next month, meaning
there would be no chance to correct matters if the patch causes a
regression. Since this failure has escaped notice for 14 years,
it's likely nobody will hit it in the field with v12.

Per bug #18652 from Alexander Lakhin.

Andrei Lepikhov and Tom Lane

Discussion: https://postgr.es/m/18652-deaa782ebcca85d1@postgresql.org

Branch
------
REL_14_STABLE

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

Modified Files
--------------
src/backend/optimizer/path/equivclass.c | 71 +++++++++++++++------------------
src/test/regress/expected/inherit.out | 30 ++++++++++++++
src/test/regress/sql/inherit.sql | 8 ++++
3 files changed, 71 insertions(+), 38 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2024-10-13 02:35:40 pgsql: Use MAX_PARALLEL_WORKER_LIMIT for max_parallel_maintenance_worke
Previous Message Jeff Davis 2024-10-12 00:01:54 pgsql: Fix missed case for builtin collation provider.