postgresql/src/include/utils
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
..
.gitignore Avoid maintaining three separate copies of the error codes list. 2011-02-03 22:32:49 -05:00
acl.h Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
aclchk_internal.h Allow on-the-fly capture of DDL event details 2015-05-11 19:14:31 -03:00
array.h Support "expanded" objects, particularly arrays, for better performance. 2015-05-14 12:08:49 -04:00
arrayaccess.h Support "expanded" objects, particularly arrays, for better performance. 2015-05-14 12:08:49 -04:00
ascii.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
attoptcache.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
builtins.h Allow on-the-fly capture of DDL event details 2015-05-11 19:14:31 -03:00
bytea.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
cash.h Add casts from int4 and int8 to numeric. 2011-04-05 09:35:43 -04:00
catcache.h Use FLEXIBLE_ARRAY_MEMBER in a bunch more places. 2015-02-20 00:11:42 -05:00
combocid.h Create an infrastructure for parallel computation in PostgreSQL. 2015-04-30 15:02:14 -04:00
date.h Define integer limits independently from the system definitions. 2015-04-02 17:43:35 +02:00
datetime.h Tweak __attribute__-wrapping macros for better pgindent results. 2015-03-26 14:03:25 -04:00
datum.h Support "expanded" objects, particularly arrays, for better performance. 2015-05-14 12:08:49 -04:00
dynahash.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
dynamic_loader.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
elog.h Tweak __attribute__-wrapping macros for better pgindent results. 2015-03-26 14:03:25 -04:00
evtcache.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
expandeddatum.h Support "expanded" objects, particularly arrays, for better performance. 2015-05-14 12:08:49 -04:00
fmgrtab.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
formatting.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
geo_decls.h Allow GiST distance function to return merely a lower-bound. 2015-05-15 14:26:51 +03:00
guc.h Use abbreviated keys for faster sorting of numeric datums. 2015-04-02 14:04:26 -04:00
guc_tables.h Add pg_settings.pending_restart column 2015-05-14 20:08:51 -04:00
help_config.h Tweak __attribute__-wrapping macros for better pgindent results. 2015-03-26 14:03:25 -04:00
hsearch.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
inet.h Add index-only scan support to inet GiST opclass. 2015-03-28 15:11:53 +02:00
int8.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
inval.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
json.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
jsonapi.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
jsonb.h Additional functions and operators for jsonb 2015-05-12 15:52:45 -04:00
logtape.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
lsyscache.h TABLESAMPLE, SQL Standard and extensible 2015-05-15 14:37:10 -04:00
memdebug.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
memutils.h Move memory context callback declarations into palloc.h. 2015-03-01 12:31:32 -05:00
nabstime.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
numeric.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
palloc.h Add palloc_extended for frontend and backend. 2015-04-03 17:36:12 +09:00
pg_crc.h Reorganize our CRC source files again. 2015-04-14 17:03:42 +03:00
pg_locale.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
pg_lsn.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
pg_rusage.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
plancache.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
portal.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
ps_status.h Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
rangetypes.h Move strategy numbers to include/access/stratnum.h 2015-05-15 17:03:16 -03:00
rel.h TABLESAMPLE, SQL Standard and extensible 2015-05-15 14:37:10 -04:00
relcache.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
relfilenodemap.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
relmapper.h Use FLEXIBLE_ARRAY_MEMBER in a bunch more places. 2015-02-20 00:11:42 -05:00
reltrigger.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
resowner.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
resowner_private.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
rls.h Fix column-privilege leak in error-message paths 2015-01-28 12:31:30 -05:00
ruleutils.h Improve performance of EXPLAIN with large range tables. 2015-01-15 13:18:12 -05:00
sampling.h TABLESAMPLE, SQL Standard and extensible 2015-05-15 14:37:10 -04:00
selfuncs.h Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
snapmgr.h Create an infrastructure for parallel computation in PostgreSQL. 2015-04-30 15:02:14 -04:00
snapshot.h Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
sortsupport.h Fix typos, update README. 2015-01-23 15:06:53 -05:00
spccache.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
syscache.h TABLESAMPLE, SQL Standard and extensible 2015-05-15 14:37:10 -04:00
timeout.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
timestamp.h Add transform functions for AT TIME ZONE. 2015-03-01 13:22:34 -05:00
tqual.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
tuplesort.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
tuplestore.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
typcache.h Use the typcache to cache constraints for domain types. 2015-03-01 14:06:55 -05:00
tzparser.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
uuid.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
varbit.h Use FLEXIBLE_ARRAY_MEMBER in a bunch more places. 2015-02-20 00:11:42 -05:00
xml.h Update copyright for 2015 2015-01-06 11:43:47 -05:00