Opened 14 minutes ago
#37503 assigned New feature
Add AddUniqueConstraintConcurrently and RemoveUniqueConstraintConcurrently operations for PostgreSQL
| Reported by: | Clifford Gama | Owned by: | Clifford Gama |
|---|---|---|---|
| Component: | contrib.postgres | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
See the 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 UniqueConstraints 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:
- Build the index with
CREATE UNIQUE INDEX CONCURRENTLY, then attach it withALTER TABLE ... ADD CONSTRAINT ... UNIQUE USING INDEX, which only takes a brief lock. This is the approach used by django-pg-migration-tools. - Raise an error.
- Fall back to the non-concurrent behavior (the original proposal).
Option 1 would allow covering every UniqueConstraint.