Search code examples
sqlpostgresqltimescaledbcontinuous-aggregates

Materialized View With No Data still loading data


My understanding of creating a materialized view WITH NO DATA was that no records would be loaded until I or a policy I've set refresh the view. However, when using timescaledb and providing this option, I can immediately query against the table and it seems like records are being loaded.

I'm following these docs: https://docs.timescale.com/timescaledb/latest/how-to-guides/continuous-aggregates/create-a-continuous-aggregate/#create-a-continuous-aggregate

By default, views are automatically refreshed. You can adjust this by setting the WITH NO DATA option.

CREATE MATERIALIZED VIEW timescaledb_view
WITH (timescaledb.continuous) AS
/* Query */
WITH NO DATA;

Yet when accessing timescaledb_view it seems completely refreshed, regardless of what query I've run. Am I misunderstanding how this is suppose to work?


Solution

  • I assume TimescaleDB 2.x as the question references latest docs.

    The default behavior for a continuous aggregate is to use real time aggregation feature, which complements materialized data with the result of the view query run on the original hypertable. So in the case of empty continuous aggregate (as in the question) a select to the view will query the original hypertable.

    The real time aggregation can be disabled by setting option timescaledb.materialized_only to true either during creation, e.g.,:

    CREATE MATERIALIZED VIEW timescaledb_view
    WITH (timescaledb.continuous, timescaledb.materialized_only=true) AS
    /* Query */
    WITH NO DATA;
    

    or by altering the materialized view:

    ALTER MATERIALIZED VIEW timescaledb_view SET (timescaledb.materialized_only = true);
    

    Then if no materialized was done either manually or by a created policy, a select to the continuous aggregate should return an empty result.