Files
buzz/docs/trigger-retirement-deployment.md
Logan Johnson b568d9bf95 Retire the 0032 roster trigger in migration 0095 (#8696)
## Summary
Migration 0095 drops the 0032 channel roster trigger
(`trg_events_guard_channel_roster_snapshot`) and its function
(`guard_channel_roster_snapshot()`).

The trigger repeats a check that the relay already does in Rust. Every
kind:39002 roster writer checks the roster against the channel's
membership before it saves it (#8316). Since #8611, no relay requires
the trigger at startup. This PR is the last step of the trigger
retirement sequence in `docs/database.md`.

This PR was split out of #8494 so the migration can roll out by itself.
A relay from this PR does not change how it stores or publishes roles,
and it has no new startup check. It serves correctly whether or not 0095
has run. #8494 stacks on this PR and must not merge until 0095 has run
in every environment.

## Details
- **Migration 0095.** `DROP TRIGGER` and `DROP FUNCTION`, with
`lock_timeout = '1s'` and `statement_timeout = '5s'` as in 0049. `DROP
TRIGGER` takes `ACCESS EXCLUSIVE` on each `events` partition in turn, so
event reads and writes block while it runs. A timeout rolls it back with
no change, and it can be retried.
- **Desired-state schema.**
`schema/functions/guard_channel_roster_snapshot.sql` is removed. This
was the last trigger on `events`, so `schema/triggers/events.sql` and
its include go too, as do the pre-attach trigger drops in
`scripts/reconcile-schema-after-pgschema.sql`.
- **Operator guide.** #8326 added
`docs/trigger-retirement-deployment.md` for migrations 0089–0091. (0092
from #9365 and 0093 from #9560 drop nothing; 0094 from #9598 drops two
plain SQL functions and has its own section there.) This PR adds 0095 to
it: its row, the two ways an older relay fails, the migrate check, and
the parent trigger a pgschema deployment must drop first. The Helm chart
README names the required stop and points to that guide.
- **Tests.** Tests that compared the trigger with the Rust check, or
that disabled the trigger first, now test the Rust check alone. Tests
that only exercised the trigger against a simulated pre-#8316 writer are
removed. The 0075 invite upgrade test now migrates to 0075 exactly.

## Rollout
**Required stop: the stop build in
`docs/trigger-retirement-deployment.md`** (a relay build at or after
`68785206`, the merge of #8325). That build contains #8316 (the app-side
roster check) and #8611 (no startup trigger check), so it covers 0095
too. `relay-v0.3.0` contains #8316 and #8611 but not #8325, so it is no
longer enough on its own: any build with 0095 also has 0089–0091.
Without the stop, an untagged `main` build between 0032 and #8611
refuses to start once the trigger is gone, and a relay without #8316 can
save stale kind:39002 snapshots (`docs/database.md` Rule 5).

**Merge gate.** Every push to main publishes relay `:main` images, and
the Helm chart defaults to `migrate.autoMigrate=true`. Do not merge this
PR until the stop build or later runs on every serving and rollback
target. That is the same gate #8326 had for 0089–0091.

After that, either way works:
- With `autoMigrate=true`, the first relay from this PR applies 0095 at
startup.
- With `autoMigrate=false`, run `buzz-admin migrate` when convenient.
Relays from this PR serve with or without 0095.

As with every new migration, an older binary with auto-migration on
refuses a database that has 0095 (sqlx `VersionMissing`). A rollback
past this release needs auto-migration off (`docs/database.md`).

## What happens next
#8494 computes the `member`/`bot` label at read time. The 0032 trigger
compares labels with the stored role exactly, so it would refuse those
rosters. #8494 adds a startup check that refuses to serve while the
trigger is installed. Merge it only after 0095 has run everywhere.

### Testing
- `migration_0095_retires_the_roster_trigger`: on a database migrated
through 0094, the trigger and function are installed. After the
remaining migrations, both are gone, and a raw kind:39002 insert that
does not match membership is accepted. It fails if 0095 does not drop
them.
- `embedded_migrator_contains_consolidated_initial_schema` pins 95
migrations and the 0095 statements, and checks that the desired-state
schema no longer installs the function.
-
`migration_schema_desired_pgschema_reconcile_matches_populated_upgrade`:
its fixture drops each retired parent trigger before pgschema runs
(pgschema drops partition copies before the parent, which PostgreSQL
refuses with 2BP01). It now drops the roster trigger too.
- Locally at this head: `cargo check`, `cargo fmt --check`, and 245/245
buzz-db unit tests pass; 760/764 buzz-db Postgres tests pass. The four
`event_follow_up_contract` tests fail the same way on `main` locally and
pass in CI. CI runs clippy and the full PostgreSQL lane.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Logan Johnson <loganj@squareup.com>
Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:56:50 -05:00

105 lines
6.1 KiB
Markdown

# Trigger retirement deployment
Four migrations drop database triggers and functions whose work the relay
now does in application code. They ship together:
| Migration | Drops | Application replacement |
| --- | --- | --- |
| `0089_retire_created_at_floor.sql` | the `events_created_at_floor` constraint trigger and its function (`0021`) | the insert-time floor check, [#8325](https://github.com/block/buzz/pull/8325) |
| `0090_retire_community_write_fence.sql` | the universal community write-fence triggers and functions | app-side write admission, [#8219](https://github.com/block/buzz/pull/8219), [#8318](https://github.com/block/buzz/pull/8318) and [#8322](https://github.com/block/buzz/pull/8322) |
| `0091_drop_nip_rs_mesh_migration_triggers.sql` | the migration-only NIP-RS watermark, purge, mention-liveness and mesh-status triggers | [#8128](https://github.com/block/buzz/pull/8128) |
| `0095_retire_channel_roster_trigger.sql` | the `0032` channel-roster trigger `trg_events_guard_channel_roster_snapshot` and its function | the app-side kind:39002 roster check, [#8316](https://github.com/block/buzz/pull/8316); since [#8611](https://github.com/block/buzz/pull/8611) no relay requires the trigger at startup |
## Required stop
**A relay build at or after commit `68785206` (the merge of
[#8325](https://github.com/block/buzz/pull/8325)) and before these migrations
is a required stop.** Such a build contains every replacement above. No tagged
relay release contains #8325: `relay-v0.3.0` has the others but not #8325, so
a deployment on a tagged release must move to a stop build first.
Do not upgrade past the stop build to one with these migrations until no older
relay runs anywhere: every pod and job, not only a rollout's new pods. The
migrations cannot detect an older writer. If an operator upgrades straight
from an older build, the first new pod applies the migrations while older pods
are still serving:
- a relay without #8325 has no floor check, so it accepts backdated channel
events that replica reads can then skip;
- a relay without #8318 fails its background jobs with
`42883 community_write_allowed(uuid) does not exist`;
- a relay without #8128 loses the NIP-RS watermark and superseded-row purge;
- a relay without #8316 does not check kind:39002 roster snapshots against
channel membership, so it can save stale rosters;
- a build with migration `0032` but without #8611 refuses to start once the
roster trigger is gone, so such a pod cannot restart during the rollout or
serve as a rollback target.
## Procedure
1. Deploy a stop build. Check the image of every relay pod and job, and
wait until none runs an older one.
2. Stop out-of-band writers (direct SQL, `COPY`, restores, seed scripts). Hold
the replica breaker closed from before the migrations until every replica
has replayed them.
3. Apply the migrations off-peak. Each one sets a 5s `lock_timeout`, and the
`DROP TRIGGER` statements take `ACCESS EXCLUSIVE` on `events` and its
partitions, so event reads and writes can stall for up to that long. A
lock timeout aborts that migration, which is safe to retry.
- **Auto-migration on:** deploy the new build. Its first pod applies the
migrations at startup.
- **Auto-migration off:** run `buzz-admin migrate` with the target
`DATABASE_URL`, confirm that it records 0089 through 0095, then deploy
the new build. The new build assumes 0091 has run: against a
database without it, NIP-RS hard deletes fail closed with
`check_violation`. The stop build works with all four applied.
4. Once every relay runs the new build, run the one-off cleanup SQL in
[#8211](https://github.com/block/buzz/pull/8211) once per database. While
stop-build pods served after 0091, a replacement could leave a
soft-deleted NIP-RS or mesh-status head that nothing purges any more. The
cleanup is idempotent.
## Desired-state deployments
A deployment that applies `schema/` with pgschema follows the same order.
Apply the migrations first, or first drop the triggers on the partitioned
parents: `events_created_at_floor`, `community_write_fence_events` and
`trg_events_guard_channel_roster_snapshot` on `events`, and
`community_write_fence_delivery_log` on `delivery_log`.
Otherwise pgschema plans the partition copies' drops before the parent's, and
PostgreSQL refuses with `2BP01`.
## Rollback
Roll back to the stop build, not further, and only with auto-migration off
(`BUZZ_AUTO_MIGRATE=false`, or `migrate.autoMigrate=false` in the Helm chart).
With it on, the stop build refuses a database that has migrations it does
not know (`VersionMissing`) and does not start. With it off, the stop build
serves, but its startup check finds the floor trigger gone and keeps the
replica fence closed, so cursor reads go to the writer.
## Plain SQL functions (`0094`)
`0094_drop_plain_sql_functions.sql` drops two functions whose work the relay
now does in application code:
| Function | Application replacement |
| --- | --- |
| `community_deletion_lock_key(uuid)` | the Rust-built lock key, [#9396](https://github.com/block/buzz/pull/9396) |
| `profile_name_fields(text)` | inline profile-name SQL, [#9397](https://github.com/block/buzz/pull/9397) |
**A relay build at or after commit `279a31da` (the merge of
[#9397](https://github.com/block/buzz/pull/9397), which follows #9396) and
before `0094` is a required stop.** Builds from commit `a8f55e63` (the merge
of [#9363](https://github.com/block/buzz/pull/9363)) up to the merge of
[#9615](https://github.com/block/buzz/pull/9615) do not qualify: their user
content cleanup still calls `community_deletion_lock_key`. No tagged relay
release is a stop build: `relay-v0.3.0` is not, so a deployment on a tagged
release must move to a stop build first. Follow the same procedure as above. An older relay that
still serves after `0094` fails every write and deletion that takes the community deletion lock, with
`42883 community_deletion_lock_key(uuid) does not exist`, and its profile name
search fails with `42883 profile_name_fields(text) does not exist`.
`0094` takes no table locks. It needs no cleanup, and a rollback to a stop
build needs auto-migration off, as above.