FULL AI ANALYSIS

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

Generated Oct 20, 2026, 9:41:02 AM

Analysis 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.

Possible 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%

Triggers or ON DELETE CASCADE causing cascading scans and locking

30%

Suggested fixes

1

Diagnostic step 1

Create the missing index with CREATE INDEX CONCURRENTLY ON audit(events_id); monitor progress in pg_stat_progress_create_index.

2

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.

3

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.

4

Diagnostic step 4

Ensure no long-running transactions: inspect pg_stat_activity and terminate or coordinate clients keeping old snapshots.

5

Diagnostic step 5

After deletes, VACUUM ANALYZE the affected tables; if bloat is severe, plan a maintenance window for VACUUM FULL or pg_repack.