Concurrent queries in sql.begin() after cached-plan invalidation (0A000) leave the connection 'idle in transaction (aborted)' and never release it
When a prepared statement's cached plan is invalidated (e.g. cached plan must not change result type, SQLSTATE 0A000, after ALTER TABLE ... ADD COLUMN on a select *) and that statement runs concurrently with another query inside sql.begin(), the transaction is left aborted on the server, ROLLBACK is never sent, and the connection is never returned to the pool. Every later query on that client hangs forever; the backend stays idle in transaction (aborted) until the server-side timeout kills it.
The same invalidation with a single statement, or with sequential awaits inside the transaction, surfaces a clean 0A000 error and the connection recovers — only the concurrent (pipelined) case leaks.
Reproduction
import postgres from 'postgres'
const url = process.env.DATABASE_URL
const admin = postgres(url, { max: 1, onnotice: () => {} })
const sql = postgres(url, { max: 1 }) // prepare: true (default)
const sleep = ms => new Promise(r => setTimeout(r, ms))
await admin`drop table if exists t`
await admin`create table t (id int primary key, a int)`
await admin`insert into t values (1, 1), (2, 2)`
// 1. cache two prepared statements on the single pooled connection
await sql`select * from t where id = ${1}`
await sql`select id from t where id = ${1} and a > ${0}`
// 2. change the result type of the first statement from another session
await admin`alter table t add column c int`
// 3. run both statements concurrently inside a transaction
try {
await sql.begin(tx => [
tx`select * from t where id = ${1}`,
tx`select id from t where id = ${1} and a > ${0}`
])
console.log('transaction: ok')
} catch (e) {
console.log('transaction error:', e.code, e.message)
}
// 4. the connection is never released: any further query on this client hangs
const next = await Promise.race([sql`select 1 as x`.then(() => 'ok'), sleep(5000).then(() => 'HANG (no result after 5s)')])
console.log('next query on same client:', next)
const [s] = await admin`
select state, query from pg_stat_activity
where datname = current_database() and pid <> pg_backend_pid() and backend_type = 'client backend'`
console.log('server backend state:', s?.state, '|', s?.query)
await admin.end()
process.exit(0)Actual output
transaction error: 25P02 current transaction is aborted, commands ignored until end of transaction block
next query on same client: HANG (no result after 5s)
server backend state: idle in transaction (aborted) | select * from t where id = $1Expected
sql.begin() rejects (ideally with the original 0A000 error rather than 25P02), ROLLBACK is sent, and the connection returns to the pool so the next query succeeds.
Environment
- postgres (postgres.js) 3.4.9, and also
masterat 411429e — same result - PostgreSQL 17.10 (plain server, no pooler)
- Node.js v22.23.1
Notes
It looks like the built-in retry for invalidated prepared statements (RevalidateCachedQuery / FetchPreparedStatement) also fires while the connection is inside a transaction block. After the first statement fails the transaction is already aborted, so the retried/pipelined statement gets 25P02 and the protocol state appears to get out of sync, so the ROLLBACK from sql.begin() never completes. Not retrying when ReadyForQuery reports an in-transaction status (and surfacing the original error instead) avoided the leak in local testing.
Possibly related (different trigger, same symptom): #1173.
Source: porsager/postgres