Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: support multiple arbiter indexes for INSERT ON CONFLICT DO UPDATE #53170

Open
mgartner opened this issue Aug 20, 2020 · 0 comments
Open
Labels
A-partial-indexes Relating to partial indexes. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) docs-done docs-known-limitation T-sql-queries SQL Queries Team X-anchored-telemetry The issue number is anchored by telemetry references.

Comments

@mgartner
Copy link
Collaborator

mgartner commented Aug 20, 2020

We currently do not support multiple arbiter indexes for INSERT ON CONFLICT DO UPDATE.

Postgres:

marcus=# CREATE TABLE t (a INT, b INT);
CREATE TABLE

marcus=# CREATE UNIQUE INDEX i ON t (a) WHERE b > 0;
CREATE INDEX

marcus=# CREATE UNIQUE INDEX i2 ON t (a) WHERE b < 0;
CREATE INDEX

marcus=# INSERT INTO t VALUES (1, 1) ON CONFLICT (a) WHERE b < 0 AND b > 0 DO UPDATE SET b = 0;
INSERT 0 1

CockroachDB:

root@127.0.0.1:63997/defaultdb> SET experimental_partial_indexes=on;
SET

root@127.0.0.1:63997/defaultdb> CREATE TABLE t (a INT, b INT);
CREATE TABLE

root@127.0.0.1:63997/defaultdb> CREATE UNIQUE INDEX i ON t (a) WHERE b > 0;
CREATE INDEX

root@127.0.0.1:63997/defaultdb> CREATE UNIQUE INDEX i2 ON t (a) WHERE b < 0;
CREATE INDEX

root@127.0.0.1:63997/defaultdb> INSERT INTO t VALUES (1, 1) ON CONFLICT (a) WHERE b < 0 AND b > 0 DO UPDATE SET b = 0;
ERROR: unimplemented: there are multiple unique or exclusion constraints matching the ON CONFLICT specification
SQLSTATE: 0A000
HINT: You have attempted to use a feature that is not yet implemented.
See: https://go.crdb.dev/issue/53170

Workaround

The current workarounds are to:

  • Make the ON CONFLICT WHERE clause more strict so that it only matches a single unique index.
  • Or, drop all but one unique partial index that matches the ON CONFLICT WHERE clause.

Notes

In order to do this, we'll have to loop through all arbiter indexes and left-join them together to build a composite canary column that dictates whether a row should be inserted or updated. This is similar to what is done for INSERT ON CONFLICT DO NOTHING with the added complexity of the canary column.

Jira issue: CRDB-3894

@mgartner mgartner added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-partial-indexes Relating to partial indexes. labels Aug 20, 2020
@mgartner mgartner added docs-known-limitation X-anchored-telemetry The issue number is anchored by telemetry references. labels Aug 26, 2020
@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Feb 16, 2023
@mgartner mgartner moved this to Backlog (DO NOT ADD NEW ISSUES) in SQL Queries Jul 24, 2023
@michae2 michae2 moved this from Backlog (DO NOT ADD NEW ISSUES) to New Backlog in SQL Queries Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-partial-indexes Relating to partial indexes. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) docs-done docs-known-limitation T-sql-queries SQL Queries Team X-anchored-telemetry The issue number is anchored by telemetry references.
Projects
Status: Backlog
Development

No branches or pull requests

2 participants