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
2 changes: 1 addition & 1 deletion packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ class Client extends EventEmitter {
// if a prepared statement has a name and properly parses
// we track that its already been executed so we don't parse
// it again on the same client
if (activeQuery.name) {
if (activeQuery.name && !activeQuery._statementClosedDueToError) {
this.connection.parsedStatements[activeQuery.name] = activeQuery.text
delete this.connection.submittedNamedStatements[activeQuery.name]
}
Expand Down
12 changes: 12 additions & 0 deletions packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ class Query extends EventEmitter {
connection.close({ type: 'S', name: this.name })
connection.sync()

if (this.name) {
// The statement is being closed on the server, so the client must not
// keep it in its parsed-statement cache. Drop any existing entry (reuse
// of an already-parsed name) and flag the query so the ParseComplete for
// the Parse we just sent (first use of the name) is not recorded either.
// Otherwise every later use of this name fails with "prepared statement
// does not exist".
this._statementClosedDueToError = true
delete connection.parsedStatements[this.name]
delete connection.submittedNamedStatements[this.name]
}

this.handleError(err, connection)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,43 @@ suite.test('query killed during query execution of prepared statement', function
})
)
})

// The client-side prepared-statement cache this guards against only exists in
// the pure-JS driver; pg-native prepares statements through libpq, so this is
// a JS-only test.
if (!helper.args.native) {
suite.test('reusing a named statement after a parameter fails to serialize', function (done) {
// A value that throws while being serialized makes the client close the
// prepared statement on the server. The client must drop it from its
// statement cache too, otherwise every later use of the same name fails
// with "prepared statement does not exist". Uses the promise API so the
// first query settles exactly once regardless of issue #3820.
const client = new Client()
client
.connect()
.then(async () => {
const bad = {
toPostgres() {
throw new Error('serialization failed')
},
}

await assert.rejects(
() => client.query({ name: 'reused-after-error', text: 'select $1::int as v', values: [bad] }),
/serialization failed/
)

// the same name, now with a value that serializes fine
const res = await client.query({
name: 'reused-after-error',
text: 'select $1::int as v',
values: [42],
})
assert.equal(res.rows[0].v, 42)

await client.end()
done()
})
.catch(done)
})
}
Loading