Files
buzz/docs/trigger-retirement-deployment.md
Luke Tornquist 3876720c34 buzz-db: drop the five single-table guard triggers (BUZZ-318 phase 2) (#9398)
> [!NOTE]
> **Rollout:** #8320 (app guards + catalog expand) is deployed, so the
hold is lifted. With `autoMigrate=true`, `0096` runs at startup under a
5s lock timeout, and a failure stops startup. With
`BUZZ_AUTO_MIGRATE=false`, deploy this binary first and then run
`buzz-admin migrate`. Both orders are safe because the expand tolerates
the objects being present or absent.

## What

The required stop for `0096` is in
`docs/trigger-retirement-deployment.md` (from #9622): a relay build at
or after #8320 (`ec778bff`). A build older than that needs the tombstone
trigger at startup and readiness.

Migration `0096_drop_single_table_guards.sql` drops five triggers and
their functions. #8320 moved each invariant into the application
writers:

| Trigger (table) | Function |
|---|---|
| `trg_channels_community_id_immutable` (channels) |
`channels_community_id_immutable()` |
| `communities_deletion_tombstone` (communities) |
`enforce_community_tombstone()` |
| `community_deletion_request_retargeting_guard`
(community_deletion_requests) |
`prevent_community_deletion_request_retargeting()` |
| `community_deletion_approval_removal_guard`
(community_deletion_approvals) |
`prevent_community_deletion_approval_removal()` |
| `community_deletion_manifest_keys_guard`
(community_deletion_manifest_keys) |
`protect_community_deletion_manifest_keys()` |

Desired state (`schema/`) drops the five functions and four trigger
files, plus the channels trigger declaration. The universal write fence
on `channels` stays. Both boot paths stay catalog-equivalent: the
pgschema-vs-migration parity check now also compares columns, key and FK
shapes, and the remaining triggers on all five tables.

Executor GUCs stay because `assert_community_write_allowed` still reads
them through the universal write fence. `transition_community` and every
phase-1 app guard are byte-identical to #8320.
`scripts/reconcile-schema-after-pgschema.sql` has no entries for these
objects.

## Tests

- #8320's six `single_table_guard_*_without_trigger` PG tests no longer
need to disable a trigger. They are now plain app-path tests with the
same assertions.
- New per-test regressions for the desired-template and migration boot
paths assert that none of the ten objects exist, including disabled
triggers and function overloads.
- Deleted assertions that only exercised raw SQL against the triggers:
retarget, rewrite and approval mutation, frozen-manifest rewrite, and
tombstone DELETE. The app-path coverage for each remains.
- The expand regression in #8320 migrates through `0086`, validates
serving, readiness and the deletion lifecycle, drops the five guards,
then validates again.
- Rebased onto main `b568d9bf` after #8696 took `0095`; renumbered to
`0096`. Blox gate at `7bda1f45`: fmt, clippy and buzz-db `--lib` pass;
the PostgreSQL lane ran 219 tests, all pass.
- Gate at `c8bc061c` (on main `051dd2d7`, then numbered `0095`, #9622
folded in): on Blox, `cargo fmt --check`, clippy and the buzz-db `--lib`
tests pass. The PostgreSQL lane (migrations, single-table guards, floor
retirement, fence and deletion) ran 208 tests: all pass.
- Earlier gate at `0840ba49`: CI was green, and the Blox PostgreSQL lane
passed 840 of 841 (the one failure, `media_reference_full_query_plans`,
is a timing check that fails the same way on main on that host).
- The rebase updates main's fence-retirement tests to expect the
tombstone trigger gone.

## What got simpler

Ten database behavior objects and nine desired-state files are gone
(about 150 lines). Each rule has a single owner, the application writer.

buzz-review-completed

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

---------

Signed-off-by: Luke Tornquist <tornquist@squareup.com>
Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
2026-11-17 22:30:49 +00:00

143 lines
8.5 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.
## Single-table guards (`0096`)
`0096_drop_single_table_guards.sql` drops five single-table guard triggers and
their functions. The application writers and the catalog expand from
[#8320](https://github.com/block/buzz/pull/8320) now own each rule:
| Table | Trigger / function | Application rule |
| --- | --- | --- |
| `channels` | `trg_channels_community_id_immutable` / `channels_community_id_immutable()` | `community_id` is bound at insert and never updated |
| `communities` | `communities_deletion_tombstone` / `enforce_community_tombstone()` | lifecycle changes go through one transition function |
| `community_deletion_requests` | `community_deletion_request_retargeting_guard` / `prevent_community_deletion_request_retargeting()` | inventory freezes are one-way |
| `community_deletion_approvals` | `community_deletion_approval_removal_guard` / `prevent_community_deletion_approval_removal()` | approvals are insert-only |
| `community_deletion_manifest_keys` | `community_deletion_manifest_keys_guard` / `protect_community_deletion_manifest_keys()` | chunk writes stop at freeze and `deleted_at` is one-way |
**A relay build at or after commit `ec778bff` (the merge of
[#8320](https://github.com/block/buzz/pull/8320)) and before `0096` is a
required stop.** No tagged relay release contains it, so a deployment on a
tagged release must move to a stop build first. For a deployment that has not
yet passed `0094`, this supersedes the `0094` stop: `ec778bff` is an ancestor
of the `0094` merge, so one build at or after `ec778bff` that is also a valid
`0094` stop satisfies both, while a build between `279a31da` and `ec778bff`
satisfies only `0094`'s. Follow the same procedure as above.
An older relay requires the tombstone trigger both at startup and on every
readiness check. Once `0096` has run, it fails both with
`community tombstone trigger is missing`: serving pods go not ready and a
restarted pod does not start, with auto-migration on or off. Its writers also
do not enforce the five rules above.
`0096` takes `ACCESS EXCLUSIVE` on those five tables in one transaction, under
a 5s `lock_timeout` that bounds each lock acquisition. Reads and writes on
`channels` can queue while it acquires the remaining locks. A lock timeout
rolls the whole migration back, and a retry is clean.
Roll back only to a build at or after `ec778bff`, with auto-migration off, as
in [Rollback](#rollback). A build below `ec778bff` does not start after
`0096`.