cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

Support UPSERT & INSERT .. ON CONFLICT under non-serializable isolations

Open mw5h opened this issue 1 year ago • 0 comments

Description

As described in #110873, explicit uniqueness checks are not yet supported under non-serializable isolations. Full support for these will require predicate locks, as described by #126592, but we can take incremental steps while we wait for those to be available. The ask here is to support INSERT .. ON CONFLICT and UPSERT using the the tombstone mechanism described in #110873.

To Reproduce

./cockroach demo --global --nodes=6 --multitenant=false

CREATE TABLE ab (a INT NOT NULL, b INT, PRIMARY KEY (a), UNIQUE (b)) LOCALITY REGIONAL BY ROW;
INSERT INTO ab VALUES (1, 1);
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED;```

ON CONFLICT:

[email protected]:26257/movr> INSERT INTO ab VALUES (2, 1) ON CONFLICT (b) DO NOTHING; ERROR: unimplemented: explicit unique checks are not yet supported under read committed isolation SQLSTATE: 0A000 HINT: You have attempted to use a feature that is not yet implemented. See: https://go.crdb.dev/issue-v/110873/dev


UPSERT:

[email protected]:26257/movr> UPSERT INTO ab VALUES (2, 1); ERROR: unimplemented: explicit unique checks are not yet supported under read committed isolation SQLSTATE: 0A000 HINT: You have attempted to use a feature that is not yet implemented. See: https://go.crdb.dev/issue-v/110873/dev


**Expected behavior**
For ON CONFLICT, PostgreSQL 16 produces:
```test=# INSERT INTO ab VALUES (2, 1) ON CONFLICT (b) DO NOTHING;
INSERT 0 0```


Under serializable, UPSERT produces:

```demo@localhost:26262/movr> upsert into ab values (2, 1);
ERROR: duplicate key value violates unique constraint "ab_b_key"
SQLSTATE: 23505
DETAIL: Key (b)=(1) already exists.
CONSTRAINT: ab_b_key
demo@localhost:26262/movr> upsert into ab values (1, 2);
INSERT 0 1

Time: 275ms total (execution 275ms / network 0ms)```


**Additional context**
UPSERT-style inserts are pretty popular, so it may be difficult for users to make use of implicit partitioning / region by row tables under READ COMMITTED isolation until this is completed.

Jira issue: CRDB-41728

mw5h avatar Aug 28 '24 22:08 mw5h