pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Be more careful about the shape of hashable subplan clauses.
Date: Sat, 15 Aug 2020 02:14:35 +0000
Message-ID: <E1k6liN-0002p3-JV@gemulon.postgresql.org> (raw)

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(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1k6liN-0002p3-JV@gemulon.postgresql.org>
Permalink:  ../E1k6liN-0002p3-JV@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1k6liN-0002p3-JV@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Be more careful about the shape of hashable subplan clauses.
  In-Reply-To: <E1k6liN-0002p3-JV@gemulon.postgresql.org>
  References: <E1k6liN-0002p3-JV@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox