Skip to content

Commit 2774bee

Browse files
committed
Prune real-time branch of deep hierarchical caggs
A single application of ts_transform_time_bucket_comparison() strips only the outermost time_bucket() from a comparison. In a hierarchical continuous aggregate the bucket input is itself another time_bucket() call. The real-time branch could not be proven empty and the refresh re-scanned the whole raw un-materialized tail of the base hypertable. Add ts_transform_nested_time_bucket_comparison(), which applies the single-level transform repeatedly until the bound is expressed on the raw column. Only the original qual and the fully reduced qual are kept; the intermediate levels have no consumer. Fixes: #10071
1 parent 0eb017d commit 2774bee

6 files changed

Lines changed: 53 additions & 85 deletions

File tree

.unreleased/pr_10175

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes: #10071 Prune the real-time branch of hierarchical continuous aggregates at any nesting depth
2+
Thanks: @viniciusrsouza for reporting the issue

src/nodes/chunk_append/exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ ts_constify_restrictinfos(PlannerInfo *root, List *restrictinfos)
10011001
* transformations again. This might allow us to exclude chunks
10021002
* based on a parameterized time_bucket expression.
10031003
*/
1004-
Expr *additional_clause = ts_transform_time_bucket_comparison(constified);
1004+
Expr *additional_clause = ts_transform_nested_time_bucket_comparison(constified);
10051005
if (additional_clause != NULL)
10061006
{
10071007
/*

src/planner/expand_hypertable.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,32 @@ ts_transform_time_bucket_comparison(Expr *node)
731731
return &op->xpr;
732732
}
733733

734+
/*
735+
* Fully unwrap a (possibly nested) time_bucket() comparison by applying the
736+
* single-level transform repeatedly until the bound reaches the raw column.
737+
*
738+
* ts_transform_time_bucket_comparison() only strips the outermost time_bucket().
739+
* In hierarchical continuous aggregates the time_bucket() input column can be
740+
* itself another time_bucket() call.
741+
*
742+
* Used both during hypertable expansion and from the chunk-append executor's
743+
* runtime constification.
744+
*/
745+
Expr *
746+
ts_transform_nested_time_bucket_comparison(Expr *qual)
747+
{
748+
Expr *nested = ts_transform_time_bucket_comparison(qual);
749+
Expr *transformed = NULL;
750+
751+
while (nested != NULL)
752+
{
753+
transformed = nested;
754+
nested = ts_transform_time_bucket_comparison(transformed);
755+
}
756+
757+
return transformed;
758+
}
759+
734760
/*
735761
* Since baserestrictinfo is not yet set by the planner, we have to derive
736762
* it ourselves. It's safe for us to miss some restrict info clauses (this
@@ -784,7 +810,7 @@ process_quals(Node *quals, CollectQualCtx *ctx, bool is_outer_join)
784810
* check for time_bucket comparisons
785811
* time_bucket(Const, time_colum) > Const
786812
*/
787-
Expr *transformed = ts_transform_time_bucket_comparison(qual);
813+
Expr *transformed = ts_transform_nested_time_bucket_comparison(qual);
788814
if (transformed != NULL)
789815
{
790816
/*
@@ -835,7 +861,7 @@ timebucket_annotate(Node *quals, CollectQualCtx *ctx)
835861
* check for time_bucket comparisons
836862
* time_bucket(Const, time_colum) > Const
837863
*/
838-
Expr *transformed = ts_transform_time_bucket_comparison(qual);
864+
Expr *transformed = ts_transform_nested_time_bucket_comparison(qual);
839865
if (transformed != NULL)
840866
{
841867
/*
@@ -1054,7 +1080,7 @@ get_simplified_restrictions(PlannerInfo *root, List *restrictions)
10541080
* check for time_bucket comparisons
10551081
* time_bucket(Const, time_colum) > Const
10561082
*/
1057-
Expr *transformed = ts_transform_time_bucket_comparison(qual);
1083+
Expr *transformed = ts_transform_nested_time_bucket_comparison(qual);
10581084
if (transformed != NULL)
10591085
{
10601086
/*

src/planner/planner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ extern void ts_plan_expand_hypertable_chunks(Hypertable *ht, PlannerInfo *root,
9797
bool include_osm);
9898
extern void ts_plan_expand_timebucket_annotate(PlannerInfo *root, RelOptInfo *rel);
9999
extern Expr *ts_transform_time_bucket_comparison(Expr *);
100+
extern Expr *ts_transform_nested_time_bucket_comparison(Expr *);
100101
extern Node *ts_constify_now(PlannerInfo *root, List *rtable, Node *node);
101102
extern void ts_planner_constraint_cleanup(PlannerInfo *root, RelOptInfo *rel);
102103
extern Node *ts_add_space_constraints(PlannerInfo *root, List *rtable, Node *node);

0 commit comments

Comments
 (0)