node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

How works the verify options ?

Open ltoinel opened this issue 4 years ago • 1 comments

Hi,

Our software seems to use sometimes connection that didn't respond. We use PGBouncer between the database and our software.

Is there a clean way to test the connection before to use the connection ? I found this interesting "verify" option on PGPool that is not clearly documented. Should we use this function to check the connection before to use it ? How do you use it in production ?

if (this.options.verify) { this.options.verify(client, cb) } else { cb(undefined, client, client.release) }

Thank-you !

ltoinel avatar Oct 07 '21 07:10 ltoinel

Hello,

We have exactly the same problem.

I've tried:

verify: (client, cb) => { client.query('SELECT 1').then(()=>{cb(false)}, (err)=>{cb(err)}) }

however, it doesn't work as expected, the connection is invalidated and removed, but the caller gets an error nevertheless.

The code called is:

 if (isNew && this.options.verify) {
    this.options.verify(client, (err) => {
      if (err) {
        client.release(err)
        return pendingItem.callback(err, undefined, NOOP)
      }

I think it should be:

return this. _acquireClient(client, pendingItem, idleListener, isNew)

maybe with some watchdog to prevent unending loop in case the validation query is invalid, or DB is broken (not only DB connection).

Ideally verify should be called once after polling from pool, and once on create.

llech avatar Feb 03 '22 11:02 llech