Skip to content

Commit 0dbe2cf

Browse files
Fix chunk skipping near PG_INT64_MAX
Remove REMAP_LAST_COORDINATE after incrementing max to make range_end exclusive. The remap undid the increment for values near the int64 boundary. Guard range_end - 1 against DIMENSION_SLICE_MAXVALUE before converting exclusive to inclusive bound. Fixes #9995
1 parent 11ec5b2 commit 0dbe2cf

4 files changed

Lines changed: 88 additions & 8 deletions

File tree

.unreleased/pr_10026

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #10026 Fix chunk skipping near PG_INT64_MAX

src/ts_catalog/chunk_column_stats.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -945,8 +945,6 @@ ts_chunk_column_stats_calculate(const Hypertable *ht, const Chunk *chunk)
945945
if (max != DIMENSION_SLICE_MAXVALUE)
946946
{
947947
max++;
948-
/* Again, check overflow */
949-
max = REMAP_LAST_COORDINATE(max);
950948
}
951949

952950
/*
@@ -1322,7 +1320,7 @@ ts_chunk_column_stats_get_chunk_ids_by_scan(DimensionRestrictInfo *dri)
13221320
* check so prepare to short circuit if one evaluates to true.
13231321
*
13241322
* No real way to know if checking range_start or range_end first will be more
1325-
* effective. So let's start with range_end checks first.
1323+
* effective. So let's start with range_start checks first.
13261324
*/
13271325
switch (open->upper_strategy)
13281326
{
@@ -1346,19 +1344,20 @@ ts_chunk_column_stats_get_chunk_ids_by_scan(DimensionRestrictInfo *dri)
13461344
goto done;
13471345
}
13481346

1349-
/* range_end checks didn't match, check for range_start now */
1347+
/* range_start checks didn't match, check for range_end now */
1348+
/* range_end is exclusive except when DIMENSION_SLICE_MAXVALUE */
1349+
int64 range_end =
1350+
(fd.range_end == DIMENSION_SLICE_MAXVALUE) ? fd.range_end : (fd.range_end - 1);
13501351
switch (open->lower_strategy)
13511352
{
13521353
case BTGreaterEqualStrategyNumber:
13531354
{
1354-
/* range_end is exclusive */
1355-
matched = (fd.range_end - 1) >= open->lower_bound;
1355+
matched = range_end >= open->lower_bound;
13561356
}
13571357
break;
13581358
case BTGreaterStrategyNumber:
13591359
{
1360-
/* range_end is exclusive */
1361-
matched = (fd.range_end - 1) > open->lower_bound;
1360+
matched = range_end > open->lower_bound;
13621361
}
13631362
break;
13641363
default:

tsl/test/expected/chunk_column_stats.out

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,4 +786,60 @@ SELECT enable_chunk_skipping('sensor_readings', 'temperature');
786786
(17,t)
787787

788788
RESET timescaledb.enable_chunk_skipping;
789+
-- Test chunk skipping with PG_INT64_MAX value
790+
SET timescaledb.enable_chunk_skipping = on;
791+
CREATE TABLE chunk_skip_bigint_max(
792+
ts timestamptz NOT NULL,
793+
ranged bigint
794+
);
795+
SELECT * FROM create_hypertable('chunk_skip_bigint_max', 'ts',
796+
chunk_time_interval => interval '1 day');
797+
hypertable_id | schema_name | table_name | created
798+
---------------+-------------+-----------------------+---------
799+
11 | public | chunk_skip_bigint_max | t
800+
801+
SELECT * FROM enable_chunk_skipping('chunk_skip_bigint_max', 'ranged');
802+
column_stats_id | enabled
803+
-----------------+---------
804+
18 | t
805+
806+
ALTER TABLE chunk_skip_bigint_max SET (timescaledb.compress);
807+
INSERT INTO chunk_skip_bigint_max VALUES ('2025-01-01', 9223372036854775806); -- PG_INT64_MAX - 1
808+
INSERT INTO chunk_skip_bigint_max VALUES ('2025-01-02', 9223372036854775807); -- PG_INT64_MAX
809+
SELECT compress_chunk(c) FROM show_chunks('chunk_skip_bigint_max') c;
810+
compress_chunk
811+
------------------------------------------
812+
_timescaledb_internal._hyper_11_14_chunk
813+
_timescaledb_internal._hyper_11_15_chunk
814+
815+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged = 9223372036854775807;
816+
count
817+
-------
818+
1
819+
820+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged >= 9223372036854775807;
821+
count
822+
-------
823+
1
824+
825+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged > 9223372036854775806;
826+
count
827+
-------
828+
1
829+
830+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged = 9223372036854775806;
831+
count
832+
-------
833+
1
834+
835+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged >= 9223372036854775806;
836+
count
837+
-------
838+
2
839+
840+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged > 9223372036854775805;
841+
count
842+
-------
843+
2
844+
789845
RESET timescaledb.enable_chunk_skipping;

tsl/test/sql/chunk_column_stats.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,5 +356,29 @@ SELECT create_hypertable('sensor_readings', 'measured_at');
356356
SELECT enable_chunk_skipping('sensor_readings', 'temperature');
357357
RESET timescaledb.enable_chunk_skipping;
358358

359+
-- Test chunk skipping with PG_INT64_MAX value
360+
SET timescaledb.enable_chunk_skipping = on;
361+
362+
CREATE TABLE chunk_skip_bigint_max(
363+
ts timestamptz NOT NULL,
364+
ranged bigint
365+
);
366+
SELECT * FROM create_hypertable('chunk_skip_bigint_max', 'ts',
367+
chunk_time_interval => interval '1 day');
368+
SELECT * FROM enable_chunk_skipping('chunk_skip_bigint_max', 'ranged');
369+
ALTER TABLE chunk_skip_bigint_max SET (timescaledb.compress);
370+
371+
INSERT INTO chunk_skip_bigint_max VALUES ('2025-01-01', 9223372036854775806); -- PG_INT64_MAX - 1
372+
INSERT INTO chunk_skip_bigint_max VALUES ('2025-01-02', 9223372036854775807); -- PG_INT64_MAX
373+
374+
SELECT compress_chunk(c) FROM show_chunks('chunk_skip_bigint_max') c;
375+
376+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged = 9223372036854775807;
377+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged >= 9223372036854775807;
378+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged > 9223372036854775806;
379+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged = 9223372036854775806;
380+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged >= 9223372036854775806;
381+
SELECT count(*) FROM chunk_skip_bigint_max WHERE ranged > 9223372036854775805;
382+
359383

360384
RESET timescaledb.enable_chunk_skipping;

0 commit comments

Comments
 (0)