[Bug]: Compression changes interval values: equal intervals with different month/day/time parts are merged by dictionary compression
What type of bug is this?
Data corruption, Incorrect result
What subsystems and features are affected?
Compression
What happened?
After compress_chunk, an interval column returns different values than were inserted.
Intervals that compare equal but have a different month/day/time split are read back as
one of them: a row inserted as 1 mon comes back as 30 days, 1 day comes back as
24:00:00, and 0 comes back as -1 mons +30 days (whichever spelling the chunk saw
first). Interval comparison treats a month as 30 days and a day as 24 hours, but the values
are not interchangeable: '2024-01-31'::date + '1 mon' is 2024-02-29, while
'2024-01-31'::date + '30 days' is 2024-03-01, so queries over the compressed chunk
return different dates than the same queries before compression. The column is stored
with the DICTIONARY algorithm (_timescaledb_functions.compressed_data_info).
TimescaleDB version affected
2.31.0-dev, built from main at dd514d5bc
PostgreSQL version used
17.11
What operating system did you use?
macOS 14 (Darwin 23.3.0) arm64
What installation method did you use?
Source
What platform did you run on?
On prem/Self-hosted
Relevant log output and stack trace
How can we reproduce the bug?
CREATE TABLE t (ts timestamptz NOT NULL, v interval);
SELECT create_hypertable('t', 'ts', chunk_time_interval => interval '1 day');
ALTER TABLE t SET (timescaledb.compress, timescaledb.compress_orderby = 'ts');
INSERT INTO t VALUES
('2024-01-01 00:00+00', '30 days'), ('2024-01-01 00:01+00', '1 mon'),
('2024-01-01 00:02+00', '24:00:00'), ('2024-01-01 00:03+00', '1 day'),
('2024-01-01 00:04+00', '-1 mons 30 days'), ('2024-01-01 00:05+00', '0');
-- before compression: the values as inserted
SELECT ts, v, '2024-01-31'::date + v AS jan31_plus_v FROM t ORDER BY ts;
-- ts | v | jan31_plus_v
-- ------------------------+------------------+---------------------
-- 2024-01-01 00:00:00+00 | 30 days | 2024-03-01 00:00:00
-- 2024-01-01 00:01:00+00 | 1 mon | 2024-02-29 00:00:00
-- 2024-01-01 00:02:00+00 | 24:00:00 | 2024-02-01 00:00:00
-- 2024-01-01 00:03:00+00 | 1 day | 2024-02-01 00:00:00
-- 2024-01-01 00:04:00+00 | -1 mons +30 days | 2024-01-30 00:00:00
-- 2024-01-01 00:05:00+00 | 00:00:00 | 2024-01-31 00:00:00
SELECT count(compress_chunk(c)) FROM show_chunks('t') c;
-- after compression: three of the six rows have changed
SELECT ts, v, '2024-01-31'::date + v AS jan31_plus_v FROM t ORDER BY ts;
-- ts | v | jan31_plus_v
-- ------------------------+------------------+---------------------
-- 2024-01-01 00:00:00+00 | 30 days | 2024-03-01 00:00:00
-- 2024-01-01 00:01:00+00 | 30 days | 2024-03-01 00:00:00 <- was 1 mon, 2024-02-29
-- 2024-01-01 00:02:00+00 | 24:00:00 | 2024-02-01 00:00:00
-- 2024-01-01 00:03:00+00 | 24:00:00 | 2024-02-01 00:00:00 <- was 1 day
-- 2024-01-01 00:04:00+00 | -1 mons +30 days | 2024-01-30 00:00:00
-- 2024-01-01 00:05:00+00 | -1 mons +30 days | 2024-01-30 00:00:00 <- was 00:00:00, 2024-01-31Source: timescale/timescaledb