-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcagg_policy_incremental.out
More file actions
1608 lines (1476 loc) · 101 KB
/
Copy pathcagg_policy_incremental.out
File metadata and controls
1608 lines (1476 loc) · 101 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
\c :TEST_DBNAME :ROLE_SUPERUSER
CREATE OR REPLACE FUNCTION ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(timeout INT = -1, mock_start_time INT = 0) RETURNS VOID
AS :MODULE_PATHNAME LANGUAGE C VOLATILE;
CREATE OR REPLACE FUNCTION ts_bgw_params_create() RETURNS VOID
AS :MODULE_PATHNAME LANGUAGE C VOLATILE;
CREATE OR REPLACE FUNCTION ts_bgw_params_destroy() RETURNS VOID
AS :MODULE_PATHNAME LANGUAGE C VOLATILE;
CREATE OR REPLACE FUNCTION ts_bgw_params_reset_time(set_time BIGINT = 0, wait BOOLEAN = false) RETURNS VOID
AS :MODULE_PATHNAME LANGUAGE C VOLATILE;
-- Create a user with specific timezone and mock time
CREATE ROLE test_cagg_refresh_policy_user WITH LOGIN;
ALTER ROLE test_cagg_refresh_policy_user SET timezone TO 'UTC';
ALTER ROLE test_cagg_refresh_policy_user SET timescaledb.current_timestamp_mock TO '2025-03-11 00:00:00+00';
GRANT ALL ON SCHEMA public TO test_cagg_refresh_policy_user;
\c :TEST_DBNAME test_cagg_refresh_policy_user
CREATE TABLE public.bgw_log(
msg_no INT,
mock_time BIGINT,
application_name TEXT,
msg TEXT
);
CREATE VIEW sorted_bgw_log AS
SELECT
msg_no,
mock_time,
application_name,
regexp_replace(regexp_replace(msg, '(Wait until|started at|execution time) [0-9]+(\.[0-9]+)?', '\1 (RANDOM)', 'g'), 'background worker "[^"]+"','connection') AS msg
FROM
bgw_log
ORDER BY
mock_time,
application_name COLLATE "C",
msg_no;
CREATE TABLE public.bgw_dsm_handle_store(
handle BIGINT
);
INSERT INTO public.bgw_dsm_handle_store VALUES (0);
SELECT ts_bgw_params_create();
ts_bgw_params_create
----------------------
CREATE TABLE conditions (
time TIMESTAMP WITH TIME ZONE NOT NULL,
device_id INTEGER,
temperature NUMERIC
);
SELECT FROM create_hypertable('conditions', by_range('time'));
--
INSERT INTO conditions
SELECT
t, d, 10
FROM
generate_series(
'2025-02-05 00:00:00+00',
'2025-03-05 00:00:00+00',
'1 hour'::interval) AS t,
generate_series(1,5) AS d;
CREATE MATERIALIZED VIEW conditions_by_day
WITH (timescaledb.continuous, timescaledb.materialized_only=true) AS
SELECT
time_bucket('1 day', time),
device_id,
count(*),
min(temperature),
max(temperature),
avg(temperature),
sum(temperature)
FROM
conditions
GROUP BY
1, 2
WITH NO DATA;
SELECT
add_continuous_aggregate_policy(
'conditions_by_day',
start_offset => NULL,
end_offset => NULL,
schedule_interval => INTERVAL '1 h',
buckets_per_batch => 10
) AS job_id \gset
SELECT
config
FROM
timescaledb_information.jobs
WHERE
job_id = :'job_id' \gset
SELECT ts_bgw_params_reset_time(0, true);
ts_bgw_params_reset_time
--------------------------
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-----------+--------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 0 | DB Scheduler | [TESTING] Registered new background worker
1 | 0 | DB Scheduler | [TESTING] Registered new background worker
2 | 0 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 0 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Sat Mar 01 00:00:00 2025 UTC, Thu Mar 06 00:00:00 2025 UTC ] (batch 1 of 4)
1 | 0 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 0 | Refresh Continuous Aggregate Policy [1000] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
3 | 0 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Wed Feb 19 00:00:00 2025 UTC, Sat Mar 01 00:00:00 2025 UTC ] (batch 2 of 4)
4 | 0 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
5 | 0 | Refresh Continuous Aggregate Policy [1000] | inserted 50 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
6 | 0 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Sun Feb 09 00:00:00 2025 UTC, Wed Feb 19 00:00:00 2025 UTC ] (batch 3 of 4)
7 | 0 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
8 | 0 | Refresh Continuous Aggregate Policy [1000] | inserted 50 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
9 | 0 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Mon Nov 24 00:00:00 4714 UTC BC, Sun Feb 09 00:00:00 2025 UTC ] (batch 4 of 4)
10 | 0 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
11 | 0 | Refresh Continuous Aggregate Policy [1000] | inserted 20 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
CREATE MATERIALIZED VIEW conditions_by_day_manual_refresh
WITH (timescaledb.continuous, timescaledb.materialized_only=true) AS
SELECT
time_bucket('1 day', time),
device_id,
count(*),
min(temperature),
max(temperature),
avg(temperature),
sum(temperature)
FROM
conditions
GROUP BY
1, 2
WITH NO DATA;
CALL refresh_continuous_aggregate('conditions_by_day_manual_refresh', NULL, NULL);
SELECT count(*) FROM conditions_by_day;
count
-------
145
SELECT count(*) FROM conditions_by_day_manual_refresh;
count
-------
145
-- Should have no differences
SELECT
count(*) > 0 AS has_diff
FROM
((SELECT * FROM conditions_by_day_manual_refresh ORDER BY 1, 2)
EXCEPT
(SELECT * FROM conditions_by_day ORDER BY 1, 2)) AS diff;
has_diff
----------
f
TRUNCATE bgw_log, conditions_by_day;
SELECT
config
FROM
alter_job(
:'job_id',
config => jsonb_set(:'config', '{max_batches_per_execution}', '2')
);
config
-----------------------------------------------------------------------------------------------------------------------------
{"end_offset": null, "start_offset": null, "buckets_per_batch": 10, "mat_hypertable_id": 2, "max_batches_per_execution": 2}
-- advance time by 1h so that job runs one more time
SELECT ts_bgw_params_reset_time(extract(epoch from interval '1 hour')::bigint * 1000000, true);
ts_bgw_params_reset_time
--------------------------
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+------------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 3600000000 | DB Scheduler | [TESTING] Registered new background worker
1 | 3600000000 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Sat Mar 01 00:00:00 2025 UTC, Thu Mar 06 00:00:00 2025 UTC ] (batch 1 of 4)
1 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
3 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Wed Feb 19 00:00:00 2025 UTC, Sat Mar 01 00:00:00 2025 UTC ] (batch 2 of 4)
4 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
5 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | inserted 50 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
6 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | reached maximum number of batches per execution (2), batches not processed (2)
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
SELECT count(*) FROM conditions_by_day;
count
-------
75
SELECT count(*) FROM conditions_by_day_manual_refresh;
count
-------
145
-- Should have differences
SELECT
count(*) > 0 AS has_diff
FROM
((SELECT * FROM conditions_by_day_manual_refresh ORDER BY 1, 2)
EXCEPT
(SELECT * FROM conditions_by_day ORDER BY 1, 2)) AS diff;
has_diff
----------
t
-- advance time by 2h so that job runs one more time
SELECT ts_bgw_params_reset_time(extract(epoch from interval '2 hour')::bigint * 1000000, true);
ts_bgw_params_reset_time
--------------------------
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+------------+--------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 3600000000 | DB Scheduler | [TESTING] Registered new background worker
1 | 3600000000 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Sat Mar 01 00:00:00 2025 UTC, Thu Mar 06 00:00:00 2025 UTC ] (batch 1 of 4)
1 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
3 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Wed Feb 19 00:00:00 2025 UTC, Sat Mar 01 00:00:00 2025 UTC ] (batch 2 of 4)
4 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
5 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | inserted 50 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
6 | 3600000000 | Refresh Continuous Aggregate Policy [1000] | reached maximum number of batches per execution (2), batches not processed (2)
0 | 7200000000 | DB Scheduler | [TESTING] Registered new background worker
1 | 7200000000 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 7200000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Sun Feb 09 00:00:00 2025 UTC, Wed Feb 19 00:00:00 2025 UTC ] (batch 1 of 2)
1 | 7200000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 7200000000 | Refresh Continuous Aggregate Policy [1000] | inserted 50 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
3 | 7200000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Mon Nov 24 00:00:00 4714 UTC BC, Sun Feb 09 00:00:00 2025 UTC ] (batch 2 of 2)
4 | 7200000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
5 | 7200000000 | Refresh Continuous Aggregate Policy [1000] | inserted 20 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
-- Should have no differences
SELECT
count(*) > 0 AS has_diff
FROM
((SELECT * FROM conditions_by_day_manual_refresh ORDER BY 1, 2)
EXCEPT
(SELECT * FROM conditions_by_day ORDER BY 1, 2)) AS diff;
has_diff
----------
f
-- Set max_batches_per_execution to 10
SELECT
config
FROM
alter_job(
:'job_id',
config => jsonb_set(:'config', '{max_batches_per_execution}', '10')
);
config
------------------------------------------------------------------------------------------------------------------------------
{"end_offset": null, "start_offset": null, "buckets_per_batch": 10, "mat_hypertable_id": 2, "max_batches_per_execution": 10}
TRUNCATE bgw_log;
-- Insert data into the past
INSERT INTO conditions
SELECT
t, d, 10
FROM
generate_series(
'2020-02-05 00:00:00+00',
'2020-03-05 00:00:00+00',
'1 hour'::interval) AS t,
generate_series(1,5) AS d;
-- advance time by 3h so that job runs one more time
SELECT ts_bgw_params_reset_time(extract(epoch from interval '3 hour')::bigint * 1000000, true);
ts_bgw_params_reset_time
--------------------------
-- Should process all three batches in the past
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-------------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 10800000000 | DB Scheduler | [TESTING] Registered new background worker
1 | 10800000000 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Tue Feb 25 00:00:00 2020 UTC, Fri Mar 06 00:00:00 2020 UTC ] (batch 1 of 3)
1 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | inserted 50 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
3 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Sat Feb 15 00:00:00 2020 UTC, Tue Feb 25 00:00:00 2020 UTC ] (batch 2 of 3)
4 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
5 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | inserted 50 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
6 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Wed Feb 05 00:00:00 2020 UTC, Sat Feb 15 00:00:00 2020 UTC ] (batch 3 of 3)
7 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
8 | 10800000000 | Refresh Continuous Aggregate Policy [1000] | inserted 50 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
SELECT count(*) FROM conditions_by_day;
count
-------
295
SELECT count(*) FROM conditions_by_day_manual_refresh;
count
-------
145
CALL refresh_continuous_aggregate('conditions_by_day_manual_refresh', NULL, NULL);
SELECT count(*) FROM conditions_by_day;
count
-------
295
SELECT count(*) FROM conditions_by_day_manual_refresh;
count
-------
295
-- Should have no differences
SELECT
count(*) > 0 AS has_diff
FROM
((SELECT * FROM conditions_by_day_manual_refresh ORDER BY 1, 2)
EXCEPT
(SELECT * FROM conditions_by_day ORDER BY 1, 2)) AS diff;
has_diff
----------
f
-- Check invalid configurations
\set ON_ERROR_STOP 0
\set VERBOSITY default
SELECT
config
FROM
alter_job(
:'job_id',
config => jsonb_set(:'config', '{max_batches_per_execution}', '-1')
);
ERROR: invalid max batches per execution
DETAIL: max_batches_per_execution: -1
HINT: The max batches per execution should be greater than or equal to zero.
SELECT
config
FROM
alter_job(
:'job_id',
config => jsonb_set(:'config', '{buckets_per_batch}', '-1')
);
ERROR: invalid buckets per batch
DETAIL: buckets_per_batch: -1
HINT: The buckets per batch should be greater than or equal to zero.
\set VERBOSITY terse
\set ON_ERROR_STOP 1
-- Truncate all data from the original hypertable
TRUNCATE bgw_log, conditions;
-- advance time by 4h so that job runs one more time
SELECT ts_bgw_params_reset_time(extract(epoch from interval '4 hour')::bigint * 1000000, true);
ts_bgw_params_reset_time
--------------------------
-- Should fallback to single batch processing because there's no data to be refreshed on the original hypertable
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-------------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 14400000000 | DB Scheduler | [TESTING] Registered new background worker
1 | 14400000000 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 14400000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Mon Nov 24 00:00:00 4714 UTC BC, Thu Mar 06 00:00:00 2025 UTC ]
1 | 14400000000 | Refresh Continuous Aggregate Policy [1000] | deleted 295 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 14400000000 | Refresh Continuous Aggregate Policy [1000] | inserted 0 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
-- Should return zero rows
SELECT count(*) FROM conditions_by_day;
count
-------
0
-- 1 day of data
INSERT INTO conditions
SELECT
t, d, 10
FROM
generate_series(
'2020-02-05 00:00:00+00',
'2020-02-06 00:00:00+00',
'1 hour'::interval) AS t,
generate_series(1,5) AS d;
TRUNCATE bgw_log;
-- advance time by 5h so that job runs one more time
SELECT ts_bgw_params_reset_time(extract(epoch from interval '5 hour')::bigint * 1000000, true);
ts_bgw_params_reset_time
--------------------------
-- Should fallback to single batch processing because the refresh size is too small
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-------------+--------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 18000000000 | DB Scheduler | [TESTING] Registered new background worker
1 | 18000000000 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 18000000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Wed Feb 05 00:00:00 2020 UTC, Fri Feb 07 00:00:00 2020 UTC ]
1 | 18000000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 18000000000 | Refresh Continuous Aggregate Policy [1000] | inserted 10 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
-- Should return 10 rows because the bucket width is `1 day` and buckets per batch is `10`
SELECT count(*) FROM conditions_by_day;
count
-------
10
TRUNCATE conditions_by_day, conditions, bgw_log;
-- Less than 1 day of data (smaller than the bucket width)
INSERT INTO conditions
VALUES ('2020-02-05 00:00:00+00', 1, 10);
-- advance time by 6h so that job runs one more time
SELECT ts_bgw_params_reset_time(extract(epoch from interval '6 hour')::bigint * 1000000, true);
ts_bgw_params_reset_time
--------------------------
-- Should fallback to single batch processing because the refresh size is too small
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-------------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 21600000000 | DB Scheduler | [TESTING] Registered new background worker
1 | 21600000000 | DB Scheduler | [TESTING] Registered new background worker
2 | 21600000000 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 21600000000 | Refresh Continuous Aggregate Policy [1000] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Mon Nov 24 00:00:00 4714 UTC BC, Thu Mar 06 00:00:00 2025 UTC ]
1 | 21600000000 | Refresh Continuous Aggregate Policy [1000] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 21600000000 | Refresh Continuous Aggregate Policy [1000] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
-- Should return 1 row
SELECT count(*) FROM conditions_by_day;
count
-------
1
SELECT delete_job(:job_id);
delete_job
------------
SELECT
add_continuous_aggregate_policy(
'conditions_by_day',
start_offset => INTERVAL '15 days',
end_offset => NULL,
schedule_interval => INTERVAL '1 h',
buckets_per_batch => 5,
refresh_newest_first => true -- explicitly set to true to test the default behavior
) AS job_id \gset
SELECT
add_continuous_aggregate_policy(
'conditions_by_day_manual_refresh',
start_offset => INTERVAL '15 days',
end_offset => NULL,
schedule_interval => INTERVAL '1 h',
buckets_per_batch => 0 -- 0 means no batching, so it will refresh all buckets in one go
) AS job_id_manual \gset
TRUNCATE bgw_log, conditions_by_day, conditions_by_day_manual_refresh, conditions;
INSERT INTO conditions
SELECT
t, d, 10
FROM
generate_series(
'2025-03-11 00:00:00+00'::timestamptz - INTERVAL '30 days',
'2025-03-11 00:00:00+00'::timestamptz,
'1 hour'::interval) AS t,
generate_series(1,5) AS d;
SELECT ts_bgw_params_reset_time(0, true);
ts_bgw_params_reset_time
--------------------------
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-----------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 0 | DB Scheduler | [TESTING] Registered new background worker
1 | 0 | DB Scheduler | [TESTING] Registered new background worker
2 | 0 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 0 | Refresh Continuous Aggregate Policy [1001] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Tue Mar 11 00:00:00 2025 UTC, Wed Mar 12 00:00:00 2025 UTC ] (batch 1 of 4)
1 | 0 | Refresh Continuous Aggregate Policy [1001] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 0 | Refresh Continuous Aggregate Policy [1001] | inserted 5 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
3 | 0 | Refresh Continuous Aggregate Policy [1001] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Thu Mar 06 00:00:00 2025 UTC, Tue Mar 11 00:00:00 2025 UTC ] (batch 2 of 4)
4 | 0 | Refresh Continuous Aggregate Policy [1001] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
5 | 0 | Refresh Continuous Aggregate Policy [1001] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
6 | 0 | Refresh Continuous Aggregate Policy [1001] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Sat Mar 01 00:00:00 2025 UTC, Thu Mar 06 00:00:00 2025 UTC ] (batch 3 of 4)
7 | 0 | Refresh Continuous Aggregate Policy [1001] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
8 | 0 | Refresh Continuous Aggregate Policy [1001] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
9 | 0 | Refresh Continuous Aggregate Policy [1001] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Mon Feb 24 00:00:00 2025 UTC, Sat Mar 01 00:00:00 2025 UTC ] (batch 4 of 4)
10 | 0 | Refresh Continuous Aggregate Policy [1001] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
11 | 0 | Refresh Continuous Aggregate Policy [1001] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
0 | 0 | Refresh Continuous Aggregate Policy [1002] | continuous aggregate refresh (individual invalidation) on "conditions_by_day_manual_refresh" in window [ Mon Feb 24 00:00:00 2025 UTC, Wed Mar 12 00:00:00 2025 UTC ]
1 | 0 | Refresh Continuous Aggregate Policy [1002] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_3"
2 | 0 | Refresh Continuous Aggregate Policy [1002] | inserted 80 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_3"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
-- Both continuous aggregates should have the same data
SELECT count(*) FROM conditions_by_day;
count
-------
80
SELECT count(*) FROM conditions_by_day_manual_refresh;
count
-------
80
-- Should have no differences
SELECT
count(*) > 0 AS has_diff
FROM
((SELECT * FROM conditions_by_day_manual_refresh ORDER BY 1, 2)
EXCEPT
(SELECT * FROM conditions_by_day ORDER BY 1, 2)) AS diff;
has_diff
----------
f
-- Testing with explicit refresh_newest_first = false (from oldest to newest)
SELECT delete_job(:job_id);
delete_job
------------
SELECT delete_job(:job_id_manual);
delete_job
------------
SELECT
add_continuous_aggregate_policy(
'conditions_by_day',
start_offset => INTERVAL '15 days',
end_offset => NULL,
schedule_interval => INTERVAL '1 h',
buckets_per_batch => 5,
refresh_newest_first => false
) AS job_id \gset
SELECT
config
FROM
timescaledb_information.jobs
WHERE
job_id = :'job_id';
config
----------------------------------------------------------------------------------------------------------------------------------
{"end_offset": null, "start_offset": "@ 15 days", "buckets_per_batch": 5, "mat_hypertable_id": 2, "refresh_newest_first": false}
TRUNCATE bgw_log, conditions_by_day;
SELECT ts_bgw_params_reset_time(0, true);
ts_bgw_params_reset_time
--------------------------
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-----------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 0 | DB Scheduler | [TESTING] Registered new background worker
1 | 0 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 0 | Refresh Continuous Aggregate Policy [1003] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Mon Feb 24 00:00:00 2025 UTC, Sat Mar 01 00:00:00 2025 UTC ] (batch 1 of 4)
1 | 0 | Refresh Continuous Aggregate Policy [1003] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
2 | 0 | Refresh Continuous Aggregate Policy [1003] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
3 | 0 | Refresh Continuous Aggregate Policy [1003] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Sat Mar 01 00:00:00 2025 UTC, Thu Mar 06 00:00:00 2025 UTC ] (batch 2 of 4)
4 | 0 | Refresh Continuous Aggregate Policy [1003] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
5 | 0 | Refresh Continuous Aggregate Policy [1003] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
6 | 0 | Refresh Continuous Aggregate Policy [1003] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Thu Mar 06 00:00:00 2025 UTC, Tue Mar 11 00:00:00 2025 UTC ] (batch 3 of 4)
7 | 0 | Refresh Continuous Aggregate Policy [1003] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
8 | 0 | Refresh Continuous Aggregate Policy [1003] | inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
9 | 0 | Refresh Continuous Aggregate Policy [1003] | continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Tue Mar 11 00:00:00 2025 UTC, Wed Mar 12 00:00:00 2025 UTC ] (batch 4 of 4)
10 | 0 | Refresh Continuous Aggregate Policy [1003] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
11 | 0 | Refresh Continuous Aggregate Policy [1003] | inserted 5 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
-- Both continuous aggregates should have the same data
SELECT count(*) FROM conditions_by_day;
count
-------
80
SELECT count(*) FROM conditions_by_day_manual_refresh;
count
-------
80
-- Should have no differences
SELECT
count(*) > 0 AS has_diff
FROM
((SELECT * FROM conditions_by_day_manual_refresh ORDER BY 1, 2)
EXCEPT
(SELECT * FROM conditions_by_day ORDER BY 1, 2)) AS diff;
has_diff
----------
f
-- Tests with Variable sized bucket
SELECT delete_job(:job_id);
delete_job
------------
TRUNCATE conditions;
INSERT INTO conditions
SELECT
t, d, 10
FROM
generate_series(
'2025-01-01 00:00:00+00',
'2025-10-08 00:00:00+00',
'1 hour'::interval) AS t,
generate_series(1,5) AS d;
CREATE MATERIALIZED VIEW conditions_by_month
WITH (timescaledb.continuous, timescaledb.materialized_only=true) AS
SELECT
time_bucket('1 month', time),
device_id,
count(*),
min(temperature),
max(temperature),
avg(temperature),
sum(temperature)
FROM
conditions
GROUP BY
1, 2
WITH NO DATA;
SELECT
add_continuous_aggregate_policy(
'conditions_by_month',
start_offset => INTERVAL '600 days',
end_offset => INTERVAL '7 days',
schedule_interval => INTERVAL '1 day',
refresh_newest_first => false
) AS job_id \gset
SELECT
config
FROM
timescaledb_information.jobs
WHERE
job_id = :'job_id';
config
-----------------------------------------------------------------------------------------------------------------
{"end_offset": "@ 7 days", "start_offset": "@ 600 days", "mat_hypertable_id": 4, "refresh_newest_first": false}
TRUNCATE bgw_log, conditions_by_day;
SELECT ts_bgw_params_reset_time(0, true);
ts_bgw_params_reset_time
--------------------------
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-----------+--------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 0 | DB Scheduler | [TESTING] Registered new background worker
1 | 0 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 0 | Refresh Continuous Aggregate Policy [1004] | continuous aggregate refresh (individual invalidation) on "conditions_by_month" in window [ Tue Aug 01 00:00:00 2023 UTC, Sat Mar 01 00:00:00 2025 UTC ]
1 | 0 | Refresh Continuous Aggregate Policy [1004] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_4"
2 | 0 | Refresh Continuous Aggregate Policy [1004] | inserted 10 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_4"
SELECT * FROM _timescaledb_catalog.continuous_aggs_materialization_ranges;
materialization_id | lowest_modified_value | greatest_modified_value
--------------------+-----------------------+-------------------------
SELECT delete_job(:job_id);
delete_job
------------
------------------------------------------------------------------------------------------
--Test that batched refresh with variable-length buckets doesn't leave remainders
-------------------------------------------------------------------------------------------
CREATE TABLE test_data (
time TIMESTAMPTZ NOT NULL,
value INT
);
SELECT public.create_hypertable(
relation => 'test_data',
time_column_name => 'time',
chunk_time_interval => interval '1 months'
);
create_hypertable
------------------------
(5,public,test_data,t)
-- Insert initial data
INSERT INTO test_data
SELECT time, 1
FROM generate_series('2024-01-01'::timestamptz, '2024-12-31'::timestamptz, '1 day'::interval) time;
-- Create continuous aggregate with monthly buckets and timezone (variable-length buckets)
CREATE MATERIALIZED VIEW batch_test_cagg
WITH (timescaledb.continuous) AS
SELECT
time_bucket('1 month'::interval, time) AS bucket,
count(*) as count
FROM test_data
GROUP BY bucket
WITH NO DATA;
-- Add a policy to enable batched refresh (batch size is 30 days by default for monthly buckets)
SELECT add_continuous_aggregate_policy('batch_test_cagg',
start_offset =>null,
end_offset => INTERVAL '1 month',
schedule_interval => INTERVAL '1 hour',
buckets_per_batch => 1
) AS job_id \gset
-- Run the policy job - this uses batched processing, 1 bucket per batch
TRUNCATE bgw_log;
SELECT ts_bgw_params_reset_time(0, true);
ts_bgw_params_reset_time
--------------------------
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
-- Verify that invalidation log has no entries other than the left and right ends with -/+ infinity
SELECT materialization_id,
_timescaledb_functions.to_timestamp(lowest_modified_value) as low,
_timescaledb_functions.to_timestamp(greatest_modified_value) as high
FROM _timescaledb_catalog.continuous_aggs_materialization_invalidation_log
WHERE materialization_id IN
(SELECT mat_hypertable_id FROM _timescaledb_catalog.continuous_agg
WHERE user_view_name = 'batch_test_cagg')
AND lowest_modified_value != -9223372036854775808 -- -infinity
AND greatest_modified_value != 9223372036854775807 -- +infinity
ORDER BY low;
materialization_id | low | high
--------------------+-----+------
--verify that there is no duplicate/overlapping refreshes.
--Each batch should cover exactly 1 month (buckets_per_batch=1).
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+-----------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 0 | DB Scheduler | [TESTING] Registered new background worker
1 | 0 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
0 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Wed Jan 01 00:00:00 2025 UTC, Sat Feb 01 00:00:00 2025 UTC ] (batch 1 of 14)
1 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
2 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 0 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
3 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Sun Dec 01 00:00:00 2024 UTC, Wed Jan 01 00:00:00 2025 UTC ] (batch 2 of 14)
4 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
5 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
6 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Fri Nov 01 00:00:00 2024 UTC, Sun Dec 01 00:00:00 2024 UTC ] (batch 3 of 14)
7 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
8 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
9 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Tue Oct 01 00:00:00 2024 UTC, Fri Nov 01 00:00:00 2024 UTC ] (batch 4 of 14)
10 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
11 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
12 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Sun Sep 01 00:00:00 2024 UTC, Tue Oct 01 00:00:00 2024 UTC ] (batch 5 of 14)
13 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
14 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
15 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Thu Aug 01 00:00:00 2024 UTC, Sun Sep 01 00:00:00 2024 UTC ] (batch 6 of 14)
16 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
17 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
18 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Mon Jul 01 00:00:00 2024 UTC, Thu Aug 01 00:00:00 2024 UTC ] (batch 7 of 14)
19 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
20 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
21 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Sat Jun 01 00:00:00 2024 UTC, Mon Jul 01 00:00:00 2024 UTC ] (batch 8 of 14)
22 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
23 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
24 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Wed May 01 00:00:00 2024 UTC, Sat Jun 01 00:00:00 2024 UTC ] (batch 9 of 14)
25 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
26 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
27 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Mon Apr 01 00:00:00 2024 UTC, Wed May 01 00:00:00 2024 UTC ] (batch 10 of 14)
28 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
29 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
30 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Fri Mar 01 00:00:00 2024 UTC, Mon Apr 01 00:00:00 2024 UTC ] (batch 11 of 14)
31 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
32 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
33 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Thu Feb 01 00:00:00 2024 UTC, Fri Mar 01 00:00:00 2024 UTC ] (batch 12 of 14)
34 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
35 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
36 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ Mon Jan 01 00:00:00 2024 UTC, Thu Feb 01 00:00:00 2024 UTC ] (batch 13 of 14)
37 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
38 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 1 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
39 | 0 | Refresh Continuous Aggregate Policy [1005] | continuous aggregate refresh (individual invalidation) on "batch_test_cagg" in window [ -infinity, Mon Jan 01 00:00:00 2024 UTC ] (batch 14 of 14)
40 | 0 | Refresh Continuous Aggregate Policy [1005] | deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_6"
41 | 0 | Refresh Continuous Aggregate Policy [1005] | inserted 0 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_6"
--now run the refresh again, should not do anything
TRUNCATE bgw_log;
SELECT ts_bgw_params_reset_time(extract(epoch from interval '1 hour')::bigint * 1000000, true);
ts_bgw_params_reset_time
--------------------------
SELECT ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(25);
ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish
------------------------------------------------------------
SELECT * FROM sorted_bgw_log;
msg_no | mock_time | application_name | msg
--------+------------+------------------+----------------------------------------------------
0 | 3600000000 | DB Scheduler | [TESTING] Registered new background worker
1 | 3600000000 | DB Scheduler | [TESTING] Wait until (RANDOM), started at (RANDOM)
--clean up
DROP TABLE test_data CASCADE;
NOTICE: drop cascades to 2 other objects
NOTICE: drop cascades to 2 other objects
------------------------------------------------------------------------------------------
-- Test incremental refresh policy crashing between batches
------------------------------------------------------------------------------------------
-- Rows processed by a batch should be visible immediately after it finishes
-- We test this by injecting an error after the first batch completes and observing the CAgg state
\c :TEST_DBNAME :ROLE_SUPERUSER
TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log, _timescaledb_catalog.continuous_aggs_materialization_invalidation_log;
\c :TEST_DBNAME test_cagg_refresh_policy_user
TRUNCATE bgw_log, conditions, conditions_by_day, conditions_by_day_manual_refresh;
SELECT delete_job(job_id) FROM timescaledb_information.jobs WHERE hypertable_name = 'conditions_by_day';
delete_job
------------
INSERT INTO conditions
SELECT
t, d, 10
FROM
generate_series(
'2025-02-05 00:00:00+00',
'2025-03-05 00:00:00+00',
'1 hour'::interval) AS t,
generate_series(1,5) AS d;
SELECT
add_continuous_aggregate_policy(
'conditions_by_day',
start_offset => NULL,
end_offset => '1 hour',
schedule_interval => INTERVAL '1 h',
buckets_per_batch => 10
) AS crash_job_id2 \gset
-- Crash after batch 1 finishes refreshing
SELECT debug_waitpoint_enable('cagg_policy_batch_1_after_refresh');
debug_waitpoint_enable
------------------------
-- CAgg state before refresh starts
SELECT min(time_bucket), max(time_bucket) FROM conditions_by_day;
min | max
-----+-----
|
SET client_min_messages TO LOG;
\set ON_ERROR_STOP 0
CALL run_job(:crash_job_id2);
LOG: statement: CALL run_job(1006);
LOG: continuous aggregate refresh (individual invalidation) on "conditions_by_day" in window [ Fri Feb 28 16:00:00 2025 PST, Sun Mar 09 17:00:00 2025 PDT ] (batch 1 of 4)
LOG: deleted 0 row(s) from materialization table "_timescaledb_internal._materialized_hypertable_2"
LOG: inserted 25 row(s) into materialization table "_timescaledb_internal._materialized_hypertable_2"
ERROR: error injected at debug point 'cagg_policy_batch_1_after_refresh'
\set ON_ERROR_STOP 1
RESET client_min_messages;
LOG: statement: RESET client_min_messages;
-- Rows processed by batch 1 should be materialized
SELECT count(*) AS rows_after_refresh FROM conditions_by_day;
rows_after_refresh
--------------------
25
SELECT min(time_bucket), max(time_bucket) FROM conditions_by_day;
min | max
------------------------------+------------------------------
Fri Feb 28 16:00:00 2025 PST | Tue Mar 04 16:00:00 2025 PST
-- Verify registered ranges are cleaned up after the crash
SELECT count(*) AS registered_ranges FROM _timescaledb_catalog.continuous_aggs_jobs_refresh_ranges;
registered_ranges
-------------------
0
SELECT debug_waitpoint_release('cagg_policy_batch_1_after_refresh');
debug_waitpoint_release
-------------------------
-- Process reminaing invalidations and verify
CALL run_job(:crash_job_id2);
-- Verify against manual refresh
CALL refresh_continuous_aggregate('conditions_by_day_manual_refresh', NULL, '1 hour'::interval);
SELECT
count(*) > 0 AS has_diff
FROM
((SELECT * FROM conditions_by_day_manual_refresh ORDER BY 1, 2)
EXCEPT
(SELECT * FROM conditions_by_day ORDER BY 1, 2)) AS diff;
has_diff
----------
f
------------------------------------------------------------------------------------------
-- Test variable-width bucket batching: demonstrates chunk gap skip and invalidation
-- gap skip with non-null start/end offsets (no null-restoration).
-- chunk_time_interval=25 days is intentionally not aligned with monthly buckets.
--
-- Chunk boundaries are fixed 25-day windows anchored to the Unix epoch
-- (Jan 1 1970), so they fall at arbitrary calendar dates. The four data points land in:
-- Mar 15 -> chunk [Feb 22, Mar 18): spans Feb and Mar bucket boundaries
-- May 15 -> chunk [May 7, Jun 1): entirely within May
-- Jun 10 -> chunk [Jun 1, Jun 26): entirely within June
-- Oct 15 -> chunk [Oct 4, Oct 29): entirely within October
--
-- June is explicitly refreshed before the policy runs, consuming its invalidation.
-- The sentinel [-inf, +inf] is cut into two residuals in mat_inval_log:
-- left [-inf, May 31 23:59:59.999999] (covers Mar and May)
-- right [Jul 1, +inf] (covers Oct and later)
-- -> gap from Jun 1 to Jun 30 means June has no pending invalidation.
--
-- Expected batch behaviour (refresh_newest_first=false, buckets_per_batch=1,
-- start_offset='10 years', end_offset='1 month'):
-- Feb: batch [Feb 1, Mar 1) -- EMPTY: the Feb 22 chunk touches the Feb bucket but
-- no data falls in February; illustrates a chunk intersecting a bucket boundary
-- Mar: batch generated (Mar chunk overlaps Mar bucket + left-residual inval)
-- Apr: SKIPPED (chunk gap: no chunks in April)
-- May: batch generated (May chunk + left-residual inval)
-- Jun: SKIPPED (invalidation gap: inval was consumed by explicit refresh)
-- Jul-Sep: SKIPPED (chunk gap)
-- Oct: batch generated (Oct chunk + right-residual inval)
-- No null-restoration: start_offset and end_offset are both non-null.
-- Final batches: [Feb 1, Mar 1), [Mar 1, Apr 1), [May 1, Jun 1), [Oct 1, Nov 1).
------------------------------------------------------------------------------------------
-- Monthly bucket boundaries are at midnight UTC; set timezone to avoid PST-shift issues.
SET timezone = 'UTC';