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: Make Vars be outer-join-aware.
Date: Mon, 30 Jan 2023 19:13:28 +0000
Message-ID: <E1pMZao-000l3w-P9@gemulon.postgresql.org> (raw)

Make Vars be outer-join-aware.

Traditionally we used the same Var struct to represent the value
of a table column everywhere in parse and plan trees.  This choice
predates our support for SQL outer joins, and it's really a pretty
bad idea with outer joins, because the Var's value can depend on
where it is in the tree: it might go to NULL above an outer join.
So expression nodes that are equal() per equalfuncs.c might not
represent the same value, which is a huge correctness hazard for
the planner.

To improve this, decorate Var nodes with a bitmapset showing
which outer joins (identified by RTE indexes) may have nulled
them at the point in the parse tree where the Var appears.
This allows us to trust that equal() Vars represent the same value.
A certain amount of klugery is still needed to cope with cases
where we re-order two outer joins, but it's possible to make it
work without sacrificing that core principle.  PlaceHolderVars
receive similar decoration for the same reason.

In the planner, we include these outer join bitmapsets into the relids
that an expression is considered to depend on, and in consequence also
add outer-join relids to the relids of join RelOptInfos.  This allows
us to correctly perceive whether an expression can be calculated above
or below a particular outer join.

This change affects FDWs that want to plan foreign joins.  They *must*
follow suit when labeling foreign joins in order to match with the
core planner, but for many purposes (if postgres_fdw is any guide)
they'd prefer to consider only base relations within the join.
To support both requirements, redefine ForeignScan.fs_relids as
base+OJ relids, and add a new field fs_base_relids that's set up by
the core planner.

Large though it is, this commit just does the minimum necessary to
install the new mechanisms and get check-world passing again.
Follow-up patches will perform some cleanup.  (The README additions
and comments mention some stuff that will appear in the follow-up.)

Patch by me; thanks to Richard Guo for review.

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

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/2489d76c4906f4461a364ca8ad7e0751ead8aa0d

Modified Files
--------------
contrib/postgres_fdw/deparse.c               |   12 +-
contrib/postgres_fdw/postgres_fdw.c          |   12 +-
doc/src/sgml/fdwhandler.sgml                 |   11 +
src/backend/commands/explain.c               |    2 +-
src/backend/executor/execScan.c              |    2 +-
src/backend/nodes/makefuncs.c                |   10 +-
src/backend/nodes/nodeFuncs.c                |    3 +-
src/backend/nodes/queryjumblefuncs.c         |    5 +
src/backend/optimizer/README                 |  433 ++++++++--
src/backend/optimizer/geqo/geqo_eval.c       |    2 +-
src/backend/optimizer/path/allpaths.c        |   36 +-
src/backend/optimizer/path/clausesel.c       |   45 +-
src/backend/optimizer/path/costsize.c        |    8 +
src/backend/optimizer/path/equivclass.c      |  132 ++-
src/backend/optimizer/path/indxpath.c        |    9 +-
src/backend/optimizer/path/joinpath.c        |   65 +-
src/backend/optimizer/path/joinrels.c        |   30 +-
src/backend/optimizer/path/tidpath.c         |    1 +
src/backend/optimizer/plan/analyzejoins.c    |   61 +-
src/backend/optimizer/plan/createplan.c      |   17 +-
src/backend/optimizer/plan/initsplan.c       | 1121 ++++++++++++++++++++------
src/backend/optimizer/plan/planner.c         |    7 +-
src/backend/optimizer/plan/setrefs.c         |  234 ++++--
src/backend/optimizer/prep/prepjointree.c    |  544 +++++++------
src/backend/optimizer/util/appendinfo.c      |   59 +-
src/backend/optimizer/util/clauses.c         |    6 +-
src/backend/optimizer/util/joininfo.c        |   19 +-
src/backend/optimizer/util/orclauses.c       |    4 +
src/backend/optimizer/util/pathnode.c        |   15 +-
src/backend/optimizer/util/placeholder.c     |  123 ++-
src/backend/optimizer/util/relnode.c         |  389 +++++++--
src/backend/optimizer/util/restrictinfo.c    |   93 ++-
src/backend/optimizer/util/var.c             |  252 +++++-
src/backend/parser/analyze.c                 |    3 +-
src/backend/parser/parse_agg.c               |    8 +-
src/backend/parser/parse_clause.c            |  235 ++++--
src/backend/parser/parse_coerce.c            |    2 +-
src/backend/parser/parse_expr.c              |    5 +
src/backend/parser/parse_relation.c          |   40 +-
src/backend/parser/parse_target.c            |    2 +-
src/backend/rewrite/rewriteManip.c           |  227 +++++-
src/backend/utils/adt/selfuncs.c             |    2 +-
src/include/catalog/catversion.h             |    2 +-
src/include/nodes/parsenodes.h               |    8 +
src/include/nodes/pathnodes.h                |  216 +++--
src/include/nodes/plannodes.h                |    4 +-
src/include/nodes/primnodes.h                |   10 +
src/include/optimizer/optimizer.h            |    2 +-
src/include/optimizer/pathnode.h             |    4 +-
src/include/optimizer/placeholder.h          |    5 +-
src/include/optimizer/prep.h                 |    3 +-
src/include/optimizer/restrictinfo.h         |    3 +
src/include/parser/parse_node.h              |    8 +
src/include/parser/parse_relation.h          |    3 +-
src/include/rewrite/rewriteManip.h           |    7 +
src/test/regress/expected/aggregates.out     |   20 +-
src/test/regress/expected/join.out           |  163 +++-
src/test/regress/expected/partition_join.out |   14 +-
src/test/regress/sql/aggregates.sql          |   14 +-
src/test/regress/sql/join.sql                |   72 ++
60 files changed, 3878 insertions(+), 966 deletions(-)



view thread (4+ messages)  latest in thread

Message-ID: <E1pMZao-000l3w-P9@gemulon.postgresql.org>
Permalink:  ../E1pMZao-000l3w-P9@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1pMZao-000l3w-P9@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: Make Vars be outer-join-aware.
  In-Reply-To: <E1pMZao-000l3w-P9@gemulon.postgresql.org>
  References: <E1pMZao-000l3w-P9@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