Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

docs: correct CONCURRENTLY guidance — migration runner is transactional (#2546)

Adam Eivyc343dfc96911
diff --git a/docs/STORAGE.md b/docs/STORAGE.md
index 017df184f6..6b222c859e 100644
--- a/docs/STORAGE.md
+++ b/docs/STORAGE.md
@@ -149,7 +149,9 @@ The escape hatch is **guarded from bitrot by the test suite** (tests boot with `
The **first** time a given index materializes on a table that **already holds many rows** (an existing install upgrading into a newly-added index), Postgres takes a `SHARE` lock that **blocks writes** (INSERT/UPDATE/DELETE) to that table until the build completes. So an existing install can see a one-time write stall at boot proportional to the table's row count — HNSW builds are the slowest. Fresh installs never see this: they build every index on an **empty** table, so the lock is effectively instant.
-This is left as-is deliberately rather than switched to `CREATE INDEX CONCURRENTLY`, because `CONCURRENTLY` cannot run inside a transaction block, needs its own retry/cleanup path (a failed concurrent build leaves an `INVALID` index that must be dropped by hand), and roughly doubles build time — too fragile to run unattended on every boot for a stall that only bites large-table upgrades. **If a future migration adds an index to a table known to already carry a large row count on existing installs, prefer a versioned db-migration** (`server/scripts/db-migrations/`) that issues `CREATE INDEX CONCURRENTLY` **outside** a transaction, rather than adding it to `ensureSchema()`.
+This is left as-is deliberately rather than switched to `CREATE INDEX CONCURRENTLY`, because `CONCURRENTLY` cannot run inside a transaction block, needs its own retry/cleanup path (a failed concurrent build leaves an `INVALID` index that must be dropped by hand), and roughly doubles build time — too fragile to run unattended on every boot for a stall that only bites large-table upgrades.
+
+**If a future index must land on a table known to already carry a large row count on existing installs,** note that the standard db-migration runner (`server/scripts/run-db-migrations.js`) wraps every migration in a `withTransaction()` block — so `CREATE INDEX CONCURRENTLY` **cannot** run there either. It would have to be issued from a dedicated **non-transactional** path (a standalone maintenance script or manual step run outside any transaction), not from `ensureSchema()` or a standard db-migration.
---
diff --git a/server/lib/db.js b/server/lib/db.js
index d67ef358a8..4fb6f0a74b 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -351,11 +351,13 @@ async function ensureSchemaImpl() {
// own retry/cleanup path (a failed CONCURRENT build leaves an INVALID index
// that must be dropped by hand), and roughly doubles build time — fragile to
// run unattended on every boot for a stall that only bites large-table upgrades.
- // If a future migration adds an index to a table known to already carry a large
- // row count on existing installs, prefer a versioned db-migration (see
- // server/scripts/db-migrations/) that issues CREATE INDEX CONCURRENTLY OUTSIDE a
- // transaction, rather than adding it here. See docs/STORAGE.md ("Boot schema
- // upgrades & the CREATE INDEX lock window").
+ // If a future index must land on a table known to already carry a large row
+ // count on existing installs, note that the standard db-migration runner
+ // (server/scripts/run-db-migrations.js) wraps every migration in a
+ // withTransaction() block, so CREATE INDEX CONCURRENTLY CANNOT run there
+ // either — it must be issued from a dedicated non-transactional path (a
+ // standalone maintenance script / manual step run outside any transaction).
+ // See docs/STORAGE.md ("Boot schema upgrades & the CREATE INDEX lock window").
const upgrades = [
`ALTER TABLE memories ADD COLUMN IF NOT EXISTS sync_sequence BIGSERIAL`,
`ALTER TABLE memories ADD COLUMN IF NOT EXISTS origin_instance_id VARCHAR(36)`,