CREATE MATERIALIZED VIEW statement is used to create continuous
aggregates. To learn more, see the
continuous aggregate how-to guides.
The syntax is:
CREATE MATERIALIZED VIEW <view_name> [ ( column_name [, ...] ) ]WITH ( timescaledb.continuous [, timescaledb.<option> = <value> ] )AS<select_query>[WITH [NO] DATA]
<select_query> is of the form:
SELECT <grouping_exprs>, <aggregate_functions>FROM <hypertable or another continuous aggregate>[WHERE ... ]GROUP BY time_bucket( <const_value>, <partition_col_of_hypertable> ),[ optional grouping exprs>][HAVING ...]
The continuous aggregate view defaults to
WITH DATA. This means that when the
view is created, it refreshes using all the current data in the underlying
hypertable or continuous aggregate. This occurs once when the view is created.
If you want the view to be refreshed regularly, you can use a refresh policy. If
you do not want the view to update when it is first created, use the
WITH NO DATA parameter. For more information, see
Continuous aggregates have some limitations of what types of queries they can support. For more information, see the continuous aggregates section.
The settings for continuous aggregates are in the informational views.
|TEXT||Name (optionally schema-qualified) of continuous aggregate view to create|
|TEXT||Optional list of names to be used for columns of the view. If not given, the column names are calculated from the query|
|TEXT||Specifies options for the continuous aggregate view|
WITH clause options:
WITH clause options:
|BOOLEAN||Return only materialized data when querying the continuous aggregate view|
|BOOLEAN||Create indexes on the continuous aggregate for columns in its |
|BOOLEAN||In TimescaleDB 2.7 and above, use the new version of continuous aggregates, which stores finalized results for aggregate functions. Supports all aggregate functions, including ones that use |
For more information, see the real-time aggregates section.
Create a daily continuous aggregate view:
CREATE MATERIALIZED VIEW continuous_aggregate_daily( timec, minl, sumt, sumh )WITH (timescaledb.continuous) ASSELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)FROM conditionsGROUP BY time_bucket('1day', timec)
Add a thirty day continuous aggregate on top of the same raw hypertable:
CREATE MATERIALIZED VIEW continuous_aggregate_thirty_day( timec, minl, sumt, sumh )WITH (timescaledb.continuous) ASSELECT time_bucket('30day', timec), min(location), sum(temperature), sum(humidity)FROM conditionsGROUP BY time_bucket('30day', timec);
Add an hourly continuous aggregate on top of the same raw hypertable:
CREATE MATERIALIZED VIEW continuous_aggregate_hourly( timec, minl, sumt, sumh )WITH (timescaledb.continuous) ASSELECT time_bucket('1h', timec), min(location), sum(temperature), sum(humidity)FROM conditionsGROUP BY time_bucket('1h', timec);
Found an issue on this page?Report an issue!