Skip to content

POC for rewriting queries with Caggs, add early validations#8967

Draft
natalya-aksman wants to merge 1 commit intotimescale:mainfrom
natalya-aksman:poc_rewrite_queries_with_caggs
Draft

POC for rewriting queries with Caggs, add early validations#8967
natalya-aksman wants to merge 1 commit intotimescale:mainfrom
natalya-aksman:poc_rewrite_queries_with_caggs

Conversation

@natalya-aksman
Copy link
Member

@natalya-aksman natalya-aksman commented Nov 21, 2025

POC currently checks whether queries can be rewritten with Caggs and rewrites queries with Caggs if it can.

GUCs enable_cagg_rewites and cagg_rewrites_debug_info are OFF by default. If only cagg_rewrites_debug_info is ON diagnostics on whether query can be rewritten with Caggs will be printed but query won't be rewritten.

Only real-time Caggs are currently considered as candidates.
Reaggregation is out of scope for this POC i.e. we look for exact match on time buckets and aggregates provided by Caggs.

Validation methods in tsl/src/continuous_aggs/common.c were refactored so they could be reused by Cagg rewrites validations.

PG15 is currently not supported as view queries in PG15_LE have dummy range table entries which complicates expression matching. As we plan to stop PG15 support in 6 months it seems like an unnecessary complication to deal with.

TODO:

  • Comprehensive unit tests covering all negative and positive cases
  • Dealing with Hierarchical Caggs rewrites
  • Matching on equivalent expressions as in "a=5" matches "5=a".
  • Dealing with params, making sure all matches are done on immutable expressions.
  • Matching LATERAL subqueries in Caggs
  • Checking whether Caggs have active invalidations to exclude them from the candidates

Issue #9225 should complete "Query rewrites with Caggs" project by adding fully real-time mode which, for each Cagg used in a query, will combine materialized data plus live data above the current Cagg watermark and live data from current Cagg invalidated ranges.

Completing #9225 will ensure that for every query eligible to be rewritten with Caggs we will use as much materialized data as possible while still providing the same output whether the query was rewritten with a Cagg or not.

@github-actions
Copy link

@melihmutlu, @svenklemm: please review this pull request.

Powered by pull-review

@natalya-aksman natalya-aksman marked this pull request as draft November 21, 2025 21:59
@natalya-aksman natalya-aksman force-pushed the poc_rewrite_queries_with_caggs branch 2 times, most recently from 76eabff to 259c405 Compare November 24, 2025 16:53
@codecov
Copy link

codecov bot commented Nov 24, 2025

Codecov Report

❌ Patch coverage is 85.21401% with 114 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.86%. Comparing base (5e3ae2e) to head (f16bfca).

Files with missing lines Patch % Lines
tsl/src/continuous_aggs/common.c 72.19% 35 Missing and 27 partials ⚠️
tsl/src/continuous_aggs/rewrite_with_caggs.c 90.24% 20 Missing and 23 partials ⚠️
src/import/setrefs.c 92.42% 1 Missing and 4 partials ⚠️
src/ts_catalog/continuous_agg.c 83.33% 1 Missing and 1 partial ⚠️
src/planner/expand_hypertable.c 0.00% 0 Missing and 1 partial ⚠️
src/planner/planner.c 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8967      +/-   ##
==========================================
+ Coverage   85.72%   85.86%   +0.14%     
==========================================
  Files         245      247       +2     
  Lines       48167    48806     +639     
  Branches    10748    10937     +189     
==========================================
+ Hits        41289    41909     +620     
- Misses       3522     3572      +50     
+ Partials     3356     3325      -31     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@natalya-aksman natalya-aksman force-pushed the poc_rewrite_queries_with_caggs branch 4 times, most recently from 24fb146 to a0273a4 Compare December 1, 2025 19:13
@natalya-aksman natalya-aksman force-pushed the poc_rewrite_queries_with_caggs branch from a0273a4 to db51394 Compare December 2, 2025 20:25
@natalya-aksman natalya-aksman force-pushed the poc_rewrite_queries_with_caggs branch 3 times, most recently from 7726617 to 5d112ca Compare December 17, 2025 22:34
@natalya-aksman natalya-aksman force-pushed the poc_rewrite_queries_with_caggs branch from 5d112ca to cc21763 Compare January 9, 2026 19:24
@natalya-aksman natalya-aksman force-pushed the poc_rewrite_queries_with_caggs branch 3 times, most recently from 4d37d7c to 004fa25 Compare January 22, 2026 19:13
@natalya-aksman natalya-aksman force-pushed the poc_rewrite_queries_with_caggs branch 3 times, most recently from dcc7234 to 974f4bb Compare February 2, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants