Skip to content

Commit

Permalink
Deprecate continuous aggregates with old format
Browse files Browse the repository at this point in the history
This patch will report a warning when upgrading to new timescaledb
extension. Also restrict users from creating cagss with old format
on timescaledb with PG15.
  • Loading branch information
sb230132 committed Nov 10, 2022
1 parent e4ba2bc commit 19ae5ec
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 78 deletions.
17 changes: 17 additions & 0 deletions sql/updates/post-update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,20 @@ FROM _timescaledb_catalog.hypertable h
INNER JOIN _timescaledb_catalog.dimension d ON (d.hypertable_id = h.id)
WHERE d.interval_length IS NULL;
DROP FUNCTION _timescaledb_internal.update_dimension_partition;

-- Report warning when partial aggregates are used
DO $$
DECLARE
cagg_name text;
BEGIN
FOR cagg_name IN
SELECT
attrelid::regclass::text
FROM _timescaledb_catalog.continuous_agg cagg
INNER JOIN pg_attribute att ON (
att.attrelid = format('%I.%I',cagg.user_view_schema,cagg.user_view_name)::regclass)
WHERE cagg.finalized = false
LOOP
RAISE WARNING 'Continuous Aggregate: % with old format will not be supported with PG15. You should upgrade to the new format', cagg_name;
END LOOP;
END $$;
13 changes: 13 additions & 0 deletions tsl/src/continuous_aggs/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,19 @@ cagg_query_supported(Query *query, StringInfo hint, StringInfo detail, const boo
return false;
}

#if PG15_GE
if (!finalized)
{
/* continuous aggregates with old format will not be allowed */
appendStringInfoString(detail,
"Continuous aggregates with partials will not be supported.");
appendStringInfoString(hint,
"Define Continuous aggregates with \"finalized\" parameter set to "
"true.");
return false;
}
#endif

return true; /* Query was OK and is supported */
}

Expand Down
11 changes: 10 additions & 1 deletion tsl/test/expected/continuous_aggs-12.out
Original file line number Diff line number Diff line change
Expand Up @@ -2210,8 +2210,10 @@ AS
SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
FROM conditions
GROUP BY time_bucket('1day', timec) WITH NO DATA;
-- enable partial aggregates only for postgres version <= 14
SELECT CASE WHEN current_setting('server_version_num')::int/10000 <= 14 THEN 'false' ELSE 'true' END AS "final_flag" \gset
CREATE MATERIALIZED VIEW conditions_summary_old(timec, minl, sumt, sumh)
WITH (timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.finalized=false)
WITH (timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.finalized=:'final_flag')
AS
SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
FROM conditions
Expand Down Expand Up @@ -2280,3 +2282,10 @@ ORDER BY timec;
Thu Nov 01 17:00:00 2018 PDT | NYC | 35 | 15
(4 rows)

\set VERBOSITY default
-- negative test on PG15
CREATE MATERIALIZED VIEW mat_m1 WITH (timescaledb.continuous, timescaledb.finalized = false)
as
select a, count(b)
from foo
group by time_bucket(1, a), a WITH NO DATA;
11 changes: 10 additions & 1 deletion tsl/test/expected/continuous_aggs-13.out
Original file line number Diff line number Diff line change
Expand Up @@ -2210,8 +2210,10 @@ AS
SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
FROM conditions
GROUP BY time_bucket('1day', timec) WITH NO DATA;
-- enable partial aggregates only for postgres version <= 14
SELECT CASE WHEN current_setting('server_version_num')::int/10000 <= 14 THEN 'false' ELSE 'true' END AS "final_flag" \gset
CREATE MATERIALIZED VIEW conditions_summary_old(timec, minl, sumt, sumh)
WITH (timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.finalized=false)
WITH (timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.finalized=:'final_flag')
AS
SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
FROM conditions
Expand Down Expand Up @@ -2280,3 +2282,10 @@ ORDER BY timec;
Thu Nov 01 17:00:00 2018 PDT | NYC | 35 | 15
(4 rows)

\set VERBOSITY default
-- negative test on PG15
CREATE MATERIALIZED VIEW mat_m1 WITH (timescaledb.continuous, timescaledb.finalized = false)
as
select a, count(b)
from foo
group by time_bucket(1, a), a WITH NO DATA;
11 changes: 10 additions & 1 deletion tsl/test/expected/continuous_aggs-14.out
Original file line number Diff line number Diff line change
Expand Up @@ -2210,8 +2210,10 @@ AS
SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
FROM conditions
GROUP BY time_bucket('1day', timec) WITH NO DATA;
-- enable partial aggregates only for postgres version <= 14
SELECT CASE WHEN current_setting('server_version_num')::int/10000 <= 14 THEN 'false' ELSE 'true' END AS "final_flag" \gset
CREATE MATERIALIZED VIEW conditions_summary_old(timec, minl, sumt, sumh)
WITH (timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.finalized=false)
WITH (timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.finalized=:'final_flag')
AS
SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
FROM conditions
Expand Down Expand Up @@ -2280,3 +2282,10 @@ ORDER BY timec;
Thu Nov 01 17:00:00 2018 PDT | NYC | 35 | 15
(4 rows)

\set VERBOSITY default
-- negative test on PG15
CREATE MATERIALIZED VIEW mat_m1 WITH (timescaledb.continuous, timescaledb.finalized = false)
as
select a, count(b)
from foo
group by time_bucket(1, a), a WITH NO DATA;
16 changes: 14 additions & 2 deletions tsl/test/expected/continuous_aggs-15.out
Original file line number Diff line number Diff line change
Expand Up @@ -2212,8 +2212,10 @@ AS
SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
FROM conditions
GROUP BY time_bucket('1day', timec) WITH NO DATA;
-- enable partial aggregates only for postgres version <= 14
SELECT CASE WHEN current_setting('server_version_num')::int/10000 <= 14 THEN 'false' ELSE 'true' END AS "final_flag" \gset
CREATE MATERIALIZED VIEW conditions_summary_old(timec, minl, sumt, sumh)
WITH (timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.finalized=false)
WITH (timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.finalized=:'final_flag')
AS
SELECT time_bucket('1day', timec), min(location), sum(temperature), sum(humidity)
FROM conditions
Expand Down Expand Up @@ -2255,7 +2257,7 @@ view_definition | SELECT time_bucket('@ 1 day'::interval, con
| sum(conditions.humidity) AS sumh +
| FROM conditions +
| GROUP BY (time_bucket('@ 1 day'::interval, conditions.timec));
finalized | f
finalized | t

\x OFF
CALL refresh_continuous_aggregate('conditions_summary_new', NULL, NULL);
Expand All @@ -2282,3 +2284,13 @@ ORDER BY timec;
Thu Nov 01 17:00:00 2018 PDT | NYC | 35 | 15
(4 rows)

\set VERBOSITY default
-- negative test on PG15
CREATE MATERIALIZED VIEW mat_m1 WITH (timescaledb.continuous, timescaledb.finalized = false)
as
select a, count(b)
from foo
group by time_bucket(1, a), a WITH NO DATA;
ERROR: invalid continuous aggregate query
DETAIL: Continuous aggregates with partials will not be supported.
HINT: Define Continuous aggregates with "finalized" parameter set to true.

0 comments on commit 19ae5ec

Please sign in to comment.