pgsql: Make Vars be outer-join-aware. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Make Vars be outer-join-aware.
Date
Msg-id E1pMZao-000l3w-P9@gemulon.postgresql.org
Whole thread Raw
Responses Re: pgsql: Make Vars be outer-join-aware.  (Robins Tharakan <tharakan@gmail.com>)
List pgsql-committers
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(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Doc: clarify behavior of boolean options in replication commands
Next
From: Thomas Munro
Date:
Subject: pgsql: Refactor rmtree() to use get_dirent_type().