This commit adds the optimizations we do for regular time bucket,
pushing down sort orders to the underlying time column, to time_bucket_gapfill.
This is made slightly more invasive by the fact that time_bucket_gapfill is
marked as VOLATILE (to prevent postgres from moving it in inappropriate ways).
We handle this by time_bucket_gapfill EC as not volatile, and letting the ususal
sort machinery do its job. This should be safe since we've already planned all
the per-relation paths and have installed the appropriate metadata to know we
will gapfill.
A number of TimescaleDB query optimizations involve operations on
functions. This refactor exposes a function cache that can be used to
quickly identify important functions and get access to relevant
auxiliary functionality and/or information. In particular, certain
functions apply to some type of (time) bucketing expression, e.g.,
expressions involving our own `time_bucket` function or PostgreSQL's
`date_trunc`.
This change recognizes the importance of time bucketing and uses the
function cache to access custom functionality around time bucketing
used in query optimizations. For example, both grouping estimates for
hash aggregates and sort transforms can be quickly accessed to make
better use of indexes when bucketing on a time column.
This refactor is also done with anticipation that this will be useful
going forward when other types of optimizations are implemented on
time bucketing expressions, or other functions that can benefit from
this cache.
The sort transform optimization to enable index usage are only
safe transformations when the modified PathKey is either the last
member of pathkeys or the only member.
Future proofing: if we ever want to make our functions available to
others they’d need to be prefixed to prevent name collisions. In
order to avoid having some functions with the ts_ prefix and
others without, we’re adding the prefix to all non-static
functions now.
This PR fixes all the formatting to be inline with the latest version of
pgindent. Since pgindent does not like variables named `type`, those
have been appropriately renamed.
Source code indentation has been updated in PostgreSQL 10 to fix a
number of issues. This update applies this new indentation to the
entire code base.
The new indentation requires a new version of pg_bsd_indent, which can
be found here:
https://git.postgresql.org/git/pg_bsd_indent.git
Handle case where there is an additional constraint on time.
This makes the pathkeys for the IndexPath incompatible with
upper level ordering. Since we know the appropriate transform
for those pathkeys already, apply it to those paths too.
Common time-series rollups are of the form SELECT date_trunc(const,
time) as t, aggregates... GROUP BY t ORDER BY t DESC LIMIT const2.
Previously, Postgres would not optimize such queries because it
could not match date_trunc(const, time) to any indexes on time, since
it has no way to know that a sort on time is always a valid sort on
date_trunc(const, time).
This commit implements this optimization for date_trunc but it could
apply to a wider range of functions, to be implemented later.