Skip to content

Reuse of named prepared query within transaction causes error #36

Description

@pixelglow

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions