-
Notifications
You must be signed in to change notification settings - Fork 848
/
compression.out
2706 lines (2522 loc) · 153 KB
/
compression.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 timescaledb.enable_transparent_decompression to OFF;
\set PREFIX 'EXPLAIN (analyze, verbose, costs off, timing off, summary off)'
\ir include/rand_generator.sql
-- 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.
--------------------------
-- cheap rand generator --
--------------------------
create table rand_minstd_state(i bigint);
create function rand_minstd_advance(bigint) returns bigint
language sql immutable as
$$
select (16807 * $1) % 2147483647
$$;
create function gen_rand_minstd() returns bigint
language sql security definer as
$$
update rand_minstd_state set i = rand_minstd_advance(i) returning i
$$;
-- seed the random num generator
insert into rand_minstd_state values (321);
--test_collation ---
--basic test with count
create table foo (a integer, b integer, c integer, d integer);
select table_name from create_hypertable('foo', 'a', chunk_time_interval=> 10);
NOTICE: adding not-null constraint to column "a"
table_name
------------
foo
(1 row)
create unique index foo_uniq ON foo (a, b);
--note that the "d" order by column is all NULL
insert into foo values( 3 , 16 , 20, NULL);
insert into foo values( 10 , 10 , 20, NULL);
insert into foo values( 20 , 11 , 20, NULL);
insert into foo values( 30 , 12 , 20, NULL);
analyze foo;
-- check that approximate_row_count works with a regular table
SELECT approximate_row_count('foo');
approximate_row_count
-----------------------
4
(1 row)
SELECT count(*) from foo;
count
-------
4
(1 row)
alter table foo set (timescaledb.compress, timescaledb.compress_segmentby = 'a,b', timescaledb.compress_orderby = 'c desc, d asc nulls last');
--test self-refencing updates
SET timescaledb.enable_transparent_decompression to ON;
update foo set c = 40
where a = (SELECT max(a) FROM foo);
SET timescaledb.enable_transparent_decompression to OFF;
select id, schema_name, table_name, compression_state as compressed, compressed_hypertable_id from
_timescaledb_catalog.hypertable order by id;
id | schema_name | table_name | compressed | compressed_hypertable_id
----+-----------------------+--------------------------+------------+--------------------------
1 | public | foo | 1 | 2
2 | _timescaledb_internal | _compressed_hypertable_2 | 2 |
(2 rows)
select * from _timescaledb_catalog.hypertable_compression order by hypertable_id, attname;
hypertable_id | attname | compression_algorithm_id | segmentby_column_index | orderby_column_index | orderby_asc | orderby_nullsfirst
---------------+---------+--------------------------+------------------------+----------------------+-------------+--------------------
1 | a | 0 | 1 | | |
1 | b | 0 | 2 | | |
1 | c | 4 | | 1 | f | t
1 | d | 4 | | 2 | t | f
(4 rows)
select * from timescaledb_information.compression_settings ORDER BY hypertable_name;
hypertable_schema | hypertable_name | attname | segmentby_column_index | orderby_column_index | orderby_asc | orderby_nullsfirst
-------------------+-----------------+---------+------------------------+----------------------+-------------+--------------------
public | foo | a | 1 | | |
public | foo | b | 2 | | |
public | foo | c | | 1 | f | t
public | foo | d | | 2 | t | f
(4 rows)
-- TEST2 compress-chunk for the chunks created earlier --
select compress_chunk( '_timescaledb_internal._hyper_1_2_chunk');
compress_chunk
----------------------------------------
_timescaledb_internal._hyper_1_2_chunk
(1 row)
select tgname , tgtype, tgenabled , relname
from pg_trigger t, pg_class rel
where t.tgrelid = rel.oid and rel.relname like '_hyper_1_2_chunk' order by tgname;
tgname | tgtype | tgenabled | relname
--------+--------+-----------+---------
(0 rows)
\x
select * from chunk_compression_stats('foo')
order by chunk_name limit 2;
-[ RECORD 1 ]------------------+----------------------
chunk_schema | _timescaledb_internal
chunk_name | _hyper_1_1_chunk
compression_status | Uncompressed
before_compression_table_bytes |
before_compression_index_bytes |
before_compression_toast_bytes |
before_compression_total_bytes |
after_compression_table_bytes |
after_compression_index_bytes |
after_compression_toast_bytes |
after_compression_total_bytes |
node_name |
-[ RECORD 2 ]------------------+----------------------
chunk_schema | _timescaledb_internal
chunk_name | _hyper_1_2_chunk
compression_status | Compressed
before_compression_table_bytes | 8192
before_compression_index_bytes | 32768
before_compression_toast_bytes | 0
before_compression_total_bytes | 40960
after_compression_table_bytes | 16384
after_compression_index_bytes | 16384
after_compression_toast_bytes | 8192
after_compression_total_bytes | 40960
node_name |
\x
select compress_chunk( '_timescaledb_internal._hyper_1_1_chunk');
compress_chunk
----------------------------------------
_timescaledb_internal._hyper_1_1_chunk
(1 row)
\x
select * from _timescaledb_catalog.compression_chunk_size
order by chunk_id;
-[ RECORD 1 ]--------------+------
chunk_id | 1
compressed_chunk_id | 6
uncompressed_heap_size | 8192
uncompressed_toast_size | 0
uncompressed_index_size | 32768
compressed_heap_size | 16384
compressed_toast_size | 8192
compressed_index_size | 16384
numrows_pre_compression | 1
numrows_post_compression | 1
numrows_frozen_immediately | 1
-[ RECORD 2 ]--------------+------
chunk_id | 2
compressed_chunk_id | 5
uncompressed_heap_size | 8192
uncompressed_toast_size | 0
uncompressed_index_size | 32768
compressed_heap_size | 16384
compressed_toast_size | 8192
compressed_index_size | 16384
numrows_pre_compression | 1
numrows_post_compression | 1
numrows_frozen_immediately | 1
\x
select ch1.id, ch1.schema_name, ch1.table_name , ch2.table_name as compress_table
from
_timescaledb_catalog.chunk ch1, _timescaledb_catalog.chunk ch2
where ch1.compressed_chunk_id = ch2.id;
id | schema_name | table_name | compress_table
----+-----------------------+------------------+--------------------------
2 | _timescaledb_internal | _hyper_1_2_chunk | compress_hyper_2_5_chunk
1 | _timescaledb_internal | _hyper_1_1_chunk | compress_hyper_2_6_chunk
(2 rows)
\set ON_ERROR_STOP 0
--cannot recompress the chunk the second time around
select compress_chunk( '_timescaledb_internal._hyper_1_2_chunk');
ERROR: chunk "_hyper_1_2_chunk" is already compressed
--TEST2a try DML on a compressed chunk
BEGIN;
insert into foo values( 11 , 10 , 20, 120);
ROLLBACK;
update foo set b =20 where a = 10;
delete from foo where a = 10;
--TEST2b try complex DML on compressed chunk
create table foo_join ( a integer, newval integer);
select table_name from create_hypertable('foo_join', 'a', chunk_time_interval=> 10);
NOTICE: adding not-null constraint to column "a"
table_name
------------
foo_join
(1 row)
insert into foo_join select generate_series(0,40, 10), 111;
create table foo_join2 ( a integer, newval integer);
select table_name from create_hypertable('foo_join2', 'a', chunk_time_interval=> 10);
NOTICE: adding not-null constraint to column "a"
table_name
------------
foo_join2
(1 row)
insert into foo_join select generate_series(0,40, 10), 222;
update foo
set b = newval
from foo_join where foo.a = foo_join.a;
update foo
set b = newval
from foo_join where foo.a = foo_join.a and foo_join.a > 10;
--here the chunk gets excluded , so succeeds --
update foo
set b = newval
from foo_join where foo.a = foo_join.a and foo.a > 20;
update foo
set b = (select f1.newval from foo_join f1 left join lateral (select newval as newval2 from foo_join2 f2 where f1.a= f2.a ) subq on true limit 1);
--upsert test --
insert into foo values(10, 12, 12, 12)
on conflict( a, b)
do update set b = excluded.b;
SELECT * from foo ORDER BY a,b;
a | b | c | d
----+-----+----+----
3 | 111 | 20 |
10 | 12 | 12 | 12
20 | 111 | 20 |
30 | 111 | 40 |
(4 rows)
--TEST2c Do DML directly on the chunk.
insert into _timescaledb_internal._hyper_1_2_chunk values(10, 12, 12, 12)
on conflict( a, b)
do update set b = excluded.b + 12;
SELECT * from foo ORDER BY a,b;
a | b | c | d
----+-----+----+----
3 | 111 | 20 |
10 | 24 | 12 | 12
20 | 111 | 20 |
30 | 111 | 40 |
(4 rows)
update _timescaledb_internal._hyper_1_2_chunk
set b = 12;
delete from _timescaledb_internal._hyper_1_2_chunk;
--TEST2d decompress the chunk and try DML
select decompress_chunk( '_timescaledb_internal._hyper_1_2_chunk');
decompress_chunk
----------------------------------------
_timescaledb_internal._hyper_1_2_chunk
(1 row)
insert into foo values( 11 , 10 , 20, 120);
update foo set b =20 where a = 10;
select * from _timescaledb_internal._hyper_1_2_chunk order by a,b;
a | b | c | d
----+----+----+-----
11 | 10 | 20 | 120
(1 row)
delete from foo where a = 10;
select * from _timescaledb_internal._hyper_1_2_chunk order by a,b;
a | b | c | d
----+----+----+-----
11 | 10 | 20 | 120
(1 row)
-- TEST3 check if compress data from views is accurate
CREATE TABLE conditions (
time TIMESTAMPTZ NOT NULL,
location TEXT NOT NULL,
location2 char(10) NOT NULL,
temperature DOUBLE PRECISION NULL,
humidity DOUBLE PRECISION NULL
);
select create_hypertable( 'conditions', 'time', chunk_time_interval=> '31days'::interval);
create_hypertable
-------------------------
(5,public,conditions,t)
(1 row)
alter table conditions set (timescaledb.compress, timescaledb.compress_segmentby = 'location', timescaledb.compress_orderby = 'time');
insert into conditions
select generate_series('2018-12-01 00:00'::timestamp, '2018-12-31 00:00'::timestamp, '1 day'), 'POR', 'klick', 55, 75;
insert into conditions
select generate_series('2018-12-01 00:00'::timestamp, '2018-12-31 00:00'::timestamp, '1 day'), 'NYC', 'klick', 55, 75;
select hypertable_id, attname, compression_algorithm_id , al.name
from _timescaledb_catalog.hypertable_compression hc,
_timescaledb_catalog.hypertable ht,
_timescaledb_catalog.compression_algorithm al
where ht.id = hc.hypertable_id and ht.table_name like 'conditions' and al.id = hc.compression_algorithm_id
ORDER BY hypertable_id, attname;
hypertable_id | attname | compression_algorithm_id | name
---------------+-------------+--------------------------+----------------------------------
5 | humidity | 3 | COMPRESSION_ALGORITHM_GORILLA
5 | location | 0 | COMPRESSION_ALGORITHM_NONE
5 | location2 | 2 | COMPRESSION_ALGORITHM_DICTIONARY
5 | temperature | 3 | COMPRESSION_ALGORITHM_GORILLA
5 | time | 4 | COMPRESSION_ALGORITHM_DELTADELTA
(5 rows)
select attname, attstorage, typname from pg_attribute at, pg_class cl , pg_type ty
where cl.oid = at.attrelid and at.attnum > 0
and cl.relname = '_compressed_hypertable_4'
and atttypid = ty.oid
order by at.attnum;
attname | attstorage | typname
---------+------------+---------
(0 rows)
SELECT ch1.schema_name|| '.' || ch1.table_name as "CHUNK_NAME", ch1.id "CHUNK_ID"
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.hypertable ht where ch1.hypertable_id = ht.id and ht.table_name like 'conditions'
ORDER BY ch1.id
LIMIT 1 \gset
SELECT count(*) from :CHUNK_NAME;
count
-------
42
(1 row)
SELECT count(*) as "ORIGINAL_CHUNK_COUNT" from :CHUNK_NAME \gset
select tableoid::regclass, count(*) from conditions group by tableoid order by tableoid;
tableoid | count
-----------------------------------------+-------
_timescaledb_internal._hyper_5_12_chunk | 42
_timescaledb_internal._hyper_5_13_chunk | 20
(2 rows)
SELECT compress_chunk(ch) FROM show_chunks('conditions') ch LIMIT 1;
compress_chunk
-----------------------------------------
_timescaledb_internal._hyper_5_12_chunk
(1 row)
--test that only one chunk was affected
--note tables with 0 rows will not show up in here.
select tableoid::regclass, count(*) from conditions group by tableoid order by tableoid;
tableoid | count
-----------------------------------------+-------
_timescaledb_internal._hyper_5_13_chunk | 20
(1 row)
SELECT compress_chunk(ch, true) FROM show_chunks('conditions') ch ORDER BY ch::text DESC LIMIT 1;
compress_chunk
-----------------------------------------
_timescaledb_internal._hyper_5_13_chunk
(1 row)
select tableoid::regclass, count(*) from conditions group by tableoid order by tableoid;
tableoid | count
----------+-------
(0 rows)
select compressed.schema_name|| '.' || compressed.table_name as "COMPRESSED_CHUNK_NAME"
from _timescaledb_catalog.chunk uncompressed, _timescaledb_catalog.chunk compressed
where uncompressed.compressed_chunk_id = compressed.id AND uncompressed.id = :'CHUNK_ID' \gset
SELECT count(*) from :CHUNK_NAME;
count
-------
0
(1 row)
SELECT count(*) from :COMPRESSED_CHUNK_NAME;
count
-------
2
(1 row)
SELECT sum(_ts_meta_count) from :COMPRESSED_CHUNK_NAME;
sum
-----
42
(1 row)
SELECT location, _ts_meta_sequence_num from :COMPRESSED_CHUNK_NAME ORDER BY 1,2;
location | _ts_meta_sequence_num
----------+-----------------------
NYC | 10
POR | 10
(2 rows)
\x
SELECT chunk_id, numrows_pre_compression, numrows_post_compression
FROM _timescaledb_catalog.chunk srcch,
_timescaledb_catalog.compression_chunk_size map,
_timescaledb_catalog.hypertable srcht
WHERE map.chunk_id = srcch.id and srcht.id = srcch.hypertable_id
and srcht.table_name like 'conditions'
order by chunk_id;
-[ RECORD 1 ]------------+---
chunk_id | 12
numrows_pre_compression | 42
numrows_post_compression | 2
-[ RECORD 2 ]------------+---
chunk_id | 13
numrows_pre_compression | 20
numrows_post_compression | 2
select * from chunk_compression_stats('conditions')
order by chunk_name;
-[ RECORD 1 ]------------------+----------------------
chunk_schema | _timescaledb_internal
chunk_name | _hyper_5_12_chunk
compression_status | Compressed
before_compression_table_bytes | 8192
before_compression_index_bytes | 16384
before_compression_toast_bytes | 8192
before_compression_total_bytes | 32768
after_compression_table_bytes | 16384
after_compression_index_bytes | 16384
after_compression_toast_bytes | 8192
after_compression_total_bytes | 40960
node_name |
-[ RECORD 2 ]------------------+----------------------
chunk_schema | _timescaledb_internal
chunk_name | _hyper_5_13_chunk
compression_status | Compressed
before_compression_table_bytes | 8192
before_compression_index_bytes | 16384
before_compression_toast_bytes | 8192
before_compression_total_bytes | 32768
after_compression_table_bytes | 16384
after_compression_index_bytes | 16384
after_compression_toast_bytes | 8192
after_compression_total_bytes | 40960
node_name |
select * from hypertable_compression_stats('foo');
-[ RECORD 1 ]------------------+------
total_chunks | 4
number_compressed_chunks | 1
before_compression_table_bytes | 8192
before_compression_index_bytes | 32768
before_compression_toast_bytes | 0
before_compression_total_bytes | 40960
after_compression_table_bytes | 16384
after_compression_index_bytes | 16384
after_compression_toast_bytes | 8192
after_compression_total_bytes | 40960
node_name |
select * from hypertable_compression_stats('conditions');
-[ RECORD 1 ]------------------+------
total_chunks | 2
number_compressed_chunks | 2
before_compression_table_bytes | 16384
before_compression_index_bytes | 32768
before_compression_toast_bytes | 16384
before_compression_total_bytes | 65536
after_compression_table_bytes | 32768
after_compression_index_bytes | 32768
after_compression_toast_bytes | 16384
after_compression_total_bytes | 81920
node_name |
vacuum full foo;
vacuum full conditions;
-- After vacuum, table_bytes is 0, but any associated index/toast storage is not
-- completely reclaimed. Sets it at 8K (page size). So a chunk which has
-- been compressed still incurs an overhead of n * 8KB (for every index + toast table) storage on the original uncompressed chunk.
select pg_size_pretty(table_bytes), pg_size_pretty(index_bytes),
pg_size_pretty(toast_bytes), pg_size_pretty(total_bytes)
from hypertable_detailed_size('conditions');
-[ RECORD 1 ]--+-------
pg_size_pretty | 16 kB
pg_size_pretty | 56 kB
pg_size_pretty | 40 kB
pg_size_pretty | 112 kB
select * from timescaledb_information.hypertables
where hypertable_name like 'foo' or hypertable_name like 'conditions'
order by hypertable_name;
-[ RECORD 1 ]-------+------------------
hypertable_schema | public
hypertable_name | conditions
owner | default_perm_user
num_dimensions | 1
num_chunks | 2
compression_enabled | t
is_distributed | f
replication_factor |
data_nodes |
tablespaces |
-[ RECORD 2 ]-------+------------------
hypertable_schema | public
hypertable_name | foo
owner | default_perm_user
num_dimensions | 1
num_chunks | 4
compression_enabled | t
is_distributed | f
replication_factor |
data_nodes |
tablespaces |
\x
SELECT count(decompress_chunk(ch)) FROM show_chunks('conditions') ch;
count
-------
2
(1 row)
SELECT count(*), count(*) = :'ORIGINAL_CHUNK_COUNT' from :CHUNK_NAME;
count | ?column?
-------+----------
42 | t
(1 row)
--check that the compressed chunk is dropped
\set ON_ERROR_STOP 0
SELECT count(*) from :COMPRESSED_CHUNK_NAME;
ERROR: relation "_timescaledb_internal.compress_hyper_6_14_chunk" does not exist at character 22
\set ON_ERROR_STOP 1
--size information is gone too
select count(*)
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.hypertable ht,
_timescaledb_catalog.compression_chunk_size map
where ch1.hypertable_id = ht.id and ht.table_name like 'conditions'
and map.chunk_id = ch1.id;
count
-------
0
(1 row)
--make sure compressed_chunk_id is reset to NULL
select ch1.compressed_chunk_id IS NULL
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.hypertable ht where ch1.hypertable_id = ht.id and ht.table_name like 'conditions';
?column?
----------
t
t
(2 rows)
-- test plans get invalidated when chunks get compressed
SET timescaledb.enable_transparent_decompression TO ON;
CREATE TABLE plan_inval(time timestamptz, device_id int);
SELECT create_hypertable('plan_inval','time');
NOTICE: adding not-null constraint to column "time"
create_hypertable
-------------------------
(7,public,plan_inval,t)
(1 row)
ALTER TABLE plan_inval SET (timescaledb.compress,timescaledb.compress_orderby='time desc');
-- create 2 chunks
INSERT INTO plan_inval SELECT * FROM (VALUES ('2000-01-01'::timestamptz,1), ('2000-01-07'::timestamptz,1)) v(time,device_id);
SET max_parallel_workers_per_gather to 0;
-- Disable hash aggregation to get a deterministic test output
SET enable_hashagg = OFF;
PREPARE prep_plan AS SELECT count(*) FROM plan_inval;
EXECUTE prep_plan;
count
-------
2
(1 row)
EXECUTE prep_plan;
count
-------
2
(1 row)
EXECUTE prep_plan;
count
-------
2
(1 row)
-- get name of first chunk
SELECT tableoid::regclass AS "CHUNK_NAME" FROM plan_inval ORDER BY time LIMIT 1
\gset
SELECT compress_chunk(:'CHUNK_NAME');
compress_chunk
-----------------------------------------
_timescaledb_internal._hyper_7_16_chunk
(1 row)
EXECUTE prep_plan;
count
-------
2
(1 row)
EXPLAIN (COSTS OFF) EXECUTE prep_plan;
QUERY PLAN
----------------------------------------------------------------------
Finalize Aggregate
-> Append
-> Partial Aggregate
-> Custom Scan (DecompressChunk) on _hyper_7_16_chunk
-> Seq Scan on compress_hyper_8_18_chunk
-> Partial Aggregate
-> Seq Scan on _hyper_7_17_chunk
(7 rows)
SET enable_hashagg = ON;
CREATE TABLE test_collation (
time TIMESTAMPTZ NOT NULL,
device_id TEXT COLLATE "C" NULL,
device_id_2 TEXT COLLATE "POSIX" NULL,
val_1 TEXT COLLATE "C" NULL,
val_2 TEXT COLLATE "POSIX" NULL
);
--we want all the data to go into 1 chunk. so use 1 year chunk interval
select create_hypertable( 'test_collation', 'time', chunk_time_interval=> '1 day'::interval);
create_hypertable
-----------------------------
(9,public,test_collation,t)
(1 row)
\set ON_ERROR_STOP 0
--forbid setting collation in compression ORDER BY clause. (parse error is fine)
alter table test_collation set (timescaledb.compress, timescaledb.compress_segmentby='device_id, device_id_2', timescaledb.compress_orderby = 'val_1 COLLATE "POSIX", val2, time');
ERROR: unable to parse ordering option "val_1 COLLATE "POSIX", val2, time"
\set ON_ERROR_STOP 1
alter table test_collation set (timescaledb.compress, timescaledb.compress_segmentby='device_id, device_id_2', timescaledb.compress_orderby = 'val_1, val_2, time');
insert into test_collation
select generate_series('2018-01-01 00:00'::timestamp, '2018-01-10 00:00'::timestamp, '2 hour'), 'device_1', 'device_3', gen_rand_minstd(), gen_rand_minstd();
insert into test_collation
select generate_series('2018-01-01 00:00'::timestamp, '2018-01-10 00:00'::timestamp, '2 hour'), 'device_2', 'device_4', gen_rand_minstd(), gen_rand_minstd();
insert into test_collation
select generate_series('2018-01-01 00:00'::timestamp, '2018-01-10 00:00'::timestamp, '2 hour'), NULL, 'device_5', gen_rand_minstd(), gen_rand_minstd();
--compress 2 chunks
SELECT compress_chunk(ch) FROM show_chunks('test_collation') ch LIMIT 2;
compress_chunk
-----------------------------------------
_timescaledb_internal._hyper_9_19_chunk
_timescaledb_internal._hyper_9_20_chunk
(2 rows)
CREATE OR REPLACE PROCEDURE reindex_compressed_hypertable(hypertable REGCLASS)
AS $$
DECLARE
hyper_id int;
BEGIN
SELECT h.compressed_hypertable_id
INTO hyper_id
FROM _timescaledb_catalog.hypertable h
WHERE h.table_name = hypertable::name;
EXECUTE format('REINDEX TABLE _timescaledb_internal._compressed_hypertable_%s',
hyper_id);
END $$ LANGUAGE plpgsql;
-- reindexing compressed hypertable to update statistics
CALL reindex_compressed_hypertable('test_collation');
--segment bys are pushed down correctly
EXPLAIN (costs off) SELECT * FROM test_collation WHERE device_id < 'a';
QUERY PLAN
----------------------------------------------------------
Append
-> Custom Scan (DecompressChunk) on _hyper_9_19_chunk
-> Seq Scan on compress_hyper_10_29_chunk
Filter: (device_id < 'a'::text)
-> Custom Scan (DecompressChunk) on _hyper_9_20_chunk
-> Seq Scan on compress_hyper_10_30_chunk
Filter: (device_id < 'a'::text)
-> Seq Scan on _hyper_9_21_chunk
Filter: (device_id < 'a'::text)
-> Seq Scan on _hyper_9_22_chunk
Filter: (device_id < 'a'::text)
-> Seq Scan on _hyper_9_23_chunk
Filter: (device_id < 'a'::text)
-> Seq Scan on _hyper_9_24_chunk
Filter: (device_id < 'a'::text)
-> Seq Scan on _hyper_9_25_chunk
Filter: (device_id < 'a'::text)
-> Seq Scan on _hyper_9_26_chunk
Filter: (device_id < 'a'::text)
-> Seq Scan on _hyper_9_27_chunk
Filter: (device_id < 'a'::text)
-> Seq Scan on _hyper_9_28_chunk
Filter: (device_id < 'a'::text)
(23 rows)
EXPLAIN (costs off) SELECT * FROM test_collation WHERE device_id < 'a' COLLATE "POSIX";
QUERY PLAN
---------------------------------------------------------------
Append
-> Custom Scan (DecompressChunk) on _hyper_9_19_chunk
-> Seq Scan on compress_hyper_10_29_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Custom Scan (DecompressChunk) on _hyper_9_20_chunk
-> Seq Scan on compress_hyper_10_30_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_21_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_22_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_23_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_24_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_25_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_26_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_27_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_28_chunk
Filter: (device_id < 'a'::text COLLATE "POSIX")
(23 rows)
\set ON_ERROR_STOP 0
EXPLAIN (costs off) SELECT * FROM test_collation WHERE device_id COLLATE "POSIX" < device_id_2 COLLATE "C";
ERROR: collation mismatch between explicit collations "POSIX" and "C" at character 96
SELECT device_id < device_id_2 FROM test_collation;
ERROR: could not determine which collation to use for string comparison
\set ON_ERROR_STOP 1
--segment meta on order bys pushdown
--should work
EXPLAIN (costs off) SELECT * FROM test_collation WHERE val_1 < 'a';
QUERY PLAN
----------------------------------------------------------
Append
-> Custom Scan (DecompressChunk) on _hyper_9_19_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on compress_hyper_10_29_chunk
Filter: (_ts_meta_min_1 < 'a'::text)
-> Custom Scan (DecompressChunk) on _hyper_9_20_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on compress_hyper_10_30_chunk
Filter: (_ts_meta_min_1 < 'a'::text)
-> Seq Scan on _hyper_9_21_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on _hyper_9_22_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on _hyper_9_23_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on _hyper_9_24_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on _hyper_9_25_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on _hyper_9_26_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on _hyper_9_27_chunk
Filter: (val_1 < 'a'::text)
-> Seq Scan on _hyper_9_28_chunk
Filter: (val_1 < 'a'::text)
(25 rows)
EXPLAIN (costs off) SELECT * FROM test_collation WHERE val_2 < 'a';
QUERY PLAN
----------------------------------------------------------
Append
-> Custom Scan (DecompressChunk) on _hyper_9_19_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on compress_hyper_10_29_chunk
Filter: (_ts_meta_min_2 < 'a'::text)
-> Custom Scan (DecompressChunk) on _hyper_9_20_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on compress_hyper_10_30_chunk
Filter: (_ts_meta_min_2 < 'a'::text)
-> Seq Scan on _hyper_9_21_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on _hyper_9_22_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on _hyper_9_23_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on _hyper_9_24_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on _hyper_9_25_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on _hyper_9_26_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on _hyper_9_27_chunk
Filter: (val_2 < 'a'::text)
-> Seq Scan on _hyper_9_28_chunk
Filter: (val_2 < 'a'::text)
(25 rows)
EXPLAIN (costs off) SELECT * FROM test_collation WHERE val_1 < 'a' COLLATE "C";
QUERY PLAN
----------------------------------------------------------------
Append
-> Custom Scan (DecompressChunk) on _hyper_9_19_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on compress_hyper_10_29_chunk
Filter: (_ts_meta_min_1 < 'a'::text COLLATE "C")
-> Custom Scan (DecompressChunk) on _hyper_9_20_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on compress_hyper_10_30_chunk
Filter: (_ts_meta_min_1 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_21_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_22_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_23_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_24_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_25_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_26_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_27_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_28_chunk
Filter: (val_1 < 'a'::text COLLATE "C")
(25 rows)
EXPLAIN (costs off) SELECT * FROM test_collation WHERE val_2 < 'a' COLLATE "POSIX";
QUERY PLAN
--------------------------------------------------------------------
Append
-> Custom Scan (DecompressChunk) on _hyper_9_19_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on compress_hyper_10_29_chunk
Filter: (_ts_meta_min_2 < 'a'::text COLLATE "POSIX")
-> Custom Scan (DecompressChunk) on _hyper_9_20_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on compress_hyper_10_30_chunk
Filter: (_ts_meta_min_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_21_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_22_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_23_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_24_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_25_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_26_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_27_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_28_chunk
Filter: (val_2 < 'a'::text COLLATE "POSIX")
(25 rows)
--cannot pushdown when op collation does not match column's collation since min/max used different collation than what op needs
EXPLAIN (costs off) SELECT * FROM test_collation WHERE val_1 < 'a' COLLATE "POSIX";
QUERY PLAN
----------------------------------------------------------
Append
-> Custom Scan (DecompressChunk) on _hyper_9_19_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on compress_hyper_10_29_chunk
-> Custom Scan (DecompressChunk) on _hyper_9_20_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on compress_hyper_10_30_chunk
-> Seq Scan on _hyper_9_21_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_22_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_23_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_24_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_25_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_26_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_27_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
-> Seq Scan on _hyper_9_28_chunk
Filter: (val_1 < 'a'::text COLLATE "POSIX")
(23 rows)
EXPLAIN (costs off) SELECT * FROM test_collation WHERE val_2 < 'a' COLLATE "C";
QUERY PLAN
----------------------------------------------------------
Append
-> Custom Scan (DecompressChunk) on _hyper_9_19_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on compress_hyper_10_29_chunk
-> Custom Scan (DecompressChunk) on _hyper_9_20_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on compress_hyper_10_30_chunk
-> Seq Scan on _hyper_9_21_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_22_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_23_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_24_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_25_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_26_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_27_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
-> Seq Scan on _hyper_9_28_chunk
Filter: (val_2 < 'a'::text COLLATE "C")
(23 rows)
--test datatypes
CREATE TABLE datatype_test(
time timestamptz NOT NULL,
int2_column int2,
int4_column int4,
int8_column int8,
float4_column float4,
float8_column float8,
date_column date,
timestamp_column timestamp,
timestamptz_column timestamptz,
interval_column interval,
numeric_column numeric,
decimal_column decimal,
text_column text,
char_column char
);
SELECT create_hypertable('datatype_test','time');
WARNING: column type "timestamp without time zone" used for "timestamp_column" does not follow best practices
create_hypertable
-----------------------------
(11,public,datatype_test,t)
(1 row)
ALTER TABLE datatype_test SET (timescaledb.compress);
INSERT INTO datatype_test VALUES ('2000-01-01',2,4,8,4.0,8.0,'2000-01-01','2001-01-01 12:00','2001-01-01 6:00','1 week', 3.41, 4.2, 'text', 'x');
SELECT count(compress_chunk(ch)) FROM show_chunks('datatype_test') ch;
count
-------
1
(1 row)
SELECT
attname, alg.name
FROM _timescaledb_catalog.hypertable ht
INNER JOIN _timescaledb_catalog.hypertable_compression htc ON ht.id=htc.hypertable_id
INNER JOIN _timescaledb_catalog.compression_algorithm alg ON alg.id=htc.compression_algorithm_id
WHERE ht.table_name='datatype_test'
ORDER BY attname;
attname | name
--------------------+----------------------------------
char_column | COMPRESSION_ALGORITHM_DICTIONARY
date_column | COMPRESSION_ALGORITHM_DELTADELTA
decimal_column | COMPRESSION_ALGORITHM_ARRAY
float4_column | COMPRESSION_ALGORITHM_GORILLA
float8_column | COMPRESSION_ALGORITHM_GORILLA
int2_column | COMPRESSION_ALGORITHM_DELTADELTA
int4_column | COMPRESSION_ALGORITHM_DELTADELTA
int8_column | COMPRESSION_ALGORITHM_DELTADELTA
interval_column | COMPRESSION_ALGORITHM_DICTIONARY
numeric_column | COMPRESSION_ALGORITHM_ARRAY
text_column | COMPRESSION_ALGORITHM_DICTIONARY
time | COMPRESSION_ALGORITHM_DELTADELTA
timestamp_column | COMPRESSION_ALGORITHM_DELTADELTA
timestamptz_column | COMPRESSION_ALGORITHM_DELTADELTA
(14 rows)
--TEST try to compress a hypertable that has a continuous aggregate
CREATE TABLE metrics(time timestamptz, device_id int, v1 float, v2 float);
SELECT create_hypertable('metrics','time');
NOTICE: adding not-null constraint to column "time"
create_hypertable
-----------------------
(13,public,metrics,t)
(1 row)
INSERT INTO metrics SELECT generate_series('2000-01-01'::timestamptz,'2000-01-10','1m'),1,0.25,0.75;
-- check expressions in view definition
CREATE MATERIALIZED VIEW cagg_expr WITH (timescaledb.continuous)
AS
SELECT
time_bucket('1d', time) AS time,
'Const'::text AS Const,
4.3::numeric AS "numeric",
first(metrics,time),
CASE WHEN true THEN 'foo' ELSE 'bar' END,
COALESCE(NULL,'coalesce'),
avg(v1) + avg(v2) AS avg1,
avg(v1+v2) AS avg2,
count(*) AS cnt
FROM metrics
GROUP BY 1 WITH NO DATA;
CALL refresh_continuous_aggregate('cagg_expr', NULL, NULL);
SELECT * FROM cagg_expr ORDER BY time LIMIT 5;
time | const | numeric | first | case | coalesce | avg1 | avg2 | cnt
------------------------------+-------+---------+----------------------------------------------+------+----------+------+------+------
Fri Dec 31 16:00:00 1999 PST | Const | 4.3 | ("Sat Jan 01 00:00:00 2000 PST",1,0.25,0.75) | foo | coalesce | 1 | 1 | 960
Sat Jan 01 16:00:00 2000 PST | Const | 4.3 | ("Sat Jan 01 16:00:00 2000 PST",1,0.25,0.75) | foo | coalesce | 1 | 1 | 1440
Sun Jan 02 16:00:00 2000 PST | Const | 4.3 | ("Sun Jan 02 16:00:00 2000 PST",1,0.25,0.75) | foo | coalesce | 1 | 1 | 1440
Mon Jan 03 16:00:00 2000 PST | Const | 4.3 | ("Mon Jan 03 16:00:00 2000 PST",1,0.25,0.75) | foo | coalesce | 1 | 1 | 1440
Tue Jan 04 16:00:00 2000 PST | Const | 4.3 | ("Tue Jan 04 16:00:00 2000 PST",1,0.25,0.75) | foo | coalesce | 1 | 1 | 1440
(5 rows)
ALTER TABLE metrics set(timescaledb.compress);
-- test rescan in compress chunk dml blocker
CREATE TABLE rescan_test(id integer NOT NULL, t timestamptz NOT NULL, val double precision, PRIMARY KEY(id, t));
SELECT create_hypertable('rescan_test', 't', chunk_time_interval => interval '1 day');
create_hypertable
---------------------------
(16,public,rescan_test,t)
(1 row)
-- compression
ALTER TABLE rescan_test SET (timescaledb.compress, timescaledb.compress_segmentby = 'id');
-- INSERT dummy data
INSERT INTO rescan_test SELECT 1, time, random() FROM generate_series('2000-01-01'::timestamptz, '2000-01-05'::timestamptz, '1h'::interval) g(time);
SELECT count(*) FROM rescan_test;
count
-------
97
(1 row)