Skip to content

Future Support for Setup Queries When a New Pool Client Connects #3617

@danieldiekmeier

Description

@danieldiekmeier

Hello! 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 pg@9.0. 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')
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions