pgsql: Be more careful about the shape of hashable subplan clauses. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Be more careful about the shape of hashable subplan clauses.
Date
Msg-id E1k6liN-0002p3-JV@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Be more careful about the shape of hashable subplan clauses.

nodeSubplan.c expects that the testexpr for a hashable ANY SubPlan
has the form of one or more OpExprs whose LHS is an expression of the
outer query's, while the RHS is an expression over Params representing
output columns of the subquery.  However, the planner only went as far
as verifying that the clauses were all binary OpExprs.  This works
99.99% of the time, because the clauses have the right shape when
emitted by the parser --- but it's possible for function inlining to
break that, as reported by PegoraroF10.  To fix, teach the planner
to check that the LHS and RHS contain the right things, or more
accurately don't contain the wrong things.  Given that this has been
broken for years without anyone noticing, it seems sufficient to just
give up hashing when it happens, rather than go to the trouble of
commuting the clauses back again (which wouldn't necessarily work
anyway).

While poking at that, I also noticed that nodeSubplan.c had a baked-in
assumption that the number of hash clauses is identical to the number
of subquery output columns.  Again, that's fine as far as parser output
goes, but it's not hard to break it via function inlining.  There seems
little reason for that assumption though --- AFAICS, the only thing
it's buying us is not having to store the number of hash clauses
explicitly.  Adding code to the planner to reject such cases would take
more code than getting nodeSubplan.c to cope, so I fixed it that way.

This has been broken for as long as we've had hashable SubPlans,
so back-patch to all supported branches.

Discussion: https://postgr.es/m/1549209182255-0.post@n3.nabble.com

Branch
------
REL_11_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9d472b51e98777102d72f8ccdfb8cef10e087f74

Modified Files
--------------
src/backend/executor/nodeSubplan.c      | 14 +++---
src/backend/optimizer/plan/subselect.c  | 77 +++++++++++++++++++++++---------
src/backend/optimizer/util/clauses.c    | 35 +++++++++++++++
src/include/nodes/execnodes.h           |  2 +
src/include/optimizer/clauses.h         |  1 +
src/test/regress/expected/subselect.out | 79 +++++++++++++++++++++++++++++++++
src/test/regress/sql/subselect.sql      | 44 ++++++++++++++++++
7 files changed, 223 insertions(+), 29 deletions(-)


pgsql-committers by date:

Previous
From: Andres Freund
Date:
Subject: pgsql: snapshot scalability: Move subxact info to ProcGlobal, remove PG
Next
From: Amit Kapila
Date:
Subject: pgsql: Mark a few logical decoding related variables with PGDLLIMPORT.