Skip to content

Commit 16d8b98

Browse files
committed
Fix timestamp out of range in CAgg refresh policy
When setting a refresh policy with `end_offset=>NULL` for a CAgg with variable sized bucket it was erroring out when there was o data to be refreshed. This was happening cause we're using the wrong util function to get the maximum value for the given type. Fixed it using the proper function to handle both the fixed and variable bucket size.
1 parent bbbebbf commit 16d8b98

File tree

5 files changed

+871
-1
lines changed

5 files changed

+871
-1
lines changed

.unreleased/pr_8559

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes: #8559 Fix `timestamp out of range` using `end_offset=NULL` on CAgg refresh policy
2+
Thanks: @nofalx for reporting the error when using `end_offset=NULL` on CAgg refresh policy

tsl/src/bgw_policy/continuous_aggregate_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ policy_refresh_cagg_get_refresh_end(const Dimension *dim, const Jsonb *config, b
137137
int64 res = get_time_from_config(dim, config, POL_REFRESH_CONF_KEY_END_OFFSET, end_isnull);
138138

139139
if (*end_isnull)
140-
return ts_time_get_end_or_max(ts_dimension_get_partition_type(dim));
140+
return ts_time_get_noend_or_max(ts_dimension_get_partition_type(dim));
141141
return res;
142142
}
143143

tsl/test/expected/cagg_policy.out

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,3 +1400,72 @@ SELECT timescaledb_experimental.add_policies('cagg');
14001400
f
14011401
(1 row)
14021402

1403+
-- Issue #6902
1404+
-- Fix timestamp out of range in a refresh policy when setting `end_offset=>NULL`
1405+
-- for a CAgg with variable sized bucket (i.e: using `time_bucket` with timezone)
1406+
CREATE TABLE issue_6902 (
1407+
ts TIMESTAMPTZ NOT NULL,
1408+
temperature NUMERIC
1409+
) WITH (
1410+
timescaledb.hypertable,
1411+
timescaledb.partition_column='ts',
1412+
timescaledb.chunk_interval='1 day',
1413+
timescaledb.compress='off'
1414+
);
1415+
INSERT INTO issue_6902
1416+
SELECT t, 1 FROM generate_series(now() - interval '3 hours', now(), interval '1 minute') AS t;
1417+
CREATE MATERIALIZED VIEW issue_6902_by_hour
1418+
WITH (timescaledb.continuous) AS
1419+
SELECT
1420+
time_bucket(INTERVAL '1 hour', ts, 'America/Sao_Paulo') AS bucket, -- using timezone
1421+
MAX(temperature),
1422+
MIN(temperature),
1423+
COUNT(*)
1424+
FROM issue_6902
1425+
GROUP BY 1
1426+
WITH NO DATA;
1427+
SELECT add_continuous_aggregate_policy (
1428+
'issue_6902_by_hour',
1429+
start_offset => INTERVAL '3 hours',
1430+
end_offset => NULL,
1431+
schedule_interval => INTERVAL '12 hour',
1432+
initial_start => now() + INTERVAL '12 hour'
1433+
) AS job_id \gset
1434+
-- 181 rows
1435+
CALL run_job(:job_id);
1436+
SELECT count(*) FROM issue_6902;
1437+
count
1438+
-------
1439+
181
1440+
(1 row)
1441+
1442+
-- run again without any change, remain 181 rows
1443+
CALL run_job(:job_id);
1444+
SELECT count(*) FROM issue_6902;
1445+
count
1446+
-------
1447+
181
1448+
(1 row)
1449+
1450+
-- change existing data
1451+
UPDATE issue_6902
1452+
SET temperature = temperature + 1;
1453+
-- run again without any change, remain 181 rows
1454+
CALL run_job(:job_id);
1455+
SELECT count(*) FROM issue_6902;
1456+
count
1457+
-------
1458+
181
1459+
(1 row)
1460+
1461+
-- insert more data
1462+
INSERT INTO issue_6902
1463+
SELECT t, 1 FROM generate_series(now() - interval '3 hours', now(), interval '1 minute') AS t;
1464+
-- run again without and should have 362 rows
1465+
CALL run_job(:job_id);
1466+
SELECT count(*) FROM issue_6902;
1467+
count
1468+
-------
1469+
362
1470+
(1 row)
1471+

tsl/test/sql/cagg_policy.sql

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,62 @@ AS SELECT time_bucket(1, a), sum(b)
676676
FROM t GROUP BY time_bucket(1, a);
677677

678678
SELECT timescaledb_experimental.add_policies('cagg');
679+
680+
-- Issue #6902
681+
-- Fix timestamp out of range in a refresh policy when setting `end_offset=>NULL`
682+
-- for a CAgg with variable sized bucket (i.e: using `time_bucket` with timezone)
683+
CREATE TABLE issue_6902 (
684+
ts TIMESTAMPTZ NOT NULL,
685+
temperature NUMERIC
686+
) WITH (
687+
timescaledb.hypertable,
688+
timescaledb.partition_column='ts',
689+
timescaledb.chunk_interval='1 day',
690+
timescaledb.compress='off'
691+
);
692+
693+
INSERT INTO issue_6902
694+
SELECT t, 1 FROM generate_series(now() - interval '3 hours', now(), interval '1 minute') AS t;
695+
696+
CREATE MATERIALIZED VIEW issue_6902_by_hour
697+
WITH (timescaledb.continuous) AS
698+
SELECT
699+
time_bucket(INTERVAL '1 hour', ts, 'America/Sao_Paulo') AS bucket, -- using timezone
700+
MAX(temperature),
701+
MIN(temperature),
702+
COUNT(*)
703+
FROM issue_6902
704+
GROUP BY 1
705+
WITH NO DATA;
706+
707+
SELECT add_continuous_aggregate_policy (
708+
'issue_6902_by_hour',
709+
start_offset => INTERVAL '3 hours',
710+
end_offset => NULL,
711+
schedule_interval => INTERVAL '12 hour',
712+
initial_start => now() + INTERVAL '12 hour'
713+
) AS job_id \gset
714+
715+
-- 181 rows
716+
CALL run_job(:job_id);
717+
SELECT count(*) FROM issue_6902;
718+
719+
-- run again without any change, remain 181 rows
720+
CALL run_job(:job_id);
721+
SELECT count(*) FROM issue_6902;
722+
723+
-- change existing data
724+
UPDATE issue_6902
725+
SET temperature = temperature + 1;
726+
727+
-- run again without any change, remain 181 rows
728+
CALL run_job(:job_id);
729+
SELECT count(*) FROM issue_6902;
730+
731+
-- insert more data
732+
INSERT INTO issue_6902
733+
SELECT t, 1 FROM generate_series(now() - interval '3 hours', now(), interval '1 minute') AS t;
734+
735+
-- run again without and should have 362 rows
736+
CALL run_job(:job_id);
737+
SELECT count(*) FROM issue_6902;

0 commit comments

Comments
 (0)