Skip to content

The transactional outbox in PostgreSQL

A worker slides shut one drawer holding an order slip and a pickup slip side by side.

Too long, didn’t read

If saving an order promises a pickup, save the order and the job in the same Postgres transaction. A worker on another connection can read both after commit. It can find the job even if it was away when the order was placed.

The order exists. Where is the pickup?

Picture the warehouse app saving order 103 and crashing before it queues the pickup. The order has a number. The worker checks for its pickup and gets a very tidy empty result.

Move both writes into one transaction. In the PostgreSQL 18.7 scratch run, the app inserted both records and paused before committing. The worker queried both tables from its own connection. What did it find?

In the recorded run, App inserts order 103 and its pickup in one transaction. Worker reads both tables from a separate connection and sees neither row. After App commits, Worker's next read finds 103 in both.

The pickup job is the outbox entry, a saved request for work. The app can confirm both records after one commit. The worker can look up the saved request later. PostgreSQL transactions

BEGIN;
INSERT INTO orders (id) VALUES (103);
INSERT INTO pickup_jobs (order_id) VALUES (103);
COMMIT;

What survives a rollback or a disconnect?

The app tries order 104 next. It writes both rows and rolls back. The worker still finds only 103 in both tables. Then the worker disconnects, and the app commits order 105 and its pickup.

Worker's separate connection Observed after the first commit
Checkpoint Pickup jobs found
104 rolled back 103
Worker reconnects after 105 commits 103, 105

While the worker is away, 105 has somewhere to wait. The worker reads at Read Committed, so each statement gets a view of what was committed when it started. A read that starts before commit can miss the row. The next statement can see it. PostgreSQL transaction isolation

The foreign key checks that a pickup job points to an order. A writer that inserts only an order can still commit it. Every path that promises a pickup has to make both writes. PostgreSQL constraints

When a second writer inserts a job

The warehouse adds an import script. It writes to the queue without sending a wake-up message. The job can be ready while the worker sleeps. If inserting a row means it’s ready to claim, a row-level AFTER INSERT trigger can send the same wake-up for both writers. PostgreSQL triggers

Writers A and B used a separate scratch database with an empty queue. The listener had already subscribed. Each check waited up to 250 milliseconds and stopped at the first signal.

CheckpointWhat happens to the writer’s rowSignal during the check
A opens the transaction and inserts 103103 is still uncommittedNone received
A commits103 is committedReceived
B inserts 104 and commits104 is committedReceived
A rolls back an insert of 105105 is discardedNone received

The listener’s final query found 103 and 104. Those were separate commits. Suppose a writer adds pickups 106 and 107 in one transaction and the trigger sends the same channel and payload for both.

PostgreSQL can deliver identical notifications from one transaction as a single wake-up. One nudge is enough. Both jobs are there when the worker checks the queue. PostgreSQL NOTIFY · How the worker checks again after reconnecting

After the app commits, the warehouse can tell a customer the pickup is queued. A wake-up tells the worker to read the queue. On startup or reconnect, it subscribes first and then checks the queue. It can then claim a job . The receipt from the pickup service is a later result.

Replay commit, rollback, and reconnect

Open an App connection and a Worker connection to the same scratch database. Create the tables from App. Leave Worker at Read Committed, the PostgreSQL default.

CREATE TABLE orders (
  id bigint PRIMARY KEY
);

CREATE TABLE pickup_jobs (
  order_id bigint PRIMARY KEY REFERENCES orders (id)
);

Run the earlier order block in App through both inserts, then pause before COMMIT. In Worker, run SELECT id FROM orders ORDER BY id; and read the pickup jobs with this query. Commit in App and repeat both reads.

SELECT order_id FROM pickup_jobs ORDER BY order_id;

Run the rollback block in App. Both reads from Worker still return only 103.

BEGIN;
INSERT INTO orders (id) VALUES (104);
INSERT INTO pickup_jobs (order_id) VALUES (104);
ROLLBACK;

Close Worker. Run the commit block from App with 105 in both inserts. Open a fresh Worker connection and read the queue. It returns 103 and 105.

Replay the two writers with the trigger

Use a new scratch database for this second fixture. Create its job table and install the trigger.

CREATE TABLE pickup_jobs (
  order_id bigint PRIMARY KEY
);
CREATE FUNCTION signal_pickup() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
  PERFORM pg_notify('pickup_ready', '');
  RETURN NEW;
END;
$$;

CREATE TRIGGER pickup_queued
AFTER INSERT ON pickup_jobs
FOR EACH ROW EXECUTE FUNCTION signal_pickup();

From a fresh listener connection, run LISTEN pickup_ready; with autocommit enabled. Keep it outside a transaction. Run Writer A through its insert, then pause before committing. Look for a notification before and after you commit. If your terminal client waits for a command before printing notifications, issue a small query from the listener at each checkpoint.

BEGIN;
INSERT INTO pickup_jobs (order_id) VALUES (103);
COMMIT;

Open a second writer connection and run its block. The listener gets a signal after that commit as well.

BEGIN;
INSERT INTO pickup_jobs (order_id) VALUES (104);
COMMIT;

Run the rollback block from Writer A. Query SELECT id FROM pickup_jobs ORDER BY id; from the listener to find 103 and 104.

BEGIN;
INSERT INTO pickup_jobs (order_id) VALUES (105);
ROLLBACK;