Skip to content

Commit 2bae041

Browse files
committed
Implement incremental refresh for manual refreshes
1 parent f45faec commit 2bae041

6 files changed

Lines changed: 207 additions & 100 deletions

File tree

tsl/src/bgw_policy/continuous_aggregate_api.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,14 @@
2727
#include "policy_config.h"
2828
#include "policy_utils.h"
2929
#include "time_utils.h"
30+
#include "continuous_aggs/refresh.h"
3031
#include "ts_catalog/continuous_agg.h"
3132

3233
/* Default max runtime for a continuous aggregate jobs is unlimited for now */
3334
#define DEFAULT_MAX_RUNTIME \
3435
DatumGetIntervalP(DirectFunctionCall3(interval_in, CStringGetDatum("0"), InvalidOid, -1))
35-
/* Default buckets per batch is 1, which means that the job will refresh 1 bucket at a time */
36-
#define DEFAULT_BUCKETS_PER_BATCH 10
3736
/* Default max batches per execution is 0, which means no limit */
3837
#define DEFAULT_MAX_BATCHES_PER_EXECUTION 0
39-
/* Default refresh newest first is true, which means from newest data to the oldest */
40-
#define DEFAULT_REFRESH_NEWEST_FIRST true
4138
/* Default compress after refresh is false, which means compression does not run after refresh */
4239
#define DEFAULT_COMPRESS_AFTER_REFRESH false
4340

tsl/src/bgw_policy/job.c

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -442,67 +442,21 @@ policy_refresh_cagg_execute(int32 job_id, Jsonb *config)
442442
PGC_S_SESSION);
443443
}
444444

445-
ContinuousAggRefreshContext context = { .callctx = CAGG_REFRESH_POLICY, .job_id = job_id };
446-
447-
/* Try to split window range into a list of ranges */
448-
List *refresh_window_list =
449-
continuous_agg_split_refresh_window(policy_data.cagg,
450-
&policy_data.refresh_window,
451-
policy_data.buckets_per_batch,
452-
policy_data.refresh_newest_first);
453-
if (refresh_window_list == NIL)
454-
{
455-
refresh_window_list = lappend(refresh_window_list, &policy_data.refresh_window);
456-
}
457-
else
458-
{
459-
context.callctx = CAGG_REFRESH_POLICY_BATCHED;
460-
}
461-
462-
context.number_of_batches = list_length(refresh_window_list);
463-
464-
ListCell *lc;
465-
int32 processing_batch = 0;
466-
foreach (lc, refresh_window_list)
467-
{
468-
InternalTimeRange *refresh_window = (InternalTimeRange *) lfirst(lc);
469-
elog(DEBUG1,
470-
"refreshing continuous aggregate \"%s\" from %s to %s",
471-
NameStr(policy_data.cagg->data.user_view_name),
472-
ts_internal_to_time_string(refresh_window->start, refresh_window->type),
473-
ts_internal_to_time_string(refresh_window->end, refresh_window->type));
474-
475-
context.processing_batch = ++processing_batch;
476-
477-
/* extend_last_bucket must only apply to the boundary batch — the one
478-
* whose window abuts the adjacent policy. For newest-first ordering
479-
* that is batch 1; for oldest-first it is the final batch.
480-
* In non-batched mode (single batch) the one batch is always the boundary. */
481-
bool apply_extend =
482-
extend_last_bucket &&
483-
(policy_data.refresh_newest_first ? processing_batch == 1 :
484-
processing_batch == context.number_of_batches);
485-
486-
continuous_agg_refresh_internal(policy_data.cagg,
487-
refresh_window,
488-
context,
489-
refresh_window->start_isnull,
490-
refresh_window->end_isnull,
491-
(context.callctx != CAGG_REFRESH_POLICY_BATCHED),
492-
false, /* force */
493-
apply_extend);
494-
DEBUG_ERROR_INJECTION(psprintf("cagg_policy_batch_%d_after_refresh", processing_batch));
495-
if (processing_batch >= policy_data.max_batches_per_execution &&
496-
processing_batch < context.number_of_batches &&
497-
policy_data.max_batches_per_execution > 0)
498-
{
499-
elog(LOG,
500-
"reached maximum number of batches per execution (%d), batches not processed (%d)",
501-
policy_data.max_batches_per_execution,
502-
context.number_of_batches - processing_batch);
503-
break;
504-
}
505-
}
445+
ContinuousAggRefreshContext context = {
446+
.callctx = CAGG_REFRESH_POLICY,
447+
.job_id = job_id,
448+
.buckets_per_batch = policy_data.buckets_per_batch,
449+
.max_batches_per_execution = policy_data.max_batches_per_execution,
450+
.refresh_newest_first = policy_data.refresh_newest_first,
451+
};
452+
453+
continuous_agg_refresh_internal(policy_data.cagg,
454+
&policy_data.refresh_window,
455+
context,
456+
policy_data.refresh_window.start_isnull,
457+
policy_data.refresh_window.end_isnull,
458+
false, /* force */
459+
extend_last_bucket);
506460

507461
if (!policy_data.include_tiered_data_isnull)
508462
{
@@ -553,6 +507,7 @@ policy_refresh_cagg_execute(int32 job_id, Jsonb *config)
553507
}
554508

555509
/* Process each chunk in its own transaction */
510+
ListCell *lc;
556511
foreach (lc, chunkid_lst)
557512
{
558513
PushActiveSnapshot(GetTransactionSnapshot());

tsl/src/continuous_aggs/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ typedef struct ContinuousAggRefreshContext
8989
int32 job_id;
9090
int32 processing_batch;
9191
int32 number_of_batches;
92+
/* Batch configuration */
93+
int32 buckets_per_batch; /* 0 = disabled */
94+
int32 max_batches_per_execution; /* 0 = no limit */
95+
bool refresh_newest_first;
9296
} ContinuousAggRefreshContext;
9397

9498
#define IS_TIME_BUCKET_INFO_TIME_BASED(bucket_function) \

tsl/src/continuous_aggs/create.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,8 @@ tsl_process_continuous_agg_viewstmt(Node *node, const char *query_string, void *
899899
context,
900900
true, /* start_isnull */
901901
true, /* end_isnull */
902-
true, /* bucketing_refresh_window */
903902
false, /* force */
904-
false /*extend_last_bucket*/);
903+
false /* extend_last_bucket */);
905904
}
906905

907906
return DDL_DONE;

0 commit comments

Comments
 (0)