pgsql: Implement SQL-standard LATERAL subqueries.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Implement SQL-standard LATERAL subqueries.
Date: 2012-08-07 23:03:27
Message-ID: E1SysoB-0006wL-Si@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Implement SQL-standard LATERAL subqueries.

This patch implements the standard syntax of LATERAL attached to a
sub-SELECT in FROM, and also allows LATERAL attached to a function in FROM,
since set-returning function calls are expected to be one of the principal
use-cases.

The main change here is a rewrite of the mechanism for keeping track of
which relations are visible for column references while the FROM clause is
being scanned. The parser "namespace" lists are no longer lists of bare
RTEs, but are lists of ParseNamespaceItem structs, which carry an RTE
pointer as well as some visibility-controlling flags. Aside from
supporting LATERAL correctly, this lets us get rid of the ancient hacks
that required rechecking subqueries and JOIN/ON and function-in-FROM
expressions for invalid references after they were initially parsed.
Invalid column references are now always correctly detected on sight.

In passing, remove assorted parser error checks that are now dead code by
virtue of our having gotten rid of add_missing_from, as well as some
comments that are obsolete for the same reason. (It was mainly
add_missing_from that caused so much fudging here in the first place.)

The planner support for this feature is very minimal, and will be improved
in future patches. It works well enough for testing purposes, though.

catversion bump forced due to new field in RangeTblEntry.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/5ebaaa49445eb1ba7b299bbea3a477d4e4c0430b

Modified Files
--------------
doc/src/sgml/keywords.sgml | 2 +-
doc/src/sgml/queries.sgml | 83 +++++++++-
doc/src/sgml/ref/select.sgml | 102 +++++++++--
src/backend/nodes/copyfuncs.c | 3 +
src/backend/nodes/equalfuncs.c | 3 +
src/backend/nodes/outfuncs.c | 3 +
src/backend/nodes/readfuncs.c | 1 +
src/backend/optimizer/geqo/geqo_eval.c | 11 +-
src/backend/optimizer/path/allpaths.c | 84 +++++++--
src/backend/optimizer/path/costsize.c | 20 ++-
src/backend/optimizer/path/joinpath.c | 30 +++-
src/backend/optimizer/plan/createplan.c | 111 +++++++++++-
src/backend/optimizer/plan/initsplan.c | 62 ++++++-
src/backend/optimizer/plan/planner.c | 3 +-
src/backend/optimizer/plan/subselect.c | 1 +
src/backend/optimizer/prep/prepjointree.c | 7 +
src/backend/optimizer/util/pathnode.c | 26 ++-
src/backend/optimizer/util/var.c | 165 +++++++++--------
src/backend/parser/analyze.c | 72 ++------
src/backend/parser/gram.y | 160 ++++++++++-------
src/backend/parser/parse_agg.c | 10 +
src/backend/parser/parse_clause.c | 284 +++++++++++++++--------------
src/backend/parser/parse_expr.c | 14 +--
src/backend/parser/parse_relation.c | 223 +++++++++++++++++------
src/backend/parser/parse_target.c | 6 +-
src/backend/utils/adt/ruleutils.c | 6 +
src/include/catalog/catversion.h | 2 +-
src/include/nodes/parsenodes.h | 9 +-
src/include/nodes/relation.h | 8 +-
src/include/optimizer/cost.h | 2 +-
src/include/optimizer/pathnode.h | 3 +-
src/include/optimizer/var.h | 3 +-
src/include/parser/kwlist.h | 1 +
src/include/parser/parse_node.h | 40 +++--
src/include/parser/parse_relation.h | 4 +
src/interfaces/ecpg/preproc/ecpg.addons | 6 +-
src/test/regress/expected/join.out | 160 ++++++++++++++++
src/test/regress/expected/rangefuncs.out | 3 +-
src/test/regress/expected/rules.out | 1 +
src/test/regress/expected/union.out | 1 +
src/test/regress/sql/join.sql | 46 +++++
41 files changed, 1300 insertions(+), 481 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Jan Urbański 2012-08-09 09:55:00 Re: Re: [COMMITTERS] pgsql: Fix mapping of PostgreSQL encodings to Python encodings.
Previous Message Robert Haas 2012-08-07 20:00:52 pgsql: Fix memory leaks in event trigger code.