Unique Index (UNIQUE)
Last updated: 2026-11-04
Unique Index (UNIQUE)
CREATE UNIQUE INDEX enforces uniqueness on the indexed columns while building the index. It behaves like a UNIQUE constraint, but the two differ in the system catalogs: a UNIQUE constraint owns an underlying index (visible via conindid in pg_constraint), while CREATE UNIQUE INDEX creates only an index and no constraint record. Unique indexes are also the usual way to implement soft deletes — a WHERE clause restricts uniqueness to the rows that are still live.
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE UNIQUE INDEX <index_name> ON <table_name> ( <column_name> ) [ WHERE <condition> ]
⚙️ Parameters
| index_name | Name of the new index. |
|---|---|
| table_name | The table to index. |
| column_name | The indexed column(s); values must be unique (NULLs never conflict and may repeat). |
| where_condition | Optional partial-index predicate that restricts uniqueness to the matching rows. |
Returns:No return value. On success the command tag is CREATE INDEX.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
CREATE TABLE
CREATE INDEX
INSERT 0 5000
ANALYZE
-- pg_indexes result: both the primary key index and the unique index are listed
indexname | indexdef
----------------------+-------------------------------------------------------------------------------------
idx_demo_unique_pkey | CREATE UNIQUE INDEX idx_demo_unique_pkey ON public.idx_demo_unique USING btree (id)
idx_unique_email | CREATE UNIQUE INDEX idx_unique_email ON public.idx_demo_unique USING btree (email)
(2 rows)
-- EXPLAIN: the unique index also speeds up lookups
Index Scan using idx_unique_email on idx_demo_unique (cost=0.29..8.30 rows=1 width=4)
Index Cond: (email = 'user42@example.com'::text)
Index Searches: 1
Buffers: shared hit=1 read=2
Planning Time: 0.185 ms
Execution Time: 0.043 ms
(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.)
📌 Key Points
- Inserting a duplicate value raises duplicate key value violates unique constraint — uniqueness is an immediate database guarantee.
- NULLs never conflict: a unique index allows many rows with NULL, matching the SQL standard where NULL does not compare equal.
- A unique constraint and a unique index are functionally equivalent, but only the constraint appears in pg_constraint and can be the target of a foreign key.
- Partial unique indexes are the standard soft-delete recipe: CREATE UNIQUE INDEX ON users (email) WHERE deleted_at IS NULL.
- Unique indexes can only be built on B-tree; HASH, GIN and GiST do not support unique constraints.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QShould I use a unique constraint or a unique index?
AUse a UNIQUE constraint when a foreign key must reference it, when it should appear in pg_constraint, or when you want to manage it with ALTER TABLE ADD CONSTRAINT. Use a unique index when you want uniqueness over an expression or a subset of rows, such as lower(email).
QWhy can a unique index hold duplicate NULLs?
AThe SQL standard says NULL is not equal to NULL, so a uniqueness check cannot decide whether two NULLs conflict; PostgreSQL therefore allows any number of NULLs in a unique index. Add a NOT NULL constraint if you truly want to forbid them.