Files
buzz/crates/buzz-db/src/lib.rs
Luke Tornquist 88697f1c59 fix(relay): apply kind 5 / 9005 deletions atomically (#8944)
## Summary

Kind 5 and 9005 deletions now store the event and soft-delete their
targets in one transaction.

Before this PR the deletion committed first. The target soft-delete ran
afterwards as a best-effort side effect, and a failure there was only
logged. Resending the deletion then returned `duplicate:` without
retrying. A crash between the two commits had the same result. Either
way the relay accepted a deletion while its target stayed live.

The bridge `include_aux` closure still compensates for those rows. Its
second hop returns deletions of the reactions and edits it found, so
clients can cancel them. This PR keeps that hop, because rows already
torn this way can't be repaired by a resend:

- Ingest rejects events more than 900 seconds from server time.
- Clients don't resend deletions that were accepted.
- During a rolling deploy, old pods keep writing torn rows until they
are replaced.

Dropping the hop waits for a follow-up, once existing torn rows have had
a verified repair and no old writers remain.

### Changes

- **Atomic deletion.** Kind 9005, and kind 5 with `e` targets, go
through `Db::insert_deletion_event`, driven by
`side_effects::persist_deletion`. The write stores the event and its
mentions, soft-deletes the targets and updates their thread counters. It
follows the existing `persisted_atomically` path used for workflow
deletion and kinds 9000/9002.
- A failure stores nothing and rejects the event, so the client can
resend.
- A resend of a stored deletion inside the ingest window soft-deletes
any target still live and is dispatched as inserted. It doesn't repair
older rows.
- Live 39005, reaction-table cleanup and the 9005 system message still
run after the commit and stay best-effort.
  - Kind 5 deletions of `a` coordinates stay a side effect.
- **Closure unchanged.** The channel window and legacy thread-reply
paths keep both hops, and the docs (`docs/bridge-channel-window.md`,
`docs/nips/NIP-CW.md`) are unchanged.

### What got simpler

Deletion no longer has a window where the event is accepted but not
applied. `handle_standard_deletion_event` is now only the `a`-coordinate
path. Target planning and soft-delete for `e` targets live in one place,
`persist_deletion`.

### Follow-ups (not in this PR)

- Repair the existing torn rows: soft-delete the live targets of stored
kind 5 / 9005 `e` deletions and fix their thread counters. Then drop the
closure's deletion-of-aux hop.
- Hop 1 still probes every partition once per row id. A partition bound
or the indexed `reactions` table would cut that.

### Related issue

N/A, found while investigating relay database CPU.

### Testing

- `torn_deletions_still_cancel_live_targets_in_both_windows` (bridge
Postgres) stores kind 5 and 9005 deletions without soft-deleting their
reaction and edit targets. It queries the channel window and the legacy
thread path with `include_aux`, without a resend, and asserts each live
target comes back with its deletion.
-
`insert_deletion_event_target_failure_stores_nothing_and_resend_applies`
(buzz-db Postgres) uses a trigger to make the target's soft-delete fail.
It asserts nothing is stored and the target is unchanged, then that a
resend applies both.
- `insert_deletion_event_resend_repairs_a_live_target` (buzz-db
Postgres): a resend repairs a deletion stored without its target's
soft-delete, and a further resend changes nothing.
- `failed_target_soft_delete_stores_nothing` and
`resent_deletion_repairs_a_live_target` (relay Postgres) cover the same
two cases through ingest, for kinds 5 and 9005.
- `test_channel_window_rows_overlays_and_exact_multiple_exhaustion`
(`e2e_nostr_interop`, Relay E2E lane) deletes its reaction and asserts
the reaction leaves the closure.
- Blox: fmt, clippy (`buzz-db`, `buzz-relay`, `buzz-test-client`), and
the `buzz-db` and `buzz-relay` Postgres lanes.

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

---------

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>
2026-11-03 14:38:26 -05:00

118 lines
5.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#![deny(unsafe_code)]
#![warn(missing_docs)]
//! buzz-db — Postgres event store for Buzz.
//!
//! ## Design invariants
//! - AUTH events (kind 22242) are never stored — they carry bearer tokens.
//! - Ephemeral events (20000–29999) are never stored — Redis pub/sub only.
//! - Events table is partitioned by month on `created_at`.
//! - No FK references to partitioned tables.
//! - Uses `sqlx::query()` (runtime) not `sqlx::query!()` (compile-time).
//!
//! ## Runtime and store ownership
//! Database runtime infrastructure and domain persistence are physically
//! separated behind this crate-root compatibility facade:
//!
//! - Runtime concerns own pool construction, writer/replica routing,
//! transactions, sessions, metrics, health support, and migrations.
//! - Store concerns own domain-specific SQL, row mapping, locking, mutation
//! rules, indexes, and focused persistence tests.
//!
//! Existing crate-root modules, records, and [`Db`] methods remain the public
//! API. The internal `runtime` and `store` namespaces are not public APIs.
/// Wrap `$retire`, an `UPDATE`/`DELETE` on `events` that retires a
/// coordinate's previous head, so the same statement drops those rows' mention
/// index entries. Mention-index reads (`#p` filters and feeds) never return a
/// retired row, so its mention rows are dead weight they must walk and
/// discard: one per member for every kind:39002 roster republish. `$1` must
/// bind the community.
///
/// The three replacement paths that index mentions (NIP-33 parameterized,
/// addressable, and the relay's member snapshot) go through this, so a head
/// and its mention rows leave together on those paths.
macro_rules! superseded_with_mentions_sql {
($retire:literal) => {
concat!(
"WITH superseded AS (",
$retire,
" RETURNING id) \
DELETE FROM event_mentions mention USING superseded \
WHERE mention.community_id = $1 AND mention.event_id = superseded.id"
)
};
}
mod runtime;
mod store;
/// Database error types.
pub mod error;
#[cfg(test)]
mod test_support;
pub use runtime::{
insert_mentions, migration, replica_fence, AdmittedTx, ColdStartError, Db, DbConfig,
DbConnectionOutcome, DbConnectionStep, DbPoolRole, DbPoolStats, DbReadinessOutcome,
ReadSession,
};
/// Valid low-cardinality `(pool_role, operation)` pairs for pool-acquisition telemetry.
pub const DB_POOL_ACQUIRE_VALID_PAIRS: [(&str, &str); 12] =
runtime::observability::POOL_ACQUIRE_VALID_PAIRS;
/// Raw Prometheus series ceiling per relay pod for the operation-aware contract.
pub const DB_POOL_ACQUIRE_RAW_SERIES_PER_POD: usize =
runtime::observability::POOL_ACQUIRE_RAW_SERIES_PER_POD;
/// Valid database connection role/step pairs with a start counter.
pub const DB_CONNECTION_STARTED_STEPS: [(DbPoolRole, DbConnectionStep); 4] =
runtime::CONNECTION_STARTED_STEPS;
/// Valid database connection role/step pairs with a duration histogram.
pub const DB_CONNECTION_DURATION_STEPS: [(DbPoolRole, DbConnectionStep); 4] =
runtime::CONNECTION_DURATION_STEPS;
/// Valid database connection role/step/outcome terminal combinations.
pub const DB_CONNECTION_TERMINALS: [(DbPoolRole, DbConnectionStep, DbConnectionOutcome); 15] =
runtime::CONNECTION_TERMINALS;
/// Raw Prometheus series ceiling per pod for connection-step telemetry.
pub const DB_CONNECTION_RAW_SERIES_PER_POD: usize = runtime::CONNECTION_RAW_SERIES_PER_POD;
pub(crate) use runtime::{
begin_community_event_write_transaction,
begin_community_event_write_transaction_with_legacy_metrics, begin_community_sweep_transaction,
begin_community_write_transaction, insert_mentions_in_transaction, observability, route_proof,
sweep_admitted_communities, ReadSessionInner, RouteDecision, RoutePredicate,
};
pub use store::{
admin_moderation, allowlist, api_token, archived_identities, artifact, artifact_sync, channel,
channel_members, community, deletion, dm, entity_deletion, event, feed, git_repo, moderation,
nip_fi_binding, operator_listener, partition, personal_read, product_feedback, push, reaction,
read_state, relay_admin_actions, relay_invite, relay_members, relay_operators, reminder,
replaceable, storage_accounting, thread, thread_window, usage, user, user_deletion,
user_deletion_fences, workflow,
};
pub use allowlist::AllowlistEntry;
pub use api_token::{ApiTokenRecord, TokenSummary};
pub use community::{
ArchivedCommunityRecord, CommunityRecord, CreateCommunityWithOwnerResult,
CreatedCommunityRecord, EnsuredCommunityRecord, OwnedCommunityRecord, UnarchiveCommunityResult,
UnarchivedCommunityRecord,
};
pub use error::{DbError, Result};
pub use event::{ChannelHeadPrecondition, ChannelHeadWriteStatus, DeletionTarget};
pub use event::{EventQuery, DEFAULT_MAX_PAGE_LIMIT};
pub use nip_fi_binding::{NipFiBindingRecord, NipFiUnbind, RowCapExceeded};
pub use reaction::ReactionEventInsertOutcome;
pub use reminder::DueReminder;
pub use store::{ArtifactSnapshot, SnapshotDenied};
pub use usage::SessionLeader;
use buzz_core::CommunityId;
/// Durable NIP-FI verified-profile projection state.
pub use store::verified_profiles;