PostgreSQL DELETE of large historical batches stalls because referencing FK table lacks index
Environment
PostgreSQL 13 on Debian 11, 8-core VM, ~500GB disk. Application writes to events table (parent) and an audit table with foreign key referencing events.id (no index on audit.events_id). Scheduled job deletes old events in multi-million rows batches during off-peak.
Symptoms
DELETE FROM events WHERE created_at < '2023-01-01' stalls for minutes and blocks other DML. Application requests time out; autovacuum does not reclaim space quickly and database bloat grows.
Observations
pg_stat_activity shows DELETE holding row-level locks and waiting for sequential scans on audit table. pg_locks indicates many ROW SHARE locks. EXPLAIN showed a sequential scan on audit referencing column. No concurrent index. VACUUM is running intermittently but does not finish while deletes queue.
Already attempted
Tried VACUUM FULL (would block), killed delete session and restarted job with smaller LIMIT but still observed long waits. Attempted CREATE INDEX concurrently on audit.events_id but it failed due to heavy I/O and long background build time.
Question
How can I safely resume deleting large historical rows without causing long locks and blocking the app; what reversible steps minimize downtime and bloat?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases27% match
PostgreSQL VACUUM stalls on partitioned table during heavy insert bursts; autovacuum appears idle
Open0 answers - Databases26% match
PostgreSQL replica SQL apply thread stalls 'waiting for table metadata lock' after large ALTER TABLE on primary
Open0 answers - Databases25% match
PostgreSQL logical replication slot grows until disk full after subscriber disconnects during nightly load
Open0 answers - Databases24% match
PostgreSQL logical replication falls behind after ALTER TABLE ADD COLUMN with non-null DEFAULT on large table
Open0 answers - Databases24% match
PostgreSQL warns of imminent transaction-id wrap after bulk DELETEs — autovacuum not preventing anti-wraparound
Solved1 answer