reliar-store-postgres 0.9.0

PostgreSQL provider for the Reliar transactional outbox and inbox: migrations, migrate(), enqueue and the SKIP LOCKED claim.
Documentation
/// Installs `subscriber` as the thread-local default and immediately rebuilds `tracing`'s
/// process-global per-callsite interest cache (RELIAR-66).
///
/// **The hazard this exists for.** `tracing::subscriber::set_default` only changes the default
/// for the calling OS thread, but a callsite's computed `Interest` (whether *any* currently
/// registered subscriber cares about it) is cached **process-wide**, not per thread. Four
/// scenarios in this binary (`outbox_enqueue_spans`'s two trials, one trial each in
/// `outbox_schema_verification` and `outbox_poison_sweep_failure`, plus
/// `recorder_spawn_capture`) each install their own thread-local recording subscriber this way
/// to capture spans/events for assertion. If a *different* trial, running concurrently on
/// another OS thread with no subscriber installed, is the first to hit one of these callsites,
/// `tracing` can cache its interest as `Interest::never` before this subscriber ever gets a
/// chance to register — this subscriber is then the active default for its own thread but never
/// sees the span/event at all (observed as an empty transcript roughly 1 run in 12 before this
/// was understood). Rebuilding right after `set_default` re-evaluates every registered
/// callsite's interest against the subscriber that is *now* the default and closes that specific
/// window, but it does not prove no other thread can still race a callsite this subscriber has
/// not yet rebuilt interest for — mid-test, another trial's callsite hit is still a race with
/// whatever this subscriber has or hasn't seen so far.
///
/// **The actual, deterministic guarantee** is not this rebuild — it is that `tests/postgres/main.rs`
/// runs every recorder trial in a second, `--test-threads 1` phase that only starts after the
/// first (ordinary) phase's `libtest_mimic::run` call has returned. That call runs its trials
/// inside `std::thread::scope`, which blocks until every worker thread it spawned has been
/// joined — so by construction, no other **trial's own** thread is still alive for the entire
/// duration of the recorder phase (the shared multi-thread `rt` and its sqlx-pool background
/// tasks from phase one *are* still running by then, but every callsite they touch was already
/// registered with interest during phase one, before this phase's subscriber ever took over, so
/// they have nothing left to race). Recorder trials also cannot race *each other* there, since
/// `--test-threads 1` runs them one at a time on the phase's own thread. This function's rebuild
/// call is kept anyway as cheap defense in depth (in case a
/// recorder trial is ever run in isolation via a test filter, without the surrounding phase
/// structure), not as the mechanism the acceptance tests actually rely on. The guard is
/// returned so the default reverts on drop, exactly like a plain `set_default`.
pub(crate) fn install_recording_subscriber<S>(subscriber: S) -> tracing::subscriber::DefaultGuard
where
    S: tracing::Subscriber + Send + Sync + 'static,
{
    let guard = tracing::subscriber::set_default(subscriber);

    tracing::callsite::rebuild_interest_cache();

    guard
}

/// Runs `fut` to completion on a **dedicated, fresh current-thread Tokio runtime** with
/// `subscriber` installed as the `tracing` default for the entire call — including for any task
/// `fut` itself hands to `tokio::spawn`.
///
/// **The hazard this exists for.** [`install_recording_subscriber`] (and a plain
/// `tracing::subscriber::set_default`) only sets the default for the calling OS thread. A
/// `#[tokio::test]`-style trial that runs on this crate's *shared, multi-thread* runtime
/// (`main.rs`'s `rt`) and then `tokio::spawn`s a task hands that task to one of the runtime's
/// worker threads — which never had the recorder installed, so a span or event the spawned task
/// emits is invisible to the recorder even though the spawning code ran under it a moment
/// earlier. No store method spawns today (RELIAR-66), so nothing in this crate's test suite has
/// hit this yet, but a recorder test written against one that does *would* silently see an
/// incomplete transcript rather than fail loudly — exactly the class of bug this card exists to
/// prevent.
///
/// Building a **current-thread** runtime here (never the shared multi-thread `rt`) is what fixes
/// it: a current-thread runtime multiplexes every task it drives — the root future and anything
/// it spawns — onto the single OS thread that calls `block_on`, so a thread-local default set
/// around that call stays in scope for all of them. See `recorder_spawn_capture.rs` for a test
/// that proves a spawned task's event is captured through this helper.
pub(crate) fn with_recorder_dispatch<S, Fut>(subscriber: S, fut: Fut) -> Fut::Output
where
    S: tracing::Subscriber + Send + Sync + 'static,
    Fut: std::future::Future,
{
    let dispatch = tracing::Dispatch::new(subscriber);

    tracing::dispatcher::with_default(&dispatch, || {
        tracing::callsite::rebuild_interest_cache();
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("build a dedicated current-thread runtime for the recorder");

        runtime.block_on(fut)
    })
}