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: document catalog upgrade lock window (#4929)

[._.]/ Adam Eivy669d3aad5b5a
diff --git a/docs/STORAGE.md b/docs/STORAGE.md
index e76323fb35..8427a91d27 100644
--- a/docs/STORAGE.md
+++ b/docs/STORAGE.md
@@ -157,7 +157,7 @@ The escape hatch is **guarded from bitrot by the test suite** (tests boot with `
`PGPASSWORD`/`PGUSER`/`PGDATABASE`/`PGPORT` are resolved from `process.env` first, then `.env`, then the backward-compatible defaults (`portos`/`portos`/`portos`/`5432`). The default `portos` password is an **intentional** local-development fallback (see the Distribution model note in [`AGENTS.md`](../AGENTS.md)); production deployments override it via `PGPASSWORD`.
-### Boot schema upgrades & the CREATE INDEX lock window
+### Boot schema upgrades & lock windows
`ensureSchema()` in `server/lib/db.js` applies **idempotent** schema upgrades on every boot (`CREATE TABLE IF NOT EXISTS`, `ADD COLUMN IF NOT EXISTS`, `CREATE INDEX IF NOT EXISTS`). Every index it creates — including the HNSW vector index and the GIN full-text index on `catalog_scraps` — is a plain, **non-`CONCURRENT`** build.
@@ -167,6 +167,19 @@ This is left as-is deliberately rather than switched to `CREATE INDEX CONCURRENT
**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.
+#### Catalog generated-column rewrite
+
+Catalog schema v2 expanded `catalog_ingredients.search_tsv`, a stored generated column, to index `physicalDescription`. PostgreSQL cannot alter a stored generation expression in place, so the compatibility path drops and re-adds the column on an upgrading v1 install. Adding it back computes the value for every existing ingredient and holds an `ACCESS EXCLUSIVE` lock on `catalog_ingredients` for the rewrite; the following GIN index recreation adds its own write-blocking build window. Reads and writes that reach this table from another still-running PortOS process wait behind those locks.
+
+PortOS deliberately accepts this **one-time, boot-time maintenance window** instead of carrying a second shadow column, trigger, batched-backfill checkpoint, and recovery protocol indefinitely. The expression check in both schema sources makes the path bounded by state: fresh installs add the column to an empty table, v2 installs skip the rewrite, and only a pre-v2 catalog pays the row-proportional cost. `ensureSchema()` completes before the new server reports ready, so it never exposes a partially upgraded catalog.
+
+For an install with an unusually large pre-v2 catalog, treat the update as planned maintenance:
+
+1. Take a normal PortOS backup before updating.
+2. Stop other PortOS processes that can use the same PostgreSQL database; federated peers use their own databases and upgrade independently.
+3. Run the normal update during a low-use window and allow startup to finish without interruption. There is no safe universal duration estimate: row count, payload size, disk speed, and PostgreSQL settings all affect the rewrite.
+4. Wait for the `Database schema upgrades applied` startup log before resuming use. If startup is interrupted, rerun the normal startup; the expression gate and `IF NOT EXISTS` statements safely converge on the v2 shape.
+
---
## Adding a new data store? Answer these
diff --git a/server/lib/db.js b/server/lib/db.js
index ca5d6d5525..93ce6dfb15 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -359,7 +359,7 @@ async function ensureSchemaImpl() {
// 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").
+ // See docs/STORAGE.md ("Boot schema upgrades & lock windows").
const upgrades = buildUpgradeDdl();
for (const sql of upgrades) {
await pool.query(sql);
diff --git a/server/lib/db/schema/catalog.js b/server/lib/db/schema/catalog.js
index 367439bc49..c3a2ffa09f 100644
--- a/server/lib/db/schema/catalog.js
+++ b/server/lib/db/schema/catalog.js
@@ -82,7 +82,12 @@ export const catalogDdl = [
// when the current generation expression is missing a v2-only field
// (`physicalDescription`). That keeps boot O(1) on already-v2 installs —
// an unconditional DROP+ADD would AccessExclusive-lock the table, rewrite
- // every row, and rebuild the GIN index on every server start.
+ // every row, and rebuild the GIN index on every server start. The one v1 ->
+ // v2 rewrite deliberately accepts that boot-time lock: Postgres cannot alter
+ // a STORED generation expression in place, and the catalog schema must be
+ // complete before the server becomes ready. Operators with unusually large
+ // catalogs should schedule that upgrade as maintenance; see docs/STORAGE.md
+ // ("Boot schema upgrades & lock windows") for the decision and runbook.
// Fresh installs (no column yet) fall through to the ADD IF NOT EXISTS
// below and skip the DROP entirely.
`DO $$
diff --git a/server/scripts/init-db.sql b/server/scripts/init-db.sql
index 51c42f8e77..a802fe2296 100644
--- a/server/scripts/init-db.sql
+++ b/server/scripts/init-db.sql
@@ -319,7 +319,12 @@ CREATE TABLE IF NOT EXISTS catalog_ingredients (
-- below inspects pg_attrdef and only drops when the existing expression is
-- missing a v2-only field (`physicalDescription`) — fresh runs of this script
-- skip the drop entirely (column absent), already-v2 installs skip it too, and
--- only an upgrading v1 install pays the table-rewrite cost. ensureSchema in
+-- only an upgrading v1 install pays the table-rewrite cost. That rewrite takes
+-- an ACCESS EXCLUSIVE lock for its duration; this is deliberately accepted at
+-- boot because Postgres cannot alter a STORED generation expression in place
+-- and the catalog schema must be complete before the server becomes ready.
+-- Operators with unusually large catalogs should schedule the upgrade as
+-- maintenance; docs/STORAGE.md carries the decision and runbook. ensureSchema in
-- server/lib/db.js mirrors the same gate. PORTOS_SCHEMA_VERSIONS.catalog is
-- bumped to 2 in lockstep so older peers can't push pre-expansion-shape rows
-- that would mismatch the indexed expression.