Skip to content

12.32.0

Choose a tag to compare

@timgit timgit released this 14 Sep 14:49
· 493 commits to master since this release

What's Changed

Transactional workers

work() takes a transactional option. The handler receives a second argument — a db for the transaction pg-boss opened around it — and everything written through that db commits with the job's completion.

await boss.work('charge-customer', { transactional: true }, async (jobs, tx) => {
  const [job] = jobs

  await tx.executeSql('INSERT INTO ledger (customer, amount) VALUES ($1, $2)', [job.data.customer, job.data.amount])

  // this job commits with the ledger row and with the completion of the job being handled
  await boss.send('send-receipt', { customer: job.data.customer }, { db: tx })
})

The handler cannot leave its side effects committed and the job unfinished, or the reverse. A throw rolls back the handler's writes and the completion together, and the job is then failed on a pooled connection, so retry counts, retry delays and dead lettering behave exactly as they do for any other worker.

Getting this before meant giving up work() — the only way to put a side effect and a completion in one transaction was a manual fetch() / complete() pair with your own polling loop, batching, backoff and error handling around it.

The job is claimed before the transaction opens, so it is active while the handler runs and behaves like any other active job: expireInSeconds bounds it, heartbeats refresh it, groupConcurrency and localGroupConcurrency see it, and a process that dies mid-handler leaves a row the supervisor reclaims. The option changes what the handler can commit atomically, and nothing about how jobs are fetched, batched, retried or supervised.

A commit needs the claim the handler started with. If anything takes the job away while the handler runs — expireInSeconds, a heartbeat the database stopped seeing, an operator's cancel() or fail(), another instance's supervisor — the transaction rolls back rather than committing side effects under a job that is about to run again. A shutdown that abandons a handler mid-flight is treated the same way. A handler that settles its own jobs through complete(), fail(), cancel() or deleteJob() with { db: tx } is recognised and unaffected; one that settles by writing the job table directly is not, and is read as a lost claim.

Each handler in flight holds a pool connection for its own duration. Size max above the transactional concurrency you registered, with room left for fetches, failures and maintenance. pg-boss emits a transactional_pool_headroom warning at work() time when it does not.

perJobResults cannot be combined with transactional: one transaction has a single outcome. It is rejected at work() rather than silently ignored.

TypeScript picks the handler signature from the option — a literal transactional: true adds the second parameter, and everything else keeps the one-argument handler.

The transaction is bounded by the database, not only by pg-boss

transactionTimeoutSeconds sets how long the server gives the handler's transaction before it kills the connection under it. It defaults to expireInSeconds plus the five seconds pg-boss allows its own rollback, and 0 removes it.

This is not the timer that ends a slow handler — expireInSeconds is, and it fires first by design. This covers what no timer inside the process can: the process failing under an open transaction, which then advertises backend_xmin and holds vacuum off every table in the database until something closes it.

It is applied as transaction_timeout where the server has it (PostgreSQL 17+, CockroachDB) and idle_in_transaction_session_timeout otherwise. A bound the connection already carries is never widened — where a role, a managed provider or a pooler set the GUC tighter, that value stands. If pg-boss cannot ask the server which parameter it recognises, it emits a transaction_timeout_probe warning once, runs unbounded, and asks again a minute later.

beginTransaction() on the database interface

IDatabase gains an optional beginTransaction(), alongside the existing listen, returning a handle with a db, commit() and rollback(). The built-in pool implements it, withTransaction() is now a wrapper over it, and a custom db adapter can implement it to enable transactional workers. work({ transactional: true }) throws at registration on an adapter without it, rather than failing on the first batch.

The handle owns its connection and releases it exactly once. A failed COMMIT releases with the error so the pool discards the client, rollback() is bounded and drops the connection when it cannot get through, and the handle's db refuses statements once settled — so a handler that outlived its expireInSeconds cannot issue SQL on a connection the pool has already handed to someone else.

Two new warnings

Both are emitted as warning events and never written to the warning table, which records episodes a database enters and leaves.

Type Reports
transactional_pool_headroom A transactional worker was registered on a pool with no connections to spare
transaction_timeout_probe The server would not answer which transaction timeout it recognises, so handlers run with no database-side bound

Upgrading

No schema change. Schema 41 is unchanged from 12.31.0, nothing migrates, and a mixed-version rollout needs no sequencing: a worker without transactional behaves exactly as it did.

CockroachDB refuses transactional workers on queues with heartbeatSeconds. The heartbeat refreshes the claimed row from a pooled connection, and under serializable isolation the handler's transaction is then refused a write to that same row, which would fail every batch. work() rejects the pair on backends that declare noTransactionalHeartbeat rather than shipping a worker that cannot complete a job; a heartbeat that arrives afterwards (on the job, or on the queue) fails that batch before its transaction opens, with an error naming where it came from. expireInSeconds is the liveness bound there.

A SQL error the handler catches leaves the transaction aborted, and PostgreSQL then rejects every later statement in it — including pg-boss's own completion, so the job fails even though the handler returned. Let such an error propagate, or isolate the statement behind a SAVEPOINT. The recorded failure names this cause rather than reading as a pg-boss bug.

Long handlers are the wrong fit. The transaction stays open for as long as the handler runs, holding its snapshot and blocking vacuum from reclaiming dead rows database-wide. For work measured in minutes, keep an ordinary worker and pass db to complete() instead.

Full Changelog: 12.31.1...12.32.0