Skip to content

unique constraint violated with compressed INSERT #10057

Description

@akuzm
-- ON CONFLICT DO NOTHING on a compressed hypertable silently inserts a 
-- duplicate when the UNIQUE NULLS NOT DISTINCT arbiter includes a
-- segmentby column and the inserted value is NULL.
          
CREATE TABLE iot_readings ( 
    ts   timestamptz NOT NULL,
    seg1 int,
    seg2 int,
    UNIQUE NULLS NOT DISTINCT (seg2, ts)
);
SELECT create_hypertable('iot_readings', 'ts', create_default_indexes => false);
                                                           
ALTER TABLE iot_readings SET ( 
    timescaledb.compress,
    timescaledb.compress_segmentby = 'seg1, seg2',
    timescaledb.compress_orderby   = 'ts'
);
  
INSERT INTO iot_readings VALUES ('2025-01-01 00:00:00+00', 1, NULL);
SELECT compress_chunk(show_chunks('iot_readings'));
  
INSERT INTO iot_readings VALUES ('2025-01-01 00:00:00+00', 1, NULL)
    ON CONFLICT DO NOTHING;             -- INSERT 0 1, duplicate inserted
SELECT count(*) FROM iot_readings;      -- 2 (expected 1)
                                     
SELECT decompress_chunk(show_chunks('iot_readings'));
-- ERROR:  duplicate key value violates unique constraint

Looks like there's some problem with handling null search for segmentby columns. It doesn't reproduce if you only have one segmentby.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ColumnstoreRelated to the column store / compressionbug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions