Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Realtime Continuous Aggregate performance #5261

Merged

Conversation

fabriziomello
Copy link
Contributor

@fabriziomello fabriziomello commented Jan 31, 2023

When calling the cagg_watermark function to get the watermark of a
Continuous Aggregate we execute a SELECT MAX(time_dimension) query
in the underlying materialization hypertable.

The problem is that a SELECT MAX(time_dimention) query can be
expensive because it will scan all hypertable chunks increasing the
planning time for a Realtime Continuous Aggregates.

Improved it by creating a new catalog table to serve as a cache table
to store the current Continous Aggregate watermark in the following
situations:

  • Create CAgg: store the minimum value of hypertable time dimension
    data type;
  • Refresh CAgg: store the last value of the time dimension materialized
    in the underlying materialization hypertable (or the minimum value of
    materialization hypertable time dimension data type if there's no
    data materialized);
  • Drop CAgg Chunks: the same as refresh cagg.

Closes #4699, #5307

@fabriziomello
Copy link
Contributor Author

Some preliminary results:

1. Last TimescaleDB release: 2.10.0

tsbench on  main [?] via tsbench took 12s 
➜ python -m src.tsbench --with-connection pgsql://fabrizio@localhost:5432/2.10.0 --benchmarks cagg_watermark              
*** Processing benchmarks on connection: pgsql://fabrizio@localhost:5432/2.10.0
*** Executing benchmark 'cagg_watermark'
*** All benchmarks are executed - done
============================================
Report for benchmark suite 'cagg_watermark'
+--------------------------------------------------------------------------------------+------------------------------------------+
| Query                                                                                | 8b549b08e28d121946eccbc7452bc476c7754dc8 |
+--------------------------------------------------------------------------------------+------------------------------------------+
| SELECT bucket, a, value FROM agg_1m  WHERE a = 1 AND bucket > '2023-01-01 00:00:00'; |                                     2.03 |
| SELECT bucket, a, value FROM agg_5m  WHERE a = 1 AND bucket > '2023-01-01 00:00:00'; |                                     8.89 |
| SELECT bucket, a, value FROM agg_15m WHERE a = 1 AND bucket > '2023-01-01 00:00:00'; |                                   158.18 |
+--------------------------------------------------------------------------------------+------------------------------------------+

Flamegraph:
result_file_name3

2. This PR

tsbench on  main [?] via tsbench took 11s 
➜ python -m src.tsbench --with-connection pgsql://fabrizio@localhost:5432/fabrizio --benchmarks cagg_watermark --no-teardown
*** Processing benchmarks on connection: pgsql://fabrizio@localhost:5432/fabrizio
*** Executing benchmark 'cagg_watermark'
*** All benchmarks are executed - done
============================================
Report for benchmark suite 'cagg_watermark'
+--------------------------------------------------------------------------------------+------------------------------------------+
| Query                                                                                | b7ad720d8243fe17a14a257434beff7430bc97c7 |
+--------------------------------------------------------------------------------------+------------------------------------------+
| SELECT bucket, a, value FROM agg_1m  WHERE a = 1 AND bucket > '2023-01-01 00:00:00'; |                                     1.76 |
| SELECT bucket, a, value FROM agg_5m  WHERE a = 1 AND bucket > '2023-01-01 00:00:00'; |                                     3.17 |
| SELECT bucket, a, value FROM agg_15m WHERE a = 1 AND bucket > '2023-01-01 00:00:00'; |                                    28.63 |
+--------------------------------------------------------------------------------------+------------------------------------------+

Flamegraph:
result_file_name4

@fabriziomello fabriziomello force-pushed the improve_cagg_watermark_performance branch 7 times, most recently from d7397f5 to 4ad547f Compare March 6, 2023 23:54
@codecov
Copy link

codecov bot commented Mar 7, 2023

Codecov Report

❗ No coverage uploaded for pull request base (main@699fcf4). Click here to learn what that means.
The diff coverage is 94.63%.

❗ Current head de52e20 differs from pull request most recent head db35aa2. Consider uploading reports for the commit db35aa2 to get more accurate results

@@           Coverage Diff           @@
##             main    #5261   +/-   ##
=======================================
  Coverage        ?   90.69%           
=======================================
  Files           ?      229           
  Lines           ?    53235           
  Branches        ?        0           
=======================================
  Hits            ?    48283           
  Misses          ?     4952           
  Partials        ?        0           
Impacted Files Coverage Δ
src/ts_catalog/catalog.c 89.37% <ø> (ø)
src/ts_catalog/catalog.h 100.00% <ø> (ø)
src/chunk.c 93.71% <85.71%> (ø)
tsl/src/continuous_aggs/materialize.c 73.85% <88.23%> (ø)
src/ts_catalog/continuous_aggs_watermark.c 95.83% <95.83%> (ø)
src/hypertable.c 87.59% <100.00%> (ø)
src/ts_catalog/continuous_agg.c 96.47% <100.00%> (ø)
tsl/src/continuous_aggs/create.c 89.16% <100.00%> (ø)
tsl/src/continuous_aggs/invalidation_threshold.c 62.24% <100.00%> (ø)
tsl/src/continuous_aggs/refresh.c 97.68% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@fabriziomello fabriziomello force-pushed the improve_cagg_watermark_performance branch 14 times, most recently from 0dea5d8 to 9deb2e7 Compare March 8, 2023 23:14
@fabriziomello fabriziomello force-pushed the improve_cagg_watermark_performance branch 10 times, most recently from f0e7ec7 to d5f59bf Compare March 20, 2023 20:29
Copy link
Contributor

@mkindahl mkindahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it looks good. A few minor things that I think need to be fixed. There is a bigger question on why some of the error would be raised since these would rather be indications of us doing something wrong with the locking. Raising errors here are safe, since they would normally not fire, but not sure if we want to have some additional tests for these cases.

src/chunk.c Show resolved Hide resolved
src/chunk.c Show resolved Hide resolved
src/hypertable.c Outdated Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Outdated Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Outdated Show resolved Hide resolved
tsl/src/continuous_aggs/create.c Outdated Show resolved Hide resolved
tsl/src/continuous_aggs/refresh.c Outdated Show resolved Hide resolved
tsl/src/continuous_aggs/materialize.c Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Show resolved Hide resolved
sql/pre_install/tables.sql Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Outdated Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Outdated Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Outdated Show resolved Hide resolved
@fabriziomello fabriziomello force-pushed the improve_cagg_watermark_performance branch from d5f59bf to 3faeca8 Compare March 22, 2023 14:05
Copy link
Contributor

@mkindahl mkindahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've had problems with VACUUM previously, so I think we should be careful about releasing locks too early, but otherwise it looks good.

src/ts_catalog/continuous_aggs_watermark.c Outdated Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Show resolved Hide resolved
src/ts_catalog/continuous_aggs_watermark.c Show resolved Hide resolved
@fabriziomello fabriziomello force-pushed the improve_cagg_watermark_performance branch from de52e20 to 2e7a04b Compare March 22, 2023 18:24
When calling the `cagg_watermark` function to get the watermark of a
Continuous Aggregate we execute a `SELECT MAX(time_dimension)` query
in the underlying materialization hypertable.

The problem is that a `SELECT MAX(time_dimention)` query can be
expensive because it will scan all hypertable chunks increasing the
planning time for a Realtime Continuous Aggregates.

Improved it by creating a new catalog table to serve as a cache table
to store the current Continous Aggregate watermark in the following
situations:
- Create CAgg: store the minimum value of hypertable time dimension
  data type;
- Refresh CAgg: store the last value of the time dimension materialized
  in the underlying materialization hypertable (or the minimum value of
  materialization hypertable time dimension data type if there's no
  data materialized);
- Drop CAgg Chunks: the same as refresh cagg.

Closes timescale#4699, timescale#5307
@fabriziomello fabriziomello force-pushed the improve_cagg_watermark_performance branch from 2e7a04b to db35aa2 Compare March 22, 2023 18:44
@fabriziomello fabriziomello merged commit 38fcd1b into timescale:main Mar 22, 2023
@timescale-automation
Copy link

Automated backport to 2.10.x not done: cherry-pick failed.

Git status

