Go to file
Richard Guo 247dea89f7 Introduce an RTE for the grouping step
If there are subqueries in the grouping expressions, each of these
subqueries in the targetlist and HAVING clause is expanded into
distinct SubPlan nodes.  As a result, only one of these SubPlan nodes
would be converted to reference to the grouping key column output by
the Agg node; others would have to get evaluated afresh.  This is not
efficient, and with grouping sets this can cause wrong results issues
in cases where they should go to NULL because they are from the wrong
grouping set.  Furthermore, during re-evaluation, these SubPlan nodes
might use nulled column values from grouping sets, which is not
correct.

This issue is not limited to subqueries.  For other types of
expressions that are part of grouping items, if they are transformed
into another form during preprocessing, they may fail to match lower
target items.  This can also lead to wrong results with grouping sets.

To fix this issue, we introduce a new kind of RTE representing the
output of the grouping step, with columns that are the Vars or
expressions being grouped on.  In the parser, we replace the grouping
expressions in the targetlist and HAVING clause with Vars referencing
this new RTE, so that the output of the parser directly expresses the
semantic requirement that the grouping expressions be gotten from the
grouping output rather than computed some other way.  In the planner,
we first preprocess all the columns of this new RTE and then replace
any Vars in the targetlist and HAVING clause that reference this new
RTE with the underlying grouping expressions, so that we will have
only one instance of a SubPlan node for each subquery contained in the
grouping expressions.

Bump catversion because this changes the querytree produced by the
parser.

Thanks to Tom Lane for the idea to invent a new kind of RTE.

Per reports from Geoff Winkless, Tobias Wendorff, Richard Guo from
various threads.

Author: Richard Guo
Reviewed-by: Ashutosh Bapat, Sutou Kouhei
Discussion: https://postgr.es/m/CAMbWs4_dp7e7oTwaiZeBX8+P1rXw4ThkZxh1QG81rhu9Z47VsQ@mail.gmail.com
2024-09-10 12:35:34 +09:00
.github Add CODE_OF_CONDUCT.md, CONTRIBUTING.md, and SECURITY.md. 2024-07-02 13:03:58 -05:00
config jit: Remove {llvm-config,clang}-N configure probes. 2024-05-16 13:58:25 +12:00
contrib Consistently use PageGetExactFreeSpace() in pgstattuple. 2024-09-09 14:34:10 -04:00
doc Don't bother checking the result of SPI_connect[_ext] anymore. 2024-09-09 12:18:34 -04:00
src Introduce an RTE for the grouping step 2024-09-10 12:35:34 +09:00
.cirrus.star
.cirrus.tasks.yml Convert node test compile-time settings into run-time parameters 2024-08-01 10:09:18 +02:00
.cirrus.yml
.dir-locals.el
.editorconfig
.git-blame-ignore-revs Add 97add39c0 to .git-blame-ignore-revs. 2024-08-15 11:43:55 -04:00
.gitattributes
.gitignore
aclocal.m4
configure Remove support for OpenSSL older than 1.1.0 2024-09-02 13:51:48 +02:00
configure.ac Remove support for OpenSSL older than 1.1.0 2024-09-02 13:51:48 +02:00
COPYRIGHT
GNUmakefile.in Allow selecting the git revision to be packaged by "make dist". 2024-05-03 11:08:50 -04:00
HISTORY
Makefile
meson_options.txt Remove spinlocks and atomics from meson_options.txt. 2024-07-30 23:37:14 +12:00
meson.build Remove support for OpenSSL older than 1.1.0 2024-09-02 13:51:48 +02:00
README.md Revise the style of a paragraph in README.md. 2024-03-21 10:16:41 -05:00

PostgreSQL Database Management System

This directory contains the source code distribution of the PostgreSQL database management system.

PostgreSQL is an advanced object-relational database management system that supports an extended subset of the SQL standard, including transactions, foreign keys, subqueries, triggers, user-defined types and functions. This distribution also contains C language bindings.

Copyright and license information can be found in the file COPYRIGHT.

General documentation about this version of PostgreSQL can be found at https://www.postgresql.org/docs/devel/. In particular, information about building PostgreSQL from the source code can be found at https://www.postgresql.org/docs/devel/installation.html.

The latest version of this software, and related software, may be obtained at https://www.postgresql.org/download/. For more information look at our web site located at https://www.postgresql.org/.