fix(storage): skip existing indexes at PostgreSQL startup and bound the wait for table locks
Merged
SA
3d ago+489+489+012345678901234567890123456789-16-16-01234567890123456789
Follow-up to #1447 (merged); it covers what that PR left.
What
- Existing indexes are skipped.
CREATE INDEX IF NOT EXISTSon an index that exists still queues for a SHARE lock on its table. That waits for every write in progress (the retentionDELETE, for one), and every later write then queues behind it.Schemaon PostgreSQL now looks the index up in the catalog first and does not send the statement when it exists. With #1447, a start against an up-to-date schema takes no table locks at all. - Needed schema changes wait for their lock in short attempts.
SchemaandAddColumnssetlock_timeoutto 500 ms inside their transaction and retry for about a minute per statement (40 attempts, 1 s apart), then returnsqlx.ErrTableLocked. Before, a realADD COLUMNorCREATE INDEXwaited indefinitely behind a long statement and stalled the table for all replicas meanwhile. 500 ms stays under the defaultdeadlock_timeout, so the schema change gives up before PostgreSQL would cancel a concurrentCREATE INDEX CONCURRENTLYit is waiting on. - What is pending is checked again under the schema lock, on every attempt. A replica that queued behind another one finds the index or column already there and does not queue for the table to change nothing.
- The audit store's index list ran through
db.Exec; it now goes throughdb.Schemaper statement, still best-effort, and stops at the first statement that gave up on the table lock.
User-visible impact
Starting or restarting a replica on PostgreSQL no longer stalls writes on the others. A start can now fail with schema change could not lock its table where it used to hang, if a long statement holds the table for the whole minute during a table or column migration; an audit log index in that position is skipped with a warning and created on the next start. Documented in docs/guides/updating.mdx. SQLite is unchanged.
For reviewers
- A statement the index pattern does not match runs as written, so a parse miss costs a lock, never a skipped index. The existence check matches
IF NOT EXISTSsemantics: any relation of that name in the table's schema. lock_timeoutis set only after the advisory lock is held, so it bounds the wait for a table and not the wait for other replicas.
Tests (PostgreSQL 18)
TestAppliedSchemaDoesNotWaitForTableLock: with an uncommitted write holding the table, re-applying an applied schema returns at once.TestSchemaChangeGivesUpOnAHeldTableLock,TestSchemaChangeRetriesUntilTheTableLockIsFree.TestSchemaChangeRechecksAfterWaitingForAnotherReplica: another replica applies the change while this one waits for the schema lock, then a write takes the table; fails with the lock timeout without the re-check.go test -raceforinternal/storage/...,auditlog,usage,ratelimit,budget,workflows,app.
Summary by CodeRabbit
- Bug Fixes
- PostgreSQL table, column, and index migrations now wait briefly for table locks and retry before failing startup if a lock remains. Changes already applied—including columns added earlier in the same startup—are skipped on retry.
- Request-log index creation now warns and continues startup if a table remains locked. The affected index and subsequent request-log indexes are skipped.