HEAD detached at origin/2.10.x
You are currently cherry-picking commit 38fcd1b7.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   CHANGELOG.md
	modified:   sql/pre_install/tables.sql
	modified:   sql/updates/post-update.sql
	modified:   sql/util_time.sql
	modified:   src/chunk.c
	modified:   src/hypertable.c
	modified:   src/hypertable.h
	modified:   src/ts_catalog/CMakeLists.txt
	modified:   src/ts_catalog/catalog.c
	modified:   src/ts_catalog/catalog.h
	modified:   src/ts_catalog/continuous_agg.c
	new file:   src/ts_catalog/continuous_aggs_watermark.c
	new file:   src/ts_catalog/continuous_aggs_watermark.h
	modified:   test/expected/drop_rename_hypertable.out
	modified:   tsl/src/continuous_aggs/create.c
	modified:   tsl/src/continuous_aggs/invalidation_threshold.c
	modified:   tsl/src/continuous_aggs/materialize.c
	modified:   tsl/src/continuous_aggs/materialize.h
	modified:   tsl/src/continuous_aggs/refresh.c
	modified:   tsl/test/expected/cagg_refresh.out
	modified:   tsl/test/expected/cagg_union_view-12.out
	modified:   tsl/test/expected/cagg_union_view-13.out
	modified:   tsl/test/expected/cagg_union_view-14.out
	modified:   tsl/test/expected/cagg_union_view-15.out
	modified:   tsl/test/shared/expected/extension.out
	modified:   tsl/test/sql/cagg_union_view.sql.in

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   sql/updates/latest-dev.sql
	both modified:   sql/updates/reverse-dev.sql


Job log

@timescale-automation timescale-automation added the auto-backport-not-done Automated backport of this PR has failed non-retriably (e.g. conflicts) label Mar 22, 2023
fabriziomello added a commit to fabriziomello/timescaledb that referenced this pull request Apr 26, 2024
In timescale#5261 we cached the Continuous Aggregate watermark value in a
metadata table to improve performance to avoid a `SELECT
max(primary_dimension)` execution at query time.

Manually DML operations on a CAgg are not recommended and instead the
user should use the `refresh_continuous_aggregate` procedure. But we
handle `TRUNCATE` over CAggs generating the necessary invalidation logs
so make sense to also update the watermark.
fabriziomello added a commit to fabriziomello/timescaledb that referenced this pull request Apr 26, 2024
In timescale#5261 we cached the Continuous Aggregate watermark value in a
metadata table to improve performance to avoid a `SELECT
max(primary_dimension)` execution at query time.

Manually DML operations on a CAgg are not recommended and instead the
user should use the `refresh_continuous_aggregate` procedure. But we
handle `TRUNCATE` over CAggs generating the necessary invalidation logs
so make sense to also update the watermark.
fabriziomello added a commit to fabriziomello/timescaledb that referenced this pull request Apr 26, 2024
In timescale#5261 we cached the Continuous Aggregate watermark value in a
metadata table to improve performance avoiding compute the watermark at
planning time.

Manually DML operations on a CAgg are not recommended and instead the
user should use the `refresh_continuous_aggregate` procedure. But we
handle `TRUNCATE` over CAggs generating the necessary invalidation logs
so make sense to also update the watermark.
fabriziomello added a commit to fabriziomello/timescaledb that referenced this pull request Apr 26, 2024
In timescale#5261 we cached the Continuous Aggregate watermark value in a
metadata table to improve performance avoiding compute the watermark at
planning time.

Manually DML operations on a CAgg are not recommended and instead the
user should use the `refresh_continuous_aggregate` procedure. But we
handle `TRUNCATE` over CAggs generating the necessary invalidation logs
so make sense to also update the watermark.
fabriziomello added a commit that referenced this pull request Apr 26, 2024
In #5261 we cached the Continuous Aggregate watermark value in a
metadata table to improve performance avoiding compute the watermark at
planning time.

Manually DML operations on a CAgg are not recommended and instead the
user should use the `refresh_continuous_aggregate` procedure. But we
handle `TRUNCATE` over CAggs generating the necessary invalidation logs
so make sense to also update the watermark.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport-not-done Automated backport of this PR has failed non-retriably (e.g. conflicts) continuous_aggregate core performance Team: Core Database
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve planning performance of real-time continuous aggs
4 participants