reliar-store-postgres 0.6.0

PostgreSQL provider for the Reliar transactional outbox: migrations, migrate(), enqueue and the SKIP LOCKED claim.
Documentation
//! The PostgreSQL version floor and the one query that checks it (ADR 0041).
//!
//! Runs at exactly two entry points — [`crate::PostgresOutboxStore::connect`] and the first
//! statement of [`crate::migrate`] — never per pooled connection and never on any hot path: the
//! floor is a property of the server, so it cannot change under a live connection in a way a
//! per-statement check would ever catch first.

use sqlx::PgExecutor;

/// The floor this crate enforces, in PostgreSQL's own `server_version_num` encoding (`major *
/// 10_000 + minor`) — `180_000` for PostgreSQL 18.0. **Public** so a host can state the same
/// requirement in its own preflight without hard-coding the number (ADR 0041). There is no
/// older-version fallback: a server below this reports
/// [`crate::PostgresStoreError::UnsupportedServerVersion`] or
/// [`crate::MigrateError::UnsupportedServerVersion`], never a degraded mode.
pub const MIN_SERVER_VERSION_NUM: u32 = 180_000;

/// Reads the connected server's `server_version_num` via `current_setting` — the integer form
/// PostgreSQL itself uses for version comparisons. Chosen over parsing `SHOW server_version` (or
/// the driver's own cached startup value) because it needs no string parsing and survives a
/// beta/RC version string a text parse would mishandle (ADR 0041).
pub(crate) async fn detected_server_version_num(
    executor: impl PgExecutor<'_>,
) -> Result<u32, sqlx::Error> {
    let raw: i32 =
        sqlx::query_scalar!(r#"SELECT current_setting('server_version_num')::int AS "value!""#)
            .fetch_one(executor)
            .await?;

    // PostgreSQL itself reports this value; it is always a small positive integer (180_000 for
    // 18.0), so this conversion cannot fail in practice. This function only *reads* the number;
    // the floor is enforced by the callers — `PostgresOutboxStore::connect` and `migrate()` compare
    // the result against `MIN_SERVER_VERSION_NUM`. An impossible negative is clamped to 0, which
    // those callers reject like any other sub-18 value, rather than being silently defaulted.
    Ok(u32::try_from(raw.max(0)).unwrap_or(0))
}