Skip to content

Commit d8057c4

Browse files
committed
Fix policy refresh window too small
When adding a CAgg refresh policy using `start_offset => '2 months'` and `end_offset => '0 months'` was failing because the policy refresh window is too small and it should cover at least two buckets in the valid time range of timestamptz. The problem was when calculating the bucket width for the variable bucket size (like months) we're assuming 31 days for each month but when converting the end offset to integer using `interval_to_int64` we use 30 days per month. Fixed it by aligning the variable bucket size calculation to also use 30 days.
1 parent bc644f7 commit d8057c4

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

tsl/src/bgw_policy/continuous_aggregate_api.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ validate_window_size(const ContinuousAgg *cagg, const CaggPolicyConfig *config)
443443
* 2. Buckets with timezones
444444
* 3. Cases 1 and 2 at the same time
445445
*
446-
* For months we simply take 31 days as the worst case scenario and
446+
* For months we simply take 30 days like on interval_to_int64 and
447447
* multiply this number by the number of months in the bucket. This
448448
* reduces the task to days/hours/minutes scenario.
449449
*
@@ -459,7 +459,7 @@ validate_window_size(const ContinuousAgg *cagg, const CaggPolicyConfig *config)
459459

460460
/* Make a temporary copy of bucket_width */
461461
Interval interval = *cagg->bucket_function->bucket_time_width;
462-
interval.day += 31 * interval.month;
462+
interval.day += 30 * interval.month;
463463
interval.month = 0;
464464
bucket_width = ts_interval_value_to_internal(IntervalPGetDatum(&interval), INTERVALOID);
465465
}

tsl/test/expected/exp_cagg_monthly.out

+28-1
Original file line numberDiff line numberDiff line change
@@ -1166,9 +1166,36 @@ ERROR: policy refresh window too small
11661166
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
11671167
start_offset => INTERVAL '65 days',
11681168
end_offset => INTERVAL '1 day',
1169+
schedule_interval => INTERVAL '1 hour') AS job_id \gset
1170+
SELECT delete_job(:job_id);
1171+
delete_job
1172+
------------
1173+
1174+
(1 row)
1175+
1176+
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
1177+
start_offset => INTERVAL '2 months',
1178+
end_offset => INTERVAL '0 months',
1179+
schedule_interval => INTERVAL '1 hour') AS job_id \gset
1180+
SELECT delete_job(:job_id);
1181+
delete_job
1182+
------------
1183+
1184+
(1 row)
1185+
1186+
\set ON_ERROR_STOP 0
1187+
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
1188+
start_offset => INTERVAL '2 months',
1189+
end_offset => INTERVAL '1 months',
1190+
schedule_interval => INTERVAL '1 hour');
1191+
ERROR: policy refresh window too small
1192+
\set ON_ERROR_STOP 1
1193+
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
1194+
start_offset => INTERVAL '3 months',
1195+
end_offset => INTERVAL '1 months',
11691196
schedule_interval => INTERVAL '1 hour');
11701197
add_continuous_aggregate_policy
11711198
---------------------------------
1172-
1000
1199+
1002
11731200
(1 row)
11741201

tsl/test/sql/exp_cagg_monthly.sql

+21
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,25 @@ SELECT add_continuous_aggregate_policy('conditions_summary_policy',
544544
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
545545
start_offset => INTERVAL '65 days',
546546
end_offset => INTERVAL '1 day',
547+
schedule_interval => INTERVAL '1 hour') AS job_id \gset
548+
549+
SELECT delete_job(:job_id);
550+
551+
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
552+
start_offset => INTERVAL '2 months',
553+
end_offset => INTERVAL '0 months',
554+
schedule_interval => INTERVAL '1 hour') AS job_id \gset
555+
556+
SELECT delete_job(:job_id);
557+
558+
\set ON_ERROR_STOP 0
559+
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
560+
start_offset => INTERVAL '2 months',
561+
end_offset => INTERVAL '1 months',
562+
schedule_interval => INTERVAL '1 hour');
563+
\set ON_ERROR_STOP 1
564+
565+
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
566+
start_offset => INTERVAL '3 months',
567+
end_offset => INTERVAL '1 months',
547568
schedule_interval => INTERVAL '1 hour');

0 commit comments

Comments
 (0)