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@postgresql.org
Subject: pgsql: Still more fixes for planner's handling of LATERAL references.
Date: Fri, 11 Dec 2015 19:22:36 +0000
Message-ID: <E1a7TH2-0003Tx-3b@gemulon.postgresql.org> (raw)
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-committers>

Still more fixes for planner's handling of LATERAL references.

More fuzz testing by Andreas Seltenreich exposed that the planner did not
cope well with chains of lateral references.  If relation X references Y
laterally, and Y references Z laterally, then we will have to scan X on the
inside of a nestloop with Z, so for all intents and purposes X is laterally
dependent on Z too.  The planner did not understand this and would generate
intermediate joins that could not be used.  While that was usually harmless
except for wasting some planning cycles, under the right circumstances it
would lead to "failed to build any N-way joins" or "could not devise a
query plan" planner failures.

To fix that, convert the existing per-relation lateral_relids and
lateral_referencers relid sets into their transitive closures; that is,
they now show all relations on which a rel is directly or indirectly
laterally dependent.  This not only fixes the chained-reference problem
but allows some of the relevant tests to be made substantially simpler
and faster, since they can be reduced to simple bitmap manipulations
instead of searches of the LateralJoinInfo list.

Also, when a PlaceHolderVar that is due to be evaluated at a join contains
lateral references, we should treat those references as indirect lateral
dependencies of each of the join's base relations.  This prevents us from
trying to join any individual base relations to the lateral reference
source before the join is formed, which again cannot work.

Andreas' testing also exposed another oversight in the "dangerous
PlaceHolderVar" test added in commit 85e5e222b1dd02f1.  Simply rejecting
unsafe join paths in joinpath.c is insufficient, because in some cases
we will end up rejecting *all* possible paths for a particular join, again
leading to "could not devise a query plan" failures.  The restriction has
to be known also to join_is_legal and its cohort functions, so that they
will not select a join for which that will happen.  I chose to move the
supporting logic into joinrels.c where the latter functions are.

Back-patch to 9.3 where LATERAL support was introduced.

Branch
------
REL9_3_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/260590e6b2fb958de79edc07d9222a39ba5913e5

Modified Files
--------------
src/backend/optimizer/path/joinpath.c  |   79 +----------
src/backend/optimizer/path/joinrels.c  |  239 ++++++++++++++++++++++++--------
src/backend/optimizer/plan/initsplan.c |  129 +++++++++++++----
src/backend/optimizer/util/relnode.c   |   41 +++---
src/include/nodes/relation.h           |    2 +
src/include/optimizer/pathnode.h       |    5 +-
src/include/optimizer/paths.h          |    2 +
src/test/regress/expected/join.out     |  163 +++++++++++++++++++++-
src/test/regress/sql/join.sql          |   59 ++++++++
9 files changed, 531 insertions(+), 188 deletions(-)


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers


view thread (4+ messages)

Message-ID: <E1a7TH2-0003Tx-3b@gemulon.postgresql.org>
Permalink:  ../E1a7TH2-0003Tx-3b@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1a7TH2-0003Tx-3b@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: Still more fixes for planner's handling of LATERAL references.
  In-Reply-To: <E1a7TH2-0003Tx-3b@gemulon.postgresql.org>
  References: <E1a7TH2-0003Tx-3b@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