Outbox
Events written in the same transaction as the change, read by named consumers with their own cursor and relayed as CloudEvents.
The outbox SQL module stores events in a table in
the same transaction as the change that caused them. An event exists only
if that transaction commits, so a rolled-back write never notifies anyone
and a committed one is never lost. Named consumers read the events in order,
each with its own cursor, and createOutbox from better-supabase/blocks/outbox
relays them to any EventSink (an HTTP endpoint, a queue, a bus).
pnpm better-supabase add outboxOnce the module is installed, the other SQL modules write their events to
it: organizations emits organization.created, organization.switched and the member
events, and impersonation emits support_session.started and
support_session.ended. Set
blocks.<module>.events to false to keep one module out of the outbox.
Emitting from SQL
emit_event(type, payload, subject, tenant, key, source) appends an event
and returns its id (a uuid on a managed table, as text). Call it from your
own functions and triggers:
perform better_supabase.emit_event(
'invoice.paid',
jsonb_build_object('invoice_id', new.id, 'amount', new.amount),
subject => 'invoices/' || new.id,
tenant => new.organization_id::text,
key => 'invoice-paid-' || new.id
);| Argument | Meaning |
|---|---|
type | Any string, usually noun.verb. Consumers filter on it |
payload | The event data. subject defaults to payload ->> 'subject' |
tenant | The organization the event belongs to, relayed as the partitionkey extension |
key | With a key, emitting the same key again for the same tenant returns the first event's id |
source | The module or function that wrote it, relayed as the producer extension |
The signed-in user (auth.uid()) is stored as the actor. Only
service_role can call emit_event directly; the module's own functions are
security definer and call it for the user. Add roles with
blocks.outbox.options.emitRoles.
track_events(table, type_prefix, tenant_column) adds a row trigger that
emits <prefix>.created, .updated and .deleted with the row as payload
and <table>/<id> as subject. An update that changes no column emits
nothing:
select better_supabase.track_events('public.invoices', 'invoice', 'organization_id');Relaying events
Create the outbox client over a server connection, register each consumer
once, then relay it from a cron route or a worker. transport emits and
reads the history; service, which defaults to transport, runs the
consumer calls, which need the service role:
Server code calls the outbox instance on db, in the caller's organization. With gen.hooks set, better-supabase gen writes the hooks:
export const { useList: useOutbox } = defineDataHooks(
"outbox",
(db: AppDb) => db.outbox,
);const rows = await db.outbox.list({ limit: 50 }).orThrow();"use client";
import { useOutbox } from "@/lib/supabase/hooks";
const rows = useOutbox({ limit: 50 });import "server-only";
import { httpSink } from "better-supabase/events";
import { outbox } from "@/lib/outbox";
export const GET = outbox.relayRoute({
secret: process.env.CRON_SECRET,
consumers: { billing: httpSink(process.env.BILLING_EVENTS_URL!) },
onError: (error, consumer) => console.error(consumer, error.message),
});relay(consumer, sink) claims up to batch events (100 by default), sends
them as one sink.send call and moves the cursor past them, until none are
left or budgetMs runs out. When the sink throws, the cursor stays where it
was and the consumer backs off (2 seconds, doubling up to maxBackoff, 10
minutes by default), so delivery is at least once. The CloudEvent id is the
row id, so a receiver can drop repeats.
After a failure the consumer claims one event at a time, so one bad event
can't hold back a whole batch. When that single event fails maxAttempts
times (an option of relay and consume, 10 by default), it moves to the consumer's dead letters and the cursor
passes it; the result's deadLettered counts them. A success resets the
attempts.
const dead = await outbox.deadLetters("search").orThrow();
// [{ consumer, event, attempts, error, deadAt }]Dead letters keep the event as it was, so you can fix the cause and emit it
again. purge deletes the ones older than its olderThan.
Each type gets typePrefix (dev.better-supabase by default) in front, the
payload is the data and the tenant is the partitionkey. The actor is not
sent: context attributes must not carry personal data, so put it in the
payload when receivers need it.
0.47 renamed some event types (work_item.created is task.created, and each
saved event is created or updated). Set legacyEventTypes: true to relay
a renamed type under its old name, for receivers that still match it, with the
new name in the currenttype extension, which the library's sinks read. Your
own sinks get the current type, without the prefix, from eventTypeOf(event)
in better-supabase/events. The option is deprecated and goes away in the next minor.
A claim leases the consumer to one worker for lease (one minute by
default), so two cron runs never send the same batch at the same time.
relayRoute checks the bearer secret and relays every consumer at the same
time within one budget, since each has its own cursor and lease.
Relaying right after an emit
A cron route relays once a minute. kick relays after each successful
emit instead, and waitUntil (bsb.waitUntil in Next.js, a Workers
ctx.waitUntil) keeps the relay running after the response. Emits made
before a relay starts share it, and an emit made while one runs queues one
more. A relay that throws is logged and never changes the emit result.
Keep the cron route as the fallback: an emit inside a transaction that
hasn't committed yet is invisible to the relay.
export const outbox = createOutbox({
transport,
source: "https://crm.example.com",
kick: {
waitUntil: bsb.waitUntil,
run: () => outbox.relay("billing", billingSink, { budgetMs: 10_000 }),
},
});An event written by emit_event in SQL or a track_events trigger never
passes through kick. The events table has a statement trigger that sends
pg_notify('better_supabase_work', '{"kind":"outbox"}') once per insert
statement, on commit, and listenForWork from better-supabase/postgres
relays when it hears it:
const listener = await listenForWork(pool, {
outbox: () => outbox.relay("billing", billingSink),
});It needs a session connection (not port 6543), and asks outbox_has_work
after each (re)connect. See
Starting jobs right away.
Consumers inside the app
consume(consumer, handler) claims and acknowledges the same way, but hands
the handler the outbox rows instead of CloudEvents: type, payload,
tenant, subject, key, actorId, position and createdAt. Use it for
consumers in your own app, such as search indexing or notifications, that need
the actor. A handler that throws or returns a failed Result leaves the
batch for the next run.
await outbox.consume("search", async (events) => {
for (const event of events) {
await index.update(event.subject, {
by: event.actorId,
at: event.createdAt,
});
}
});relayRoute takes a handler in place of a sink too:
consumers: { search: (events) => indexEvents(events) }.
Which events a consumer gets
Pass subscribers, a SubscriberPolicy, to keep events from consumers that
may not read them. Without it, createOutbox uses
authorization.delivery.subscribers. relay and consume call the policy
once per claimed event with the consumer as a service subscriber
({ principal: "search", kind: "service" }). An event it leaves out is
acknowledged without reaching the sink or handler, and the result's refused
counts it. A policy that throws fails the batch like a sink that throws.
Consumers are your own server code, not principals, so the block has no
default policy for them under an authorization provider; the sinks they
feed (webhooks, notifications, realtime) check their own subscribers.
const outbox = createOutbox({
transport: sqlTransport(postgres.admin),
source: "https://crm.example.com",
subscribers: (event, subscribers) =>
event.type.startsWith("billing.")
? subscribers.filter((s) => s.principal === "ledger")
: subscribers,
});The outbox SQL doesn't filter: every registered consumer claims every event, and the policy runs in TypeScript.
hooks run around emit, register, unregister, history,
deadLetters and purge, the methods that return a result. With
defineBlocks, set the CloudEvents source with
outboxBlock.with({ source: "https://crm.example.com" }). The outbox tables
take no app columns: the app's data goes in each event's payload.
Ordering and open transactions
Positions come from an identity column, which hands out numbers before
commit, so a transaction that started first can commit after a later one.
The claim holds back every event whose transaction id (xid) is not older
than the oldest running transaction (pg_snapshot_xmin), and orders and
tracks events by (xid, position). An event that commits late therefore
sorts after the consumer's cursor and is delayed, never skipped. A trigger
sets xid to the writing transaction's id on every insert and keeps it on an
update, so a client that may insert events can't plant one that sorts past the
cursors or never settles. A
long-running transaction delays all consumers until it ends. A new consumer
starts after the latest event, but never past the current snapshot, so rows
restored from another database with higher transaction ids don't hide the
next event from it. A consumer registered in a transaction that emitted
events starts after those events, so it also misses events that
transactions which started earlier commit later; register it outside that
transaction, or with fromStart: true, when it needs them.
When none of the claimed events match a consumer's types, its cursor
still moves past the settled events, so a consumer of rare types doesn't
rescan the whole table. outbox.unregister(consumer) removes a consumer
you no longer relay, so purges stop waiting for it. purge with
{ ignoreIdle: "7 days" } also stops waiting for a consumer that hasn't
claimed for that long, without removing it.
Into a queue
To process events with retries, relay them into a job queue with the event id as the dedupe key:
const toJobs = {
send: async (events: readonly CloudEvent[]) => {
for (const event of events) {
await jobs.enqueue("events", event, { dedupeKey: event.id }).orThrow();
}
},
};
await outbox.relay("jobs", toJobs);History and retention
outbox.history({ subject: 'invoices/42' }) returns the kept events for one
subject, oldest first, for an activity feed or a debugging view. Filter by
type and page with cursor (a position) and limit.
outbox.purge(olderThan, batch) (SQL purge_outbox(older_than, batch))
deletes up to batch events (10,000 by default) older than
blocks.outbox.options.retention (30 days by default), but never one that a
registered consumer hasn't passed (see ignoreIdle above). It returns how many it deleted; run it
again while that equals batch. Schedule it with the jobs schedules or
pg_cron.
Partitioned events
blocks.outbox.options.partitioned creates a new outbox_events table
partitioned by month on created_at, with the primary key
(id, created_at) and a sequence for the position. purge_outbox then drops
every month past the retention cutoff that no active consumer still waits
for, before it deletes the remaining rows. A key stays unique per tenant
through a lock in emit_event instead of a unique index. Schedule
better_supabase.maintain_partitions() once a month to add the months ahead.
The option applies when the module creates the table. An existing table
stays as it is, and better-supabase doctor reports it as BS335.
Existing tables
Map tables.events to an events table you already have and rename its
columns. The block adds the xid column (and its index) when the table lacks
it, so upgrade doesn't ask you to declare it first. Map xid to
null to keep the table as it is: the claim then orders by position alone
and waits settle (5 seconds by default, and never zero) after an event's
created_at, which skips an event whose transaction commits later than
that. Other columns you don't have map to null.
export default defineConfig({
blocks: {
outbox: {
tables: { events: "public.domain_events" },
idType: "uuid",
columns: {
events: { type: "kind", position: "seq", xid: null, key: null },
},
options: { settle: "2 seconds" },
},
},
});position must be a bigint that only grows. A managed table has a uuid
id and a separate position identity column; map both when the outbox runs
over your table, or map them to the same column when its ids are a bigint sequence.
Options marked migration-only match an existing schema. The config accepts them only when the outbox runs over a table your app already has, and doctor warns about them (BS314) until you remove them.
| Option | Default | Meaning |
|---|---|---|
retention | 30 days | Default age for purge_outbox |
emitRoles | ["service_role"] | Roles allowed to call emit_event directly |
settle | 5 seconds | The wait before a claim reads an event, without xid; never zero |
defaultSource | none | The source emit_event stores when the caller passes none; migration-only |
blockSource | better-supabase/{module} | The source of events from SQL modules; migration-only |
Error codes
hint | When |
|---|---|
OUTBOX_TYPE_REQUIRED | emit_event without a type |
OUTBOX_UNKNOWN_CONSUMER | Claiming for a consumer that isn't registered |
Without the client
db.outbox is this client. Code that does not go through bsb.db() builds it with a transport and an organization id:
import { createOutbox, sqlTransport } from "better-supabase/blocks/outbox";
export const outbox = createOutbox({
transport: sqlTransport(postgres.admin),
source: "https://crm.example.com",
typePrefix: "com.example.crm",
});
await outbox.register("billing", {
types: ["invoice.*", "organization.created"],
});Last updated on
Queue
Background jobs on Supabase Queues, safe retries and exactly-once webhook handling on the Postgres you already have.
Workflows
A run registry for any workflow engine that members read through RLS, cron schedules, counting semaphores, admission control for starts, and useWorkflowRuns over Realtime.