Sub topic 8
Enforcing Uniqueness on a Subset of Rows
Assume you know basic CREATE TABLE, SELECT, and WHERE syntax. You should also understand that an index stores searchable values from table rows. This chapter focuses on PostgreSQL unique partial indexes, which combine uniqueness with a filter.
Why ordinary uniqueness can be too strict
Suppose an application allows a user to delete an account without removing its row. The row keeps historical information, but its email address should become available for a new account.
A normal unique constraint applies to every row:
ALTER TABLE users
ADD CONSTRAINT users_email_key UNIQUE (email);This prevents two rows from having the same email, including one active row and one soft-deleted row. That is stricter than the business rule.
The rule is:
- Active users must have distinct email addresses.
- Deleted users do not participate in email uniqueness.
- An active user and a deleted user may share an email address.
A unique partial index expresses this rule:
CREATE UNIQUE INDEX users_active_email_key
ON users (email)
WHERE deleted_at IS NULL;The WHERE clause is the index predicate. PostgreSQL adds only rows satisfying that predicate to the index. The UNIQUE property then applies only to those indexed rows.
How the index enforces the rule
Consider these rows:
| id | deleted_at | |
|---|---|---|
| 1 | [email protected] | NULL |
| 2 | [email protected] | 2026-01-10 09:00:00+00 |
| 3 | [email protected] | NULL |
Rows 1 and 3 are indexed because they are active. Row 2 is not indexed because it is deleted.
This insert succeeds because the new row is deleted:
INSERT INTO users (email, deleted_at)
VALUES ('[email protected]', now());This insert fails because another active row already has the same email:
INSERT INTO users (email)
VALUES ('[email protected]');PostgreSQL reports a duplicate-key violation for users_active_email_key. The database enforces the rule even when two requests try to create the same active account at nearly the same time.
Predicate matching and NULL values
The index predicate describes which rows are protected. To use the index for a lookup, PostgreSQL must be able to determine that the query’s conditions imply the predicate. For example:
EXPLAIN
SELECT id
FROM users
WHERE deleted_at IS NULL
AND email = '[email protected]';This query states the same active-row condition, so the planner can consider users_active_email_key.
A partial index does not automatically make every query on email use that index. A query that omits deleted_at IS NULL asks about both active and deleted rows, so the partial index cannot answer it by itself.
Remember that SQL treats NULL specially. A unique index normally allows multiple NULL values because NULL means an unknown value, not a value equal to another NULL. If every active row must have an email, keep email NOT NULL as well as the unique partial index.
Choosing this design
Use a unique partial index when uniqueness applies to a clearly defined subset, such as active records, unpublished records, or rows belonging to the current version. Keep the predicate simple and stable so both writers and queries express the same rule.
If uniqueness should apply to every row, use a regular UNIQUE constraint instead. If the rule depends on changing application state or a value in another table, a partial index is usually the wrong mechanism because its predicate is evaluated from the indexed row and must remain immutable.
Exercise
What does the following index guarantee, and which insert violates it? CREATE UNIQUE INDEX one_primary_address ON addresses (user_id) WHERE is_primary;
Show answer
It guarantees that each user has at most one row where is_primary is true. An insert or update that gives a user a second primary address violates the index. Rows where is_primary is false are not subject to this uniqueness rule.