Block REFRESH MATERIALIZED VIEW on caggs

This commit is contained in:
Sven Klemm 2020-09-28 11:05:00 +02:00 committed by Sven Klemm
parent 98e1aa25ef
commit db0e210b8f
7 changed files with 47 additions and 41 deletions

View File

@ -3397,24 +3397,21 @@ process_refresh_mat_view_start(ProcessUtilityArgs *args)
RefreshMatViewStmt *stmt = castNode(RefreshMatViewStmt, args->parsetree);
Oid view_relid = RangeVarGetRelid(stmt->relation, NoLock, true);
const ContinuousAgg *cagg;
LOCAL_FCINFO(fcinfo, 3);
if (!OidIsValid(view_relid))
return DDL_CONTINUE;
cagg = ts_continuous_agg_find_by_relid(view_relid);
if (NULL == cagg)
return DDL_CONTINUE;
if (cagg)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("operation not supported on continuous aggregate"),
errdetail("A continuous aggregate does not support REFRESH MATERIALIZED VIEW."),
errhint("Use \"refresh_continuous_aggregate\" or set up a policy to refresh the "
"continuous aggregate.")));
PreventInTransactionBlock(args->context == PROCESS_UTILITY_TOPLEVEL, "REFRESH");
FC_SET_ARG(fcinfo, 0, ObjectIdGetDatum(view_relid));
FC_SET_NULL(fcinfo, 1);
FC_SET_NULL(fcinfo, 2);
ts_cm_functions->continuous_agg_refresh(fcinfo);
return DDL_DONE;
return DDL_CONTINUE;
}
/*

View File

@ -13,7 +13,7 @@ SELECT generate_series('2017-12-01 00:00'::timestamp, '2017-12-31 00:00'::timest
SELECT * FROM mat_before ORDER BY bucket, location;
REFRESH MATERIALIZED VIEW mat_before;
CALL refresh_continuous_aggregate('mat_before',NULL,NULL);
--the max of the temp for the POR should now be 165
SELECT * FROM mat_before ORDER BY bucket, location;

View File

@ -8,7 +8,7 @@
SELECT * FROM cagg.realtime_mat ORDER BY bucket, location;
REFRESH MATERIALIZED VIEW cagg.realtime_mat;
CALL refresh_continuous_aggregate('cagg.realtime_mat',NULL,NULL);
SELECT * FROM cagg.realtime_mat ORDER BY bucket, location;
@ -17,5 +17,5 @@ SELECT view_name, schedule_interval, materialized_only, materialization_hypertab
SELECT maxtemp FROM mat_ignoreinval ORDER BY 1;
SELECT count(*) FROM mat_inval;
REFRESH MATERIALIZED VIEW mat_inval;
CALL refresh_continuous_aggregate('mat_inval',NULL,NULL);
SELECT count(*) FROM mat_inval;

View File

@ -130,4 +130,11 @@ BEGIN
END IF;
END $$;
-- have to use psql conditional here because the procedure call can't be in transaction
SELECT extversion < '2.0.0' AS has_refresh_mat_view from pg_extension WHERE extname = 'timescaledb' \gset
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW mat_before;
\else
CALL refresh_continuous_aggregate('mat_before',NULL,NULL);
\endif

View File

@ -2,6 +2,8 @@
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
SELECT extversion < '2.0.0' AS has_refresh_mat_view from pg_extension WHERE extname = 'timescaledb' \gset
CREATE TYPE custom_type AS (high int, low int);
CREATE TABLE conditions_before (
@ -125,7 +127,11 @@ BEGIN
END IF;
END $$;
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW mat_before;
\else
CALL refresh_continuous_aggregate('mat_before',NULL,NULL);
\endif
-- we create separate schema for realtime agg since we dump all view definitions in public schema
-- but realtime agg view definition is not stable across versions
@ -229,7 +235,11 @@ BEGIN
END IF;
END $$;
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW cagg.realtime_mat;
\else
CALL refresh_continuous_aggregate('cagg.realtime_mat',NULL,NULL);
\endif
-- test ignore_invalidation_older_than migration --
DO LANGUAGE PLPGSQL $$
@ -262,7 +272,11 @@ BEGIN
END IF;
END $$;
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW mat_ignoreinval;
\else
CALL refresh_continuous_aggregate('mat_ignoreinval',NULL,NULL);
\endif
-- test new data beyond the invalidation threshold is properly handled --
CREATE TABLE inval_test (time TIMESTAMPTZ, location TEXT, temperature DOUBLE PRECISION);
@ -304,7 +318,11 @@ BEGIN
END IF;
END $$;
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW mat_inval;
\else
CALL refresh_continuous_aggregate('mat_inval',NULL,NULL);
\endif
INSERT INTO inval_test
SELECT generate_series('2118-12-01 00:00'::timestamp, '2118-12-20 00:00'::timestamp, '1 day'), 'POR', generate_series(135.25, 140.0, 0.25);

View File

@ -402,25 +402,12 @@ SELECT * FROM weekly_temp_with_data;
Sun May 03 17:00:00 2020 PDT | 3 | 19
(8 rows)
-- Test that REFRESH MATERIALIZED VIEW works as an alternative to
-- refresh_continuous_aggregate()
\set ON_ERROR_STOP 0
-- REFRESH MATERIALIZED VIEW is blocked on continuous aggregates
REFRESH MATERIALIZED VIEW weekly_temp_without_data;
SELECT * FROM weekly_temp_without_data;
day | device | avg_temp
------------------------------+--------+------------------
Sun Apr 26 17:00:00 2020 PDT | 3 | 21.5631067961165
Sun Apr 26 17:00:00 2020 PDT | 1 | 17.2474226804124
Sun May 03 17:00:00 2020 PDT | 0 | 16.7659574468085
Sun May 03 17:00:00 2020 PDT | 1 | 22.7272727272727
Sun Apr 26 17:00:00 2020 PDT | 0 | 17.8181818181818
Sun Apr 26 17:00:00 2020 PDT | 2 | 18.9803921568627
Sun May 03 17:00:00 2020 PDT | 2 | 15.811320754717
Sun May 03 17:00:00 2020 PDT | 3 | 19
(8 rows)
ERROR: operation not supported on continuous aggregate
-- These should fail since we do not allow refreshing inside a
-- transaction, not even as part of CREATE MATERIALIZED VIEW.
\set ON_ERROR_STOP 0
DO LANGUAGE PLPGSQL $$ BEGIN
CREATE MATERIALIZED VIEW weekly_conditions
WITH (timescaledb.continuous,

View File

@ -237,15 +237,12 @@ GROUP BY 1,2 WITH DATA;
SELECT * FROM weekly_temp_without_data;
SELECT * FROM weekly_temp_with_data;
-- Test that REFRESH MATERIALIZED VIEW works as an alternative to
-- refresh_continuous_aggregate()
\set ON_ERROR_STOP 0
-- REFRESH MATERIALIZED VIEW is blocked on continuous aggregates
REFRESH MATERIALIZED VIEW weekly_temp_without_data;
SELECT * FROM weekly_temp_without_data;
-- These should fail since we do not allow refreshing inside a
-- transaction, not even as part of CREATE MATERIALIZED VIEW.
\set ON_ERROR_STOP 0
DO LANGUAGE PLPGSQL $$ BEGIN
CREATE MATERIALIZED VIEW weekly_conditions
WITH (timescaledb.continuous,