CREATE MATERIALIZED VIEW (Continuous Aggregate)

The CREATE MATERIALIZED VIEW statement is used to create continuous aggregates.

The syntax is:

  1. CREATE MATERIALIZED VIEW <view_name> [ ( column_name [, ...] ) ]
  2. WITH ( timescaledb.continuous [, timescaledb.<option> = <value> ] )
  3. AS
  4. <select_query>
  5. [WITH [NO] DATA]

<select_query> is of the form:

  1. SELECT <grouping_exprs>, <aggregate_functions>
  2. FROM <hypertable>
  3. [WHERE ... ]
  4. GROUP BY time_bucket( <const_value>, <partition_col_of_hypertable> ),
  5. [ optional grouping exprs>]
  6. [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 its underlying hypertable. 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 refresh_continuous_aggregate.

Continuous aggregates have some limitations of what types of queries they can support, described in more length below. For example, the FROM clause must provide only one hypertable, and joins, CTEs, views or subqueries are not supported. The GROUP BY clause must include a time bucket on the hypertable time column, and all aggregates must be parallelizable.

Some important things to remember when constructing your SELECT query:

  • Only a single hypertable can be specified in the FROM clause of the SELECT query. You cannot include more hypertables, joins, tables, views, or subqueries.
  • The hypertable used in the SELECT query might not have row-level-security policies enabled.
  • The GROUP BY clause must include a time_bucket expression that uses the time dimension of the hypertable. For more information, see the time_bucket section.
  • You cannot use time_bucket_gapfill in continuous aggregates, but you can run them in a SELECT query from the continuous aggregate view.
  • You can usually use aggregates that are parallelized by PostgreSQL in the view definition, including most aggregates distributed by PostgreSQL. However, the ORDER BY, DISTINCT and FILTER clauses are not supported.
  • All functions and their arguments included in SELECT, GROUP BY and HAVING clauses must be immutable.
  • The view cannot be a security barrier view.
  • You cannot use Window functions with continuous aggregates.

The settings for continuous aggregates are in the informational views.

Parameters

NameTypeDescription
<view_name>TEXTName (optionally schema-qualified) of continuous aggregate view to create
<column_name>TEXTOptional list of names to be used for columns of the view. If not given, the column names are calculated from the query
WITH clauseTEXTSpecifies options for the continuous aggregate view
<select_query>TEXTA SELECT query that uses the specified syntax

Required WITH clause options:

NameTypeDescription
timescaledb.continuousBOOLEANIf timescaledb.continuous is not specified, this is a regular PostgresSQL materialized view

Optional WITH clause options:

NameTypeDescriptionDefault value
timescaledb.materialized_onlyBOOLEANReturn only materialized data when querying the continuous aggregate viewFALSE
timescaledb.create_group_indexesBOOLEANCreate indexes on the continuous aggregate for columns in its GROUP BY clause. Indexes are in the form (<GROUP_BY_COLUMN>, time_bucket)TRUE
timescaledb.finalizedBOOLEANIn 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 FILTER, ORDER BY, and DISTINCT clauses.TRUE

For more information, see the real-time aggregates section.

Sample usage

Create a daily continuous aggregate view:

  1. CREATE MATERIALIZED VIEW continuous_aggregate_daily( timec, minl, sumt, sumh )
  2. WITH (timescaledb.continuous) AS
  3. SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
  4. FROM conditions
  5. GROUP BY time_bucket('1day', timec)

Add a thirty day continuous aggregate on top of the same raw hypertable:

  1. CREATE MATERIALIZED VIEW continuous_aggregate_thirty_day( timec, minl, sumt, sumh )
  2. WITH (timescaledb.continuous) AS
  3. SELECT time_bucket('30day', timec), min(location), sum(temperature), sum(humidity)
  4. FROM conditions
  5. GROUP BY time_bucket('30day', timec);

Add an hourly continuous aggregate on top of the same raw hypertable:

  1. CREATE MATERIALIZED VIEW continuous_aggregate_hourly( timec, minl, sumt, sumh )
  2. WITH (timescaledb.continuous) AS
  3. SELECT time_bucket('1h', timec), min(location), sum(temperature), sum(humidity)
  4. FROM conditions
  5. GROUP BY time_bucket('1h', timec);