← Back to PostgreSQL, page 5

postgresql-0083 · Hard senior

Find foreign keys whose referencing columns have no index, using the system catalogs.

PostgreSQL indexes the referenced primary key but not the referencing column. Without an index on the child column, deleting or updating a parent scans the whole child table, and joins from parent to children are slow. A catalog query comparing pg_constraint foreign keys with pg_index leading columns lists the gaps.

Full answer

Test data:

sql
CREATE TABLE customers (id int PRIMARY KEY);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers(id));
CREATE TABLE reviews (id int PRIMARY KEY, customer_id int REFERENCES customers(id), order_id int REFERENCES orders(id));
CREATE INDEX reviews_customer ON reviews (customer_id);
sql
SELECT c.conrelid::regclass::text AS table_name, c.conname AS fk, a.attname AS column_name
FROM pg_constraint c
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1]
WHERE c.contype = 'f'
  AND c.connamespace = current_schema()::regnamespace
  AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE i.indrelid = c.conrelid AND i.indkey[0] = c.conkey[1])
ORDER BY 1, 2;

Result on PostgreSQL 17:

text
 table_name |            fk           | column_name
------------+-------------------------+-------------
 orders     | orders_customer_id_fkey | customer_id
 reviews    | reviews_order_id_fkey   | order_id
(2 rows)

The query reports orders.customer_id and reviews.order_id, while reviews.customer_id is skipped because an index starts with that column. It checks only the first column of each key, which covers the common single-column case; composite foreign keys need a comparison of the full column lists. Not every foreign key needs an index: tiny tables and parents that are never deleted can do without, but on large child tables the missing index turns each parent delete into a full scan while holding locks.

sql
CREATE TABLE customers (id int PRIMARY KEY);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers(id));
CREATE TABLE reviews (id int PRIMARY KEY, customer_id int REFERENCES customers(id), order_id int REFERENCES orders(id));
CREATE INDEX reviews_customer ON reviews (customer_id);

SELECT c.conrelid::regclass::text AS table_name, c.conname AS fk, a.attname AS column_name
FROM pg_constraint c
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1]
WHERE c.contype = 'f'
  AND c.connamespace = current_schema()::regnamespace
  AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE i.indrelid = c.conrelid AND i.indkey[0] = c.conkey[1])
ORDER BY 1, 2;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Referencing columns are not indexed automatically
  • Missing indexes slow parent deletes and joins
  • pg_constraint lists foreign keys
  • pg_index shows leading columns
  • Composite keys need full comparison

Common mistakes

  • Assuming foreign keys create indexes
  • Indexing every foreign key blindly on tiny tables

Follow-ups

  • How would you extend this to composite keys?
  • What does a missing index do to ON DELETE CASCADE?

Tips

  • Explain the leading column check