postgresql/src/include
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
..
access Add BRIN infrastructure for "inclusion" opclasses 2015-05-15 18:05:22 -03:00
bootstrap Tweak __attribute__-wrapping macros for better pgindent results. 2015-03-26 14:03:25 -04:00
catalog Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
commands Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
common Reorganize our CRC source files again. 2015-04-14 17:03:42 +03:00
datatype Define integer limits independently from the system definitions. 2015-04-02 17:43:35 +02:00
executor TABLESAMPLE, SQL Standard and extensible 2015-05-15 14:37:10 -04:00
foreign Add support for doing late row locking in FDWs. 2015-05-12 14:10:17 -04:00
lib Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
libpq Create an infrastructure for parallel computation in PostgreSQL. 2015-04-30 15:02:14 -04:00
mb Teach UtfToLocal/LocalToUtf to support algorithmic encoding conversions. 2015-05-14 22:27:12 -04:00
nodes Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
optimizer Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
parser Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
port Use Intel SSE 4.2 CRC instructions where available. 2015-04-14 17:05:03 +03:00
portability Use mmap MAP_NOSYNC option to limit shared memory writes 2015-03-21 22:06:19 -04:00
postmaster At promotion, archive last segment from old timeline with .partial suffix. 2015-05-08 21:59:01 +03:00
regex Update copyright for 2015 2015-01-06 11:43:47 -05:00
replication Map basebackup tablespaces using a tablespace_map file 2015-05-12 09:29:10 -04:00
rewrite Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
snowball Update copyright for 2015 2015-01-06 11:43:47 -05:00
storage Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
tcop Allow on-the-fly capture of DDL event details 2015-05-11 19:14:31 -03:00
tsearch Fix statically allocated struct with FLEXIBLE_ARRAY_MEMBER member. 2015-02-20 17:50:18 -05:00
utils Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
.gitignore Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
c.h Define integer limits independently from the system definitions. 2015-04-02 17:43:35 +02:00
fmgr.h Create an infrastructure for parallel computation in PostgreSQL. 2015-04-30 15:02:14 -04:00
funcapi.h Add transforms feature 2015-04-26 10:33:14 -04:00
getaddrinfo.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
getopt_long.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
Makefile Install all headers for the new atomics API. 2014-10-02 16:52:21 +02:00
miscadmin.h Add new OID alias type regrole 2015-05-09 13:06:49 -04:00
pg_config_ext.h.in Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
pg_config_ext.h.win32 Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
pg_config_manual.h Define integer limits independently from the system definitions. 2015-04-02 17:43:35 +02:00
pg_config.h.in Use Intel SSE 4.2 CRC instructions where available. 2015-04-14 17:05:03 +03:00
pg_config.h.win32 Oops, fix misspelled #endif 2015-04-14 22:00:52 +03:00
pg_getopt.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
pg_trace.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
pgstat.h Add system view pg_stat_ssl 2015-04-12 19:07:46 +02:00
pgtar.h Error when creating names too long for tar format 2015-02-24 13:41:07 -05:00
pgtime.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
port.h TABLESAMPLE, SQL Standard and extensible 2015-05-15 14:37:10 -04:00
postgres_ext.h pgindent run for 9.4 2014-05-06 12:12:18 -04:00
postgres_fe.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
postgres.h Support "expanded" objects, particularly arrays, for better performance. 2015-05-14 12:08:49 -04:00
rusagestub.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
windowapi.h Update copyright for 2015 2015-01-06 11:43:47 -05:00