Full answer
Test data:
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');Output:
ERROR: duplicate key value violates unique constraint "playlist_pos_key"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:
song | position
--------+----------
intro | 2
chorus | 1
(2 rows)UPDATE playlist2 SET position = 1 WHERE song = 'outro';Result on PostgreSQL 17:
ERROR: duplicate key value violates unique constraint "playlist2_pos_key"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:
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.
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