Skip to blog content
From the notebook

Postgres doesn't index foreign keys: what it costs, and a query to find the missing ones

Postgres creates an index for every primary key and unique constraint, but not for the referencing side of a foreign key. Measured on PostgreSQL 17.5 with 200,000 rows: a lookup by the foreign key took about 11 ms instead of 0.44 ms, and a cascading delete of 50 parents took about 560 ms instead of 4 ms. Plus a catalog query that lists every unindexed foreign key.

Download .md

No, Postgres doesn’t create an index for a foreign key. It indexes the referenced side automatically (that’s a primary key or unique constraint), but not the referencing column, such as contacts.company_id. Without that index, two common operations scan the whole child table: looking up a parent’s children, and deleting or updating a parent, because Postgres must find the rows that point at it (for ON DELETE CASCADE, RESTRICT or SET NULL). I measured both on PostgreSQL 17.5 with 200,000 contacts across 2,000 companies. Fetching one company’s contacts took about 11 ms without the index and 0.44 ms with it. Deleting 50 companies with a cascade took about 560 ms without it and 4 ms with it. Supabase’s database advisor flagged four of these in a CRM I built; a catalog query below finds them in any Postgres database.

Foreign keys without an index, PostgreSQL 17.5, 2,000 companies and 200,000 contacts. Postgres created only companies_pkey and contacts_pkey, nothing on contacts.company_id. Fetching one company's 100 contacts: a sequential scan of about 11 ms without an index, a bitmap index scan of 0.44 ms with one. Deleting 50 companies with ON DELETE CASCADE: about 560 ms without the index, 4 ms with it. Fix: create index on contacts (company_id), and find the rest with a catalog query over pg_constraint and pg_index.

The parent’s primary key is indexed for free; the column that points at it isn’t.

Which indexes does Postgres create for you?

schema.sql
create table companies (id int primary key, name text);
create table contacts (
  id int primary key,
  company_id int not null references companies(id) on delete cascade,
  email text
);

After loading 2,000 companies and 200,000 contacts, pg_indexes lists exactly two indexes: companies_pkey and contacts_pkey. The foreign key constraint contacts_company_id_fkey exists, but no index supports it. Postgres leaves this to you on purpose: not every foreign key is queried from the child side, and every index slows down writes.

What does the missing index cost?

I ran both operations 20 times each in PGlite, which is PostgreSQL 17.5 compiled to WebAssembly, three runs in total:

OperationWithout indexWith index
One company’s 100 contacts (where company_id = 42)10.9–12.2 ms, sequential scan0.44 ms, bitmap index scan
Delete 50 companies, on delete cascade543–583 ms4 ms

The lookup reads all 200,000 rows to return 100. The delete is worse, because the cascade runs the same search once per deleted parent: 50 full scans of the child table, inside one transaction that holds locks the whole time. On a busy table, that’s the difference between an instant admin action and one that blocks writers.

The absolute numbers are from a laptop, and a real server’s will differ. The shape won’t: without the index, both costs grow with the size of the child table, not with the number of matching rows.

How do you find unindexed foreign keys?

This query lists every foreign key whose first column isn’t the first column of some index on the same table:

unindexed-fks.sql
select c.conrelid::regclass as table_name, c.conname as constraint_name, 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 not exists (
    select 1 from pg_index i
    where i.indrelid = c.conrelid and i.indkey[0] = c.conkey[1]
  );

Before the fix it returns contacts | contacts_company_id_fkey | company_id; after it, nothing. It checks only the first column, which covers single-column keys. For a composite foreign key, make sure an index starts with all of its columns in order. Supabase runs a similar check in its database advisor, under “unindexed foreign keys”.

The fix, and when to skip it

fix.sql
create index concurrently contacts_company_id_idx on contacts (company_id);

concurrently builds the index without blocking writes, which matters on a live table (it can’t run inside a transaction block, so migration tools may need it in a file of its own). Skip the index only when the child table is tiny and the parent is never deleted or re-keyed, for example a lookup table of ten countries. When in doubt, add it: an unused index costs some write time and disk, while a missing one turns deletes into full scans.

Summary

  • Postgres indexes primary keys and unique constraints, not the referencing column of a foreign key.
  • Without that index, child lookups and parent deletes or updates scan the whole child table; a cascade scans it once per parent.
  • Measured on PostgreSQL 17.5, 200,000 rows: ~11 ms vs 0.44 ms per lookup and ~560 ms vs 4 ms to delete 50 parents.
  • Find them with a pg_constraint/pg_index query and fix with create index concurrently.

The same hardening pass in that CRM also fixed how it read client IP addresses for rate limiting: X-Forwarded-For and the real client IP. Its migrations had their own trap: when migrations applied by hand break supabase db push. For full-text search with proper indexes in SQLite, see SQLite FTS5 and BM25 in Python.

Part of the guide Reliable backends in Node.js, Python and Go, which puts related articles in reading order.