How do I simultaneously update unique keys in PostgreSQL?
-
16-03-2021 - |
質問
CREATE TABLE widget (
id serial PRIMARY KEY,
name text NOT NULL,
ordinal int NOT NULL UNIQUE
);
I have the data
id | name | ordinal
----+------+---------
1 | A | 1
2 | B | 2
3 | C | 3
I would like to update it to
id | name | ordinal
----+------+---------
1 | A | 3
2 | B | 2
3 | C | 1
Touching records as little as possible (i.e. don't rewrite the entire record set, don't kick off unnecessary triggers), what's the generally applicable approach to updating ordinal
to be the target values?
A vanilla update just gives me constraint violations, even if it happens in a single statement.
And dropping and recreating the unique constraint is expensive and poor concurrency.
This seems like a common enough problem that there ought to be a good way to do this, that I just can't think of.
解決
Define the constraint as deferrable:
CREATE TABLE widget (
id serial PRIMARY KEY,
name text NOT NULL,
ordinal int NOT NULL UNIQUE DEFERRABLE
);
Then you can update it in one statement:
update widget
set ordinal = t.new_ordinal
from (
values (1, 3), (3,1)
) as t(id, new_ordinal)
where t.id = widget.id
他のヒント
A plain UNIQUE
constraint is NOT DEFERRABLE
by default. Unique violations are checked after each row. The manual phrases this as:
checked immediately after every command
But it's really checked for every individual written row.
If you define the UNIQUE
constraint DEFERRABLE
(like a_horse provided), unique violations are checked after each statement. Multiple CTEs within the same query still count as a single statement in this respect.
CREATE TABLE widget_deferrable (
id serial PRIMARY KEY,
ordinal int NOT NULL UNIQUE DEFERRABLE -- !
);
If you also actually set the constraint as DEFERRED
, the check for unique violations is deferred until after each transaction.
CREATE TABLE widget_deferred (
id serial PRIMARY KEY,
ordinal int NOT NULL UNIQUE DEFERRABLE INITIALLY DEFERRED --!
);
Comprehensive demo:
db<>fiddle here
Further reading: