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 4e6b504
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 12 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.
22 changes: 16 additions & 6 deletions tsl/test/sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ set(TEST_FILES
bgw_custom.sql
bgw_policy.sql
cagg_errors.sql
cagg_errors_deprecated.sql
cagg_invalidation.sql
cagg_permissions.sql
cagg_policy.sql
Expand Down Expand Up @@ -54,12 +53,7 @@ if(CMAKE_BUILD_TYPE MATCHES Debug)
cagg_ddl_dist_ht.sql
cagg_drop_chunks.sql
cagg_dump.sql
cagg_migrate_integer.sql
cagg_migrate_integer_dist_ht.sql
cagg_migrate_timestamp.sql
cagg_migrate_timestamp_dist_ht.sql
cagg_multi.sql
continuous_aggs_deprecated.sql
cagg_tableam.sql
cagg_usage.sql
cagg_policy_run.sql
Expand Down Expand Up @@ -103,6 +97,22 @@ if((${PG_VERSION_MAJOR} GREATER_EQUAL "14"))
endif()
endif()

# Run cagg migration tests only on PG version < 15
if((${PG_VERSION_MAJOR} LESS "15"))
if(CMAKE_BUILD_TYPE MATCHES Debug)
list(
APPEND
TEST_FILES
cagg_migrate_integer.sql
cagg_migrate_integer_dist_ht.sql
cagg_migrate_timestamp.sql
cagg_migrate_timestamp_dist_ht.sql
continuous_aggs_deprecated.sql)
else()
list(APPEND TEST_FILES cagg_errors_deprecated.sql)
endif()
endif()

set(SOLO_TESTS
bgw_db_scheduler
troubleshooting_job_errors
Expand Down
13 changes: 12 additions & 1 deletion tsl/test/sql/continuous_aggs.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -1490,8 +1490,11 @@ 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 All @@ -1516,3 +1519,11 @@ SELECT *
FROM conditions_summary_new
NATURAL JOIN conditions_summary_old
ORDER BY timec;

\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;

0 comments on commit 4e6b504

Please sign in to comment.