mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
fix(migrations): retire the measurements prefix index without waiting at boot
0416 ran DROP INDEX CONCURRENTLY at boot, which waits without bound for every transaction on the table; a pg_dump or a backfill outlasted the start period and the next boot stopped at P3009. The migration now tries a plain drop with a two-second lock wait and tolerates the timeout, and the nightly measurement maintenance drops what is left concurrently under a bounded wait, retrying the next night. A fresh or shadow database takes the lock at once, so the history still ends where the schema does. The guard refuses a concurrent drop in a migration and ties every retired index to a tolerant one.
This commit is contained in:
1 parent
a275a5bb65
commit
2b81b4f23e
5 files changed
+388
-19
No files matched your search
@@ -1,4 +1,4 @@
|
||||
-- Drop `measurements_user_id_type_measured_at_idx`.
|
||||
-- Retire `measurements_user_id_type_measured_at_idx`.
|
||||
--
|
||||
-- `(user_id, type, measured_at)` is the leading part of the unique key
|
||||
-- `(user_id, type, measured_at, source, sleep_stage)`, and live reads have
|
||||
@@ -8,10 +8,28 @@
|
||||
-- seeded table). The index cost every write to the densest table in the
|
||||
-- schema and, on a bloated instance, more space than the table itself.
|
||||
--
|
||||
-- `DROP INDEX CONCURRENTLY` takes no lock that blocks reads or writes: it
|
||||
-- waits for the transactions already using the index to finish and then
|
||||
-- removes it. It refuses to run inside a transaction block, and under
|
||||
-- `migrate deploy` it does so whenever the file holds any other statement
|
||||
-- (Prisma 7 on Postgres 16 reports 25001), so this file holds exactly this
|
||||
-- one statement. `IF EXISTS` makes a re-run a no-op.
|
||||
DROP INDEX CONCURRENTLY IF EXISTS "measurements_user_id_type_measured_at_idx";
|
||||
-- This runs at boot, so it must not wait on the table. A plain `DROP INDEX`
|
||||
-- needs ACCESS EXCLUSIVE on `measurements`, held for the instant the catalog
|
||||
-- changes; `DROP INDEX CONCURRENTLY` blocks nobody but waits, without bound,
|
||||
-- for every transaction on the table (a pg_dump, a backfill), long enough for
|
||||
-- the container to be replaced mid-migration and the next boot to stop at
|
||||
-- P3009. So the drop is tried once with a two-second lock wait (the most the
|
||||
-- table's other statements can queue behind the request), and a timeout is
|
||||
-- swallowed: the migration then succeeds with the index still there, and
|
||||
-- the nightly measurement maintenance drops it with
|
||||
-- `DROP INDEX CONCURRENTLY IF EXISTS` (`RETIRED_MEASUREMENT_INDEXES` in
|
||||
-- `src/lib/jobs/measurement-maintenance.ts`). A fresh database and Prisma's
|
||||
-- shadow database take the lock at once, so the migration history ends
|
||||
-- without the index, as the schema does. The pattern, for the next large
|
||||
-- index: docs/ops/measurement-maintenance.md, "Retired indexes".
|
||||
--
|
||||
-- @retry-on-lock-timeout
|
||||
|
||||
DO $migration$
|
||||
BEGIN
|
||||
PERFORM set_config('lock_timeout', '2s', true);
|
||||
DROP INDEX IF EXISTS "measurements_user_id_type_measured_at_idx";
|
||||
EXCEPTION WHEN lock_not_available THEN
|
||||
RAISE NOTICE 'measurements_user_id_type_measured_at_idx is in use; the nightly measurement maintenance drops it';
|
||||
END
|
||||
$migration$;
|
||||
Reference in new issue
Block a user