Skip to content

Explicit Resource Management for pg pool client#3661

Draft
hyperair wants to merge 7 commits intobrianc:masterfrom
hyperair:disposable-pool-client
Draft

Explicit Resource Management for pg pool client#3661
hyperair wants to merge 7 commits intobrianc:masterfrom
hyperair:disposable-pool-client

Conversation

@hyperair
Copy link
Copy Markdown

@hyperair hyperair commented Apr 27, 2026

Add support for Explicit Resource Management for pg.Pool clients.

This is implemented using [Symbol.dispose] instead of [Symbol.asyncDispose] because client.release() is a synchronous function, not async.

Additionally, add a second property client.destroyOnDispose so that Symbol.dispose supports calling client.release(true). This is useful for a usecase that changes connection parameters in a way that is troublesome to reset upon release to the pool, e.g.

const pool = new Pool({
    database: 'foo',
    user: 'foo',
    password: 'foo',
    port: 5432,

    statement_timeout: 10_000,
});

async function getSlowClient() {
    const client = await pool.connect();
    await client.query("SET statement_timeout = 0");
    client.destroyOnDispose = true;
}

async function performReallySlowQuery() {
    using client = await getSlowClient();

    client.query(`select pg_sleep_for('30 minutes')`);
}

Fixes: #3515

@hyperair hyperair force-pushed the disposable-pool-client branch 3 times, most recently from bac8c73 to 7d69534 Compare April 27, 2026 05:05
@hyperair
Copy link
Copy Markdown
Author

Hmm, looks like eslint needs to be upgraded before it'll parse the using test

When `Symbol.dispose` is defined, define a disposer function that simply calls
`this.release()`. This lets the `PoolClient` with the `using` syntax work with any
downstream-overridden `release` functions.

This makes `PoolClient` support Explicit Resource Management when the runtime
supports it.

Fixes: brianc#3515
Add a `destroyOnDispose` property on the client to signal that
`[Symbol.dispose]()` should call `client.release(true)` instead of
`client.release()`.
@hyperair hyperair force-pushed the disposable-pool-client branch from 7d69534 to 0cb5f67 Compare April 27, 2026 08:42
@hyperair hyperair marked this pull request as draft April 27, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Symbol.asyncDispose support

1 participant