Closed
Description
If you have an outstanding transaction and you reuse a named, prepared query, this causes an error.
client.connect();
client.query("BEGIN"); \\ 1
for (var i = 0; i < 2; ++i)
client.query({name: "X", text: "SELECT $1::INTEGER", values:[0]}, function (err, result) { if (err) console.log(err); });
client.query("COMMIT"); \\ 2
client.on("drain", function() { console.log("finished"); client.end(); });
The above code errors with:
{ length: 78,
name: 'error',
severity: 'ERROR',
code: '42P03',
message: 'cursor "X" already exists',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
file: 'portalmem.c',
line: '207',
routine: 'CreatePortal' }
To fix the error, remove the lines comment as 1 and 2 i.e. remove the surrounding transaction, and all will be fine. Also you can change the loop to go through only once, or remove the query name, to fix the bug.
This is a fairly typical idiom, where you open a transaction so that you can pipeline several INSERT's into it. Having the INSERT statement prepared helps tremendously with this sort of situation.
Metadata
Metadata
Assignees
Labels
No labels