postgresql/doc/src/sgml/ref
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
..
abort.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
allfiles.sgml Add transforms feature 2015-04-26 10:33:14 -04:00
alter_aggregate.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_collation.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_conversion.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_database.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_default_privileges.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
alter_domain.sgml doc: ALTER DOMAIN VALIDATE CONSTRAINT can also fail 2015-03-19 13:02:09 -04:00
alter_event_trigger.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_extension.sgml Add transforms feature 2015-04-26 10:33:14 -04:00
alter_foreign_data_wrapper.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_foreign_table.sgml Allow foreign tables to participate in inheritance. 2015-03-22 13:53:21 -04:00
alter_function.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_group.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_index.sgml Rework 'MOVE ALL' to 'ALTER .. ALL IN TABLESPACE' 2014-08-21 19:06:17 -04:00
alter_language.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_large_object.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_materialized_view.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_opclass.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_operator.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_opfamily.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_policy.sgml Fix up some loose ends for CURRENT_USER as RoleSpec 2015-04-30 16:57:05 -03:00
alter_role.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_rule.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
alter_schema.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_sequence.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_server.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_system.sgml Improve documentation around parameter-setting and ALTER SYSTEM. 2014-12-14 18:09:51 -05:00
alter_table.sgml doc: Move ALTER TABLE IF EXISTS description to better place 2015-04-24 13:22:18 -04:00
alter_tablespace.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_trigger.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
alter_tsconfig.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_tsdictionary.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_tsparser.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
alter_tstemplate.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
alter_type.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_user_mapping.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_user.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
alter_view.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
analyze.sgml Allow foreign tables to participate in inheritance. 2015-03-22 13:53:21 -04:00
begin.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
checkpoint.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
close.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
cluster.sgml Add GUC and storage parameter to set the maximum size of GIN pending list. 2014-11-11 21:08:21 +09:00
clusterdb.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
comment.sgml Add transforms feature 2015-04-26 10:33:14 -04:00
commit_prepared.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
commit.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
copy.sgml Correct stdin/stdout usage in COPY .. PROGRAM 2014-09-30 15:55:28 -04:00
create_aggregate.sgml doc: Fix copy-and-paste mistakes 2014-10-13 22:22:20 -04:00
create_cast.sgml docs: remove unnecessary references to old PG versions 2014-02-24 12:56:37 -05:00
create_collation.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_conversion.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_database.sgml Allow CREATE/ALTER DATABASE to manipulate datistemplate and datallowconn. 2014-07-01 20:10:38 -04:00
create_domain.sgml Apply table and domain CHECK constraints in name order. 2015-03-23 16:59:35 -04:00
create_event_trigger.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_extension.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_foreign_data_wrapper.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_foreign_table.sgml doc: CREATE FOREIGN TABLE now allows CHECK ( ... ) NO INHERIT 2015-05-15 14:42:15 -04:00
create_function.sgml Add transforms feature 2015-04-26 10:33:14 -04:00
create_group.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_index.sgml docs: consistently uppercase index method and add spacing 2015-05-15 11:42:34 -04:00
create_language.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_materialized_view.sgml doc: Fix markup 2014-12-13 14:16:16 -05:00
create_opclass.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_operator.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_opfamily.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_policy.sgml Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
create_role.sgml Row-Level Security Policies (RLS) 2014-09-19 11:18:35 -04:00
create_rule.sgml Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
create_schema.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
create_sequence.sgml Fix documentation for CREATE SEQUENCE IF NOT EXISTS. 2014-10-03 10:25:48 +03:00
create_server.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_table_as.sgml Add CINE option for CREATE TABLE AS and CREATE MATERIALIZED VIEW 2014-12-13 13:56:09 -05:00
create_table.sgml Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
create_tablespace.sgml Fix up some loose ends for CURRENT_USER as RoleSpec 2015-04-30 16:57:05 -03:00
create_transform.sgml Add transforms feature 2015-04-26 10:33:14 -04:00
create_trigger.sgml Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
create_tsconfig.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_tsdictionary.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_tsparser.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_tstemplate.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_type.sgml docs: clarify the use of shell types 2015-03-20 18:48:52 -04:00
create_user_mapping.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_user.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
create_view.sgml Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. 2015-05-08 05:43:10 +02:00
createdb.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
createlang.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
createuser.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
deallocate.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
declare.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
delete.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
discard.sgml Improve DISCARD documentation. 2014-08-17 15:59:03 -04:00
do.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_aggregate.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_cast.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_collation.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_conversion.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_database.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_domain.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_event_trigger.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_extension.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_foreign_data_wrapper.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_foreign_table.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_function.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_group.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_index.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_language.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_materialized_view.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_opclass.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_operator.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_opfamily.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_owned.sgml Fix up some loose ends for CURRENT_USER as RoleSpec 2015-04-30 16:57:05 -03:00
drop_policy.sgml Rename pg_rowsecurity -> pg_policy and other fixes 2014-11-27 01:15:57 -05:00
drop_role.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_rule.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_schema.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_sequence.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_server.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_table.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_tablespace.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_transform.sgml Add transforms feature 2015-04-26 10:33:14 -04:00
drop_trigger.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_tsconfig.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_tsdictionary.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_tsparser.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_tstemplate.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_type.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_user_mapping.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_user.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
drop_view.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
dropdb.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
droplang.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
dropuser.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
ecpg-ref.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
end.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
execute.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
explain.sgml Print planning time only in EXPLAIN ANALYZE, not plain EXPLAIN. 2014-10-15 18:50:13 -04:00
fetch.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
grant.sgml Allow CURRENT/SESSION_USER to be used in certain commands 2015-03-09 15:41:54 -03:00
import_foreign_schema.sgml Implement IMPORT FOREIGN SCHEMA. 2014-07-10 15:01:43 -04:00
initdb.sgml doc: Add link to how to specify time zone names to initdb man page 2014-12-14 20:02:04 -05:00
insert.sgml Minor ON CONFLICT related comments and doc fixes. 2015-05-08 19:24:14 +02:00
listen.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
load.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
lock.sgml Allow LOCK TABLE .. ROW EXCLUSIVE MODE with INSERT 2015-05-11 15:44:12 -04:00
move.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
notify.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
pg_basebackup.sgml Map basebackup tablespaces using a tablespace_map file 2015-05-12 09:29:10 -04:00
pg_config-ref.sgml docs: remove unnecessary references to old PG versions 2014-02-24 12:56:37 -05:00
pg_controldata.sgml Add -D option to specify data directory to pg_controldata and pg_resetxlog. 2014-09-25 13:37:19 +03:00
pg_ctl-ref.sgml pg_ctl: change default shutdown mode from 'smart' to 'fast' 2015-03-31 11:46:27 -04:00
pg_dump.sgml Add pg_dump --snapshot option 2014-11-17 22:15:07 +00:00
pg_dumpall.sgml pg_dump et al: Add --if-exists option 2014-03-03 15:02:18 -03:00
pg_isready.sgml docs: Improve pg_isready details about username/dbname 2014-09-06 12:43:11 -04:00
pg_receivexlog.sgml Rephrase the documentation on pg_receivexlog --synchronous option. 2015-02-03 10:37:44 +02:00
pg_recvlogical.sgml doc: Wording and formatting improvements in new logical decoding docs 2014-10-30 22:53:11 -04:00
pg_resetxlog.sgml Keep track of transaction commit timestamps 2014-12-03 11:53:02 -03:00
pg_restore.sgml Code review for row security. 2014-09-24 16:32:22 -04:00
pg_rewind.sgml Add markup for replaceable parameters to pg_rewind doc. 2015-04-03 12:21:16 +09:00
pg_xlogdump.sgml Move pg_xlogdump from contrib/ to src/bin/ 2015-04-21 19:03:49 -04:00
pgarchivecleanup.sgml Move pg_archivecleanup from contrib/ to src/bin/ 2015-04-11 23:29:18 -04:00
pgbench.sgml Move pgbench from contrib/ to src/bin/ 2015-04-13 13:07:16 -04:00
pgtestfsync.sgml Move pg_test_fsync from contrib/ to src/bin/ 2015-04-19 22:20:49 -04:00
pgtesttiming.sgml Move pg_test_timing from contrib/ to src/bin/ 2015-04-20 21:30:12 -04:00
pgupgrade.sgml pg_upgrade: document need for text search files to be copied 2015-04-16 19:51:24 -04:00
postgres-ref.sgml Properly document that -r is only honored from the command-line. 2014-09-06 11:10:52 -04:00
postmaster.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
prepare_transaction.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
prepare.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
psql-ref.sgml psql: fix \connect with URIs and conninfo strings 2015-04-02 12:30:57 -03:00
reassign_owned.sgml Fix up some loose ends for CURRENT_USER as RoleSpec 2015-04-30 16:57:05 -03:00
refresh_materialized_view.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
reindex.sgml Support VERBOSE option in REINDEX command. 2015-05-15 20:09:57 +09:00
reindexdb.sgml Support --verbose option in reindexdb. 2015-05-15 21:45:55 +09:00
release_savepoint.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
reset.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
revoke.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
rollback_prepared.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
rollback_to.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
rollback.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
savepoint.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
security_label.sgml Move test modules from contrib to src/test/modules 2014-11-29 23:55:00 -03:00
select_into.sgml docs: remove unnecessary references to old PG versions 2014-02-24 12:56:37 -05:00
select.sgml Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
set_constraints.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
set_role.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
set_session_auth.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
set_transaction.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
set.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
show.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
start_transaction.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
truncate.sgml Allow foreign tables to participate in inheritance. 2015-03-22 13:53:21 -04:00
unlisten.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00
update.sgml Implement UPDATE tab SET (col1,col2,...) = (SELECT ...), ... 2014-06-18 13:22:34 -04:00
vacuum.sgml Have VACUUM log number of skipped pages due to pins 2014-12-18 17:18:33 -03:00
vacuumdb.sgml vacuumdb: enable parallel mode 2015-01-23 15:02:45 -03:00
values.sgml doc: Improve DocBook XML validity 2014-02-23 21:31:08 -05:00