GeezerVison is showing github.com — Future Support for Setup Queries When a New Pool Client Connects · Issue #3617 · brianc/node-postgres
Future Support for Setup Queries When a New Pool Client Connects #3617DescriptionHello! Thank you for all your work on this module. I've been using it for many years and it was always very solid.
I'm running into a little problem with this new deprecation warning
Calling client.query() when the client is already executing a query is deprecated and will be removed in [email protected]. Use asycn/await or an external async flow control mechanism instead.
I get this a warning a bunch of times while running tests and on app startup. I tracked it down to the following code:
const pool = new pg.Pool(poolConfig)
pool.on('connect', async (client) => {
await client.query("SET TIME ZONE 'Europe/Berlin'")
})
I adapted this from the Pool › Events › connect docs:
Whenever the pool establishes a new client connection to the PostgreSQL backend it will emit the connect event with the newly connected client. This presents an opportunity for you to run setup commands on a client.
const pool = new Pool()
pool.on('connect', (client) => {
client.query('SET DATESTYLE = iso, mdy')
})
I'm using async/await keywords in my example, but they don't matter, because the actual place (in pg-pool) where the event is emitted from does/can not await. (I'm actually not sure if EventEmitter#emit even supports async/await.)
|
if (isNew) { |
|
this.emit('connect', client) |
|
} |
This means that it is quite likely that a fresh client will still try to execute the setup query when the new query is being enqueued. So it looks like the recommended way to do client setup will stop working once the internal queue is actually removed.
I'm not sure what the best solution for this is. Maybe there really is a way to attach async event handlers that are actually awaits directly, or maybe it could be possible to add a callback when creating the pool?
const pool = new Pool({
async onClientConnect(client) {
await client.query('SET DATESTYLE = iso, mdy')
}
})Reacted by Stephen Haberman and Pierre Viara Activitybrianc commented on Mar 2, 2026 thanks! that is an excellent point & something I've actually craved myself in the past! I will ship this later this week! ❤️
idea: events aren't really the best way to handle 'connect' 'create' 'release' or 'destroy' of clients, but callbacks that can "block" by returning a promise (e.g. being async) are much more flexible. Maybe I just add all 4 of those callbacks:
onConnect // called when client first establishes a connection, before it is checked out for the first time
onCheckout // called every time before the client is checked out, including immediately after `onConnect` if the client is new
onRelease // called every time a client is returned to the pool
onClose // called when a client is shut down due to pool shutdown or idle timeout
danieldiekmeier commented on Mar 2, 2026 ContributorAuthor More actions Thank you, that sounds amazing! I‘m looking forward to trying it! stephenh commented on Mar 2, 2026 Oh wow, I just noticed these warnings as well -- we're doing similar "on connect, issue a query to setup our audit trail" -- thanks @danieldiekmeier for filing and @brianc for already having a fix/approach in mind! brianc commented on Mar 2, 2026 I am working on this now. FWIW I don't think onCheckout and onRelease make much sense really since you, the caller, know when your checking out a client and releasing it. But I have onConnect working and am adding onClose. Should have a PR up today. brianc commented on Mar 3, 2026 I added the connect part in #3620 ! Will release semver minor tomorrow brianc commented on Mar 4, 2026
I added the connect part in #3620 ! Will release semver minor tomorrow
addendum: will release semver minor w/ this change whenever it's ready, but definitely targeting this week. :) Reacted by Daniel Diekmeier brianc commented on Mar 4, 2026 it's out now - [email protected] - please see referenced PR for more info. Will document it here later today. Reacted by Stephen Haberman, Daniel Diekmeier, Fidra Sofyan and Pierre Viara danieldiekmeier commented on Mar 5, 2026 ContributorAuthor More actions @brianc I just installed 8.20 in our project and migrated to onConnect. It works like a charm!
Thank you for your fast work on this, and thank you for the very friendly communication all the way through – it really is much appreciated! stephenh commented on Mar 5, 2026 brianc commented on Mar 5, 2026 thanks for the offer to create a PR @stephenh ! I honestly don't think I've ever contributed to it myself which is shame on me probably at this point. I think I'd like to pull the type defs into this repo itself, possibly w/ the 9.0 release, so they cant be more in lockstep w/ the rest of the library. I have a pretty significant amount of typescript experience at this point (the only time I write actual raw ecmascript these days is in this repo, in fact); however, I am not 100% sure how to publish types along w/ an npm library so I might ask for some help from the community on getting it moved over & set up. I'll circle back once I get the 9.x feature branch cut at the milestone fully put together (working on that now)
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|