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: Fix pull_varnos' miscomputation of relids set for a PlaceHolderV
Date: Thu, 21 Jan 2021 20:37:43 +0000
Message-ID: <E1l2gi7-00052f-G6@gemulon.postgresql.org> (raw)

Fix pull_varnos' miscomputation of relids set for a PlaceHolderVar.

Previously, pull_varnos() took the relids of a PlaceHolderVar as being
equal to the relids in its contents, but that fails to account for the
possibility that we have to postpone evaluation of the PHV due to outer
joins.  This could result in a malformed plan.  The known cases end up
triggering the "failed to assign all NestLoopParams to plan nodes"
sanity check in createplan.c, but other symptoms may be possible.

The right value to use is the join level we actually intend to evaluate
the PHV at.  We can get that from the ph_eval_at field of the associated
PlaceHolderInfo.  However, there are some places that call pull_varnos()
before the PlaceHolderInfos have been created; in that case, fall back
to the conservative assumption that the PHV will be evaluated at its
syntactic level.  (In principle this might result in missing some legal
optimization, but I'm not aware of any cases where it's an issue in
practice.)  Things are also a bit ticklish for calls occurring during
deconstruct_jointree(), but AFAICS the ph_eval_at fields should have
reached their final values by the time we need them.

The main problem in making this work is that pull_varnos() has no
way to get at the PlaceHolderInfos.  We can fix that easily, if a
bit tediously, in HEAD by passing it the planner "root" pointer.
In the back branches that'd cause an unacceptable API/ABI break for
extensions, so leave the existing entry points alone and add new ones
with the additional parameter.  (If an old entry point is called and
encounters a PHV, it'll fall back to using the syntactic level,
again possibly missing some valid optimization.)

Back-patch to v12.  The computation is surely also wrong before that,
but it appears that we cannot reach a bad plan thanks to join order
restrictions imposed on the subquery that the PlaceHolderVar came from.
The error only became reachable when commit 4be058fe9 allowed trivial
subqueries to be collapsed out completely, eliminating their join order
restrictions.

Per report from Stephan Springl.

Discussion: https://postgr.es/m/171041.1610849523@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/55dc86eca70b1dc18a79c141b3567efed910329d

Modified Files
--------------
contrib/postgres_fdw/postgres_fdw.c       |  3 +-
src/backend/optimizer/path/clausesel.c    | 12 +++---
src/backend/optimizer/path/costsize.c     |  2 +-
src/backend/optimizer/path/equivclass.c   | 17 +++++---
src/backend/optimizer/path/indxpath.c     | 61 ++++++++++++++++-----------
src/backend/optimizer/path/pathkeys.c     |  2 +-
src/backend/optimizer/path/tidpath.c      | 18 ++++----
src/backend/optimizer/plan/analyzejoins.c |  2 +-
src/backend/optimizer/plan/initsplan.c    | 31 ++++++++------
src/backend/optimizer/plan/subselect.c    |  4 +-
src/backend/optimizer/prep/prepjointree.c | 40 +++++++++++-------
src/backend/optimizer/util/clauses.c      |  4 +-
src/backend/optimizer/util/inherit.c      |  5 ++-
src/backend/optimizer/util/orclauses.c    |  3 +-
src/backend/optimizer/util/placeholder.c  |  2 +-
src/backend/optimizer/util/restrictinfo.c | 39 ++++++++++-------
src/backend/optimizer/util/var.c          | 69 +++++++++++++++++++++----------
src/backend/utils/adt/selfuncs.c          |  4 +-
src/include/optimizer/clauses.h           |  2 +-
src/include/optimizer/optimizer.h         |  7 ++--
src/include/optimizer/paths.h             |  3 +-
src/include/optimizer/planmain.h          |  3 +-
src/include/optimizer/restrictinfo.h      |  7 ++--
src/test/regress/expected/join.out        | 36 ++++++++++++++++
src/test/regress/sql/join.sql             | 16 +++++++
25 files changed, 261 insertions(+), 131 deletions(-)



view thread (3+ messages)  latest in thread

Message-ID: <E1l2gi7-00052f-G6@gemulon.postgresql.org>
Permalink:  ../E1l2gi7-00052f-G6@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1l2gi7-00052f-G6@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: Fix pull_varnos' miscomputation of relids set for a PlaceHolderV
  In-Reply-To: <E1l2gi7-00052f-G6@gemulon.postgresql.org>
  References: <E1l2gi7-00052f-G6@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