Databases

PostgreSQL DELETE of large historical batches stalls because referencing FK table lacks index

nordicbyte
1mo ago
Environment

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

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

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

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

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?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Missing index on foreign-key column leads to sequential scans during delete checks 95%
Long-running transactions holding snapshots, preventing VACUUM progress and lock release 60%
Large DELETE in one transaction causing excessive WAL and autovacuum lag 55%

Summary

Root cause is a missing index on the foreign-key column in the referencing table. When deleting rows from the parent, PostgreSQL must check for referencing rows; without an index it must perform an expensive sequential scan, holding locks and causing long waits. This both blocks the DELETE and causes autovacuum/backfill pressure, increasing bloat. Long-running transactions or prepared connections can exacerbate lock retention, and attempting non-concurrent blocking fixes (VACUUM FULL, CREATE INDEX without CONCURRENTLY) will worsen availability.

Suggested Fixes

Diagnostic step 1
Create the missing index with CREATE INDEX CONCURRENTLY ON audit(events_id); monitor progress in pg_stat_progress_create_index.
Diagnostic step 2
If index build is too slow, build the index on a replica and promote or use logical copy: create new table with index, copy in batches, swap with minimal lock.
Diagnostic step 3
Switch DELETE to batched deletes: loop DELETE FROM events WHERE created_at < X LIMIT 10000; COMMIT between batches; monitor pg_stat_activity and lock waits.

Continue troubleshooting

Matched from the title, description and tags