The one-connection rule
For: a developer who writes a hook on Postgres, and above all on Workers through Hyperdrive.
What it does: this page explains two hangs that a hook can cause, and the ways to avoid them.
Before you start
Section titled “Before you start”- Read Run your code at a hook point for the before hooks and the after hooks.
- Read Workers on Postgres through Hyperdrive for the client of one connection.
The setup
Section titled “The setup”On Workers, the Postgres client of each request has one connection: max: 1. A transaction holds that connection from BEGIN to COMMIT.
A before hook runs inside the flow’s transaction, before the first write. The one exception is a lock write: some flows lock a row before the hook, as A second hang tells. Core waits for the hook to settle before it goes on.
The hang
Section titled “The hang”- Core opens a transaction, which takes the one connection.
- Core calls the before hook and waits.
- The hook runs a query on the same client.
- The query waits for a free connection.
- No connection comes free, because the transaction waits for the hook.
The request hangs until the client ends. postgres.js has no default queue timeout. No error names the cause.
The ways out
Section titled “The ways out”- A second connection. Give the hook a pool of two or more, or a separate client. The hook’s query then runs on its own connection.
- The after hook. Write the app’s row from the after hook, after the commit. A failed write there leaves the core row without the app’s row, and core logs the failure.
- D1. On D1 a write through the binding is a separate request and takes no lock, so the hang cannot occur.
A before hook must also not open a transaction on core’s storage. It runs inside one already.
A second hang: the flow’s own lock
Section titled “A second hang: the flow’s own lock”Some flows lock a row before their before hook, such as the user row of an organization create. Locks over a before hook lists each flow and its rows.
- Core opens a transaction and locks the row.
- Core calls the before hook and waits.
- The hook awaits a core write of the same row, such as
auth.api.user.updateof the same user. - The write waits for the row lock.
- The lock holds until the commit, and the commit waits for the hook.
This hang occurs with a pool of any size, because a second connection waits for the same lock. No error names the cause. A second connection is no way out here. Write from the after hook, or start the write with no await and catch its error. D1 has no row lock, so this hang cannot occur there.
Why core does not stop it
Section titled “Why core does not stop it”Core sets no timeout on a hook, as it sets none on the mail port. The rule stays with the app, and the adapter README states it beside the setting.