## Summary Phase 2 of retiring the two remaining plain SQL functions. Phase 1 moved their work into the relay: - #9396 builds the community deletion lock key in Rust (`deletion::community_deletion_lock_key`, locked with `hashtextextended($1, 0)`). - #9397 reads profile names with inline SQL (`PROFILE_NAME_FIELDS` in `buzz-search`). No relay at or after #9397's merge (`279a31da`) calls either function. This PR drops them: - **Migration 0093** runs `DROP FUNCTION community_deletion_lock_key(UUID)` and `DROP FUNCTION profile_name_fields(TEXT)` under a 5s `lock_timeout`, with no `IF EXISTS` and no `CASCADE`. If anything still depends on a function, the migration fails instead. Migrations 0029 and 0064 stay frozen. No database function calls either one any more: 0090 dropped the fence functions, which were the only SQL callers of the lock key. - **Desired state:** removes both `schema/functions/` files and their `schema.sql` includes. Both boot paths stay catalog-equivalent (`admin_schema_parity_between_desired_state_and_migrations`). - **Tests:** - New `migration_0093_drops_plain_sql_functions`. It migrates to 0092, checks that both functions exist and that serving and deletion catalog validation pass, then runs 0093 and checks that both functions are gone and that validation still passes. This covers new code on either side of the drop. - The migration listing test pins 0093's statements and that it has no `CASCADE`. - `desired_state_catalog_has_no_universal_fence_and_still_boots` now asserts that both functions are absent. - Replaces the Rust-vs-SQL lock key parity test with `community_deletion_lock_key_bytes_are_fixed`, a unit test that maps fixed UUIDs (nil, max, and one with hex letters) to their exact keys. Older replicas and the repair-script key in `docs/database.md` build the same string, so a changed prefix or UUID format must fail a test. - Removes two relay test-schema wrappers (`bridge.rs`, `ingest.rs`) that forwarded `community_deletion_lock_key` to `public`. Writers no longer call it, and the wrapper's `CREATE FUNCTION` would fail once the function is gone. - **Docs:** `docs/trigger-retirement-deployment.md` gets a `0093` section with the required stop. ## Deploy gate **Required stop:** any relay build at or after `279a31da` (#9397's merge, which follows #9396). `relay-v0.3.0` does not contain it. A relay older than that which still serves after 0093 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`. 0093 takes no table locks and needs no cleanup. Migration number: #8696 and #9398 also propose `0093`. Whichever lands second renumbers. ## Review-Proven Rules - **RPR #2 (every path):** I checked every reference on `main` and at the stop build `279a31da`. At both, only test code reads either function: no boot, readiness, catalog or runtime query depends on them. Serving and deletion catalog validation pass before and after 0093, in the new test. - **RPR #3 (falsifiable):** removing the `profile_name_fields` drop from 0093 turns `migration_0093_drops_plain_sql_functions` red. - **RPR #3 (lock key bytes):** a changed prefix, `.simple()` UUID formatting, or uppercase formatting each turns `community_deletion_lock_key_bytes_are_fixed` red. ## What got simpler Two database functions, their desired-state files, a cross-language parity test and two test-only SQL wrappers are gone. After this PR, the only functions left in `schema/functions/` are trigger functions. ## Validation Blox gate at `95ac71c8`: - `cargo fmt --all --check`, and `cargo clippy -p buzz-db -p buzz-relay --all-targets -D warnings`. - `buzz-db --lib` unit tests: 216/216, including the migration listing test. - `buzz-db` Postgres tests (`migration::`, `deletion::`, `nip_fi_binding::` postgres_tests): 130/130. These include `migration_0093_drops_plain_sql_functions`, `admin_schema_parity_between_desired_state_and_migrations` and `deletion_surface_parity_between_migration_0029_and_schema_sql`. - `buzz-search` `profile_names*` Postgres tests: 4/4, on a fully migrated database where `profile_name_fields` no longer exists. - The two relay tests whose test-schema wrapper this PR removes: 2/2. - Must-fail check: with the `profile_name_fields` drop removed from 0093, `migration_0093_drops_plain_sql_functions` fails. Second Blox gate at `e1b5513a` (the lock key vector test): - `cargo fmt --all --check`, and `cargo clippy -p buzz-db --all-targets -D warnings`. - `buzz-db --lib` unit tests: 217/217. - Must-fail checks: a changed prefix, `.simple()` and uppercase UUID formatting each fail `community_deletion_lock_key_bytes_are_fixed`. The first gate run found that `deletion_surface_parity_between_migration_0029_and_schema_sql` still expected `community_deletion_lock_key` in `schema.sql`. The second commit fixes that. 🤖 Generated with [Claude Code](https://claude.com/claude-code) buzz-review-completed --------- Signed-off-by: Luke Tornquist <tornquist@squareup.com> Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
5.2 KiB
Trigger retirement deployment
Three 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 |
0090_retire_community_write_fence.sql |
the universal community write-fence triggers and functions | app-side write admission, #8219, #8318 and #8322 |
0091_drop_nip_rs_mesh_migration_triggers.sql |
the migration-only NIP-RS watermark, purge, mention-liveness and mesh-status triggers | #8128 |
Required stop
A relay build at or after commit 68785206 (the merge of
#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.
Procedure
- Deploy a stop build. Check the image of every relay pod and job, and wait until none runs an older one.
- 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. - Apply the migrations off-peak. Each one sets a 5s
lock_timeout, and theDROP TRIGGERstatements takeACCESS EXCLUSIVEoneventsand 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 migratewith the targetDATABASE_URL, confirm that it records 0089, 0090 and 0091, then deploy the new build. The new build assumes 0091 has run: against a database without it, NIP-RS hard deletes fail closed withcheck_violation. The stop build works with all three applied.
- Once every relay runs the new build, run the one-off cleanup SQL in #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 and community_write_fence_events 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 (0093)
0093_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 |
profile_name_fields(text) |
inline profile-name SQL, #9397 |
A relay build at or after commit 279a31da (the merge of
#9397, which follows #9396) and
before 0093 is a required stop. No tagged relay release contains it:
relay-v0.3.0 does 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 0093 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.
0093 takes no table locks. It needs no cleanup, and a rollback to a stop
build needs auto-migration off, as above.