fix(auditlog): replace an interrupted index build without blocking audit log writes

Merged
SA
3d ago
+98-30

What

#1499 landed the fix this PR was opened for: audit index builds and startup schema changes now run under one session lock, so a starting replica can no longer drop an index another replica is building. This branch now takes main's implementation and keeps only what it does not cover.

A build interrupted midway leaves an invalid index, which the next start drops and rebuilds. That drop was a plain DROP INDEX: it waits for every open transaction that has touched audit_logs, and audit writes queue behind it until that transaction ends. Both drops (model indexes, trigram search index) are now DROP INDEX CONCURRENTLY IF EXISTS.

User-visible impact

After an interrupted index build (for example a pod killed mid-rollout), the next start no longer stalls audit logging behind a long-running query on audit_logs.

Tests

  • TestRebuildingInterruptedIndexKeepsAuditLogsWritable: with a transaction open on audit_logs, a write succeeds while the rebuild waits. It fails on main, where the write times out, for both the model and the search index.
  • go test -race ./internal/auditlog/ ./internal/storage/... against PostgreSQL 18.

Summary by CodeRabbit

  • Bug Fixes
    • Request logs remain writable while invalid search indexes are rebuilt, reducing interruptions during recovery.
    • Invalid indexes are dropped and recreated without blocking normal request-log writes. Errors during rebuilding are reported, while valid existing indexes remain unchanged.
    • Search-index recovery now follows the same concurrent rebuilding behavior across affected index types, helping keep the service responsive during maintenance.