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.
| Kind | constraint |
|---|---|
| Example Language | SQL |
📝 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
| conindid | A pg_constraint column pointing at the OID of the index backing the constraint; 0 when there is none. |
|---|---|
| USING INDEX | Promotes 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
- A constraint's index is named `
_
_key` (or ` _pkey` for a primary key) and disappears with the constraint.
- A foreign key creates no index on the referenced side; add your own index on the child's referencing column, otherwise cascading deletes get slow.
- `ALTER TABLE ... ADD CONSTRAINT ... UNIQUE USING INDEX` promotes an existing unique index into a constraint, a common way to consolidate duplicate indexes.
- With USING INDEX the index must be unique, non-partial, non-expression and not already owned by another constraint.
💡 Related Cases
More case studies coming soon.📚 Related Reference
❓ FAQ
QHow do I find which index backs a constraint?AQuery `pg_constraint.conindid`, which holds the index OID; casting it with `::regclass` yields the index name directly: `SELECT conname, conindid::regclass FROM pg_constraint WHERE conrelid = 't'::regclass`.QWhy does the index disappear when I drop the constraint?AThat index belongs to the constraint rather than standing on its own, so dropping one drops the other. If you want the index to outlive any constraint, create it with CREATE UNIQUE INDEX and avoid adding a redundant UNIQUE constraint on the same column.🙏 帮我们做得更好
我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……
如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