Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,16 @@ class Client extends EventEmitter {
this._activeQuery = null
if (activeQuery.name) {
delete this.connection.submittedNamedStatements[activeQuery.name]
// pipelined queries already on the wire skipped their own Parse relying on this one.
// If it failed before parseComplete the backend will answer them with
// 'prepared statement does not exist', so hand them the original error instead.
if (!this.connection.parsedStatements[activeQuery.name]) {
for (const query of this._sentQueryQueue) {
if (query.name === activeQuery.name) {
query._pipelineParseError = msg
}
}
}
}
activeQuery.handleError(msg, this.connection)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ class Query extends EventEmitter {
}

handleError(err, connection) {
// the named statement this query relied on was never created (26000): report why
if (this._pipelineParseError && err && err.code === '26000') {
err = this._pipelineParseError
}
// need to sync after error during a prepared statement
if (this._canceledDueToError) {
err = this._canceledDueToError
Expand Down
23 changes: 23 additions & 0 deletions packages/pg/test/integration/client/pipelining-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ suite.test(
}
)

// Two pipelined queries share a statement name and the parse fails: the second skipped
// its own Parse, so the backend answers 'prepared statement does not exist'. The client
// must report the original parse error to it instead.
suite.test(
'named statement parse failure reports the real error to the queries behind it',
!helper.args.native &&
async function () {
const client = helper.client(undefined, { pipeline: true })

const results = await Promise.allSettled([
client.query({ name: 'shared-bad', text: 'SELECT no_such_column_xyz' }),
client.query({ name: 'shared-bad', text: 'SELECT no_such_column_xyz' }),
])

assert.equal(results[0].status, 'rejected')
assert.equal(results[1].status, 'rejected')
assert.ok(results[0].reason.message.includes('no_such_column_xyz'), results[0].reason.message)
assert.ok(results[1].reason.message.includes('no_such_column_xyz'), results[1].reason.message)

await client.end()
}
)

// #14: query_timeout with pipelining
// When an already-sent pipelined query times out, the connection is destroyed
// to unblock the pipeline — subsequent queries error rather than hanging.
Expand Down
Loading