Constraints and Their Indexes

Last updated: 2026-11-04

Constraints and Their Indexes

Primary keys, unique constraints and foreign keys are all implemented with indexes underneath, and pg_constraint.conindid records the mapping. The relationship matters operationally: dropping a constraint also drops the index it owns, while a standalone index is unaffected, so the two have different lifecycles and must be managed separately.

Kindconstraint
Example LanguageSQL

📝 Syntax

Find the backing index: `pg_constraint.conindid`
Turn an index into a constraint: `ALTER TABLE t ADD CONSTRAINT c UNIQUE USING INDEX idx`
Drop both constraint and index: `ALTER TABLE t DROP CONSTRAINT c`

⚙️ Parameters

conindidA pg_constraint column pointing at the OID of the index backing the constraint; 0 when there is none.
USING INDEXPromotes an existing unique index into a table constraint, after which the index belongs to the constraint.

Returns:The query returns each constraint's name, kind and backing index. `UNIQUE USING INDEX` reports `"..." is not a unique index` when the given index is not unique.

▶ 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:

SELECT c.conname AS name, 'constraint' AS kind, c.conindid::regclass::TEXT AS index_name
  FROM pg_constraint c
 WHERE c.conrelid = 'accounts'::regclass AND c.contype IN ('p','u')
UNION ALL
SELECT i.indexname, 'standalone index', i.indexname FROM pg_indexes i
 WHERE i.tablename = 'accounts'
   AND i.indexname NOT IN (SELECT c2.conindid::regclass::TEXT FROM pg_constraint c2
                            WHERE c2.conrelid = 'accounts'::regclass AND c2.contype IN ('p','u'))
 ORDER BY kind, name
 name               | kind             | index_name
--------------------+------------------+--------------------
 accounts_email_key | constraint       | accounts_email_key
 accounts_pkey      | constraint       | accounts_pkey
 accounts_email_uq  | standalone index | accounts_email_uq
(3 rows)

📌 Key Points