Skip to content

Multiple queries in parametized query #1400

Closed

Description

@officer-rosmarino

If I run the following code

query = `
  INSERT INTO test_table (id, name, age) VALUES ($1, $2, $3);
  UPDATE test_table SET name='Alice' WHERE id=1`;
params = [2, 'Bob', 78];

 client.query(query, query_params)
  .then(() => {
    client.release();
    console.log('ok'); 
  }).catch(err => {
    client.release();
    console.log(err.message);
  });

I end up getting the following cannot insert multiple commands into a prepared statement.

Why is it like this? What are the reasons?
I think it would be really useful to be able to run multiple commands in the same prepared statement, in order to be able to run a single query with many ( N) commands, instead of N different queries with 1 single command.

I'd like to open up a discussion about this, if it has not been discussed before.

Activity

  1. abenhamdine commented on Aug 4, 2017

    @abenhamdine
    Contributor

    see #1388 and related threads for more explanation

  2. officer-rosmarino commented on Aug 4, 2017

    @officer-rosmarino
    Author

    @abenhamdine thanks!

  3. brianc commented on Aug 4, 2017

    @brianc
    Owner

    @honestserpent PostgreSQL itself doesn't support this operation. Use a flow control library, async/await, etc & send each query one after another. Or if you don't care about transactions you can use await Promise.all([pool.query('one'), pool.query('two')]) and do two queries at once. Generally though the best thing to do is just send them one at a time - that's how PostgreSQL was designed to work on a protocol level and it ends up being the easiest to reason about errors in your application this way as well I've found. 😄

    @abenhamdine I'll try to get to that doc as soon as I can! It definitely does seem like a question that comes up frequently.

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