postgresql/src/include/catalog
David Rowley 9d9c02ccd1 Teach planner and executor about monotonic window funcs
Window functions such as row_number() always return a value higher than
the previously returned value for tuples in any given window partition.

Traditionally queries such as;

SELECT * FROM (
   SELECT *, row_number() over (order by c) rn
   FROM t
) t WHERE rn <= 10;

were executed fairly inefficiently.  Neither the query planner nor the
executor knew that once rn made it to 11 that nothing further would match
the outer query's WHERE clause.  It would blindly continue until all
tuples were exhausted from the subquery.

Here we implement means to make the above execute more efficiently.

This is done by way of adding a pg_proc.prosupport function to various of
the built-in window functions and adding supporting code to allow the
support function to inform the planner if the window function is
monotonically increasing, monotonically decreasing, both or neither.  The
planner is then able to make use of that information and possibly allow
the executor to short-circuit execution by way of adding a "run condition"
to the WindowAgg to allow it to determine if some of its execution work
can be skipped.

This "run condition" is not like a normal filter.  These run conditions
are only built using quals comparing values to monotonic window functions.
For monotonic increasing functions, quals making use of the btree
operators for <, <= and = can be used (assuming the window function column
is on the left). You can see here that once such a condition becomes false
that a monotonic increasing function could never make it subsequently true
again.  For monotonically decreasing functions the >, >= and = btree
operators for the given type can be used for run conditions.

The best-case situation for this is when there is a single WindowAgg node
without a PARTITION BY clause.  Here when the run condition becomes false
the WindowAgg node can simply return NULL.  No more tuples will ever match
the run condition.  It's a little more complex when there is a PARTITION
BY clause.  In this case, we cannot return NULL as we must still process
other partitions.  To speed this case up we pull tuples from the outer
plan to check if they're from the same partition and simply discard them
if they are.  When we find a tuple belonging to another partition we start
processing as normal again until the run condition becomes false or we run
out of tuples to process.

When there are multiple WindowAgg nodes to evaluate then this complicates
the situation.  For intermediate WindowAggs we must ensure we always
return all tuples to the calling node.  Any filtering done could lead to
incorrect results in WindowAgg nodes above.  For all intermediate nodes,
we can still save some work when the run condition becomes false.  We've
no need to evaluate the WindowFuncs anymore.  Other WindowAgg nodes cannot
reference the value of these and these tuples will not appear in the final
result anyway.  The savings here are small in comparison to what can be
saved in the top-level WingowAgg, but still worthwhile.

Intermediate WindowAgg nodes never filter out tuples, but here we change
WindowAgg so that the top-level WindowAgg filters out tuples that don't
match the intermediate WindowAgg node's run condition.  Such filters
appear in the "Filter" clause in EXPLAIN for the top-level WindowAgg node.

Here we add prosupport functions to allow the above to work for;
row_number(), rank(), dense_rank(), count(*) and count(expr).  It appears
technically possible to do the same for min() and max(), however, it seems
unlikely to be useful enough, so that's not done here.

Bump catversion

Author: David Rowley
Reviewed-by: Andy Fan, Zhihong Yu
Discussion: https://postgr.es/m/CAApHDvqvp3At8++yF8ij06sdcoo1S_b2YoaT9D4Nf+MObzsrLQ@mail.gmail.com
2022-04-08 10:34:36 +12:00
..
.gitignore Build in some knowledge about foreign-key relationships in the catalogs. 2021-02-02 17:11:55 -05:00
Makefile Update copyright for 2022 2022-01-07 19:04:57 -05:00
binary_upgrade.h pg_upgrade: Preserve relfilenodes and tablespace OIDs. 2022-01-17 13:40:27 -05:00
catalog.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
catversion.h Teach planner and executor about monotonic window funcs 2022-04-08 10:34:36 +12:00
dependency.h Allow granting SET and ALTER SYSTEM privileges on GUC parameters. 2022-04-06 13:24:33 -04:00
duplicate_oids Update copyright for 2022 2022-01-07 19:04:57 -05:00
genbki.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
heap.h Move pg_attrdef manipulation code into new file catalog/pg_attrdef.c. 2022-03-21 14:38:23 -04:00
index.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
indexing.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
namespace.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
objectaccess.h Allow granting SET and ALTER SYSTEM privileges on GUC parameters. 2022-04-06 13:24:33 -04:00
objectaddress.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
partition.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_aggregate.dat Add range_agg with multirange inputs 2022-03-30 20:16:23 +02:00
pg_aggregate.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_am.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_am.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_amop.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_amop.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_amproc.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_amproc.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_attrdef.h Move pg_attrdef manipulation code into new file catalog/pg_attrdef.c. 2022-03-21 14:38:23 -04:00
pg_attribute.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_auth_members.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_authid.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_authid.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_cast.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_cast.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_class.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_class.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_collation.dat Add option to use ICU as global locale provider 2022-03-17 11:13:16 +01:00
pg_collation.h Add option to use ICU as global locale provider 2022-03-17 11:13:16 +01:00
pg_constraint.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_control.h Remove exclusive backup mode 2022-04-06 14:41:03 -04:00
pg_conversion.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_conversion.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_database.dat Add option to use ICU as global locale provider 2022-03-17 11:13:16 +01:00
pg_database.h Add option to use ICU as global locale provider 2022-03-17 11:13:16 +01:00
pg_db_role_setting.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_default_acl.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_depend.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_description.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_enum.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_event_trigger.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_extension.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_foreign_data_wrapper.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_foreign_server.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_foreign_table.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_index.h Add UNIQUE null treatment option 2022-02-03 11:48:21 +01:00
pg_inherits.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_init_privs.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_language.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_language.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_largeobject.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_largeobject_metadata.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_namespace.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_namespace.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_opclass.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_opclass.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_operator.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_operator.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_opfamily.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_opfamily.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_parameter_acl.h Allow granting SET and ALTER SYSTEM privileges on GUC parameters. 2022-04-06 13:24:33 -04:00
pg_partitioned_table.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_policy.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_proc.dat Teach planner and executor about monotonic window funcs 2022-04-08 10:34:36 +12:00
pg_proc.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_publication.h Revert "Logical decoding of sequences" 2022-04-07 20:06:36 +02:00
pg_publication_namespace.h Revert "Logical decoding of sequences" 2022-04-07 20:06:36 +02:00
pg_publication_rel.h Allow specifying column lists for logical replication 2022-03-26 01:01:27 +01:00
pg_range.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_range.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_replication_origin.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_rewrite.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_seclabel.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_sequence.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_shdepend.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_shdescription.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_shseclabel.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_statistic.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_statistic_ext.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_statistic_ext_data.h Add stxdinherit flag to pg_statistic_ext_data 2022-01-16 13:38:01 +01:00
pg_subscription.h Reorder subskiplsn in pg_subscription to avoid alignment issues. 2022-04-07 09:39:25 +05:30
pg_subscription_rel.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_tablespace.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_tablespace.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_transform.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_trigger.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_config.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_config.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_config_map.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_config_map.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_dict.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_dict.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_parser.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_parser.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_template.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_ts_template.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_type.dat Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_type.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pg_user_mapping.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
reformat_dat_file.pl Update copyright for 2022 2022-01-07 19:04:57 -05:00
renumber_oids.pl Update copyright for 2022 2022-01-07 19:04:57 -05:00
storage.h Add new block-by-block strategy for CREATE DATABASE. 2022-03-29 11:48:36 -04:00
storage_xlog.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
toasting.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
unused_oids pg_upgrade: Preserve database OIDs. 2022-01-24 14:23:43 -05:00