You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I made an assumption that the same can happen on regular postgresql if we manually abrupt the connection from the server side (terminating connection due to administrator command) while cursor is open and I was able to replicate it with this code (Obviously you need to fill configure clients with your settings):
const{ Client }=require('pg')constPgCursor=require('pg-cursor')constclient=newClient({user: process.env.POSTGRES_USER,host: process.env.POSTGRES_HOST,database: process.env.POSTGRES_DATABASE,password: process.env.POSTGRES_PASSWORD,port: +(process.env.POSTGRES_PORT||5432),})client.on('error',function(err: Error){console.error('In error handler:',err)})constterminatingClient=newClient({user: process.env.POSTGRES_USER,host: process.env.POSTGRES_HOST,database: process.env.POSTGRES_DATABASE,password: process.env.POSTGRES_PASSWORD,port: +(process.env.POSTGRES_PORT||5432),})terminatingClient.on('error',function(err: Error){console.error('terminatingClient In error handler:',err)})functionterminatePostgresConnectionQuery(processID: number,db: string){return`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid = ${processID} AND datname = '${db}';`};(async()=>{try{awaitclient.connect()awaitterminatingClient.connect()constpgCursor=(client.query(newPgCursor(`SELECT NOW()`)))console.log('Process id:',client.processID)awaitterminatingClient.query(terminatePostgresConnectionQuery(client.processID,process.env.POSTGRES_DATABASE??'db'))console.log('Made terminatingQuery')awaitpgCursor.close()console.log('cursor closed')}catch(err){console.error('In catch: ',err)}finally{try{awaitclient.query('SELECT NOW()')}catch(err){console.error('Error in finally',err)}console.log('Exiting')awaitclient.end()awaitterminatingClient.end()}})()
Here we create two connections. One is used to create a cursor and make a select query. Another is used to kill the first one via pg_terminate_backend.
It never reaches console.log('cursor closed').
The text was updated successfully, but these errors were encountered:
I'm using pgcursor on Amazon Aurora PostgreSQL. If connection is terminated due to serverless scale event timeout, the promise returned by
close
methods of the Cursor never resolves. Probably because it never gets thereadyForQuery
event https://github.com/brianc/node-postgres/blob/master/packages/pg-cursor/index.js#L218I made an assumption that the same can happen on regular postgresql if we manually abrupt the connection from the server side (terminating connection due to administrator command) while cursor is open and I was able to replicate it with this code (Obviously you need to fill configure clients with your settings):
Here we create two connections. One is used to create a cursor and make a select query. Another is used to kill the first one via
pg_terminate_backend
.It never reaches
console.log('cursor closed')
.The text was updated successfully, but these errors were encountered: