-
Notifications
You must be signed in to change notification settings - Fork 848
/
cagg_watermark-16.out
1829 lines (1709 loc) · 127 KB
/
cagg_watermark-16.out
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.
\set EXPLAIN_ANALYZE 'EXPLAIN (analyze,costs off,timing off,summary off)'
CREATE TABLE continuous_agg_test(time int, data int);
select create_hypertable('continuous_agg_test', 'time', chunk_time_interval=> 10);
NOTICE: adding not-null constraint to column "time"
create_hypertable
----------------------------------
(1,public,continuous_agg_test,t)
(1 row)
CREATE OR REPLACE FUNCTION integer_now_test1() returns int LANGUAGE SQL STABLE as $$ SELECT coalesce(max(time), 0) FROM continuous_agg_test $$;
SELECT set_integer_now_func('continuous_agg_test', 'integer_now_test1');
set_integer_now_func
----------------------
(1 row)
-- watermark tabels start out empty
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
(0 rows)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
(0 rows)
-- inserting into a table that does not have continuous_agg_insert_trigger doesn't change the watermark
INSERT INTO continuous_agg_test VALUES (10, 1), (11, 2), (21, 3), (22, 4);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
(0 rows)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
(0 rows)
\c :TEST_DBNAME :ROLE_SUPERUSER
CREATE TABLE continuous_agg_test_mat(time int);
select create_hypertable('continuous_agg_test_mat', 'time', chunk_time_interval=> 10);
NOTICE: adding not-null constraint to column "time"
create_hypertable
--------------------------------------
(2,public,continuous_agg_test_mat,t)
(1 row)
INSERT INTO _timescaledb_catalog.continuous_agg VALUES (2, 1, NULL, '','','','',0,'','');
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- create the trigger
CREATE TRIGGER continuous_agg_insert_trigger
AFTER INSERT ON continuous_agg_test
FOR EACH ROW EXECUTE FUNCTION _timescaledb_functions.continuous_agg_invalidation_trigger(1);
-- inserting into the table still doesn't change the watermark since there's no
-- continuous_aggs_invalidation_threshold. We treat that case as a invalidation_watermark of
-- BIG_INT_MIN, since the first run of the aggregation will need to scan the
-- entire table anyway.
INSERT INTO continuous_agg_test VALUES (10, 1), (11, 2), (21, 3), (22, 4);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
(0 rows)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
(0 rows)
-- set the continuous_aggs_invalidation_threshold to 15, any insertions below that value need an invalidation
\c :TEST_DBNAME :ROLE_SUPERUSER
INSERT INTO _timescaledb_catalog.continuous_aggs_invalidation_threshold VALUES (1, 15);
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
INSERT INTO continuous_agg_test VALUES (10, 1), (11, 2), (21, 3), (22, 4);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
1 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
1 | 10 | 22
(1 row)
-- INSERTs only above the continuous_aggs_invalidation_threshold won't change the continuous_aggs_hypertable_invalidation_log
INSERT INTO continuous_agg_test VALUES (21, 3), (22, 4);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
1 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
1 | 10 | 22
(1 row)
-- INSERTs only below the continuous_aggs_invalidation_threshold will change the continuous_aggs_hypertable_invalidation_log
INSERT INTO continuous_agg_test VALUES (10, 1), (11, 2);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
1 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
1 | 10 | 22
1 | 10 | 11
(2 rows)
-- test INSERTing other values
INSERT INTO continuous_agg_test VALUES (1, 7), (12, 6), (24, 5), (51, 4);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
1 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
1 | 10 | 22
1 | 10 | 11
1 | 1 | 51
(3 rows)
-- INSERT after dropping a COLUMN
ALTER TABLE continuous_agg_test DROP COLUMN data;
INSERT INTO continuous_agg_test VALUES (-1), (-2), (-3), (-4);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
1 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
1 | 10 | 22
1 | 10 | 11
1 | 1 | 51
1 | -4 | -1
(4 rows)
INSERT INTO continuous_agg_test VALUES (100);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
1 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
1 | 10 | 22
1 | 10 | 11
1 | 1 | 51
1 | -4 | -1
(4 rows)
-- INSERT after adding a COLUMN
ALTER TABLE continuous_agg_test ADD COLUMN d BOOLEAN;
INSERT INTO continuous_agg_test VALUES (-6, true), (-7, false), (-3, true), (-4, false);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
1 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
1 | 10 | 22
1 | 10 | 11
1 | 1 | 51
1 | -4 | -1
1 | -7 | -3
(5 rows)
INSERT INTO continuous_agg_test VALUES (120, false), (200, true);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
1 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
1 | 10 | 22
1 | 10 | 11
1 | 1 | 51
1 | -4 | -1
1 | -7 | -3
(5 rows)
\c :TEST_DBNAME :ROLE_SUPERUSER
DELETE FROM _timescaledb_catalog.continuous_agg where mat_hypertable_id = 2;
DELETE FROM _timescaledb_config.bgw_job WHERE id = 2;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
DROP TABLE continuous_agg_test CASCADE;
\c :TEST_DBNAME :ROLE_SUPERUSER
TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
TRUNCATE _timescaledb_catalog.continuous_aggs_invalidation_threshold;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- CREATE VIEW creates the invalidation trigger correctly
CREATE TABLE ca_inval_test(time int);
SELECT create_hypertable('ca_inval_test', 'time', chunk_time_interval=> 10);
NOTICE: adding not-null constraint to column "time"
create_hypertable
----------------------------
(3,public,ca_inval_test,t)
(1 row)
CREATE OR REPLACE FUNCTION integer_now_test2() returns int LANGUAGE SQL STABLE as $$ SELECT coalesce(max(time), 0) FROM ca_inval_test $$;
SELECT set_integer_now_func('ca_inval_test', 'integer_now_test2');
set_integer_now_func
----------------------
(1 row)
CREATE MATERIALIZED VIEW cit_view
WITH (timescaledb.continuous, timescaledb.materialized_only=false)
AS SELECT time_bucket('5', time), COUNT(time)
FROM ca_inval_test
GROUP BY 1 WITH NO DATA;
INSERT INTO ca_inval_test SELECT generate_series(0, 5);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-------------
3 | -2147483648
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
(0 rows)
\c :TEST_DBNAME :ROLE_SUPERUSER
UPDATE _timescaledb_catalog.continuous_aggs_invalidation_threshold
SET watermark = 15
WHERE hypertable_id = 3;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
INSERT INTO ca_inval_test SELECT generate_series(5, 15);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
3 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
3 | 5 | 15
(1 row)
INSERT INTO ca_inval_test SELECT generate_series(16, 20);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
3 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
3 | 5 | 15
(1 row)
\c :TEST_DBNAME :ROLE_SUPERUSER
TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- updates below the threshold update both the old and new values
UPDATE ca_inval_test SET time = 5 WHERE time = 6;
UPDATE ca_inval_test SET time = 7 WHERE time = 5;
UPDATE ca_inval_test SET time = 17 WHERE time = 14;
UPDATE ca_inval_test SET time = 12 WHERE time = 16;
-- updates purely above the threshold are not logged
UPDATE ca_inval_test SET time = 19 WHERE time = 18;
UPDATE ca_inval_test SET time = 17 WHERE time = 19;
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
3 | 15
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
3 | 5 | 6
3 | 5 | 7
3 | 14 | 17
3 | 12 | 16
(4 rows)
DROP TABLE ca_inval_test CASCADE;
NOTICE: drop cascades to 3 other objects
\c :TEST_DBNAME :ROLE_SUPERUSER
TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
TRUNCATE _timescaledb_catalog.continuous_aggs_invalidation_threshold;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- invalidation trigger is created correctly on chunks that existed before
-- the view was created
CREATE TABLE ts_continuous_test(time INTEGER, location INTEGER);
SELECT create_hypertable('ts_continuous_test', 'time', chunk_time_interval => 10);
NOTICE: adding not-null constraint to column "time"
create_hypertable
---------------------------------
(5,public,ts_continuous_test,t)
(1 row)
CREATE OR REPLACE FUNCTION integer_now_test3() returns int LANGUAGE SQL STABLE as $$ SELECT coalesce(max(time), 0) FROM ts_continuous_test $$;
SELECT set_integer_now_func('ts_continuous_test', 'integer_now_test3');
set_integer_now_func
----------------------
(1 row)
INSERT INTO ts_continuous_test SELECT i, i FROM
(SELECT generate_series(0, 29) AS i) AS i;
CREATE MATERIALIZED VIEW continuous_view
WITH (timescaledb.continuous, timescaledb.materialized_only=false)
AS SELECT time_bucket('5', time), COUNT(location)
FROM ts_continuous_test
GROUP BY 1 WITH NO DATA;
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-------------
5 | -2147483648
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
(0 rows)
\c :TEST_DBNAME :ROLE_SUPERUSER
UPDATE _timescaledb_catalog.continuous_aggs_invalidation_threshold
SET watermark = 2
WHERE hypertable_id = 5;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
INSERT INTO ts_continuous_test VALUES (1, 1);
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
5 | 2
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
5 | 1 | 1
(1 row)
-- aborts don't get written
BEGIN;
INSERT INTO ts_continuous_test VALUES (-20, -20);
ABORT;
SELECT * FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold;
hypertable_id | watermark
---------------+-----------
5 | 2
(1 row)
SELECT * from _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log;
hypertable_id | lowest_modified_value | greatest_modified_value
---------------+-----------------------+-------------------------
5 | 1 | 1
(1 row)
DROP TABLE ts_continuous_test CASCADE;
NOTICE: drop cascades to 3 other objects
----
-- Test watermark invalidation and chunk exclusion with prepared and ad-hoc queries
----
CREATE TABLE chunks(time timestamptz, device int, value float);
SELECT FROM create_hypertable('chunks','time',chunk_time_interval:='1d'::interval);
NOTICE: adding not-null constraint to column "time"
--
(1 row)
CREATE MATERIALIZED VIEW chunks_1h WITH (timescaledb.continuous, timescaledb.materialized_only = false)
AS SELECT time_bucket('1 hour', time) AS bucket, device, max(value) AS max FROM chunks GROUP BY 1, 2;
NOTICE: continuous aggregate "chunks_1h" is already up-to-date
-- Get id of the materialization hypertable
SELECT id AS "MAT_HT_ID_1H" FROM _timescaledb_catalog.hypertable
WHERE table_name=(
SELECT materialization_hypertable_name
FROM timescaledb_information.continuous_aggregates
WHERE view_name='chunks_1h'
) \gset
SELECT materialization_hypertable_schema || '.' || materialization_hypertable_name AS "MAT_HT_NAME_1H"
FROM timescaledb_information.continuous_aggregates
WHERE view_name='chunks_1h'
\gset
-- Prepared scan on hypertable (identical to the query of a real-time CAgg)
PREPARE ht_scan_realtime_1h AS
SELECT bucket, device, max
FROM :MAT_HT_NAME_1H
WHERE bucket < COALESCE(_timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H)), '-infinity'::timestamp with time zone)
UNION ALL
SELECT time_bucket('01:00:00'::interval, chunks."time") AS bucket,
chunks.device,
max(chunks.value) AS max
FROM chunks
WHERE chunks."time" >= COALESCE(_timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H)), '-infinity'::timestamp with time zone)
GROUP BY (time_bucket('01:00:00'::interval, chunks."time")), chunks.device;
PREPARE cagg_scan_1h AS SELECT * FROM chunks_1h;
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
------------------------------------------------------------------------------
HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, chunks."time"), chunks.device
Batches: 1
-> Result (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)
INSERT INTO chunks VALUES ('1901-08-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H));
to_timestamp
------------------------------
Wed Jul 31 17:00:00 1901 PST
(1 row)
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Append (actual rows=1 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_16_chunk."time"), _hyper_7_16_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_16_chunk_chunks_time_idx on _hyper_7_16_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
(9 rows)
-- Compare prepared statement with ad-hoc query
EXECUTE cagg_scan_1h;
bucket | device | max
------------------------------+--------+-----
Wed Jul 31 16:00:00 1901 PST | 1 | 2
(1 row)
SELECT * FROM chunks_1h;
bucket | device | max
------------------------------+--------+-----
Wed Jul 31 16:00:00 1901 PST | 1 | 2
(1 row)
-- Add new chunks to the non materialized part of the CAgg
INSERT INTO chunks VALUES ('1910-08-01 01:01:01+01', 1, 2);
:EXPLAIN_ANALYZE EXECUTE cagg_scan_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Append (actual rows=2 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> HashAggregate (actual rows=1 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_16_chunk."time"), _hyper_7_16_chunk.device
Batches: 1
-> Result (actual rows=1 loops=1)
-> Append (actual rows=1 loops=1)
-> Index Scan using _hyper_7_16_chunk_chunks_time_idx on _hyper_7_16_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> Index Scan using _hyper_7_18_chunk_chunks_time_idx on _hyper_7_18_chunk (actual rows=1 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
(12 rows)
:EXPLAIN_ANALYZE SELECT * FROM chunks_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Append (actual rows=2 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> HashAggregate (actual rows=1 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_16_chunk."time"), _hyper_7_16_chunk.device
Batches: 1
-> Result (actual rows=1 loops=1)
-> Append (actual rows=1 loops=1)
-> Index Scan using _hyper_7_16_chunk_chunks_time_idx on _hyper_7_16_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> Index Scan using _hyper_7_18_chunk_chunks_time_idx on _hyper_7_18_chunk (actual rows=1 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
(12 rows)
INSERT INTO chunks VALUES ('1911-08-01 01:01:01+01', 1, 2);
:EXPLAIN_ANALYZE EXECUTE cagg_scan_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Append (actual rows=3 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> HashAggregate (actual rows=2 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_16_chunk."time"), _hyper_7_16_chunk.device
Batches: 1
-> Result (actual rows=2 loops=1)
-> Append (actual rows=2 loops=1)
-> Index Scan using _hyper_7_16_chunk_chunks_time_idx on _hyper_7_16_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> Index Scan using _hyper_7_18_chunk_chunks_time_idx on _hyper_7_18_chunk (actual rows=1 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> Index Scan using _hyper_7_19_chunk_chunks_time_idx on _hyper_7_19_chunk (actual rows=1 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
(14 rows)
:EXPLAIN_ANALYZE SELECT * FROM chunks_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Append (actual rows=3 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> HashAggregate (actual rows=2 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_16_chunk."time"), _hyper_7_16_chunk.device
Batches: 1
-> Result (actual rows=2 loops=1)
-> Append (actual rows=2 loops=1)
-> Index Scan using _hyper_7_16_chunk_chunks_time_idx on _hyper_7_16_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> Index Scan using _hyper_7_18_chunk_chunks_time_idx on _hyper_7_18_chunk (actual rows=1 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
-> Index Scan using _hyper_7_19_chunk_chunks_time_idx on _hyper_7_19_chunk (actual rows=1 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1901 PST'::timestamp with time zone)
(14 rows)
-- Materialize CAgg and check for plan time chunk exclusion
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
:EXPLAIN_ANALYZE EXECUTE cagg_scan_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=3 loops=1)
-> Append (actual rows=3 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_19_chunk."time"), _hyper_7_19_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_19_chunk_chunks_time_idx on _hyper_7_19_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
(14 rows)
:EXPLAIN_ANALYZE SELECT * FROM chunks_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=3 loops=1)
-> Append (actual rows=3 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_19_chunk."time"), _hyper_7_19_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_19_chunk_chunks_time_idx on _hyper_7_19_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
(14 rows)
-- Check plan when chunk_append and constraint_aware_append cannot be used
-- There should be no plans for scans of chunks that are materialized in the CAgg
-- on the underlying hypertable
SET timescaledb.enable_chunk_append = OFF;
SET timescaledb.enable_constraint_aware_append = OFF;
:EXPLAIN_ANALYZE SELECT * FROM chunks_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=3 loops=1)
-> Append (actual rows=3 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_19_chunk."time"), _hyper_7_19_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_19_chunk_chunks_time_idx on _hyper_7_19_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Mon Jul 31 17:00:00 1911 PST'::timestamp with time zone)
(14 rows)
RESET timescaledb.enable_chunk_append;
RESET timescaledb.enable_constraint_aware_append;
-- Insert new values and check watermark changes
INSERT INTO chunks VALUES ('1920-08-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H));
to_timestamp
------------------------------
Sat Jul 31 17:00:00 1920 PST
(1 row)
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=4 loops=1)
-> Append (actual rows=4 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sat Jul 31 17:00:00 1920 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sat Jul 31 17:00:00 1920 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sat Jul 31 17:00:00 1920 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sat Jul 31 17:00:00 1920 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_22_chunk."time"), _hyper_7_22_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_22_chunk_chunks_time_idx on _hyper_7_22_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Sat Jul 31 17:00:00 1920 PST'::timestamp with time zone)
(16 rows)
-- Compare prepared statement with ad-hoc query
EXECUTE cagg_scan_1h;
bucket | device | max
------------------------------+--------+-----
Wed Jul 31 16:00:00 1901 PST | 1 | 2
Sun Jul 31 16:00:00 1910 PST | 1 | 2
Mon Jul 31 16:00:00 1911 PST | 1 | 2
Sat Jul 31 16:00:00 1920 PST | 1 | 2
(4 rows)
SELECT * FROM chunks_1h;
bucket | device | max
------------------------------+--------+-----
Wed Jul 31 16:00:00 1901 PST | 1 | 2
Sun Jul 31 16:00:00 1910 PST | 1 | 2
Mon Jul 31 16:00:00 1911 PST | 1 | 2
Sat Jul 31 16:00:00 1920 PST | 1 | 2
(4 rows)
INSERT INTO chunks VALUES ('1930-08-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H));
to_timestamp
------------------------------
Thu Jul 31 17:00:00 1930 PST
(1 row)
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=5 loops=1)
-> Append (actual rows=5 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Thu Jul 31 17:00:00 1930 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Thu Jul 31 17:00:00 1930 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Thu Jul 31 17:00:00 1930 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Thu Jul 31 17:00:00 1930 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_25_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_25_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Thu Jul 31 17:00:00 1930 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_24_chunk."time"), _hyper_7_24_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_24_chunk_chunks_time_idx on _hyper_7_24_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Thu Jul 31 17:00:00 1930 PST'::timestamp with time zone)
(18 rows)
-- Two invalidations without prepared statement execution between
INSERT INTO chunks VALUES ('1931-08-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
INSERT INTO chunks VALUES ('1932-08-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H));
to_timestamp
------------------------------
Sun Jul 31 17:00:00 1932 PST
(1 row)
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=7 loops=1)
-> Append (actual rows=7 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_25_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_25_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_27_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_27_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_29_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_29_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_28_chunk."time"), _hyper_7_28_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_28_chunk_chunks_time_idx on _hyper_7_28_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
(22 rows)
-- Multiple prepared statement executions followed by one invalidation
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=7 loops=1)
-> Append (actual rows=7 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_25_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_25_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_27_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_27_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_29_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_29_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_28_chunk."time"), _hyper_7_28_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_28_chunk_chunks_time_idx on _hyper_7_28_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
(22 rows)
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=7 loops=1)
-> Append (actual rows=7 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_25_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_25_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_27_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_27_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_29_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_29_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_28_chunk."time"), _hyper_7_28_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_28_chunk_chunks_time_idx on _hyper_7_28_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Sun Jul 31 17:00:00 1932 PST'::timestamp with time zone)
(22 rows)
INSERT INTO chunks VALUES ('1940-08-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=8 loops=1)
-> Append (actual rows=8 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_25_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_25_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_27_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_27_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_29_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_29_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_31_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_31_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_30_chunk."time"), _hyper_7_30_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_30_chunk_chunks_time_idx on _hyper_7_30_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
(24 rows)
-- Compare prepared statement with ad-hoc query
EXECUTE cagg_scan_1h;
bucket | device | max
------------------------------+--------+-----
Wed Jul 31 16:00:00 1901 PST | 1 | 2
Sun Jul 31 16:00:00 1910 PST | 1 | 2
Mon Jul 31 16:00:00 1911 PST | 1 | 2
Sat Jul 31 16:00:00 1920 PST | 1 | 2
Thu Jul 31 16:00:00 1930 PST | 1 | 2
Fri Jul 31 16:00:00 1931 PST | 1 | 2
Sun Jul 31 16:00:00 1932 PST | 1 | 2
Wed Jul 31 16:00:00 1940 PST | 1 | 2
(8 rows)
SELECT * FROM chunks_1h;
bucket | device | max
------------------------------+--------+-----
Wed Jul 31 16:00:00 1901 PST | 1 | 2
Sun Jul 31 16:00:00 1910 PST | 1 | 2
Mon Jul 31 16:00:00 1911 PST | 1 | 2
Sat Jul 31 16:00:00 1920 PST | 1 | 2
Thu Jul 31 16:00:00 1930 PST | 1 | 2
Fri Jul 31 16:00:00 1931 PST | 1 | 2
Sun Jul 31 16:00:00 1932 PST | 1 | 2
Wed Jul 31 16:00:00 1940 PST | 1 | 2
(8 rows)
-- Delete data from hypertable - data is only present in cagg after this point. If the watermark in the prepared
-- statement is not moved to the most-recent watermark, we would see an empty result.
TRUNCATE chunks;
EXECUTE cagg_scan_1h;
bucket | device | max
------------------------------+--------+-----
Wed Jul 31 16:00:00 1901 PST | 1 | 2
Sun Jul 31 16:00:00 1910 PST | 1 | 2
Mon Jul 31 16:00:00 1911 PST | 1 | 2
Sat Jul 31 16:00:00 1920 PST | 1 | 2
Thu Jul 31 16:00:00 1930 PST | 1 | 2
Fri Jul 31 16:00:00 1931 PST | 1 | 2
Sun Jul 31 16:00:00 1932 PST | 1 | 2
Wed Jul 31 16:00:00 1940 PST | 1 | 2
(8 rows)
SELECT * FROM chunks_1h;
bucket | device | max
------------------------------+--------+-----
Wed Jul 31 16:00:00 1901 PST | 1 | 2
Sun Jul 31 16:00:00 1910 PST | 1 | 2
Mon Jul 31 16:00:00 1911 PST | 1 | 2
Sat Jul 31 16:00:00 1920 PST | 1 | 2
Thu Jul 31 16:00:00 1930 PST | 1 | 2
Fri Jul 31 16:00:00 1931 PST | 1 | 2
Sun Jul 31 16:00:00 1932 PST | 1 | 2
Wed Jul 31 16:00:00 1940 PST | 1 | 2
(8 rows)
-- Refresh the CAgg
CALL refresh_continuous_aggregate('chunks_1h', NULL, NULL);
EXECUTE cagg_scan_1h;
bucket | device | max
--------+--------+-----
(0 rows)
SELECT * FROM chunks_1h;
bucket | device | max
--------+--------+-----
(0 rows)
-- Check new watermark
SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H));
to_timestamp
------------------------------
Wed Jul 31 17:00:00 1940 PST
(1 row)
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=0 loops=1)
-> Append (actual rows=0 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_25_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_25_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_27_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_27_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_29_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_29_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_31_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_31_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Wed Jul 31 17:00:00 1940 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, "time"), device
Batches: 1
-> Result (actual rows=0 loops=1)
One-Time Filter: false
(23 rows)
-- Update after truncate
INSERT INTO chunks VALUES ('1950-08-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H));
to_timestamp
------------------------------
Mon Jul 31 17:00:00 1950 PST
(1 row)
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=1 loops=1)
-> Append (actual rows=1 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_25_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_25_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_27_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_27_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_29_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_29_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_31_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_31_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> Index Scan using _hyper_8_33_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_33_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_32_chunk."time"), _hyper_7_32_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_32_chunk_chunks_time_idx on _hyper_7_32_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
(26 rows)
-- Test with CAgg on CAgg
CREATE MATERIALIZED VIEW chunks_1d WITH (timescaledb.continuous, timescaledb.materialized_only = false)
AS SELECT time_bucket('1 days', bucket) AS bucket, device, max(max) AS max FROM chunks_1h GROUP BY 1, 2;
NOTICE: refreshing continuous aggregate "chunks_1d"
SELECT id AS "MAT_HT_ID_1D" FROM _timescaledb_catalog.hypertable
WHERE table_name=(
SELECT materialization_hypertable_name
FROM timescaledb_information.continuous_aggregates
WHERE view_name='chunks_1d'
) \gset
SELECT materialization_hypertable_schema || '.' || materialization_hypertable_name AS "MAT_HT_NAME_1D"
FROM timescaledb_information.continuous_aggregates
WHERE view_name='chunks_1d'
\gset
-- Prepared scan on hypertable (identical to the query of a real-time CAgg)
PREPARE ht_scan_realtime_1d AS
SELECT bucket, device, max
FROM :MAT_HT_NAME_1D
WHERE bucket < COALESCE(_timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1D)), '-infinity'::timestamp with time zone)
UNION ALL
SELECT time_bucket('@ 1 day'::interval, chunks_1h.bucket) AS bucket,
chunks_1h.device,
max(chunks_1h.max) AS max
FROM chunks_1h
WHERE chunks_1h.bucket >= COALESCE(_timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1D)), '-infinity'::timestamp with time zone)
GROUP BY (time_bucket('@ 1 day'::interval, chunks_1h.bucket)), chunks_1h.device;
PREPARE cagg_scan_1d AS SELECT * FROM chunks_1d;
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1d;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Append (actual rows=1 loops=1)
-> Index Scan using _hyper_9_34_chunk__materialized_hypertable_9_bucket_idx on _hyper_9_34_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Tue Aug 01 16:00:00 1950 PST'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 day'::interval, (time_bucket('@ 1 hour'::interval, "time"))), device
Batches: 1
-> Result (actual rows=0 loops=1)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, "time"), device
Batches: 1
-> Result (actual rows=0 loops=1)
One-Time Filter: false
(12 rows)
INSERT INTO chunks VALUES ('2000-08-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
CALL refresh_continuous_aggregate('chunks_1d', '1900-01-01', '2021-06-01');
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1d;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Append (actual rows=2 loops=1)