postgresql/src/backend/commands
Andres Freund f3d3118532 Support GROUPING SETS, CUBE and ROLLUP.
This SQL standard functionality allows to aggregate data by different
GROUP BY clauses at once. Each grouping set returns rows with columns
grouped by in other sets set to NULL.

This could previously be achieved by doing each grouping as a separate
query, conjoined by UNION ALLs. Besides being considerably more concise,
grouping sets will in many cases be faster, requiring only one scan over
the underlying data.

The current implementation of grouping sets only supports using sorting
for input. Individual sets that share a sort order are computed in one
pass. If there are sets that don't share a sort order, additional sort &
aggregation steps are performed. These additional passes are sourced by
the previous sort step; thus avoiding repeated scans of the source data.

The code is structured in a way that adding support for purely using
hash aggregation or a mix of hashing and sorting is possible. Sorting
was chosen to be supported first, as it is the most generic method of
implementation.

Instead of, as in an earlier versions of the patch, representing the
chain of sort and aggregation steps as full blown planner and executor
nodes, all but the first sort are performed inside the aggregation node
itself. This avoids the need to do some unusual gymnastics to handle
having to return aggregated and non-aggregated tuples from underlying
nodes, as well as having to shut down underlying nodes early to limit
memory usage.  The optimizer still builds Sort/Agg node to describe each
phase, but they're not part of the plan tree, but instead additional
data for the aggregation node. They're a convenient and preexisting way
to describe aggregation and sorting.  The first (and possibly only) sort
step is still performed as a separate execution step. That retains
similarity with existing group by plans, makes rescans fairly simple,
avoids very deep plans (leading to slow explains) and easily allows to
avoid the sorting step if the underlying data is sorted by other means.

A somewhat ugly side of this patch is having to deal with a grammar
ambiguity between the new CUBE keyword and the cube extension/functions
named cube (and rollup). To avoid breaking existing deployments of the
cube extension it has not been renamed, neither has cube been made a
reserved keyword. Instead precedence hacking is used to make GROUP BY
cube(..) refer to the CUBE grouping sets feature, and not the function
cube(). To actually group by a function cube(), unlikely as that might
be, the function name has to be quoted.

Needs a catversion bump because stored rules may change.

Author: Andrew Gierth and Atri Sharma, with contributions from Andres Freund
Reviewed-By: Andres Freund, Noah Misch, Tom Lane, Svenne Krap, Tomas
    Vondra, Erik Rijkers, Marti Raudsepp, Pavel Stehule
Discussion: CAOeZVidmVRe2jU6aMk_5qkxnB7dfmPROzM7Ur8JPW5j8Y5X-Lw@mail.gmail.com
2015-05-16 03:46:31 +02:00
..
aggregatecmds.c Change many routines to return ObjectAddress rather than OID 2015-03-03 14:10:50 -03:00
alter.c Remove variable shadowing 2015-04-07 17:14:00 -03:00
analyze.c TABLESAMPLE, SQL Standard and extensible 2015-05-15 14:37:10 -04:00
async.c Use FLEXIBLE_ARRAY_MEMBER in a number of other places. 2015-02-21 16:12:14 -05:00
cluster.c Support VERBOSE option in REINDEX command. 2015-05-15 20:09:57 +09:00
collationcmds.c Change many routines to return ObjectAddress rather than OID 2015-03-03 14:10:50 -03:00
comment.c Change many routines to return ObjectAddress rather than OID 2015-03-03 14:10:50 -03:00
constraint.c Fix incorrect checking of deferred exclusion constraint after a HOT update. 2015-05-11 12:25:43 -04:00
conversioncmds.c Change many routines to return ObjectAddress rather than OID 2015-03-03 14:10:50 -03:00
copy.c Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
createas.c Represent columns requiring insert and update privileges indentently. 2015-05-08 00:20:46 +02:00
dbcommands.c Move WAL-related definitions from dbcommands.h to separate header file. 2015-03-09 15:50:49 +02:00
define.c Update copyright for 2015 2015-01-06 11:43:47 -05:00
discard.c Update copyright for 2015 2015-01-06 11:43:47 -05:00
dropcmds.c Add transforms feature 2015-04-26 10:33:14 -04:00
event_trigger.c Allow on-the-fly capture of DDL event details 2015-05-11 19:14:31 -03:00
explain.c Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
extension.c Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
foreigncmds.c Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
functioncmds.c Add transforms feature 2015-04-26 10:33:14 -04:00
indexcmds.c Support VERBOSE option in REINDEX command. 2015-05-15 20:09:57 +09:00
lockcmds.c Allow LOCK TABLE .. ROW EXCLUSIVE MODE with INSERT 2015-05-11 15:44:12 -04:00
Makefile Row-Level Security Policies (RLS) 2014-09-19 11:18:35 -04:00
matview.c Change many routines to return ObjectAddress rather than OID 2015-03-03 14:10:50 -03:00
opclasscmds.c Allow on-the-fly capture of DDL event details 2015-05-11 19:14:31 -03:00
operatorcmds.c Allow named parameters to be specified using => in addition to := 2015-03-10 11:09:41 -04:00
policy.c Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
portalcmds.c Update copyright for 2015 2015-01-06 11:43:47 -05:00
prepare.c Use FLEXIBLE_ARRAY_MEMBER in a bunch more places. 2015-02-20 00:11:42 -05:00
proclang.c Add transforms feature 2015-04-26 10:33:14 -04:00
schemacmds.c Allow on-the-fly capture of DDL event details 2015-05-11 19:14:31 -03:00
seclabel.c Change many routines to return ObjectAddress rather than OID 2015-03-03 14:10:50 -03:00
sequence.c Create an infrastructure for parallel computation in PostgreSQL. 2015-04-30 15:02:14 -04:00
tablecmds.c Support VERBOSE option in REINDEX command. 2015-05-15 20:09:57 +09:00
tablespace.c Warn about tablespace creation in PGDATA 2015-04-28 17:35:12 -04:00
trigger.c Minor ON CONFLICT related comments and doc fixes. 2015-05-08 19:24:14 +02:00
tsearchcmds.c Allow on-the-fly capture of DDL event details 2015-05-11 19:14:31 -03:00
typecmds.c Add transforms feature 2015-04-26 10:33:14 -04:00
user.c Integrate pg_upgrade_support module into backend 2015-04-14 19:26:37 -04:00
vacuum.c Teach autovacuum about multixact member wraparound. 2015-05-08 12:53:00 -04:00
vacuumlazy.c Add log_min_autovacuum_duration per-table option 2015-04-03 11:55:50 -03:00
variable.c Update copyright for 2015 2015-01-06 11:43:47 -05:00
view.c Require non-NULL pstate for all addRangeTableEntryFor* functions. 2015-03-11 15:26:43 -04:00