-
Notifications
You must be signed in to change notification settings - Fork 848
/
dist_hypertable-15.out
6164 lines (5533 loc) · 323 KB
/
dist_hypertable-15.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.
-- Need to be super user to create extension and add data nodes
\c :TEST_DBNAME :ROLE_CLUSTER_SUPERUSER;
\unset ECHO
psql:include/filter_exec.sql:5: NOTICE: schema "test" already exists, skipping
psql:include/remote_exec.sql:5: NOTICE: schema "test" already exists, skipping
\set DATA_NODE_1 :TEST_DBNAME _1
\set DATA_NODE_2 :TEST_DBNAME _2
\set DATA_NODE_3 :TEST_DBNAME _3
\set DATA_NODE_4 :TEST_DBNAME _4
\set TABLESPACE_1 :TEST_DBNAME _1
\set TABLESPACE_2 :TEST_DBNAME _2
SELECT
test.make_tablespace_path(:'TEST_TABLESPACE1_PREFIX', :'TEST_DBNAME') AS spc1path,
test.make_tablespace_path(:'TEST_TABLESPACE2_PREFIX', :'TEST_DBNAME') AS spc2path
\gset
SELECT node_name, database, node_created, database_created, extension_created
FROM (
SELECT (add_data_node(name, host => 'localhost', DATABASE => name)).*
FROM (VALUES (:'DATA_NODE_1'), (:'DATA_NODE_2'), (:'DATA_NODE_3')) v(name)
) a;
node_name | database | node_created | database_created | extension_created
----------------------+----------------------+--------------+------------------+-------------------
db_dist_hypertable_1 | db_dist_hypertable_1 | t | t | t
db_dist_hypertable_2 | db_dist_hypertable_2 | t | t | t
db_dist_hypertable_3 | db_dist_hypertable_3 | t | t | t
(3 rows)
GRANT USAGE ON FOREIGN SERVER :DATA_NODE_1, :DATA_NODE_2, :DATA_NODE_3 TO PUBLIC;
-- View to see dimension partitions. Note RIGHT JOIN to see that
-- dimension partitions are cleaned up (deleted) properly.
CREATE VIEW hypertable_partitions AS
SELECT table_name, dimension_id, range_start, data_nodes
FROM _timescaledb_catalog.hypertable h
INNER JOIN _timescaledb_catalog.dimension d ON (d.hypertable_id = h.id)
RIGHT JOIN _timescaledb_catalog.dimension_partition dp ON (dp.dimension_id = d.id)
ORDER BY dimension_id, range_start;
GRANT SELECT ON hypertable_partitions TO :ROLE_1;
-- Import testsupport.sql file to data nodes
\unset ECHO
GRANT CREATE ON SCHEMA public TO :ROLE_1;
SET ROLE :ROLE_1;
--Ensure INSERTs use DataNodeDispatch. DataNodeCopy is tested later
SET timescaledb.enable_distributed_insert_with_copy=false;
-- Verify lack of tables
SELECT node_name FROM timescaledb_information.data_nodes ORDER BY node_name;
node_name
----------------------
db_dist_hypertable_1
db_dist_hypertable_2
db_dist_hypertable_3
(3 rows)
\set ON_ERROR_STOP 0
-- Test that one cannot directly create TimescaleDB foreign tables
CREATE FOREIGN TABLE foreign_table (time timestamptz, device int, temp float) SERVER :DATA_NODE_1;
ERROR: operation not supported
\set ON_ERROR_STOP 1
-- Create distributed hypertables. Add a trigger and primary key
-- constraint to test how those work
CREATE TABLE disttable(time timestamptz, device int CHECK (device > 0), color int, temp float, PRIMARY KEY (time,device));
SELECT * FROM create_distributed_hypertable('disttable', 'time', 'device', 1);
WARNING: insufficient number of partitions for dimension "device"
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
1 | public | disttable | t
(1 row)
-- Increase the number of partitions. Expect warning since still too
-- low. Dimension partitions should be updated to reflect new
-- partitioning.
SELECT * FROM hypertable_partitions WHERE table_name = 'disttable';
table_name | dimension_id | range_start | data_nodes
------------+--------------+----------------------+------------------------
disttable | 2 | -9223372036854775808 | {db_dist_hypertable_1}
(1 row)
SELECT * FROM set_number_partitions('disttable', 2);
WARNING: insufficient number of partitions for dimension "device"
set_number_partitions
-----------------------
(1 row)
SELECT * FROM hypertable_partitions WHERE table_name = 'disttable';
table_name | dimension_id | range_start | data_nodes
------------+--------------+----------------------+------------------------
disttable | 2 | -9223372036854775808 | {db_dist_hypertable_1}
disttable | 2 | 1073741823 | {db_dist_hypertable_2}
(2 rows)
-- Set number of partitions equal to the number of servers should not
-- raise a warning.
SELECT * FROM hypertable_partitions WHERE table_name = 'disttable';
table_name | dimension_id | range_start | data_nodes
------------+--------------+----------------------+------------------------
disttable | 2 | -9223372036854775808 | {db_dist_hypertable_1}
disttable | 2 | 1073741823 | {db_dist_hypertable_2}
(2 rows)
SELECT * FROM set_number_partitions('disttable', 3, 'device');
set_number_partitions
-----------------------
(1 row)
SELECT * FROM hypertable_partitions WHERE table_name = 'disttable';
table_name | dimension_id | range_start | data_nodes
------------+--------------+----------------------+------------------------
disttable | 2 | -9223372036854775808 | {db_dist_hypertable_1}
disttable | 2 | 715827882 | {db_dist_hypertable_2}
disttable | 2 | 1431655764 | {db_dist_hypertable_3}
(3 rows)
-- Show the number of slices
SELECT h.table_name, d.column_name, d.num_slices
FROM _timescaledb_catalog.hypertable h, _timescaledb_catalog.dimension d
WHERE h.id = d.hypertable_id
AND h.table_name = 'disttable';
table_name | column_name | num_slices
------------+-------------+------------
disttable | device | 3
disttable | time |
(2 rows)
-- This table tests both 1-dimensional tables and under-replication
-- (replication_factor > num_data_nodes).
CREATE TABLE underreplicated(time timestamptz, device int, temp float);
\set ON_ERROR_STOP 0
-- can't create an under-replicated hypertable
SELECT * FROM create_hypertable('underreplicated', 'time', replication_factor => 4);
ERROR: replication factor too large for hypertable "underreplicated"
\set ON_ERROR_STOP 1
-- can create under-replicated hypertable byf force detaching a data node
RESET ROLE;
SELECT node_name, database, node_created, database_created, extension_created
FROM add_data_node(:'DATA_NODE_4', host => 'localhost', database => :'DATA_NODE_4');
node_name | database | node_created | database_created | extension_created
----------------------+----------------------+--------------+------------------+-------------------
db_dist_hypertable_4 | db_dist_hypertable_4 | t | t | t
(1 row)
GRANT USAGE ON FOREIGN SERVER :DATA_NODE_4 TO PUBLIC;
GRANT CREATE ON SCHEMA public TO :ROLE_1;
SET ROLE :ROLE_1;
SELECT * FROM create_hypertable('underreplicated', 'time', replication_factor => 4);
NOTICE: adding not-null constraint to column "time"
hypertable_id | schema_name | table_name | created
---------------+-------------+-----------------+---------
2 | public | underreplicated | t
(1 row)
--create new session to clear out connections
\c :TEST_DBNAME :ROLE_CLUSTER_SUPERUSER;
SELECT * FROM delete_data_node(:'DATA_NODE_4', force => true, drop_database => true);
WARNING: insufficient number of data nodes for distributed hypertable "underreplicated"
delete_data_node
------------------
t
(1 row)
SET ROLE :ROLE_1;
CREATE OR REPLACE FUNCTION test_trigger()
RETURNS TRIGGER LANGUAGE PLPGSQL AS
$BODY$
DECLARE
cnt INTEGER;
BEGIN
SELECT count(*) INTO cnt FROM public.disttable;
RAISE WARNING 'FIRING trigger when: % level: % op: % cnt: % trigger_name %',
tg_when, tg_level, tg_op, cnt, tg_name;
IF TG_OP = 'DELETE' THEN
RETURN OLD;
END IF;
RETURN NEW;
END
$BODY$;
-- Create the trigger function on the data nodes:
CALL distributed_exec($$
CREATE OR REPLACE FUNCTION test_trigger()
RETURNS TRIGGER LANGUAGE PLPGSQL AS
$BODY$
DECLARE
cnt INTEGER;
BEGIN
SELECT count(*) INTO cnt FROM public.disttable;
RAISE WARNING 'FIRING trigger when: % level: % op: % cnt: % trigger_name %',
tg_when, tg_level, tg_op, cnt, tg_name;
IF TG_OP = 'DELETE' THEN
RETURN OLD;
END IF;
RETURN NEW;
END
$BODY$;
$$);
CREATE TRIGGER _0_test_trigger_insert
BEFORE INSERT ON disttable
FOR EACH ROW EXECUTE FUNCTION test_trigger();
SELECT * FROM _timescaledb_catalog.hypertable_data_node ORDER BY 1,2,3;
hypertable_id | node_hypertable_id | node_name | block_chunks
---------------+--------------------+----------------------+--------------
1 | 1 | db_dist_hypertable_1 | f
1 | 1 | db_dist_hypertable_2 | f
1 | 1 | db_dist_hypertable_3 | f
2 | 2 | db_dist_hypertable_1 | f
2 | 2 | db_dist_hypertable_2 | f
2 | 2 | db_dist_hypertable_3 | f
(6 rows)
SELECT * FROM _timescaledb_catalog.chunk_data_node ORDER BY 1,2,3;
chunk_id | node_chunk_id | node_name
----------+---------------+-----------
(0 rows)
-- The constraints, indexes, and triggers on the hypertable
SELECT * FROM test.show_constraints('disttable');
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
------------------------+------+---------------+----------------+--------------+------------+----------+-----------
disttable_device_check | c | {device} | - | (device > 0) | f | f | t
disttable_pkey | p | {time,device} | disttable_pkey | | f | f | t
(2 rows)
SELECT * FROM test.show_indexes('disttable');
Index | Columns | Expr | Unique | Primary | Exclusion | Tablespace
---------------------------+---------------+------+--------+---------+-----------+------------
disttable_device_time_idx | {device,time} | | f | f | f |
disttable_pkey | {time,device} | | t | t | f |
disttable_time_idx | {time} | | f | f | f |
(3 rows)
SELECT * FROM test.show_triggers('disttable');
Trigger | Type | Function
------------------------+------+--------------------------------------
_0_test_trigger_insert | 7 | test_trigger
ts_insert_blocker | 7 | _timescaledb_internal.insert_blocker
(2 rows)
-- Drop a column. This will make the attribute numbers of the
-- hypertable's root relation differ from newly created chunks. It is
-- a way to test that we properly handle attributed conversion between
-- the root table and chunks
ALTER TABLE disttable DROP COLUMN color;
-- EXPLAIN some inserts to see what plans and explain output for
-- remote inserts look like
EXPLAIN (COSTS FALSE)
INSERT INTO disttable VALUES
('2017-01-01 06:01', 1, 1.1);
QUERY PLAN
-----------------------------------------------
Custom Scan (HypertableModify)
Insert on distributed hypertable disttable
-> Insert on disttable
-> Custom Scan (DataNodeCopy)
-> Custom Scan (ChunkDispatch)
-> Result
(6 rows)
EXPLAIN (VERBOSE, COSTS FALSE)
INSERT INTO disttable VALUES
('2017-01-01 06:01', 1, 1.1);
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------
Custom Scan (HypertableModify)
Insert on distributed hypertable public.disttable
Data nodes: db_dist_hypertable_1, db_dist_hypertable_2, db_dist_hypertable_3
-> Insert on public.disttable
-> Custom Scan (DataNodeCopy)
Output: 'Sun Jan 01 06:01:00 2017 PST'::timestamp with time zone, 1, NULL::integer, '1.1'::double precision
Remote SQL: COPY public.disttable ("time", device, temp) FROM STDIN WITH (FORMAT binary)
-> Custom Scan (ChunkDispatch)
Output: 'Sun Jan 01 06:01:00 2017 PST'::timestamp with time zone, 1, NULL::integer, '1.1'::double precision
-> Result
Output: 'Sun Jan 01 06:01:00 2017 PST'::timestamp with time zone, 1, NULL::integer, '1.1'::double precision
(11 rows)
-- Create some chunks through insertion
INSERT INTO disttable VALUES
('2017-01-01 06:01', 1, 1.1),
('2017-01-01 09:11', 3, 2.1),
('2017-01-01 09:21', 3, 2.2),
('2017-01-01 08:11', 3, 2.3),
('2017-01-01 08:01', 1, 1.2),
('2017-01-02 08:01', 2, 1.3),
('2017-01-02 09:01', 2, 1.4),
('2017-01-02 08:21', 2, 1.5),
('2018-07-02 08:01', 87, 1.6),
('2018-07-02 09:01', 87, 1.4),
('2018-07-02 09:21', 87, 1.8),
('2018-07-01 06:01', 13, 1.4),
('2018-07-01 06:21', 13, 1.5),
('2018-07-01 07:01', 13, 1.4),
('2018-07-01 09:11', 90, 2.7),
('2018-07-01 08:01', 29, 1.5),
('2018-07-01 09:21', 90, 2.8),
('2018-07-01 08:21', 29, 1.2);
-- EXPLAIN some updates/deletes to see what plans and explain output for
-- remote operations look like
EXPLAIN (VERBOSE, COSTS FALSE)
UPDATE disttable SET temp = 3.7 WHERE device = 1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Update on public.disttable
Update on public.disttable disttable_1
Foreign Update on _timescaledb_internal._dist_hyper_1_1_chunk disttable_2
Remote SQL: UPDATE _timescaledb_internal._dist_hyper_1_1_chunk SET temp = $2 WHERE ctid = $1
Foreign Update on _timescaledb_internal._dist_hyper_1_4_chunk disttable_3
Remote SQL: UPDATE _timescaledb_internal._dist_hyper_1_4_chunk SET temp = $2 WHERE ctid = $1
-> Result
Output: '3.7'::double precision, disttable.tableoid, disttable.ctid, (NULL::record)
-> Append
-> Seq Scan on public.disttable disttable_1
Output: disttable_1.tableoid, disttable_1.ctid, NULL::record
Filter: (disttable_1.device = 1)
-> Foreign Scan on _timescaledb_internal._dist_hyper_1_1_chunk disttable_2
Output: disttable_2.tableoid, disttable_2.ctid, disttable_2.*
Data node: db_dist_hypertable_1
Remote SQL: SELECT "time", device, temp, ctid FROM _timescaledb_internal._dist_hyper_1_1_chunk WHERE ((device = 1))
-> Foreign Scan on _timescaledb_internal._dist_hyper_1_4_chunk disttable_3
Output: disttable_3.tableoid, disttable_3.ctid, disttable_3.*
Data node: db_dist_hypertable_1
Remote SQL: SELECT "time", device, temp, ctid FROM _timescaledb_internal._dist_hyper_1_4_chunk WHERE ((device = 1))
(20 rows)
EXPLAIN (VERBOSE, COSTS FALSE)
DELETE FROM disttable WHERE device = 1;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Delete on public.disttable
Delete on public.disttable disttable_1
Foreign Delete on _timescaledb_internal._dist_hyper_1_1_chunk disttable_2
Remote SQL: DELETE FROM _timescaledb_internal._dist_hyper_1_1_chunk WHERE ctid = $1
Foreign Delete on _timescaledb_internal._dist_hyper_1_4_chunk disttable_3
Remote SQL: DELETE FROM _timescaledb_internal._dist_hyper_1_4_chunk WHERE ctid = $1
-> Append
-> Seq Scan on public.disttable disttable_1
Output: disttable_1.tableoid, disttable_1.ctid
Filter: (disttable_1.device = 1)
-> Foreign Scan on _timescaledb_internal._dist_hyper_1_1_chunk disttable_2
Output: disttable_2.tableoid, disttable_2.ctid
Data node: db_dist_hypertable_1
Remote SQL: SELECT ctid FROM _timescaledb_internal._dist_hyper_1_1_chunk WHERE ((device = 1))
-> Foreign Scan on _timescaledb_internal._dist_hyper_1_4_chunk disttable_3
Output: disttable_3.tableoid, disttable_3.ctid
Data node: db_dist_hypertable_1
Remote SQL: SELECT ctid FROM _timescaledb_internal._dist_hyper_1_4_chunk WHERE ((device = 1))
(18 rows)
-- Test distributed ANALYZE.
--
-- First show no statistics
-- reltuples is initially -1 before any VACUUM/ANALYZE has been run on PG14
SELECT relname, relkind, CASE WHEN reltuples > 0 THEN reltuples ELSE 0 END AS reltuples, relpages
FROM pg_class
WHERE oid = 'disttable'::regclass;
relname | relkind | reltuples | relpages
-----------+---------+-----------+----------
disttable | r | 0 | 0
(1 row)
SELECT relname, relkind, CASE WHEN reltuples > 0 THEN reltuples ELSE 0 END AS reltuples, relpages
FROM pg_class cl, (SELECT show_chunks AS chunk FROM show_chunks('disttable')) ch
WHERE cl.oid = ch.chunk::regclass;
relname | relkind | reltuples | relpages
-----------------------+---------+-----------+----------
_dist_hyper_1_1_chunk | f | 0 | 0
_dist_hyper_1_2_chunk | f | 0 | 0
_dist_hyper_1_3_chunk | f | 0 | 0
_dist_hyper_1_4_chunk | f | 0 | 0
_dist_hyper_1_5_chunk | f | 0 | 0
_dist_hyper_1_6_chunk | f | 0 | 0
(6 rows)
ANALYZE disttable;
-- Show updated statistics
SELECT relname, relkind, CASE WHEN reltuples > 0 THEN reltuples ELSE 0 END AS reltuples, relpages
FROM pg_class
WHERE oid = 'disttable'::regclass;
relname | relkind | reltuples | relpages
-----------+---------+-----------+----------
disttable | r | 0 | 0
(1 row)
SELECT relname, relkind, reltuples, relpages
FROM pg_class cl, (SELECT show_chunks AS chunk FROM show_chunks('disttable')) ch
WHERE cl.oid = ch.chunk::regclass;
relname | relkind | reltuples | relpages
-----------------------+---------+-----------+----------
_dist_hyper_1_1_chunk | f | 2 | 1
_dist_hyper_1_2_chunk | f | 3 | 1
_dist_hyper_1_3_chunk | f | 3 | 1
_dist_hyper_1_4_chunk | f | 3 | 1
_dist_hyper_1_5_chunk | f | 3 | 1
_dist_hyper_1_6_chunk | f | 4 | 1
(6 rows)
-- Test distributed VACUUM.
--
VACUUM (FULL, ANALYZE) disttable;
VACUUM FULL disttable;
VACUUM disttable;
\set ON_ERROR_STOP 0
-- VACUUM VERBOSE is not supported at the moment
VACUUM VERBOSE disttable;
ERROR: operation not supported on distributed hypertable
\set ON_ERROR_STOP 1
-- Test prepared statement
PREPARE dist_insert (timestamptz, int, float) AS
INSERT INTO disttable VALUES ($1, $2, $3);
EXECUTE dist_insert ('2017-01-01 06:05', 1, 1.4);
-- Show chunks created
SELECT (_timescaledb_internal.show_chunk(show_chunks)).*
FROM show_chunks('disttable');
chunk_id | hypertable_id | schema_name | table_name | relkind | slices
----------+---------------+-----------------------+-----------------------+---------+---------------------------------------------------------------------------------------------
1 | 1 | _timescaledb_internal | _dist_hyper_1_1_chunk | f | {"time": [1482969600000000, 1483574400000000], "device": [-9223372036854775808, 715827882]}
2 | 1 | _timescaledb_internal | _dist_hyper_1_2_chunk | f | {"time": [1482969600000000, 1483574400000000], "device": [1431655764, 9223372036854775807]}
3 | 1 | _timescaledb_internal | _dist_hyper_1_3_chunk | f | {"time": [1482969600000000, 1483574400000000], "device": [715827882, 1431655764]}
4 | 1 | _timescaledb_internal | _dist_hyper_1_4_chunk | f | {"time": [1530144000000000, 1530748800000000], "device": [-9223372036854775808, 715827882]}
5 | 1 | _timescaledb_internal | _dist_hyper_1_5_chunk | f | {"time": [1530144000000000, 1530748800000000], "device": [715827882, 1431655764]}
6 | 1 | _timescaledb_internal | _dist_hyper_1_6_chunk | f | {"time": [1530144000000000, 1530748800000000], "device": [1431655764, 9223372036854775807]}
(6 rows)
-- Show that there are assigned node_chunk_id:s in chunk data node mappings
SELECT * FROM _timescaledb_catalog.chunk_data_node ORDER BY 1,2,3;
chunk_id | node_chunk_id | node_name
----------+---------------+----------------------
1 | 1 | db_dist_hypertable_1
2 | 1 | db_dist_hypertable_3
3 | 1 | db_dist_hypertable_2
4 | 2 | db_dist_hypertable_1
5 | 2 | db_dist_hypertable_2
6 | 2 | db_dist_hypertable_3
(6 rows)
SELECT * FROM hypertable_partitions;
table_name | dimension_id | range_start | data_nodes
------------+--------------+----------------------+------------------------
disttable | 2 | -9223372036854775808 | {db_dist_hypertable_1}
disttable | 2 | 715827882 | {db_dist_hypertable_2}
disttable | 2 | 1431655764 | {db_dist_hypertable_3}
(3 rows)
-- Show that chunks are created on data nodes and that each data node
-- has their own unique slice in the space (device) dimension.
SELECT * FROM test.remote_exec(ARRAY[:'DATA_NODE_1', :'DATA_NODE_2', :'DATA_NODE_3'], $$
SELECT (_timescaledb_internal.show_chunk(show_chunks)).*
FROM show_chunks('disttable');
$$);
NOTICE: [db_dist_hypertable_1]:
SELECT (_timescaledb_internal.show_chunk(show_chunks)).*
FROM show_chunks('disttable')
NOTICE: [db_dist_hypertable_1]:
chunk_id|hypertable_id|schema_name |table_name |relkind|slices
--------+-------------+---------------------+---------------------+-------+-------------------------------------------------------------------------------------------
1| 1|_timescaledb_internal|_dist_hyper_1_1_chunk|r |{"time": [1482969600000000, 1483574400000000], "device": [-9223372036854775808, 715827882]}
2| 1|_timescaledb_internal|_dist_hyper_1_4_chunk|r |{"time": [1530144000000000, 1530748800000000], "device": [-9223372036854775808, 715827882]}
(2 rows)
NOTICE: [db_dist_hypertable_2]:
SELECT (_timescaledb_internal.show_chunk(show_chunks)).*
FROM show_chunks('disttable')
NOTICE: [db_dist_hypertable_2]:
chunk_id|hypertable_id|schema_name |table_name |relkind|slices
--------+-------------+---------------------+---------------------+-------+---------------------------------------------------------------------------------
1| 1|_timescaledb_internal|_dist_hyper_1_3_chunk|r |{"time": [1482969600000000, 1483574400000000], "device": [715827882, 1431655764]}
2| 1|_timescaledb_internal|_dist_hyper_1_5_chunk|r |{"time": [1530144000000000, 1530748800000000], "device": [715827882, 1431655764]}
(2 rows)
NOTICE: [db_dist_hypertable_3]:
SELECT (_timescaledb_internal.show_chunk(show_chunks)).*
FROM show_chunks('disttable')
NOTICE: [db_dist_hypertable_3]:
chunk_id|hypertable_id|schema_name |table_name |relkind|slices
--------+-------------+---------------------+---------------------+-------+-------------------------------------------------------------------------------------------
1| 1|_timescaledb_internal|_dist_hyper_1_2_chunk|r |{"time": [1482969600000000, 1483574400000000], "device": [1431655764, 9223372036854775807]}
2| 1|_timescaledb_internal|_dist_hyper_1_6_chunk|r |{"time": [1530144000000000, 1530748800000000], "device": [1431655764, 9223372036854775807]}
(2 rows)
remote_exec
-------------
(1 row)
SELECT * FROM test.remote_exec(ARRAY[:'DATA_NODE_1', :'DATA_NODE_2', :'DATA_NODE_3'], $$
SELECT * FROM disttable;
$$);
NOTICE: [db_dist_hypertable_1]:
SELECT * FROM disttable
NOTICE: [db_dist_hypertable_1]:
time |device|temp
----------------------------+------+----
Sun Jan 01 06:01:00 2017 PST| 1| 1.1
Sun Jan 01 08:01:00 2017 PST| 1| 1.2
Sun Jan 01 06:05:00 2017 PST| 1| 1.4
Mon Jul 02 08:01:00 2018 PDT| 87| 1.6
Mon Jul 02 09:01:00 2018 PDT| 87| 1.4
Mon Jul 02 09:21:00 2018 PDT| 87| 1.8
(6 rows)
NOTICE: [db_dist_hypertable_2]:
SELECT * FROM disttable
NOTICE: [db_dist_hypertable_2]:
time |device|temp
----------------------------+------+----
Mon Jan 02 08:01:00 2017 PST| 2| 1.3
Mon Jan 02 09:01:00 2017 PST| 2| 1.4
Mon Jan 02 08:21:00 2017 PST| 2| 1.5
Sun Jul 01 06:01:00 2018 PDT| 13| 1.4
Sun Jul 01 06:21:00 2018 PDT| 13| 1.5
Sun Jul 01 07:01:00 2018 PDT| 13| 1.4
(6 rows)
NOTICE: [db_dist_hypertable_3]:
SELECT * FROM disttable
NOTICE: [db_dist_hypertable_3]:
time |device|temp
----------------------------+------+----
Sun Jan 01 09:11:00 2017 PST| 3| 2.1
Sun Jan 01 09:21:00 2017 PST| 3| 2.2
Sun Jan 01 08:11:00 2017 PST| 3| 2.3
Sun Jul 01 09:11:00 2018 PDT| 90| 2.7
Sun Jul 01 08:01:00 2018 PDT| 29| 1.5
Sun Jul 01 09:21:00 2018 PDT| 90| 2.8
Sun Jul 01 08:21:00 2018 PDT| 29| 1.2
(7 rows)
remote_exec
-------------
(1 row)
SELECT node_name FROM timescaledb_information.data_nodes ORDER BY node_name;
node_name
----------------------
db_dist_hypertable_1
db_dist_hypertable_2
db_dist_hypertable_3
(3 rows)
SELECT * FROM hypertable_detailed_size('disttable') ORDER BY node_name;
table_bytes | index_bytes | toast_bytes | total_bytes | node_name
-------------+-------------+-------------+-------------+----------------------
81920 | 122880 | 0 | 204800 | db_dist_hypertable_1
81920 | 122880 | 0 | 204800 | db_dist_hypertable_2
81920 | 122880 | 0 | 204800 | db_dist_hypertable_3
0 | 24576 | 0 | 24576 |
(4 rows)
-- Show what some queries would look like on the frontend
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT * FROM disttable;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------
Custom Scan (AsyncAppend)
Output: disttable."time", disttable.device, disttable.temp
-> Append
-> Custom Scan (DataNodeScan) on public.disttable disttable_1
Output: disttable_1."time", disttable_1.device, disttable_1.temp
Data node: db_dist_hypertable_1
Chunks: _dist_hyper_1_1_chunk, _dist_hyper_1_4_chunk
Remote SQL: SELECT "time", device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2])
-> Custom Scan (DataNodeScan) on public.disttable disttable_2
Output: disttable_2."time", disttable_2.device, disttable_2.temp
Data node: db_dist_hypertable_2
Chunks: _dist_hyper_1_3_chunk, _dist_hyper_1_5_chunk
Remote SQL: SELECT "time", device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2])
-> Custom Scan (DataNodeScan) on public.disttable disttable_3
Output: disttable_3."time", disttable_3.device, disttable_3.temp
Data node: db_dist_hypertable_3
Chunks: _dist_hyper_1_2_chunk, _dist_hyper_1_6_chunk
Remote SQL: SELECT "time", device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2])
(18 rows)
SELECT * FROM disttable;
time | device | temp
------------------------------+--------+------
Sun Jan 01 06:01:00 2017 PST | 1 | 1.1
Sun Jan 01 08:01:00 2017 PST | 1 | 1.2
Sun Jan 01 06:05:00 2017 PST | 1 | 1.4
Mon Jul 02 08:01:00 2018 PDT | 87 | 1.6
Mon Jul 02 09:01:00 2018 PDT | 87 | 1.4
Mon Jul 02 09:21:00 2018 PDT | 87 | 1.8
Mon Jan 02 08:01:00 2017 PST | 2 | 1.3
Mon Jan 02 09:01:00 2017 PST | 2 | 1.4
Mon Jan 02 08:21:00 2017 PST | 2 | 1.5
Sun Jul 01 06:01:00 2018 PDT | 13 | 1.4
Sun Jul 01 06:21:00 2018 PDT | 13 | 1.5
Sun Jul 01 07:01:00 2018 PDT | 13 | 1.4
Sun Jan 01 09:11:00 2017 PST | 3 | 2.1
Sun Jan 01 09:21:00 2017 PST | 3 | 2.2
Sun Jan 01 08:11:00 2017 PST | 3 | 2.3
Sun Jul 01 09:11:00 2018 PDT | 90 | 2.7
Sun Jul 01 08:01:00 2018 PDT | 29 | 1.5
Sun Jul 01 09:21:00 2018 PDT | 90 | 2.8
Sun Jul 01 08:21:00 2018 PDT | 29 | 1.2
(19 rows)
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT time_bucket('3 hours', time) AS time, device, avg(temp) AS avg_temp
FROM disttable GROUP BY 1, 2
ORDER BY 1;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GroupAggregate
Output: (time_bucket('@ 3 hours'::interval, disttable."time")), disttable.device, avg(disttable.temp)
Group Key: (time_bucket('@ 3 hours'::interval, disttable."time")), disttable.device
-> Custom Scan (AsyncAppend)
Output: (time_bucket('@ 3 hours'::interval, disttable."time")), disttable.device, disttable.temp
-> Merge Append
Sort Key: (time_bucket('@ 3 hours'::interval, disttable_1."time")), disttable_1.device
-> Result
Output: time_bucket('@ 3 hours'::interval, disttable_1."time"), disttable_1.device, disttable_1.temp
-> Custom Scan (DataNodeScan) on public.disttable disttable_1
Output: disttable_1."time", disttable_1.device, disttable_1.temp
Data node: db_dist_hypertable_1
Chunks: _dist_hyper_1_1_chunk, _dist_hyper_1_4_chunk
Remote SQL: SELECT "time", device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) ORDER BY public.time_bucket('03:00:00'::interval, "time") ASC NULLS LAST, device ASC NULLS LAST
-> Result
Output: time_bucket('@ 3 hours'::interval, disttable_2."time"), disttable_2.device, disttable_2.temp
-> Custom Scan (DataNodeScan) on public.disttable disttable_2
Output: disttable_2."time", disttable_2.device, disttable_2.temp
Data node: db_dist_hypertable_2
Chunks: _dist_hyper_1_3_chunk, _dist_hyper_1_5_chunk
Remote SQL: SELECT "time", device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) ORDER BY public.time_bucket('03:00:00'::interval, "time") ASC NULLS LAST, device ASC NULLS LAST
-> Result
Output: time_bucket('@ 3 hours'::interval, disttable_3."time"), disttable_3.device, disttable_3.temp
-> Custom Scan (DataNodeScan) on public.disttable disttable_3
Output: disttable_3."time", disttable_3.device, disttable_3.temp
Data node: db_dist_hypertable_3
Chunks: _dist_hyper_1_2_chunk, _dist_hyper_1_6_chunk
Remote SQL: SELECT "time", device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) ORDER BY public.time_bucket('03:00:00'::interval, "time") ASC NULLS LAST, device ASC NULLS LAST
(28 rows)
-- Execute some queries on the frontend and return the results
SELECT * FROM disttable;
time | device | temp
------------------------------+--------+------
Sun Jan 01 06:01:00 2017 PST | 1 | 1.1
Sun Jan 01 08:01:00 2017 PST | 1 | 1.2
Sun Jan 01 06:05:00 2017 PST | 1 | 1.4
Mon Jul 02 08:01:00 2018 PDT | 87 | 1.6
Mon Jul 02 09:01:00 2018 PDT | 87 | 1.4
Mon Jul 02 09:21:00 2018 PDT | 87 | 1.8
Mon Jan 02 08:01:00 2017 PST | 2 | 1.3
Mon Jan 02 09:01:00 2017 PST | 2 | 1.4
Mon Jan 02 08:21:00 2017 PST | 2 | 1.5
Sun Jul 01 06:01:00 2018 PDT | 13 | 1.4
Sun Jul 01 06:21:00 2018 PDT | 13 | 1.5
Sun Jul 01 07:01:00 2018 PDT | 13 | 1.4
Sun Jan 01 09:11:00 2017 PST | 3 | 2.1
Sun Jan 01 09:21:00 2017 PST | 3 | 2.2
Sun Jan 01 08:11:00 2017 PST | 3 | 2.3
Sun Jul 01 09:11:00 2018 PDT | 90 | 2.7
Sun Jul 01 08:01:00 2018 PDT | 29 | 1.5
Sun Jul 01 09:21:00 2018 PDT | 90 | 2.8
Sun Jul 01 08:21:00 2018 PDT | 29 | 1.2
(19 rows)
SELECT time_bucket('3 hours', time) AS time, device, avg(temp) AS avg_temp
FROM disttable
GROUP BY 1, 2
ORDER BY 1;
time | device | avg_temp
------------------------------+--------+------------------
Sun Jan 01 04:00:00 2017 PST | 1 | 1.25
Sun Jan 01 07:00:00 2017 PST | 1 | 1.2
Sun Jan 01 07:00:00 2017 PST | 3 | 2.2
Mon Jan 02 07:00:00 2017 PST | 2 | 1.4
Sun Jul 01 05:00:00 2018 PDT | 13 | 1.43333333333333
Sun Jul 01 08:00:00 2018 PDT | 29 | 1.35
Sun Jul 01 08:00:00 2018 PDT | 90 | 2.75
Mon Jul 02 08:00:00 2018 PDT | 87 | 1.6
(8 rows)
SELECT time_bucket('3 hours', time) AS time, device, avg(temp) AS avg_temp
FROM disttable GROUP BY 1, 2
HAVING avg(temp) > 1.2
ORDER BY 1;
time | device | avg_temp
------------------------------+--------+------------------
Sun Jan 01 04:00:00 2017 PST | 1 | 1.25
Sun Jan 01 07:00:00 2017 PST | 3 | 2.2
Mon Jan 02 07:00:00 2017 PST | 2 | 1.4
Sun Jul 01 05:00:00 2018 PDT | 13 | 1.43333333333333
Sun Jul 01 08:00:00 2018 PDT | 29 | 1.35
Sun Jul 01 08:00:00 2018 PDT | 90 | 2.75
Mon Jul 02 08:00:00 2018 PDT | 87 | 1.6
(7 rows)
SELECT time_bucket('3 hours', time) AS time, device, avg(temp) AS avg_temp
FROM disttable
WHERE temp > 2
GROUP BY 1, 2
HAVING avg(temp) > 1.2
ORDER BY 1;
time | device | avg_temp
------------------------------+--------+----------
Sun Jan 01 07:00:00 2017 PST | 3 | 2.2
Sun Jul 01 08:00:00 2018 PDT | 90 | 2.75
(2 rows)
-- Test AsyncAppend when using min/max aggregates
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT max(temp)
FROM disttable;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Result
Output: $0
InitPlan 1 (returns $0)
-> Limit
Output: disttable.temp
-> Custom Scan (AsyncAppend)
Output: disttable.temp
-> Merge Append
Sort Key: disttable_1.temp DESC
-> Custom Scan (DataNodeScan) on public.disttable disttable_1
Output: disttable_1.temp
Data node: db_dist_hypertable_1
Chunks: _dist_hyper_1_1_chunk, _dist_hyper_1_4_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) AND ((temp IS NOT NULL)) ORDER BY temp DESC NULLS FIRST LIMIT 1
-> Custom Scan (DataNodeScan) on public.disttable disttable_2
Output: disttable_2.temp
Data node: db_dist_hypertable_2
Chunks: _dist_hyper_1_3_chunk, _dist_hyper_1_5_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) AND ((temp IS NOT NULL)) ORDER BY temp DESC NULLS FIRST LIMIT 1
-> Custom Scan (DataNodeScan) on public.disttable disttable_3
Output: disttable_3.temp
Data node: db_dist_hypertable_3
Chunks: _dist_hyper_1_2_chunk, _dist_hyper_1_6_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) AND ((temp IS NOT NULL)) ORDER BY temp DESC NULLS FIRST LIMIT 1
(24 rows)
SELECT max(temp)
FROM disttable;
max
-----
2.8
(1 row)
-- Test turning off async append
SET timescaledb.enable_async_append = OFF;
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT max(temp)
FROM disttable;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Result
Output: $0
InitPlan 1 (returns $0)
-> Limit
Output: disttable_1.temp
-> Merge Append
Sort Key: disttable_1.temp DESC
-> Custom Scan (DataNodeScan) on public.disttable disttable_1
Output: disttable_1.temp
Data node: db_dist_hypertable_1
Chunks: _dist_hyper_1_1_chunk, _dist_hyper_1_4_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) AND ((temp IS NOT NULL)) ORDER BY temp DESC NULLS FIRST LIMIT 1
-> Custom Scan (DataNodeScan) on public.disttable disttable_2
Output: disttable_2.temp
Data node: db_dist_hypertable_2
Chunks: _dist_hyper_1_3_chunk, _dist_hyper_1_5_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) AND ((temp IS NOT NULL)) ORDER BY temp DESC NULLS FIRST LIMIT 1
-> Custom Scan (DataNodeScan) on public.disttable disttable_3
Output: disttable_3.temp
Data node: db_dist_hypertable_3
Chunks: _dist_hyper_1_2_chunk, _dist_hyper_1_6_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) AND ((temp IS NOT NULL)) ORDER BY temp DESC NULLS FIRST LIMIT 1
(22 rows)
SET timescaledb.enable_async_append = ON;
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT min(temp), max(temp)
FROM disttable;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------
Aggregate
Output: min(disttable.temp), max(disttable.temp)
-> Custom Scan (AsyncAppend)
Output: disttable.temp
-> Append
-> Custom Scan (DataNodeScan) on public.disttable disttable_1
Output: disttable_1.temp
Data node: db_dist_hypertable_1
Chunks: _dist_hyper_1_1_chunk, _dist_hyper_1_4_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2])
-> Custom Scan (DataNodeScan) on public.disttable disttable_2
Output: disttable_2.temp
Data node: db_dist_hypertable_2
Chunks: _dist_hyper_1_3_chunk, _dist_hyper_1_5_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2])
-> Custom Scan (DataNodeScan) on public.disttable disttable_3
Output: disttable_3.temp
Data node: db_dist_hypertable_3
Chunks: _dist_hyper_1_2_chunk, _dist_hyper_1_6_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2])
(20 rows)
SELECT min(temp), max(temp)
FROM disttable;
min | max
-----+-----
1.1 | 2.8
(1 row)
-- Test AsyncAppend when using window functions
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT device, temp, avg(temp) OVER (PARTITION BY device)
FROM disttable
ORDER BY device, temp;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Incremental Sort
Output: disttable_1.device, disttable_1.temp, (avg(disttable_1.temp) OVER (?))
Sort Key: disttable_1.device, disttable_1.temp
Presorted Key: disttable_1.device
-> WindowAgg
Output: disttable_1.device, disttable_1.temp, avg(disttable_1.temp) OVER (?)
-> Merge Append
Sort Key: disttable_1.device
-> Custom Scan (DataNodeScan) on public.disttable disttable_1
Output: disttable_1.device, disttable_1.temp
Data node: db_dist_hypertable_1
Chunks: _dist_hyper_1_1_chunk, _dist_hyper_1_4_chunk
Remote SQL: SELECT device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) ORDER BY device ASC NULLS LAST
-> Custom Scan (DataNodeScan) on public.disttable disttable_2
Output: disttable_2.device, disttable_2.temp
Data node: db_dist_hypertable_2
Chunks: _dist_hyper_1_3_chunk, _dist_hyper_1_5_chunk
Remote SQL: SELECT device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) ORDER BY device ASC NULLS LAST
-> Custom Scan (DataNodeScan) on public.disttable disttable_3
Output: disttable_3.device, disttable_3.temp
Data node: db_dist_hypertable_3
Chunks: _dist_hyper_1_2_chunk, _dist_hyper_1_6_chunk
Remote SQL: SELECT device, temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) ORDER BY device ASC NULLS LAST
(23 rows)
SELECT device, temp, avg(temp) OVER (PARTITION BY device)
FROM disttable
ORDER BY device, temp;
device | temp | avg
--------+------+------------------
1 | 1.1 | 1.23333333333333
1 | 1.2 | 1.23333333333333
1 | 1.4 | 1.23333333333333
2 | 1.3 | 1.4
2 | 1.4 | 1.4
2 | 1.5 | 1.4
3 | 2.1 | 2.2
3 | 2.2 | 2.2
3 | 2.3 | 2.2
13 | 1.4 | 1.43333333333333
13 | 1.4 | 1.43333333333333
13 | 1.5 | 1.43333333333333
29 | 1.2 | 1.35
29 | 1.5 | 1.35
87 | 1.4 | 1.6
87 | 1.6 | 1.6
87 | 1.8 | 1.6
90 | 2.7 | 2.75
90 | 2.8 | 2.75
(19 rows)
-- Test remote explain
-- Make sure that chunks_in function only expects one-dimensional integer arrays
\set ON_ERROR_STOP 0
SELECT "time" FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[[2], [1]])
ORDER BY "time" DESC NULLS FIRST LIMIT 1;
ERROR: invalid number of array dimensions for chunks_in
\set ON_ERROR_STOP 1
SET timescaledb.enable_remote_explain = ON;
-- Check that datanodes use ChunkAppend plans with chunks_in function in the
-- "Remote SQL" when using max(time).
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT max(time)
FROM disttable;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Result
Output: $0
InitPlan 1 (returns $0)
-> Limit
Output: disttable."time"
-> Custom Scan (AsyncAppend)
Output: disttable."time"
-> Merge Append
Sort Key: disttable_1."time" DESC
-> Custom Scan (DataNodeScan) on public.disttable disttable_1
Output: disttable_1."time"
Data node: db_dist_hypertable_1
Chunks: _dist_hyper_1_4_chunk, _dist_hyper_1_1_chunk
Remote SQL: SELECT "time" FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[2, 1]) AND (("time" IS NOT NULL)) ORDER BY "time" DESC NULLS FIRST LIMIT 1
Remote EXPLAIN:
Limit
Output: disttable."time"
-> Custom Scan (ChunkAppend) on public.disttable
Output: disttable."time"
Order: disttable."time" DESC
Startup Exclusion: false
Runtime Exclusion: false
-> Index Only Scan using _dist_hyper_1_4_chunk_disttable_time_idx on _timescaledb_internal._dist_hyper_1_4_chunk
Output: _dist_hyper_1_4_chunk."time"
Index Cond: (_dist_hyper_1_4_chunk."time" IS NOT NULL)
-> Index Only Scan using _dist_hyper_1_1_chunk_disttable_time_idx on _timescaledb_internal._dist_hyper_1_1_chunk
Output: _dist_hyper_1_1_chunk."time"
Index Cond: (_dist_hyper_1_1_chunk."time" IS NOT NULL)
-> Custom Scan (DataNodeScan) on public.disttable disttable_2
Output: disttable_2."time"
Data node: db_dist_hypertable_2
Chunks: _dist_hyper_1_5_chunk, _dist_hyper_1_3_chunk
Remote SQL: SELECT "time" FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[2, 1]) AND (("time" IS NOT NULL)) ORDER BY "time" DESC NULLS FIRST LIMIT 1
Remote EXPLAIN:
Limit
Output: disttable."time"
-> Custom Scan (ChunkAppend) on public.disttable
Output: disttable."time"
Order: disttable."time" DESC
Startup Exclusion: false
Runtime Exclusion: false
-> Index Only Scan using _dist_hyper_1_5_chunk_disttable_time_idx on _timescaledb_internal._dist_hyper_1_5_chunk
Output: _dist_hyper_1_5_chunk."time"
Index Cond: (_dist_hyper_1_5_chunk."time" IS NOT NULL)
-> Index Only Scan using _dist_hyper_1_3_chunk_disttable_time_idx on _timescaledb_internal._dist_hyper_1_3_chunk
Output: _dist_hyper_1_3_chunk."time"
Index Cond: (_dist_hyper_1_3_chunk."time" IS NOT NULL)
-> Custom Scan (DataNodeScan) on public.disttable disttable_3
Output: disttable_3."time"
Data node: db_dist_hypertable_3
Chunks: _dist_hyper_1_6_chunk, _dist_hyper_1_2_chunk
Remote SQL: SELECT "time" FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[2, 1]) AND (("time" IS NOT NULL)) ORDER BY "time" DESC NULLS FIRST LIMIT 1
Remote EXPLAIN:
Limit
Output: disttable."time"
-> Custom Scan (ChunkAppend) on public.disttable
Output: disttable."time"
Order: disttable."time" DESC
Startup Exclusion: false
Runtime Exclusion: false
-> Index Only Scan using _dist_hyper_1_6_chunk_disttable_time_idx on _timescaledb_internal._dist_hyper_1_6_chunk
Output: _dist_hyper_1_6_chunk."time"
Index Cond: (_dist_hyper_1_6_chunk."time" IS NOT NULL)
-> Index Only Scan using _dist_hyper_1_2_chunk_disttable_time_idx on _timescaledb_internal._dist_hyper_1_2_chunk
Output: _dist_hyper_1_2_chunk."time"
Index Cond: (_dist_hyper_1_2_chunk."time" IS NOT NULL)
(69 rows)
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT max(temp)
FROM disttable;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Result
Output: $0
InitPlan 1 (returns $0)
-> Limit
Output: disttable.temp
-> Custom Scan (AsyncAppend)
Output: disttable.temp
-> Merge Append
Sort Key: disttable_1.temp DESC
-> Custom Scan (DataNodeScan) on public.disttable disttable_1
Output: disttable_1.temp
Data node: db_dist_hypertable_1
Chunks: _dist_hyper_1_1_chunk, _dist_hyper_1_4_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) AND ((temp IS NOT NULL)) ORDER BY temp DESC NULLS FIRST LIMIT 1
Remote EXPLAIN:
Limit
Output: _dist_hyper_1_1_chunk.temp
-> Sort
Output: _dist_hyper_1_1_chunk.temp
Sort Key: _dist_hyper_1_1_chunk.temp DESC
-> Append
-> Seq Scan on _timescaledb_internal._dist_hyper_1_1_chunk
Output: _dist_hyper_1_1_chunk.temp
Filter: (_dist_hyper_1_1_chunk.temp IS NOT NULL)
-> Seq Scan on _timescaledb_internal._dist_hyper_1_4_chunk
Output: _dist_hyper_1_4_chunk.temp
Filter: (_dist_hyper_1_4_chunk.temp IS NOT NULL)
-> Custom Scan (DataNodeScan) on public.disttable disttable_2
Output: disttable_2.temp
Data node: db_dist_hypertable_2
Chunks: _dist_hyper_1_3_chunk, _dist_hyper_1_5_chunk
Remote SQL: SELECT temp FROM public.disttable WHERE _timescaledb_internal.chunks_in(public.disttable.*, ARRAY[1, 2]) AND ((temp IS NOT NULL)) ORDER BY temp DESC NULLS FIRST LIMIT 1
Remote EXPLAIN:
Limit