﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37503	Add AddUniqueConstraintConcurrently and RemoveUniqueConstraintConcurrently operations for PostgreSQL	Clifford Gama	Clifford Gama	"See the [https://github.com/django/new-features/issues/124 new feature proposal] for a full discussion.

`AddIndexConcurrently` and `RemoveIndexConcurrently` let `Meta.indexes` entries be created and dropped without locking writes to the table, but `UniqueConstraint` in `Meta.constraints` has no equivalent. Many `UniqueConstraint`s are built as `CREATE UNIQUE INDEX` (those with `expressions`, `condition`, `include`, or `opclasses`), so they can use `CONCURRENTLY` directly.

The proposal is to add `AddUniqueConstraintConcurrently` and `RemoveUniqueConstraintConcurrently` to `django.contrib.postgres.operations`, with `atomic = False` and the same in-transaction check as the existing concurrent index operations.

There's an open question about what to do with plain `fields=[...]` constraints, which are created with `ALTER TABLE ... ADD CONSTRAINT ... UNIQUE`. The options are:
 1. Build the index with `CREATE UNIQUE INDEX CONCURRENTLY`, then attach it with `ALTER TABLE ... ADD CONSTRAINT ... UNIQUE USING INDEX`, which only takes a brief lock. This is the approach used by [https://django-pg-migration-tools.readthedocs.io/en/latest/usage/operations.html#SaferAddUniqueConstraint django-pg-migration-tools].
 2. Raise an error.
 3. Fall back to the non-concurrent behavior (the original proposal).

Option 1 would allow covering every `UniqueConstraint`.

"	New feature	assigned	contrib.postgres	dev	Normal				Unreviewed	0	0	0	0	0	0
