Skip to content

Commit

Permalink
Don't abort additional INSERTs when hitting first conflict
Browse files Browse the repository at this point in the history
When an INSERT with ON CONFLICT DO NOTHING hits the first conflicts
it would abort additional INSERTS following the INSERT triggering
the DO NOTHING clause leading to missed INSERTs.

Fixes #7672
  • Loading branch information
svenklemm committed Feb 10, 2025
1 parent 89062cb commit 41b141f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .unreleased/pr_7673
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes: #7673 Don't abort additional INSERTs when hitting first conflict
Thanks: @bjornuppeke for reporting a problem with INSERT INTO ... ON CONFLICT DO NOTHING on compressed chunks
2 changes: 1 addition & 1 deletion src/nodes/hypertable_modify.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ ExecModifyTable(CustomScanState *cs_node, PlanState *pstate)
cds->skip_current_tuple = false;
if (node->ps.instrument)
node->ps.instrument->ntuples2++;
return NULL;
continue;
}

/* No more tuples to process? */
Expand Down
30 changes: 30 additions & 0 deletions tsl/test/expected/compression_conflicts.out
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,33 @@ VALUES
(41, 1609478100000, 'val1')
ON CONFLICT DO NOTHING;
INFO: Using index scan with scan keys: index 1, heap 4, memory 2.
INFO: Using index scan with scan keys: index 1, heap 4, memory 2.
RESET timescaledb.debug_compression_path_info;
-- gh issue #7672
-- check additional INSERTS after hitting ON CONFLICT clause still go through
CREATE TABLE test_i7672(time timestamptz, device text, primary key(time,device));
SELECT create_hypertable('test_i7672', 'time');
create_hypertable
--------------------------
(13,public,test_i7672,t)
(1 row)

ALTER TABLE test_i7672 SET (timescaledb.compress, timescaledb.compress_orderby='time DESC', timescaledb.compress_segmentby='device');
INSERT INTO test_i7672 VALUES ('2025-01-01','d1');
SELECT count(*) FROM (SELECT compress_chunk(show_chunks('test_i7672'))) c;
count
-------
1
(1 row)

INSERT INTO test_i7672 VALUES
('2025-01-01','d1'),
('2025-01-01','d2')
ON CONFLICT DO NOTHING;
SELECT * FROM test_i7672 t ORDER BY t;
time | device
------------------------------+--------
Wed Jan 01 00:00:00 2025 PST | d1
Wed Jan 01 00:00:00 2025 PST | d2
(2 rows)

18 changes: 18 additions & 0 deletions tsl/test/sql/compression_conflicts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,21 @@ VALUES
(41, 1609478100000, 'val1')
ON CONFLICT DO NOTHING;

RESET timescaledb.debug_compression_path_info;

-- gh issue #7672
-- check additional INSERTS after hitting ON CONFLICT clause still go through
CREATE TABLE test_i7672(time timestamptz, device text, primary key(time,device));
SELECT create_hypertable('test_i7672', 'time');
ALTER TABLE test_i7672 SET (timescaledb.compress, timescaledb.compress_orderby='time DESC', timescaledb.compress_segmentby='device');
INSERT INTO test_i7672 VALUES ('2025-01-01','d1');

SELECT count(*) FROM (SELECT compress_chunk(show_chunks('test_i7672'))) c;

INSERT INTO test_i7672 VALUES
('2025-01-01','d1'),
('2025-01-01','d2')
ON CONFLICT DO NOTHING;

SELECT * FROM test_i7672 t ORDER BY t;

0 comments on commit 41b141f

Please sign in to comment.