[Bug]: Multi-row INSERT into a compressed chunk raises a false duplicate-key error, or silently drops rows with ON CONFLICT DO NOTHING, after a row with NULL in a segmentby key column
What type of bug is this?
Unexpected error, Data corruption
What subsystems and features are affected?
Compression, Data ingestion
What happened?
A hypertable has a unique index on (ts, k) and columnstore settings with segmentby = 's, k'
(the key column k is a segmentby column but not the first one, so the compressed chunk's
index cannot serve it). A chunk is compressed holding the row (10, 1, 'a'). Then a single
INSERT statement adds two rows, neither of which conflicts with anything:
(20, 1, NULL) followed by (10, 1, 'b'). The statement fails:
ERROR: duplicate key value violates unique constraint "_hyper_1_1_chunk_t_ts_k_idx"The same two rows in the other order are accepted. With ON CONFLICT DO NOTHING the
statement reports INSERT 0 1 and the second row, (10, 1, 'd'), is silently not stored,
although no row with that key exists. A plain PostgreSQL table with the same unique index
accepts all of these statements.
timescaledb.debug_compression_path_info shows the uniqueness check running with
scan keys: index 0, heap 2, memory 1 for the failing statement and heap 3 for the
reversed one: when the statement's first row has NULL in the segmentby key column, that
column gets no scan key, and every later row of the statement is then checked against
the compressed batches on the time column alone, so (10, 1, 'b') is taken to conflict
with (10, 1, 'a').
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 int8 NOT NULL, s int, k text, v int);
SELECT create_hypertable('t', 'ts', chunk_time_interval => 1000000);
CREATE UNIQUE INDEX ON t (ts, k);
ALTER TABLE t SET (timescaledb.compress, timescaledb.compress_segmentby = 's, k', timescaledb.compress_orderby = 'ts');
INSERT INTO t VALUES (10, 1, 'a', 1);
SELECT count(compress_chunk(c)) FROM show_chunks('t') c;
SET timescaledb.debug_compression_path_info = on;
-- neither row conflicts with (10, 1, 'a'); the first row has NULL in the key column k
INSERT INTO t VALUES (20, 1, NULL, 2), (10, 1, 'b', 3);
-- INFO: Using table scan with scan keys: index 0, heap 2, memory 1.
-- ERROR: duplicate key value violates unique constraint "_hyper_1_1_chunk_t_ts_k_idx"
-- the same two rows in the other order are accepted
INSERT INTO t VALUES (10, 1, 'c', 3), (20, 1, NULL, 4);
-- INFO: Using table scan with scan keys: index 0, heap 3, memory 1.
-- INSERT 0 2
-- with ON CONFLICT DO NOTHING the second row is silently dropped
INSERT INTO t VALUES (30, 1, NULL, 5), (10, 1, 'd', 6) ON CONFLICT DO NOTHING;
-- INSERT 0 1
RESET timescaledb.debug_compression_path_info;
SELECT * FROM t ORDER BY ts, k;
-- ts | s | k | v
-- ----+---+---+---
-- 10 | 1 | a | 1
-- 10 | 1 | c | 3
-- 20 | 1 | | 4
-- 30 | 1 | | 5
-- (4 rows) <- (10, 1, 'd', 6) is missingSource: timescale/timescaledb