I don't trust ON DELETE CASCADE
Postgres ON DELETE CASCADE turns a single-row delete into an unbounded transaction that can cause outages at scale. The cascade holds RowExclusiveLock on every table in the tree, so a migration requesting AccessExclusiveLock queues behind it and blocks all later writes. It can also generate hundreds of gigabytes of WAL that stall synchronous replication, and it falls back to sequential scans on unindexed foreign keys. With logical replication it can leave orphaned rows on subscribers, which skip cascade triggers, and it fails outright on published tables lacking a replica identity. The recommended alternative is soft deletes via a deleted_at column, followed by batched background reaping and vacuum, keeping transactions to hundreds or low thousands of rows.
Related posts
- Postgres DELETE Can Actualy Scale
DBOS mediumAdeptPostgres DELETE can scale for data retention if you design around MVCC churn, cache locality and dead tuples. In DBOS's durable workflow system, ON DELETE CASCADE foreign keys from step_outputs to workflow_status forced scattered page fetches, so the team dropped the foreign keys and batch-deleted each table separately in physical creation order, using a NOT EXISTS anti-join to keep data for still-active workflows. A created_at index lookup occasionally took 20 minutes because millions of unvacuumed dead tuples piled up at the front of the index and each had to be checked against the heap. Running a manual VACUUM before and after each retention pass fixed it, and the combined changes made deletion an order of magnitude faster, while keeping per-row flexibility that partition dropping can't offer for long-running or heterogeneous retention.
- Navigating PostgreSQL Locks with Neon and Atlas
Neon mediumAdept
Adding a UNIQUE constraint or setting a column NOT NULL in Postgres triggers a full table scan under an ACCESS EXCLUSIVE lock, which blocks all reads and writes and can take down an API on large tables. The safer sequences are CREATE UNIQUE INDEX CONCURRENTLY followed by ADD UNIQUE USING INDEX, and a NOT VALID CHECK constraint followed by VALIDATE before SET NOT NULL. To catch such migrations automatically, a GitHub Actions pipeline runs Atlas's migrate lint (check PG105) on pull requests. It then applies the migration to a Neon branch cloned from production, with a 10s statement_timeout hook, so long-locking migrations fail the build before deployment.
- Post mortem on Linear incident from Jan 24th, 2024
Linear mediumAdept
A database migration using `TRUNCATE TABLE ... CASCADE` on two new, unused tables silently wiped every production table with foreign keys to them, including issue and document descriptions, comments, notifications and reactions. Layered caching in Linear's sync engine hid the loss for about 30 minutes, and sync-lag warnings were excluded from alerting. Because point-in-time recovery had never been tested, the team restored a full backup from hours earlier, causing an hour of downtime and five hours of lost changes. They then replayed sync-engine action logs per workspace, with dry-runs first, recovering over 99% of data within 36 hours; follow-ups include revoking TRUNCATE privileges, linting migrations, PITR tooling and a read-only mode.
- How we migrate TBs from Heroku without logical replication
PlanetScale mediumAdeptPlanetScale's open-source Heroku Migrator moves multi-terabyte Postgres databases off Heroku standard plans, which lack WAL access and logical replication, by using Bucardo trigger-based replication: triggers record changes in delta tables, a one-time COPY loads existing data, and a worker replays the backlog before a controlled switchover with verification. Version 2.0 fixes failures seen on large migrations. Bucardo's single-transaction initial copy restarted from zero after any interruption, so the migrator now drops secondary indexes before the copy and rebuilds them in parallel afterwards, raising throughput from about 60 to nearly 300 GB/h. It also disables transaction_timeout on Bucardo's connections, sets TCP keepalives so silently dropped long-lived connections fail within about a minute, and tunes host TCP buffers. A discovery CLI and the Liftoff assessment flag tables without primary keys, incompatible extensions and sizing before the migration starts.
- What's new in Postgres 15?
Supabase easyNovicePostgreSQL 15 tightens the default permission model by revoking CREATE on the public schema from non-owners and adds security_invoker views that check the caller's permissions. Other headline changes include in-memory and on-disk sorts up to 4x faster, the SQL-standard MERGE command, and row/column filtering, two-phase commit and conflict-skipping options for logical replication. It also brings parallel SELECT DISTINCT, JSON logging, Zstandard/LZ4 compression for WAL and server-side base backups, and new regexp functions. The recommendation is to upgrade mainly for bug fixes and stability, using pg_upgrade and checking the release notes for cases that need manual action, such as revoking public-schema CREATE on migrated databases.