Skip to content

Commit 15cf640

Browse files
committed
Bucket options for refresh_continuous_aggregate()
1 parent 2bae041 commit 15cf640

7 files changed

Lines changed: 1166 additions & 18 deletions

File tree

tsl/src/bgw_policy/job.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,6 @@ policy_refresh_cagg_execute(int32 job_id, Jsonb *config)
453453
continuous_agg_refresh_internal(policy_data.cagg,
454454
&policy_data.refresh_window,
455455
context,
456-
policy_data.refresh_window.start_isnull,
457-
policy_data.refresh_window.end_isnull,
458456
false, /* force */
459457
extend_last_bucket);
460458

tsl/src/continuous_aggs/create.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,14 +891,14 @@ tsl_process_continuous_agg_viewstmt(Node *node, const char *query_string, void *
891891
* - ts_compute_circumscribed_bucketed_refresh_window_variable()
892892
*/
893893
refresh_window.start = cagg_get_time_min(cagg);
894+
refresh_window.start_isnull = true;
894895
refresh_window.end = ts_time_get_noend_or_max(refresh_window.type);
896+
refresh_window.end_isnull = true;
895897

896898
ContinuousAggRefreshContext context = { .callctx = CAGG_REFRESH_CREATION };
897899
continuous_agg_refresh_internal(cagg,
898900
&refresh_window,
899901
context,
900-
true, /* start_isnull */
901-
true, /* end_isnull */
902902
false, /* force */
903903
false /* extend_last_bucket */);
904904
}

tsl/src/continuous_aggs/refresh.c

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <utils/lsyscache.h>
1919
#include <utils/snapmgr.h>
2020

21+
#include "bgw_policy/policies_v2.h"
2122
#include "debug_point.h"
2223
#include "dimension.h"
2324
#include "dimension_slice.h"
@@ -666,16 +667,60 @@ continuous_agg_refresh(PG_FUNCTION_ARGS)
666667
refresh_window.end_isnull = true;
667668
}
668669

670+
/*
671+
* For manual refreshes batching is opt-in: callers must set
672+
* buckets_per_batch in the options JSONB to trigger incremental refresh.
673+
* Default 0 = atomic single-pass refresh (keeps the previous behaviour).
674+
*/
675+
int32 buckets_per_batch = 0;
676+
int32 max_batches_per_execution = 0;
677+
bool refresh_newest_first = DEFAULT_REFRESH_NEWEST_FIRST;
678+
679+
if (!PG_ARGISNULL(4))
680+
{
681+
Jsonb *options = PG_GETARG_JSONB_P(4);
682+
bool found;
683+
684+
int32 v = ts_jsonb_get_int32_field(options, POL_REFRESH_CONF_KEY_BUCKETS_PER_BATCH, &found);
685+
if (found)
686+
buckets_per_batch = v;
687+
688+
v = ts_jsonb_get_int32_field(options,
689+
POL_REFRESH_CONF_KEY_MAX_BATCHES_PER_EXECUTION,
690+
&found);
691+
if (found)
692+
max_batches_per_execution = v;
693+
694+
bool b =
695+
ts_jsonb_get_bool_field(options, POL_REFRESH_CONF_KEY_REFRESH_NEWEST_FIRST, &found);
696+
if (found)
697+
refresh_newest_first = b;
698+
}
699+
700+
if (buckets_per_batch < 0)
701+
ereport(ERROR,
702+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
703+
errmsg("invalid buckets per batch"),
704+
errdetail("buckets_per_batch: %d", buckets_per_batch),
705+
errhint("The buckets per batch should be greater than or equal to zero.")));
706+
707+
if (max_batches_per_execution < 0)
708+
ereport(ERROR,
709+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
710+
errmsg("invalid max batches per execution"),
711+
errdetail("max_batches_per_execution: %d", max_batches_per_execution),
712+
errhint(
713+
"The max batches per execution should be greater than or equal to zero.")));
714+
669715
ContinuousAggRefreshContext context = {
670716
.callctx = CAGG_REFRESH_WINDOW,
671-
.buckets_per_batch = DEFAULT_BUCKETS_PER_BATCH,
672-
.refresh_newest_first = DEFAULT_REFRESH_NEWEST_FIRST,
717+
.buckets_per_batch = buckets_per_batch,
718+
.max_batches_per_execution = max_batches_per_execution,
719+
.refresh_newest_first = refresh_newest_first,
673720
};
674721
continuous_agg_refresh_internal(cagg,
675722
&refresh_window,
676723
context,
677-
PG_ARGISNULL(1),
678-
PG_ARGISNULL(2),
679724
force,
680725
false /*extend_last_bucket*/);
681726

@@ -1082,8 +1127,8 @@ continuous_agg_refresh_single_window(const ContinuousAgg *cagg_arg,
10821127
void
10831128
continuous_agg_refresh_internal(const ContinuousAgg *cagg,
10841129
const InternalTimeRange *refresh_window,
1085-
ContinuousAggRefreshContext context, const bool start_isnull,
1086-
const bool end_isnull, bool force, bool extend_last_bucket)
1130+
ContinuousAggRefreshContext context, bool force,
1131+
bool extend_last_bucket)
10871132
{
10881133
/* Like regular materialized views, require owner to refresh. */
10891134
if (!object_ownercheck(RelationRelationId, cagg->relid, GetUserId()))
@@ -1140,11 +1185,12 @@ continuous_agg_refresh_internal(const ContinuousAgg *cagg,
11401185
(context.refresh_newest_first ? processing_batch == 1 :
11411186
processing_batch == context.number_of_batches);
11421187

1143-
elog(DEBUG1,
1144-
"refreshing continuous aggregate \"%s\" in window [ %s, %s ]",
1145-
NameStr(cagg->data.user_view_name),
1146-
ts_internal_to_time_string(batch_window->start, batch_window->type),
1147-
ts_internal_to_time_string(batch_window->end, batch_window->type));
1188+
if (context.number_of_batches > 1)
1189+
elog(DEBUG1,
1190+
"refreshing continuous aggregate \"%s\" in window [ %s, %s ]",
1191+
NameStr(cagg->data.user_view_name),
1192+
ts_internal_to_time_string(batch_window->start, batch_window->type),
1193+
ts_internal_to_time_string(batch_window->end, batch_window->type));
11481194

11491195
bool refreshed = continuous_agg_refresh_single_window(cagg,
11501196
batch_window,

tsl/src/continuous_aggs/refresh.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ Hypertable *cagg_get_hypertable_or_fail(int32 hypertable_id);
2121
extern Datum continuous_agg_refresh(PG_FUNCTION_ARGS);
2222
extern void continuous_agg_refresh_internal(const ContinuousAgg *cagg,
2323
const InternalTimeRange *refresh_window,
24-
ContinuousAggRefreshContext context,
25-
const bool start_isnull, const bool end_isnull,
26-
bool force, bool extend_last_bucket);
24+
ContinuousAggRefreshContext context, bool force,
25+
bool extend_last_bucket);
2726
extern List *continuous_agg_split_refresh_window(const ContinuousAgg *cagg,
2827
const InternalTimeRange *original_refresh_window,
2928
int32 buckets_per_batch,

0 commit comments

Comments
 (0)