I've setup my application's pool with an on('error') hook to log errors that happen outside queries.
However, this hook is not always active between queries. If I acquire a connection from the pool to run a transaction, and that transaction falls afoul of the idle_in_transaction_session_timeout, the error thrown from there doesn't happen during a query, and so if I don't take extra steps to add an on('error') listener for each such connection, my app dies.
But I also have to make sure to manually remove those, since releasing the client back to the pool doesn't do so!
Is there a recommended way to do this "nicely"?
I see some possible ways to make this more ergonomic, which might be applied in combination:
- Apply that idle error listener from the pool even when the client is acquired
- This probably would need to be combined with some logic to avoid calling it if a "per-acquire" error listener is attached
- Automatically clear client error listeners on release to the pool
I've setup my application's pool with an
on('error')hook to log errors that happen outside queries.However, this hook is not always active between queries. If I acquire a connection from the pool to run a transaction, and that transaction falls afoul of the
idle_in_transaction_session_timeout, the error thrown from there doesn't happen during a query, and so if I don't take extra steps to add anon('error')listener for each such connection, my app dies.But I also have to make sure to manually remove those, since releasing the client back to the pool doesn't do so!
Is there a recommended way to do this "nicely"?
I see some possible ways to make this more ergonomic, which might be applied in combination:
client.removeAllListeners('error')just before re-adding the idle listener here: https://github.com/brianc/node-postgres/blob/master/packages/pg-pool/index.js#L329