← Back to PostgreSQL, page 5

postgresql-0085 · Hard senior

Design reliable event publishing from a PostgreSQL-backed service with the transactional outbox pattern.

Write the business change and an outbox row in the same transaction, so either both happen or neither does. A relay process reads unsent outbox rows with FOR UPDATE SKIP LOCKED, publishes them to the broker, and marks them sent; consumers deduplicate by event id because delivery is at least once. This avoids the dual-write problem of committing to the database and publishing separately.

Full answer

Sample schema and data:

sql
CREATE TABLE orders (id int PRIMARY KEY, status text);
CREATE TABLE outbox (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  topic text NOT NULL,
  payload jsonb NOT NULL,
  sent_at timestamptz
);
CREATE INDEX outbox_unsent ON outbox (id) WHERE sent_at IS NULL;
sql
BEGIN; INSERT INTO orders VALUES (1, 'paid'); INSERT INTO outbox (topic, payload) VALUES ('order.paid', '{"order_id": 1}'); COMMIT;

Output:

text
COMMIT
sql
BEGIN; INSERT INTO orders VALUES (2, 'paid'); INSERT INTO outbox (topic, payload) VALUES ('order.paid', '{"order_id": 2}'); ROLLBACK; SELECT (SELECT count(*) FROM orders) AS orders, (SELECT count(*) FROM outbox) AS events;

PostgreSQL returns:

text
 orders | events
--------+--------
 1      | 1
(1 row)
sql
BEGIN; SELECT id, topic, payload FROM outbox WHERE sent_at IS NULL ORDER BY id LIMIT 100 FOR UPDATE SKIP LOCKED;

Result on PostgreSQL 17:

text
 id |   topic    |     payload
----+------------+-----------------
 1  | order.paid | {"order_id": 1}
(1 row)
sql
UPDATE outbox SET sent_at = now() WHERE id = 1; COMMIT; SELECT count(*) FILTER (WHERE sent_at IS NULL) AS unsent FROM outbox;

Result:

text
 unsent
--------
 0
(1 row)

The first transaction stores the order and its event together; the second rolls back and leaves neither, so the database and the event stream cannot disagree. The relay picks unsent events with SKIP LOCKED, so several relay instances can run, publishes them (outside this demo) and marks them sent. If the relay crashes after publishing but before committing, the event is published again, which is why consumers must be idempotent. Further points: keep outbox rows small, delete or partition sent rows, preserve order per aggregate if consumers need it, and consider logical decoding tools such as Debezium when polling latency matters.

sql
CREATE TABLE orders (id int PRIMARY KEY, status text);
CREATE TABLE outbox (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  topic text NOT NULL,
  payload jsonb NOT NULL,
  sent_at timestamptz
);
CREATE INDEX outbox_unsent ON outbox (id) WHERE sent_at IS NULL;

BEGIN; INSERT INTO orders VALUES (1, 'paid'); INSERT INTO outbox (topic, payload) VALUES ('order.paid', '{"order_id": 1}'); COMMIT;

BEGIN; INSERT INTO orders VALUES (2, 'paid'); INSERT INTO outbox (topic, payload) VALUES ('order.paid', '{"order_id": 2}'); ROLLBACK; SELECT (SELECT count(*) FROM orders) AS orders, (SELECT count(*) FROM outbox) AS events;

BEGIN; SELECT id, topic, payload FROM outbox WHERE sent_at IS NULL ORDER BY id LIMIT 100 FOR UPDATE SKIP LOCKED;

UPDATE outbox SET sent_at = now() WHERE id = 1; COMMIT; SELECT count(*) FILTER (WHERE sent_at IS NULL) AS unsent FROM outbox;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Write the change and the event in one transaction
  • A relay publishes unsent rows
  • SKIP LOCKED lets relays scale
  • Delivery is at least once; consumers deduplicate
  • Clean up sent rows

Common mistakes

  • Publishing to the broker inside the database transaction
  • Assuming exactly-once delivery

Follow-ups

  • How would you keep per-order event ordering?
  • When would logical decoding replace polling?

Tips

  • Show the rollback case