forked from timescale/timescaledb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompact_chunk.sql
More file actions
1250 lines (996 loc) · 58.5 KB
/
Copy pathcompact_chunk.sql
File metadata and controls
1250 lines (996 loc) · 58.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
-- Tests for the compact_chunk function.
-- compact_chunk merges overlapping compressed batches within a chunk
-- without decompressing/recompressing batches that are already ordered.
CREATE TABLE metrics (time TIMESTAMPTZ NOT NULL, device TEXT, value float) WITH (tsdb.hypertable, tsdb.orderby='time');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- compact_chunk with no overlapping batches (no-op case)
-- Insert 3000 ordered rows: Jan 2 00:00 to Jan 4 02:00 PST.
-- Direct compress insert creates 3 batches: 3x1000 rows.
INSERT INTO metrics
SELECT '2025-01-02'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(1,3000) i;
-- Status should be COMPRESSED,UNORDERED after direct compress insert
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics') chunk;
-- Get the first compressed chunk for inspection
SELECT cs.compress_relid::regclass::text AS "CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics'
ORDER BY ch.id LIMIT 1 \gset
-- Show batch metadata: 3 non-overlapping batches (ordered by min time)
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1;
-- compact_chunk on non-overlapping batches is a no-op; returns the chunk regclass
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics') chunk;
-- Show batch metadata: 3 non-overlapping batches with same ctids as before
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1;
-- Status UNORDERED removed: compact_chunk clears the UNORDERED flag
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics') chunk;
-- compact_chunk with overlapping batches
-- Insert data that overlaps with existing batches.
-- Jan 1 to Jan 3 data overlaps with the existing Jan 2 to Jan 4 batches
-- in the compressed chunk.
INSERT INTO metrics
SELECT '2025-01-01'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(1,3000) i;
-- Show compressed chunk metadata: overlapping batches are now visible.
-- The new batches from the Jan 1 insert interleave with the old Jan 2 batches.
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1;
-- Status should contain UNORDERED flag
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics') chunk;
-- compact_chunk should identify and merge the overlapping batches.
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics') chunk;
-- Show compressed chunk metadata: no overlapping batches anymore.
-- We should see new ctids of the newly merged batches.
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1;
-- Status should not contain UNORDERED flag
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics') chunk;
-- compact_chunk with negative max_batches must fail
\set ON_ERROR_STOP 0
SELECT _timescaledb_functions.compact_chunk(chunk, -1) FROM show_chunks('metrics') chunk;
\set ON_ERROR_STOP 1
-- compact an uncompressed chunk
-- Create a new uncompressed chunk for a different time range by
-- inserting with direct compress insert disabled.
SET timescaledb.enable_direct_compress_insert = false;
INSERT INTO metrics VALUES ('2025-12-01', 'd1', -1.0);
SET timescaledb.enable_direct_compress_insert = true;
-- The new chunk for Dec 2025 should be uncompressed (empty status array)
SELECT _timescaledb_functions.chunk_status_text(chunk) AS chunk_status,
chunk::text AS chunk_name
FROM show_chunks('metrics') chunk
WHERE NOT (_timescaledb_functions.chunk_status_text(chunk) && ARRAY['COMPRESSED'])
ORDER BY chunk;
-- compact_chunk on an uncompressed chunk must fail
SELECT chunk AS "UNCOMPRESSED_CHUNK"
FROM show_chunks('metrics') chunk
WHERE NOT (_timescaledb_functions.chunk_status_text(chunk) && ARRAY['COMPRESSED'])
LIMIT 1 \gset
\set ON_ERROR_STOP 0
SELECT _timescaledb_functions.compact_chunk(:'UNCOMPRESSED_CHUNK');
\set ON_ERROR_STOP 1
-- compact a partially compressed chunk
-- Insert an uncompressed row into the already-compressed Jan 2 chunk.
-- This makes chunk 1 PARTIAL (it has both compressed and uncompressed rows).
SET timescaledb.enable_direct_compress_insert = false;
INSERT INTO metrics VALUES ('2025-01-02 12:00', 'd1', -1.0);
SET timescaledb.enable_direct_compress_insert = true;
-- Chunk 1 should now show PARTIAL status
SELECT _timescaledb_functions.chunk_status_text(chunk) AS chunk_status,
chunk::text AS chunk_name
FROM show_chunks('metrics') chunk
WHERE _timescaledb_functions.chunk_status_text(chunk) && ARRAY['PARTIAL']
ORDER BY chunk;
-- compact_chunk on a partially compressed chunk must fail
SELECT chunk AS "PARTIAL_CHUNK"
FROM show_chunks('metrics') chunk
WHERE _timescaledb_functions.chunk_status_text(chunk) && ARRAY['PARTIAL']
LIMIT 1 \gset
\set ON_ERROR_STOP 0
SELECT _timescaledb_functions.compact_chunk(:'PARTIAL_CHUNK');
\set ON_ERROR_STOP 1
-- compact_chunk is STRICT, a NULL argument returns NULL
SELECT _timescaledb_functions.compact_chunk(NULL::regclass);
-- compact_chunk in read-only mode must fail
SET default_transaction_read_only TO on;
\set ON_ERROR_STOP 0
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics') chunk;
\set ON_ERROR_STOP 1
SET default_transaction_read_only TO off;
-- compact_chunk in REPEATABLE READ must fail
\set ON_ERROR_STOP 0
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics') chunk;
COMMIT;
\set ON_ERROR_STOP 1
-- Create a hypertable with a segmentby column.
-- Each segment ('d1', 'd2') will have its own set of batches, and
-- compact_chunk should handle overlaps per segment independently.
CREATE TABLE metrics_seg (time TIMESTAMPTZ NOT NULL, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='time', tsdb.segmentby='device');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert 2000 rows for each of two devices: non-overlapping within each segment
INSERT INTO metrics_seg
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(0,1999) i;
INSERT INTO metrics_seg
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd2', i::float
FROM generate_series(0,1999) i;
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_seg') chunk;
-- Get the compressed chunk for metrics_seg
SELECT cs.compress_relid::regclass::text AS "SEG_CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_seg'
ORDER BY ch.id LIMIT 1 \gset
-- 4 batches: 2 per segment, no overlaps
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :SEG_CHUNK_FULL_NAME
ORDER BY device, _ts_meta_min_1;
-- compact_chunk with no overlaps: no-op for both segments
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_seg') chunk;
-- Same ctids: nothing was rewritten
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :SEG_CHUNK_FULL_NAME
ORDER BY device, _ts_meta_min_1;
-- status should be updated to COMPRESSED only
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_seg') chunk;
-- Insert overlapping data
INSERT INTO metrics_seg
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(0,1999) i;
INSERT INTO metrics_seg
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd2', i::float
FROM generate_series(0,1999) i;
-- 8 batches: 4 per segment, overlapping
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :SEG_CHUNK_FULL_NAME
ORDER BY device, _ts_meta_min_1;
-- status should be updated to COMPRESSED, UNORDERED
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_seg') chunk;
-- compact_chunk with overlaps: combines the batches
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_seg') chunk;
-- New ctids: overlapping batches merged and re-sorted
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :SEG_CHUNK_FULL_NAME
ORDER BY device, _ts_meta_min_1;
-- status should be COMPRESSED only (UNORDERED cleared after compaction)
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_seg') chunk;
-- compact_chunk with nullable secondary orderby column (NULLS FIRST)
-- The second batch starts at a boundary tie on col1 (time) with a NULL
-- in col2 (value). With NULLS FIRST, (time=T, value=NULL) should sort
-- BEFORE (time=T, value=non-null), but the second batch is positioned
-- after the first in the index — an ordering violation.
CREATE TABLE metrics_nullable (time TIMESTAMPTZ, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='time,value NULLS FIRST');
SET timescaledb.enable_direct_compress_insert = true;
-- Insert batch 1 (1000 rows): time [00:01..16:40], all non-null values.
-- Last row: (16:40, 1000).
INSERT INTO metrics_nullable
SELECT '2025-01-02'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(1,1000) i;
-- compact_chunk on nullable orderby with no nulls in data should succeed (no-op)
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_nullable') chunk;
-- Get the compressed chunk for metrics_nullable
SELECT cs.compress_relid::regclass::text AS "NULLABLE_CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_nullable'
ORDER BY ch.id LIMIT 1 \gset
-- 1 batch, no nulls
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLABLE_CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1;
-- Insert batch 2 (1000 rows): starts at batch 1's max time (i=1000, time=16:40).
-- First row has NULL value: (16:40, NULL).
-- With NULLS FIRST, (16:40, NULL) should sort BEFORE (16:40, 1000) from
-- batch 1's last row. But batch 2 is after batch 1 in the index → unordered.
INSERT INTO metrics_nullable
SELECT '2025-01-02'::timestamptz + ((999 + i) || ' minute')::interval,
'd1',
CASE WHEN i = 1 THEN NULL ELSE (1000 + i)::float END
FROM generate_series(1,1000) i;
-- 2 batches: boundary tie on col1, NULL in col2 at the boundary
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLABLE_CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1;
-- compact_chunk should detect the boundary-tie overlap via NULL in col2
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_nullable') chunk;
-- After compaction: batches merged and re-sorted
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLABLE_CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1;
-- Verify total row count is preserved
SELECT count(*) FROM metrics_nullable;
-- Verify NULL rows are accessible
SELECT count(*) AS null_value_count FROM metrics_nullable WHERE value IS NULL;
-- Verify ordering at boundary: NULL must come before non-null with NULLS FIRST
SELECT time, value FROM metrics_nullable
WHERE time = '2025-01-02'::timestamptz + '1000 minutes'::interval
ORDER BY time, value NULLS FIRST;
DROP TABLE metrics_nullable;
-- compact_chunk with DESC orderby column
CREATE TABLE metrics_desc (time TIMESTAMPTZ NOT NULL, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='time DESC', tsdb.segmentby='device');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert non-overlapping data for two devices
INSERT INTO metrics_desc
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(0,1999) i;
INSERT INTO metrics_desc
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd2', i::float
FROM generate_series(0,1999) i;
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_desc') chunk;
-- Get the compressed chunk for metrics_desc
SELECT cs.compress_relid::regclass::text AS "DESC_CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_desc'
ORDER BY ch.id LIMIT 1 \gset
-- 4 batches: 2 per segment, ordered by max time descending
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :DESC_CHUNK_FULL_NAME
ORDER BY device, _ts_meta_max_1 DESC;
-- No overlaps: compact_chunk is a no-op
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_desc') chunk;
-- Same ctids: nothing was rewritten
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :DESC_CHUNK_FULL_NAME
ORDER BY device, _ts_meta_max_1 DESC;
-- Status should be COMPRESSED only after compaction
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_desc') chunk;
-- Insert overlapping data
INSERT INTO metrics_desc
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(0,1999) i;
INSERT INTO metrics_desc
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd2', i::float
FROM generate_series(0,1999) i;
-- 8 batches: 4 per segment, overlapping
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :DESC_CHUNK_FULL_NAME
ORDER BY device, _ts_meta_max_1 DESC;
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_desc') chunk;
-- compact_chunk should merge overlapping batches with DESC ordering
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_desc') chunk;
-- New ctids: overlapping batches merged
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :DESC_CHUNK_FULL_NAME
ORDER BY device, _ts_meta_max_1 DESC;
-- Status should be COMPRESSED only
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_desc') chunk;
-- Verify data is correctly ordered (DESC) within each segment
SELECT device, time FROM metrics_desc WHERE device = 'd1' ORDER BY device, time DESC LIMIT 5;
DROP TABLE metrics_desc;
-- compact_chunk with multi-column orderby
-- Tests that overlap detection works correctly when using ORDER BY device, time.
-- The bug: secondary column min/max metadata is a global aggregate across
-- all rows, not scoped to the primary column's value. This causes false
-- negatives where interleaving batches go undetected.
CREATE TABLE metrics_multi (time TIMESTAMPTZ NOT NULL, device TEXT NOT NULL, value float)
WITH (tsdb.hypertable, tsdb.orderby='device,time');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert 3 batches with different devices creating boundary ties on col1:
-- Batch 1 (500 rows): device d1..d2, time [00:01..08:20]
-- Batch 2 (500 rows): device d2..d3, time [08:21..16:40]
-- The d2 rows in both batches create a boundary tie on col1 (device).
-- Col2 (time) ranges are non-overlapping within the d2 group → no actual overlap.
INSERT INTO metrics_multi
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
CASE WHEN i <= 250 THEN 'd1' ELSE 'd2' END,
i::float
FROM generate_series(1,500) i;
INSERT INTO metrics_multi
SELECT '2025-01-03'::timestamptz + ((500 + i) || ' minute')::interval,
CASE WHEN i <= 250 THEN 'd2' ELSE 'd3' END,
(500 + i)::float
FROM generate_series(1,500) i;
-- Get the compressed chunk for metrics_multi
SELECT cs.compress_relid::regclass::text AS "MULTI_CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_multi'
ORDER BY ch.id LIMIT 1 \gset
-- 2 batches: boundary tie on col1 (device=d2), non-overlapping time ranges
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_device, _ts_meta_v2_last_device, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :MULTI_CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1, _ts_meta_min_2;
-- compact_chunk: boundary tie on col1, but no overlap on col2 (time) — should be a no-op
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_multi') chunk;
-- Same ctids: nothing was rewritten
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_device, _ts_meta_v2_last_device, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :MULTI_CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1, _ts_meta_min_2;
-- Now insert truly overlapping data within the d2 device group:
-- Batch 3 (500 rows): device d2, time range overlaps with both existing d2 ranges
INSERT INTO metrics_multi
SELECT '2025-01-03'::timestamptz + ((250 + i) || ' minute')::interval, 'd2', (250 + i)::float
FROM generate_series(1,500) i;
-- 3 batches: the new d2 batch overlaps with the d2 portions of existing batches
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_device, _ts_meta_v2_last_device, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :MULTI_CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1, _ts_meta_min_2;
-- compact_chunk: boundary tie on col1 with actual overlap on col2 — must detect and merge
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_multi') chunk;
-- New ctids: overlapping batches merged
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_device, _ts_meta_v2_last_device, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :MULTI_CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1, _ts_meta_min_2;
-- Total row count preserved
SELECT count(*) FROM metrics_multi;
DROP TABLE metrics_multi;
-- compact_chunk with multi-column orderby, second column DESC
-- Same boundary-tie logic but the secondary column uses descending order.
-- The sort operator for col2 is now ">" instead of "<", so the boundary
-- decompression must use the correct comparator.
CREATE TABLE metrics_multi_desc (time TIMESTAMPTZ NOT NULL, device TEXT NOT NULL, value float)
WITH (tsdb.hypertable, tsdb.orderby='device,time DESC');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert 2 batches with multiple devices, DESC time ordering:
-- Batch 1 (500 rows): device d2..d3, time [08:21..16:40] DESC
-- Batch 2 (500 rows): device d1..d2, time [00:01..08:20] DESC
-- The d2 rows create a boundary tie on col1.
-- With DESC, batch 1's d2 trailing edge (min time=08:21) and batch 2's
-- d2 leading edge (max time=08:20) are non-overlapping → no actual overlap.
INSERT INTO metrics_multi_desc
SELECT '2025-01-03'::timestamptz + ((500 + i) || ' minute')::interval,
CASE WHEN i <= 250 THEN 'd2' ELSE 'd3' END,
(500 + i)::float
FROM generate_series(1,500) i;
INSERT INTO metrics_multi_desc
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
CASE WHEN i <= 250 THEN 'd1' ELSE 'd2' END,
i::float
FROM generate_series(1,500) i;
-- Get the compressed chunk
SELECT cs.compress_relid::regclass::text AS "MULTI_DESC_CHUNK"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_multi_desc'
ORDER BY ch.id LIMIT 1 \gset
-- 2 batches: boundary tie on col1 (device=d2), non-overlapping DESC time ranges
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_device, _ts_meta_v2_last_device, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :MULTI_DESC_CHUNK
ORDER BY _ts_meta_min_1, _ts_meta_max_2 DESC;
-- compact_chunk: boundary tie on col1, no overlap on col2 (DESC) — should be a no-op
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_multi_desc') chunk;
-- Same ctids: nothing was rewritten
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_device, _ts_meta_v2_last_device, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :MULTI_DESC_CHUNK
ORDER BY _ts_meta_min_1, _ts_meta_max_2 DESC;
-- Now insert overlapping data within the d2 device group
INSERT INTO metrics_multi_desc
SELECT '2025-01-03'::timestamptz + ((250 + i) || ' minute')::interval, 'd2', (250 + i)::float
FROM generate_series(1,500) i;
-- 3 batches: the new d2 batch overlaps with existing d2 portions
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_device, _ts_meta_v2_last_device, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :MULTI_DESC_CHUNK
ORDER BY _ts_meta_min_1, _ts_meta_max_2 DESC;
-- compact_chunk: boundary tie on col1, actual overlap on col2 (DESC) — must merge
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_multi_desc') chunk;
-- New ctids: overlapping batches merged
SELECT ctid, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_device, _ts_meta_v2_last_device, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :MULTI_DESC_CHUNK
ORDER BY _ts_meta_min_1, _ts_meta_max_2 DESC;
-- Total row count preserved
SELECT count(*) FROM metrics_multi_desc;
DROP TABLE metrics_multi_desc;
-- compact_chunk with segmentby + multi-column orderby + nullable orderby column
-- Combines all three features:
-- segmentby='device' multiple segments processed independently
-- orderby='time,value' multi-column overlap detection with boundary ties
-- 'value' is nullable boundary tie NULL values must respect NULL ordering
CREATE TABLE metrics_combined (time TIMESTAMPTZ NOT NULL, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='time,value', tsdb.segmentby='device');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert non-null data for two segments: 1000 rows each, non-overlapping
INSERT INTO metrics_combined
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(1,1000) i;
INSERT INTO metrics_combined
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd2', (i + 1000)::float
FROM generate_series(1,1000) i;
-- Get the compressed chunk
SELECT cs.compress_relid::regclass::text AS "COMBINED_CHUNK"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_combined'
ORDER BY ch.id LIMIT 1 \gset
-- 2 batches: 1 per segment, no overlaps, no nulls
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :COMBINED_CHUNK
ORDER BY device, _ts_meta_min_1;
-- No overlaps, no nulls: compact_chunk is a no-op
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_combined') chunk;
-- Same ctids as before: nothing was rewritten
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :COMBINED_CHUNK
ORDER BY device, _ts_meta_min_1;
-- Status should be COMPRESSED only
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_combined') chunk;
-- Insert overlapping data with NULLs in the nullable orderby column (value)
-- for both segments. Every 5th row has NULL value.
INSERT INTO metrics_combined
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd1',
CASE WHEN i % 5 = 0 THEN NULL ELSE (i + 2000)::float END
FROM generate_series(1,1000) i;
INSERT INTO metrics_combined
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd2',
CASE WHEN i % 5 = 0 THEN NULL ELSE (i + 3000)::float END
FROM generate_series(1,1000) i;
-- 4 batches: original + overlapping with nulls. Both segments overlap on time col1.
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :COMBINED_CHUNK
ORDER BY device, _ts_meta_min_1;
-- Status should be UNORDERED
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_combined') chunk;
-- compact_chunk must:
-- 1. Detect overlaps per segment via multi-column boundary-tie logic
-- 2. Recompress overlapping rows into ordered batches
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_combined') chunk;
-- After compaction: new ctids, overlapping batches merged and re-sorted
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :COMBINED_CHUNK
ORDER BY device, _ts_meta_min_1;
-- Status should be COMPRESSED only (UNORDERED cleared).
-- NULLs in the secondary orderby column (value) just sort within the batch per
-- NULLS FIRST/LAST; only the first orderby column (time, NOT NULL) affects
-- batch placement.
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_combined') chunk;
-- Total row count preserved
SELECT count(*) FROM metrics_combined;
-- NULL rows preserved per segment
SELECT device, count(*) AS null_value_count FROM metrics_combined WHERE value IS NULL GROUP BY device ORDER BY device;
-- Data integrity per segment
SELECT device, count(*) FROM metrics_combined GROUP BY device ORDER BY device;
-- Verify ordering is correct within segment d1
SELECT device, time, value FROM metrics_combined
WHERE device = 'd1' ORDER BY time, value LIMIT 5;
SELECT device, time, value FROM metrics_combined
WHERE device = 'd1' AND value IS NULL ORDER BY time LIMIT 5;
DROP TABLE metrics_combined;
-- compact_chunk with nullable first orderby column (NULLS LAST, default)
-- A NULL boundary value makes a mixed-null batch overlap its neighbor, so the
-- overlap merge re-sorts both batches; the NULLs land at the end (NULLS LAST).
-- A mixed-null batch that overlaps nothing is already ordered and left untouched.
CREATE TABLE metrics_nulls_last (time TIMESTAMPTZ NOT NULL, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='value,time', tsdb.segmentby='device');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert non-null data for two segments
INSERT INTO metrics_nulls_last
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(1,1000) i;
INSERT INTO metrics_nulls_last
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd2', (i + 1000)::float
FROM generate_series(1,1000) i;
SELECT cs.compress_relid::regclass::text AS "NULLS_LAST_CHUNK"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_nulls_last'
ORDER BY ch.id LIMIT 1 \gset
-- 2 non-overlapping batches, no nulls
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_LAST_CHUNK
ORDER BY device, _ts_meta_min_1;
-- Insert overlapping data with NULLs in value (first orderby column)
INSERT INTO metrics_nulls_last
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd1',
CASE WHEN i % 5 = 0 THEN NULL ELSE (i + 2000)::float END
FROM generate_series(1,1000) i;
INSERT INTO metrics_nulls_last
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd2',
CASE WHEN i % 5 = 0 THEN NULL ELSE (i + 3000)::float END
FROM generate_series(1,1000) i;
-- 4 batches with overlaps and nulls in first orderby column
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_LAST_CHUNK
ORDER BY device, _ts_meta_min_1;
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_nulls_last') chunk;
-- After compaction: the mixed-null batch does not overlap its neighbor, so it
-- stays as-is (nulls already sort to the end of the batch with NULLS LAST)
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_LAST_CHUNK
ORDER BY device, _ts_meta_min_1;
-- UNORDERED cleared: NULLs fold into the end of the segment (NULLS LAST)
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_nulls_last') chunk;
-- Data integrity
SELECT count(*) FROM metrics_nulls_last;
SELECT device, count(*) FROM metrics_nulls_last GROUP BY device ORDER BY device;
SELECT device, count(*) AS null_count FROM metrics_nulls_last WHERE value IS NULL GROUP BY device ORDER BY device;
-- Now test the overlap path with NULLs: insert data that truly overlaps
-- on the value range AND contains NULLs, with a nullable first orderby column.
-- The NULL rows must survive the overlap merge and end up ordered correctly.
INSERT INTO metrics_nulls_last
SELECT '2025-01-03'::timestamptz + ((2000 + i) || ' minute')::interval,
'd1',
CASE WHEN i % 5 = 0 THEN NULL ELSE (i + 500)::float END
FROM generate_series(1,500) i;
-- Batches before overlap compaction
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_LAST_CHUNK
WHERE device = 'd1'
ORDER BY _ts_meta_min_1 NULLS LAST;
-- This overlaps with existing d1 data (value 501..900 overlaps with 1..1000 and 2001..2999)
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_nulls_last') chunk;
-- After overlap merge: NULLs from overlapping batches preserved
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_LAST_CHUNK
WHERE device = 'd1'
ORDER BY _ts_meta_min_1 NULLS LAST;
-- Data integrity: all rows including NULLs from the overlap merge must survive
SELECT count(*) FROM metrics_nulls_last WHERE device = 'd1';
SELECT count(*) AS null_count FROM metrics_nulls_last WHERE device = 'd1' AND value IS NULL;
DROP TABLE metrics_nulls_last;
-- compact_chunk with nullable first orderby column (NULLS FIRST)
-- With NULLS FIRST, the re-sort places NULLs at the start of each segment, so a
-- null-containing batch is already correctly positioned.
CREATE TABLE metrics_nulls_first (time TIMESTAMPTZ NOT NULL, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='value NULLS FIRST,time', tsdb.segmentby='device');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert non-null data for two segments
INSERT INTO metrics_nulls_first
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd1', i::float
FROM generate_series(1,1000) i;
INSERT INTO metrics_nulls_first
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval, 'd2', (i + 1000)::float
FROM generate_series(1,1000) i;
SELECT cs.compress_relid::regclass::text AS "NULLS_FIRST_CHUNK"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_nulls_first'
ORDER BY ch.id LIMIT 1 \gset
-- 2 non-overlapping batches, no nulls
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_FIRST_CHUNK
ORDER BY device, _ts_meta_min_1;
-- Insert overlapping data with NULLs in value (first orderby column)
INSERT INTO metrics_nulls_first
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd1',
CASE WHEN i % 5 = 0 THEN NULL ELSE (i + 2000)::float END
FROM generate_series(1,1000) i;
INSERT INTO metrics_nulls_first
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd2',
CASE WHEN i % 5 = 0 THEN NULL ELSE (i + 3000)::float END
FROM generate_series(1,1000) i;
-- 4 batches with overlaps and nulls in first orderby column
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_FIRST_CHUNK
ORDER BY device, _ts_meta_min_1;
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_nulls_first') chunk;
-- After compaction: with NULLS FIRST, null batches at segment start are fine
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_FIRST_CHUNK
ORDER BY device, _ts_meta_min_1;
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_nulls_first') chunk;
-- Data integrity
SELECT count(*) FROM metrics_nulls_first;
SELECT device, count(*) FROM metrics_nulls_first GROUP BY device ORDER BY device;
SELECT device, count(*) AS null_count FROM metrics_nulls_first WHERE value IS NULL GROUP BY device ORDER BY device;
-- Same overlap+NULLs test for NULLS FIRST
INSERT INTO metrics_nulls_first
SELECT '2025-01-03'::timestamptz + ((2000 + i) || ' minute')::interval,
'd1',
CASE WHEN i % 5 = 0 THEN NULL ELSE (i + 500)::float END
FROM generate_series(1,500) i;
-- Batches before overlap compaction
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_FIRST_CHUNK
WHERE device = 'd1'
ORDER BY _ts_meta_min_1 NULLS FIRST;
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_nulls_first') chunk;
-- After overlap merge: NULLs from overlapping batches preserved
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :NULLS_FIRST_CHUNK
WHERE device = 'd1'
ORDER BY _ts_meta_min_1 NULLS FIRST;
-- Data integrity: NULLs from overlap merge must survive
SELECT count(*) FROM metrics_nulls_first WHERE device = 'd1';
SELECT count(*) AS null_count FROM metrics_nulls_first WHERE device = 'd1' AND value IS NULL;
DROP TABLE metrics_nulls_first;
-- compact_chunk with a mixed-null batch whose NULLs overlap a later batch
-- A batch with both NULL and non-NULL values in the first orderby column has a
-- NULL last row (NULLS LAST) that sorts after a following non-null batch, so the
-- two batches overlap and the merge re-sorts them with the NULLs back at the end.
-- Without firstlast metadata the NULL boundary is invisible and the overlap is
-- missed, leaving NULLs in the wrong index position.
CREATE TABLE metrics_mixed_nulls (time TIMESTAMPTZ NOT NULL, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='value,time', tsdb.segmentby='device');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert 1800 non-null rows + 200 NULL rows for device 'd1'.
-- DCI sorts by (value NULLS LAST, time), producing:
-- Batch 1 (1000 rows): value [1..1000] — all non-null
-- Batch 2 (1000 rows): value [1001..1800] + 200 NULLs — mixed!
-- Batch 2's firstlast metadata: first=1001, last=NULL (NULLS LAST).
INSERT INTO metrics_mixed_nulls
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd1',
CASE WHEN i > 1800 THEN NULL ELSE i::float END
FROM generate_series(1,2000) i;
-- Insert 1000 more non-null rows with values [1801..2800], forming Batch 3
-- (first=1801, last=2800). Batch 2's non-null range [1001..1800] sits below
-- Batch 3, but Batch 2's NULL last row sorts after Batch 3 (NULLS LAST), so the
-- two batches overlap and must be merged.
INSERT INTO metrics_mixed_nulls
SELECT '2025-01-03'::timestamptz + ((2000 + i) || ' minute')::interval,
'd1',
(1800 + i)::float
FROM generate_series(1,1000) i;
SELECT cs.compress_relid::regclass::text AS "MIXED_NULLS_CHUNK"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_mixed_nulls'
ORDER BY ch.id LIMIT 1 \gset
-- Show batch metadata before compaction: 3 batches, batch 2 has mixed nulls
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :MIXED_NULLS_CHUNK
ORDER BY device, _ts_meta_min_1;
-- compact_chunk must detect the overlap and re-sort the NULLs back to the end
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_mixed_nulls') chunk;
-- After compaction: Batch 2 and Batch 3 merged, NULLs folded into the trailing
-- batch at the end (NULLS LAST).
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :MIXED_NULLS_CHUNK
ORDER BY device, _ts_meta_min_1 NULLS LAST;
-- Status should be COMPRESSED only (UNORDERED cleared correctly this time)
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_mixed_nulls') chunk;
-- Data integrity
SELECT count(*) FROM metrics_mixed_nulls;
SELECT count(*) AS null_count FROM metrics_mixed_nulls WHERE value IS NULL;
-- Verify ordering is correct: NULLs must come after all non-null values
-- This query will return wrong results if the bug is present (NULLs between 1800 and 1801)
SELECT value FROM metrics_mixed_nulls
WHERE device = 'd1'
ORDER BY value NULLS LAST, time
LIMIT 5 OFFSET 1795;
DROP TABLE metrics_mixed_nulls;
-- compact_chunk with nullable SECONDARY orderby column at boundary tie
-- Regression test: when orderby='time,value' and value is nullable,
-- two batches can tie on col1 (time) at the boundary. If the prev batch's
-- last row has NULL in col2 (value), the overlap check must compare using
-- NULLS FIRST/LAST semantics so the tie is correctly seen as an overlap.
CREATE TABLE metrics_secondary_null (time TIMESTAMPTZ NOT NULL, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='time,value', tsdb.segmentby='device');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert batch 1 (500 rows): time [00:01..08:20], with NULL value at the
-- last timestamp (i=500, time=08:20). With orderby='time,value NULLS LAST',
-- the last row in sorted order is (08:20, NULL).
INSERT INTO metrics_secondary_null
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd1',
CASE WHEN i = 500 THEN NULL ELSE i::float END
FROM generate_series(1,500) i;
-- Insert batch 2 (500 rows): starts exactly at batch 1's max time (08:20).
-- This creates a boundary tie on col1 (time). All values are non-null.
-- The first row (08:20, 1001) ties with batch 1's last row (08:20, NULL).
-- With NULLS LAST, (08:20, NULL) should sort AFTER (08:20, 1001), but in
-- index order batch 1 comes before batch 2 — an ordering violation.
INSERT INTO metrics_secondary_null
SELECT '2025-01-03'::timestamptz + ((499 + i) || ' minute')::interval,
'd1',
(1000 + i)::float
FROM generate_series(1,500) i;
SELECT cs.compress_relid::regclass::text AS "SEC_NULL_CHUNK"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_secondary_null'
ORDER BY ch.id LIMIT 1 \gset
-- Show batch metadata: col1 (time) ranges overlap at the boundary
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :SEC_NULL_CHUNK
ORDER BY device, _ts_meta_min_1;
-- compact_chunk must detect the boundary-tie overlap caused by NULL in col2
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_secondary_null') chunk;
-- After compaction: batches should be merged and re-sorted
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :SEC_NULL_CHUNK
ORDER BY device, _ts_meta_min_1;
-- Status should be COMPRESSED only
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_secondary_null') chunk;
-- Data integrity
SELECT count(*) FROM metrics_secondary_null;
SELECT count(*) AS null_count FROM metrics_secondary_null WHERE value IS NULL;
-- Verify ordering: at the boundary time (08:20 = +500 min), non-null value
-- must come before NULL with NULLS LAST ordering.
SELECT time, value FROM metrics_secondary_null
WHERE device = 'd1' AND time = '2025-01-03'::timestamptz + '500 minutes'::interval
ORDER BY time, value NULLS LAST;
DROP TABLE metrics_secondary_null;
-- compact_chunk with nullable SECONDARY orderby column at boundary tie (NULLS FIRST)
-- Mirror of the NULLS LAST test above. With NULLS FIRST, the curr batch's
-- first row has NULL in col2, which should sort BEFORE the prev batch's
-- last non-null col2 value — meaning the curr batch actually starts earlier
-- than its non-null min suggests, causing an ordering violation.
CREATE TABLE metrics_secondary_null_first (time TIMESTAMPTZ NOT NULL, device TEXT, value float)
WITH (tsdb.hypertable, tsdb.orderby='time,value NULLS FIRST', tsdb.segmentby='device');
SET timescaledb.enable_direct_compress_insert = true;
SET timescaledb.enable_direct_compress_insert_sort_batches = true;
SET timescaledb.enable_direct_compress_insert_client_sorted = false;
-- Insert batch 1 (500 rows): time [00:01..08:20], all non-null values.
INSERT INTO metrics_secondary_null_first
SELECT '2025-01-03'::timestamptz + (i || ' minute')::interval,
'd1',
i::float
FROM generate_series(1,500) i;
-- Insert batch 2 (500 rows): starts at batch 1's max time (08:20).
-- The first row at time=08:20 has NULL value. With NULLS FIRST,
-- (08:20, NULL) should sort BEFORE (08:20, 500) from batch 1's last row.
-- But batch 2 comes after batch 1 in index order — ordering violation.
INSERT INTO metrics_secondary_null_first
SELECT '2025-01-03'::timestamptz + ((499 + i) || ' minute')::interval,
'd1',
CASE WHEN i = 1 THEN NULL ELSE (1000 + i)::float END
FROM generate_series(1,500) i;
SELECT cs.compress_relid::regclass::text AS "SEC_NF_CHUNK"
FROM _timescaledb_catalog.chunk ch
JOIN _timescaledb_catalog.compression_settings cs
ON cs.relid = ch.relid
JOIN _timescaledb_catalog.hypertable ht ON ch.hypertable_id = ht.id
WHERE ht.table_name = 'metrics_secondary_null_first'
ORDER BY ch.id LIMIT 1 \gset
-- Show batch metadata: boundary tie on col1 (time) at 08:20
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time, _ts_meta_min_2, _ts_meta_max_2, _ts_meta_v2_first_value, _ts_meta_v2_last_value
FROM :SEC_NF_CHUNK
ORDER BY device, _ts_meta_min_1;
-- compact_chunk must detect the boundary-tie overlap caused by NULL in col2
SELECT _timescaledb_functions.compact_chunk(chunk) FROM show_chunks('metrics_secondary_null_first') chunk;
-- After compaction: batches should be merged and re-sorted
SELECT ctid, device, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_v2_first_time, _ts_meta_v2_last_time
FROM :SEC_NF_CHUNK
ORDER BY device, _ts_meta_min_1;
-- Status should be COMPRESSED only
SELECT DISTINCT _timescaledb_functions.chunk_status_text(chunk) FROM show_chunks('metrics_secondary_null_first') chunk;