From f663f9e1e8c6f1ea183b2f6d6b9ac5a148905d00 Mon Sep 17 00:00:00 2001 From: Eva <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz> Date: Thu, 27 Aug 2026 15:31:59 -0400 Subject: [PATCH] Revert "fix(db): disable heartbeat vacuum truncation (#6898)" This reverts commit 0ccf934b88f610f5f235862ecd51e2dcbae2cb74. Co-authored-by: Eva <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz> Signed-off-by: Eva <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz> --- .github/workflows/ci.yml | 4 +- AGENTS.md | 1 - crates/buzz-db/src/migration.rs | 87 +------------------ ...0034_replica_heartbeat_vacuum_truncate.sql | 7 -- schema/schema.sql | 2 - ...chema.sql => attach-schema-partitions.sql} | 42 ++------- scripts/run-desktop-release-smoke.sh | 2 +- scripts/start-isolated-test-relay.sh | 2 +- scripts/start-relay-for-tests.sh | 2 +- 9 files changed, 14 insertions(+), 135 deletions(-) delete mode 100644 migrations/0034_replica_heartbeat_vacuum_truncate.sql rename scripts/{reconcile-schema-after-pgschema.sql => attach-schema-partitions.sql} (85%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81b439a73d..ba212a36e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -516,7 +516,7 @@ jobs: run: | ./bin/pgschema apply --file schema/schema.sql --auto-approve docker exec -i -e PGPASSWORD=buzz_dev buzz-postgres \ - psql -U buzz -d buzz -v ON_ERROR_STOP=1 < scripts/reconcile-schema-after-pgschema.sql + psql -U buzz -d buzz -v ON_ERROR_STOP=1 < scripts/attach-schema-partitions.sql docker exec -e PGPASSWORD=buzz_dev buzz-postgres \ psql -U buzz -d buzz -qtA -c " INSERT INTO communities (id, host) @@ -677,7 +677,7 @@ jobs: run: | ./bin/pgschema apply --file schema/schema.sql --auto-approve docker exec -i -e PGPASSWORD=buzz_dev buzz-postgres \ - psql -U buzz -d buzz -v ON_ERROR_STOP=1 < scripts/reconcile-schema-after-pgschema.sql + psql -U buzz -d buzz -v ON_ERROR_STOP=1 < scripts/attach-schema-partitions.sql docker exec -e PGPASSWORD=buzz_dev buzz-postgres \ psql -U buzz -d buzz -qtA -c " INSERT INTO communities (id, host) diff --git a/AGENTS.md b/AGENTS.md index 24115501ad..6d97f4dfd9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -469,7 +469,6 @@ description. See [PR #803](https://github.com/block/buzz/pull/803). 4. **Worktrees: `cd` in the same command** — shell CWD doesn't persist between tool calls. Use `cd /path && cargo build` as one command. 5. **Desktop crate excluded from root workspace** — `cargo test` at repo root does NOT run desktop tests. Use `cargo test --manifest-path desktop/src-tauri/Cargo.toml` explicitly. 6. **React render perf: `React.memo` is all-or-nothing** — it only skips a re-render when *every* prop is reference-stable; one unstable prop (inline arrow/JSX, or a hook returning a fresh `{}`/`[]`/`Map` each render) defeats it. Two repeat offenders: (a) React Query results (`useMutation`/`useQuery`) are a **new object each render** — depend on the stable method (`mutation.mutateAsync`), not the object; (b) derived `Map`/array state that recomputes on a version bump — wrap in a content-equality ref cache (`shared/hooks/useStableReference.ts`). When chasing interaction lag, **measure with DevTools closed and no perf probes** (an open Web Inspector + per-keystroke `console.log` inflate the numbers), and isolate by removing one suspect at a time rather than guessing. -7. **`pgschema` omits seed DML and some storage parameters** — Fresh desired-state bootstraps use `./bin/pgschema apply`, which does not execute `INSERT` statements or preserve every table storage parameter from `schema/schema.sql`. Put each unsupported invariant in `scripts/reconcile-schema-after-pgschema.sql` as an idempotent convergence statement plus a live catalog or data assertion. Every `pgschema apply` caller must run that script. A string assertion against `schema.sql` alone does not prove the pgschema-created database has the intended state. --- diff --git a/crates/buzz-db/src/migration.rs b/crates/buzz-db/src/migration.rs index c91577b760..75201afba8 100644 --- a/crates/buzz-db/src/migration.rs +++ b/crates/buzz-db/src/migration.rs @@ -175,11 +175,7 @@ async fn reject_legacy_nip_rs_cardinality_ambiguity(conn: &mut PgConnection) -> #[cfg(test)] mod tests { use super::*; - use std::{ - collections::BTreeSet, - fs, - path::{Path, PathBuf}, - }; + use std::collections::BTreeSet; const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1 @@ -649,7 +645,7 @@ mod tests { let mut migrations: Vec<_> = MIGRATOR.iter().collect(); migrations.sort_by_key(|migration| migration.version); - assert_eq!(migrations.len(), 34); + assert_eq!(migrations.len(), 33); assert_eq!(migrations[0].version, 1); assert_eq!(&*migrations[0].description, "initial schema"); assert!(migrations[0] @@ -1102,85 +1098,6 @@ mod tests { extract_roster_fence(roster_fence), extract_roster_fence(desired_schema) ); - - // The single-row heartbeat table is updated continuously. Prevent - // autovacuum from truncating its heap so standby queries are not - // cancelled by the ACCESS EXCLUSIVE truncation lock replay. - assert_eq!(migrations[33].version, 34); - let heartbeat_vacuum = migrations[33].sql.as_str(); - assert!(heartbeat_vacuum.contains("ALTER TABLE replica_heartbeat")); - assert!(heartbeat_vacuum.contains("vacuum_truncate = false")); - assert!(desired_schema.contains("vacuum_truncate = false")); - - // pgschema intentionally reconciles DDL, not seed DML or table storage - // parameters. Its post-apply reconciliation must restore and verify - // both parts of the live heartbeat contract for fresh bootstraps. - let pgschema_reconciliation = - include_str!("../../../scripts/reconcile-schema-after-pgschema.sql"); - assert!(pgschema_reconciliation - .contains("ALTER TABLE replica_heartbeat SET (vacuum_truncate = false)")); - assert!(pgschema_reconciliation.contains("INSERT INTO replica_heartbeat (id) VALUES (1)")); - assert!(pgschema_reconciliation.contains("ON CONFLICT (id) DO NOTHING")); - assert!(pgschema_reconciliation.contains("pg_class")); - assert!(pgschema_reconciliation.contains("reloptions")); - } - - #[test] - fn every_pgschema_apply_runs_post_apply_reconciliation() { - fn files_under(root: &Path) -> Vec { - let mut pending = vec![root.to_owned()]; - let mut files = Vec::new(); - - while let Some(path) = pending.pop() { - for entry in fs::read_dir(&path) - .unwrap_or_else(|error| panic!("could not read {}: {error}", path.display())) - { - let path = entry.expect("directory entry").path(); - if path.is_dir() { - pending.push(path); - } else { - files.push(path); - } - } - } - - files - } - - let repo_root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); - let roots = [ - repo_root.join("scripts"), - repo_root.join(".github/workflows"), - ]; - let mut apply_count = 0; - - for path in roots.iter().flat_map(|root| files_under(root)) { - let Ok(contents) = fs::read_to_string(&path) else { - continue; - }; - let lines: Vec<_> = contents.lines().collect(); - - for (index, line) in lines.iter().enumerate() { - if !line.contains("./bin/pgschema apply") { - continue; - } - - apply_count += 1; - let following_lines = &lines[index + 1..(index + 7).min(lines.len())]; - assert!( - following_lines.iter().any(|line| line.contains( - "scripts/reconcile-schema-after-pgschema.sql" - )), - "{} must run scripts/reconcile-schema-after-pgschema.sql immediately after pgschema apply", - path.display() - ); - } - } - - assert!( - apply_count > 0, - "expected at least one pgschema apply caller" - ); } #[test] diff --git a/migrations/0034_replica_heartbeat_vacuum_truncate.sql b/migrations/0034_replica_heartbeat_vacuum_truncate.sql deleted file mode 100644 index 479b049488..0000000000 --- a/migrations/0034_replica_heartbeat_vacuum_truncate.sql +++ /dev/null @@ -1,7 +0,0 @@ --- replica_heartbeat is a single-row table updated continuously by every relay --- pod. Autovacuum heap truncation briefly takes ACCESS EXCLUSIVE on the table; --- replaying that lock on a hot standby can cancel concurrent heartbeat reads. --- The table cannot reclaim meaningful disk space by truncating one page, so --- disable only the truncation phase while retaining normal autovacuum cleanup. - -ALTER TABLE replica_heartbeat SET (vacuum_truncate = false); diff --git a/schema/schema.sql b/schema/schema.sql index ce0e10e932..c461039485 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1199,8 +1199,6 @@ CREATE TABLE replica_heartbeat ( id smallint PRIMARY KEY CHECK (id = 1), epoch uuid NOT NULL DEFAULT gen_random_uuid(), token bigint NOT NULL DEFAULT 0 -) WITH ( - vacuum_truncate = false ); INSERT INTO replica_heartbeat (id) VALUES (1); diff --git a/scripts/reconcile-schema-after-pgschema.sql b/scripts/attach-schema-partitions.sql similarity index 85% rename from scripts/reconcile-schema-after-pgschema.sql rename to scripts/attach-schema-partitions.sql index 7c3a7be871..a67bb706b1 100644 --- a/scripts/reconcile-schema-after-pgschema.sql +++ b/scripts/attach-schema-partitions.sql @@ -1,10 +1,11 @@ --- Reconcile schema details that pgschema does not preserve. +-- Attach partition child tables after pgschema apply. -- --- pgschema reconciles DDL, but it does not execute seed DML or preserve every --- table storage parameter from schema/schema.sql. It also currently emits --- partition children as standalone CREATE TABLE statements. Every pgschema --- apply caller must run this idempotent script so fresh bootstraps converge on --- the same live database contract as migration-managed databases. +-- pgschema currently emits existing partition children as standalone CREATE TABLE +-- statements when applying schema/schema.sql in CI. The tables exist, but they +-- are not attached to their partitioned parents, so inserts into events or +-- delivery_log fail with "no partition of relation ... found for row". Keep this +-- idempotent: raw psql/schema.sql already attaches these partitions, while +-- pgschema-created schemas need this repair step. DO $$ BEGIN @@ -196,32 +197,3 @@ BEGIN FOR VALUES FROM ('2026-07-01') TO (MAXVALUE); END IF; END $$; - --- pgschema reconciles DDL but does not apply seed DML or table storage --- parameters from schema/schema.sql. Restore those parts of the desired-state --- contract explicitly and fail the bootstrap if the live catalog disagrees. -ALTER TABLE replica_heartbeat SET (vacuum_truncate = false); - -INSERT INTO replica_heartbeat (id) VALUES (1) -ON CONFLICT (id) DO NOTHING; - -DO $$ -BEGIN - IF NOT EXISTS ( - SELECT 1 - FROM pg_class AS relation - JOIN pg_namespace AS namespace ON namespace.oid = relation.relnamespace - WHERE namespace.nspname = current_schema() - AND relation.relname = 'replica_heartbeat' - AND COALESCE( - relation.reloptions @> ARRAY['vacuum_truncate=false']::text[], - false - ) - ) THEN - RAISE EXCEPTION 'replica_heartbeat must disable vacuum truncation after pgschema apply'; - END IF; - - IF (SELECT count(*) FROM replica_heartbeat WHERE id = 1) <> 1 THEN - RAISE EXCEPTION 'replica_heartbeat must contain its singleton row after pgschema apply'; - END IF; -END $$; diff --git a/scripts/run-desktop-release-smoke.sh b/scripts/run-desktop-release-smoke.sh index 31ad3efac0..85aee2e05c 100755 --- a/scripts/run-desktop-release-smoke.sh +++ b/scripts/run-desktop-release-smoke.sh @@ -89,7 +89,7 @@ export PGSCHEMA_PLAN_HOST=localhost PGSCHEMA_PLAN_PORT=5432 PGSCHEMA_PLAN_DB="${ export PGSCHEMA_PLAN_USER=buzz PGSCHEMA_PLAN_PASSWORD=buzz_dev ./bin/pgschema apply --file schema/schema.sql --auto-approve docker exec -i -e PGPASSWORD=buzz_dev buzz-postgres \ - psql -U buzz -d "${DB_NAME}" -v ON_ERROR_STOP=1 < scripts/reconcile-schema-after-pgschema.sql + psql -U buzz -d "${DB_NAME}" -v ON_ERROR_STOP=1 < scripts/attach-schema-partitions.sql BUZZ_DB_NAME="${DB_NAME}" BUZZ_COMMUNITY_HOST="${COMMUNITY_HOST}" ./scripts/setup-desktop-test-data.sh docker exec buzz-redis redis-cli -n "${REDIS_DB}" FLUSHDB >/dev/null phase database "${phase_start}" diff --git a/scripts/start-isolated-test-relay.sh b/scripts/start-isolated-test-relay.sh index b737a73ea9..1f6b6620c1 100755 --- a/scripts/start-isolated-test-relay.sh +++ b/scripts/start-isolated-test-relay.sh @@ -98,7 +98,7 @@ export PGSCHEMA_PLAN_HOST=localhost PGSCHEMA_PLAN_PORT=${PG_PORT} export PGSCHEMA_PLAN_DB=buzz PGSCHEMA_PLAN_USER=buzz PGSCHEMA_PLAN_PASSWORD=buzz_dev export PGHOST=localhost PGPORT=${PG_PORT} PGUSER=buzz PGDATABASE=buzz ./bin/pgschema apply --file schema/schema.sql --auto-approve -psql_h < scripts/reconcile-schema-after-pgschema.sql +psql_h < scripts/attach-schema-partitions.sql ok "Schema applied" # ── Deployment community + channels + members ──────────────────────────────── diff --git a/scripts/start-relay-for-tests.sh b/scripts/start-relay-for-tests.sh index 272bafd705..4bd1a27408 100755 --- a/scripts/start-relay-for-tests.sh +++ b/scripts/start-relay-for-tests.sh @@ -105,7 +105,7 @@ export PGSCHEMA_PLAN_PASSWORD=buzz_dev ./bin/pgschema apply --file schema/schema.sql --auto-approve docker exec -i -e PGPASSWORD="${PGPASSWORD}" buzz-postgres \ - psql -U "${PGUSER}" -d "${PGDATABASE}" -v ON_ERROR_STOP=1 < scripts/reconcile-schema-after-pgschema.sql + psql -U "${PGUSER}" -d "${PGDATABASE}" -v ON_ERROR_STOP=1 < scripts/attach-schema-partitions.sql ok "Schema applied" # ── Seed the deployment community ────────────────────────────────────────────