3 places
Page 3 of 8 · Foreign keys

A foreign key protects a link only inside one database

It refuses broken links with no app code, but it cannot cross databases, and copied tables ignore it.

The purpose

A foreign key makes the database refuse a broken link

A foreign key is one line in the table definition. Look at the marked line in this seat table:

seat.sqlsql
create table quest (
id uuid primary key,
seats int not null
);
create table seat (
quest_id uuid not null references quest(id) on delete cascade,
person_id text not null,
primary key (quest_id, person_id)
);

That line gives 2 promises. A seat for a quest that does not exist is refused. And when the quest is deleted, its seats go in the same moment.

The promise holds at every moment, with no code of ours.

There is no worker and no retry. A seat never points at nothing, not even briefly.

How important

A foreign key matters most where a broken link costs money

Not every broken link hurts the same. Compare 2 cases.

A paid seat in a deleted quest is lost money. Somebody must find it and refund it. Here the link must never break.

A like on a deleted post is invisible. Nobody can open the post, so nobody sees the like. Here a short broken moment is fine.

This table sorts common links by what a broken moment costs:

Where a foreign key earns its keep.
LinkA broken moment costsForeign key needed
Seat to quest, with a limitA seat nobody can use, or 11 seats of 10Yes
Gem ledger line to gem balanceWrong moneyYes
Check-in to habitA wrong streakYes, and it is free: same database
Like or comment to postNothing visibleNo
Friend row to the other personA friend who is gone, for a momentNo

So the rule of thumb: money and limits need a foreign key. Social links can do without.

The limit

A foreign key cannot cross two databases, and a copied table ignores it

A foreign key lives inside one Postgres. It cannot point from US West to Singapore.

There is a second, quieter limit. Postgres switches foreign key checks off while it applies copied rows. From docs

We tested what that means. Our list of charities is written in one region and copied to the others. A habit row points at a charity with a normal foreign key.

Then we hard deleted one charity at the source. This is what the copy in Singapore did:

3rows
in Singapore still pointed at the deleted charity
Measured
0errors
were raised by Postgres for those 3 broken links
Measured

The foreign key was there, and it stayed silent. 2 habit rows and 1 penalty row pointed at nothing.

Postgres 17 manual, logical replication architecture: the apply process runs with session_replication_role set to replica, which disables foreign key checks. Our test: 22 Nov 2026.

The count

Most links stay foreign keys in both models

Losing foreign keys everywhere would be a big cost. That is not what happens.

We counted the links in the test database, which models a habit app with friends and quests:

19strong refs
stay foreign keys, because both rows live in one database
Measured
12weak refs
cross databases and need our own parts
Measured
4 of 32
foreign keys of our habit app today would become weak refs
Measured

The first 2 tiles count the test database. The third counts our real habit app, a different database.

Most links never leave one database. Only links that cross a region lose the foreign key.