← Back to PostgreSQL, page 4

postgresql-0073 · Hard senior

Swap the positions of two items under a unique constraint using a deferrable constraint.

A non-deferrable unique constraint is checked as each row changes, so swapping two values in one UPDATE can fail partway through. A DEFERRABLE constraint is checked at the end of each statement, and SET CONSTRAINTS ... DEFERRED postpones the check to COMMIT. Only constraints declared deferrable can be deferred, and deferrable unique constraints cannot be used as ON CONFLICT arbiters.

Full answer

Test data:

sql
CREATE TABLE playlist (song text, position int, CONSTRAINT playlist_pos_key UNIQUE (position));
CREATE TABLE playlist2 (song text, position int, CONSTRAINT playlist2_pos_key UNIQUE (position) DEFERRABLE INITIALLY IMMEDIATE);
INSERT INTO playlist VALUES ('intro', 1), ('chorus', 2), ('outro', 3);
INSERT INTO playlist2 SELECT * FROM playlist;
sql
UPDATE playlist SET position = CASE song WHEN 'intro' THEN 2 WHEN 'chorus' THEN 1 END WHERE song IN ('intro', 'chorus');

Output:

text
ERROR:  duplicate key value violates unique constraint "playlist_pos_key"
sql
UPDATE playlist2 SET position = CASE song WHEN 'intro' THEN 2 WHEN 'chorus' THEN 1 END WHERE song IN ('intro', 'chorus') RETURNING song, position;

PostgreSQL returns:

text
  song  | position
--------+----------
 intro  | 2
 chorus | 1
(2 rows)
sql
UPDATE playlist2 SET position = 1 WHERE song = 'outro';

Result on PostgreSQL 17:

text
ERROR:  duplicate key value violates unique constraint "playlist2_pos_key"
sql
BEGIN; SET CONSTRAINTS playlist2_pos_key DEFERRED; UPDATE playlist2 SET position = 1 WHERE song = 'outro'; UPDATE playlist2 SET position = 3 WHERE song = 'chorus'; COMMIT; SELECT song, position FROM playlist2 ORDER BY position;

Result:

text
  song  | position
--------+----------
 outro  | 1
 intro  | 2
 chorus | 3
(3 rows)

Swapping two positions in one UPDATE fails on the plain table, because a non-deferrable unique index is checked as each row changes, and the first changed row collides with the row not yet updated. The same single statement succeeds on the deferrable table even in IMMEDIATE mode: PostgreSQL checks deferrable unique constraints at the end of each statement. Moves that span several statements still fail in IMMEDIATE mode, as the third run shows. After SET CONSTRAINTS ... DEFERRED, the check runs at COMMIT, so outro and chorus can be moved one statement at a time. A design alternative is to store positions with gaps (10, 20, 30) or fractional ranks so that most reorderings touch one row.

sql
CREATE TABLE playlist (song text, position int, CONSTRAINT playlist_pos_key UNIQUE (position));
CREATE TABLE playlist2 (song text, position int, CONSTRAINT playlist2_pos_key UNIQUE (position) DEFERRABLE INITIALLY IMMEDIATE);
INSERT INTO playlist VALUES ('intro', 1), ('chorus', 2), ('outro', 3);
INSERT INTO playlist2 SELECT * FROM playlist;

UPDATE playlist SET position = CASE song WHEN 'intro' THEN 2 WHEN 'chorus' THEN 1 END WHERE song IN ('intro', 'chorus');

UPDATE playlist2 SET position = CASE song WHEN 'intro' THEN 2 WHEN 'chorus' THEN 1 END WHERE song IN ('intro', 'chorus') RETURNING song, position;

UPDATE playlist2 SET position = 1 WHERE song = 'outro';

BEGIN; SET CONSTRAINTS playlist2_pos_key DEFERRED; UPDATE playlist2 SET position = 1 WHERE song = 'outro'; UPDATE playlist2 SET position = 3 WHERE song = 'chorus'; COMMIT; SELECT song, position FROM playlist2 ORDER BY position;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Non-deferrable uniques are checked per row
  • Deferrable ones at statement end or commit
  • SET CONSTRAINTS DEFERRED inside a transaction
  • Deferrable uniques cannot arbitrate ON CONFLICT
  • Gapped positions reduce swaps

Common mistakes

  • Using temporary negative values as a workaround everywhere
  • Deferring constraints without need

Follow-ups

  • What is the cost of deferrable unique indexes?
  • How would you design drag-and-drop ordering at scale?

Tips

  • Show the statement-end check