Skip to content

Commit

Permalink
Fix policy refresh window too small
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fabriziomello committed May 6, 2024
1 parent bc644f7 commit d8057c4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
4 changes: 2 additions & 2 deletions tsl/src/bgw_policy/continuous_aggregate_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ validate_window_size(const ContinuousAgg *cagg, const CaggPolicyConfig *config)
* 2. Buckets with timezones
* 3. Cases 1 and 2 at the same time
*
* For months we simply take 31 days as the worst case scenario and
* For months we simply take 30 days like on interval_to_int64 and
* multiply this number by the number of months in the bucket. This
* reduces the task to days/hours/minutes scenario.
*
Expand All @@ -459,7 +459,7 @@ validate_window_size(const ContinuousAgg *cagg, const CaggPolicyConfig *config)

/* Make a temporary copy of bucket_width */
Interval interval = *cagg->bucket_function->bucket_time_width;
interval.day += 31 * interval.month;
interval.day += 30 * interval.month;
interval.month = 0;
bucket_width = ts_interval_value_to_internal(IntervalPGetDatum(&interval), INTERVALOID);
}
Expand Down
29 changes: 28 additions & 1 deletion tsl/test/expected/exp_cagg_monthly.out
Original file line number Diff line number Diff line change
Expand Up @@ -1166,9 +1166,36 @@ ERROR: policy refresh window too small
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
start_offset => INTERVAL '65 days',
end_offset => INTERVAL '1 day',
schedule_interval => INTERVAL '1 hour') AS job_id \gset
SELECT delete_job(:job_id);
delete_job
------------

(1 row)

SELECT add_continuous_aggregate_policy('conditions_summary_policy',
start_offset => INTERVAL '2 months',
end_offset => INTERVAL '0 months',
schedule_interval => INTERVAL '1 hour') AS job_id \gset
SELECT delete_job(:job_id);
delete_job
------------

(1 row)

\set ON_ERROR_STOP 0
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
start_offset => INTERVAL '2 months',
end_offset => INTERVAL '1 months',
schedule_interval => INTERVAL '1 hour');
ERROR: policy refresh window too small
\set ON_ERROR_STOP 1
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
start_offset => INTERVAL '3 months',
end_offset => INTERVAL '1 months',
schedule_interval => INTERVAL '1 hour');
add_continuous_aggregate_policy
---------------------------------
1000
1002
(1 row)

21 changes: 21 additions & 0 deletions tsl/test/sql/exp_cagg_monthly.sql
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,25 @@ SELECT add_continuous_aggregate_policy('conditions_summary_policy',
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
start_offset => INTERVAL '65 days',
end_offset => INTERVAL '1 day',
schedule_interval => INTERVAL '1 hour') AS job_id \gset

SELECT delete_job(:job_id);

SELECT add_continuous_aggregate_policy('conditions_summary_policy',
start_offset => INTERVAL '2 months',
end_offset => INTERVAL '0 months',
schedule_interval => INTERVAL '1 hour') AS job_id \gset

SELECT delete_job(:job_id);

\set ON_ERROR_STOP 0
SELECT add_continuous_aggregate_policy('conditions_summary_policy',
start_offset => INTERVAL '2 months',
end_offset => INTERVAL '1 months',
schedule_interval => INTERVAL '1 hour');
\set ON_ERROR_STOP 1

SELECT add_continuous_aggregate_policy('conditions_summary_policy',
start_offset => INTERVAL '3 months',
end_offset => INTERVAL '1 months',
schedule_interval => INTERVAL '1 hour');

0 comments on commit d8057c4

Please sign in to comment.