-- name: InsertOutboxEvent :exec INSERT INTO outbox_events ( event_id, event_type, schema_version, aggregate_type, aggregate_id, aggregate_version, payload ) VALUES ( sqlc.arg(event_id), sqlc.arg(event_type), sqlc.arg(schema_version), sqlc.arg(aggregate_type), sqlc.arg(aggregate_id), sqlc.arg(aggregate_version), sqlc.arg(payload) ); -- Locks the next relay batch. For each aggregate only its earliest unpublished -- event (lowest aggregate_version, then position) qualifies, so a relay never -- holds a later version while an earlier one is still pending, whether that -- earlier row is locked by another relay or was just rolled back. Rows locked -- by another relay are skipped without waiting. The statement snapshot can -- only see a later version after the earlier version's transaction committed, -- because aggregate writers check the expected version, so scanning without a -- position watermark cannot overtake an earlier version. -- name: LockPendingOutboxEvents :many SELECT pending.event_id, pending.event_type, pending.schema_version, pending.aggregate_type, pending.aggregate_id, pending.aggregate_version, pending.payload, pending.occurred_at FROM outbox_events AS pending WHERE pending.published_at IS NULL AND NOT EXISTS ( SELECT 1 FROM outbox_events AS earlier WHERE earlier.aggregate_type = pending.aggregate_type AND earlier.aggregate_id = pending.aggregate_id AND earlier.published_at IS NULL AND (earlier.aggregate_version, earlier.position) < (pending.aggregate_version, pending.position) ) ORDER BY pending.position LIMIT sqlc.arg(batch_limit) FOR UPDATE OF pending SKIP LOCKED; -- Marks the events that the broker acknowledged. The relay passes only IDs -- whose rows its own transaction locked, so the statement never waits and the -- count equals the number of distinct IDs. -- name: MarkOutboxEventsPublished :execrows UPDATE outbox_events SET published_at = clock_timestamp() WHERE event_id = ANY(sqlc.arg(event_ids)::uuid []) AND published_at IS NULL; -- Reads the relay backlog for metrics without locking. Both subqueries use -- the partial index on unpublished positions, so the cost follows the backlog -- size, not the table size. The age comes from the database clock, the same -- clock that set occurred_at. -- name: GetOutboxBacklog :one SELECT ( SELECT count(*) FROM outbox_events AS pending WHERE pending.published_at IS NULL )::bigint AS pending_events, coalesce(( SELECT extract(EPOCH FROM clock_timestamp() - oldest.occurred_at) FROM outbox_events AS oldest WHERE oldest.published_at IS NULL ORDER BY oldest.position LIMIT 1 ), 0)::float8 AS oldest_pending_age_seconds; -- Bounds the claim transaction on the server. statement_timeout stops a -- statement that runs too long; idle_in_transaction_session_timeout ends the -- session, and with it the transaction and its row locks, when the client -- stays silent longer than a publish may take, for example after the process -- froze or lost the network without closing the connection. Both settings -- are local to the transaction. -- name: SetRelayTransactionTimeouts :exec SELECT set_config('statement_timeout', sqlc.arg(statement_timeout_ms)::text, true), set_config( 'idle_in_transaction_session_timeout', sqlc.arg(idle_timeout_ms)::text, true );