question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extended Query: Support Batch Execution

See original GitHub issue

Hi!

The Extended Query protocol enables drivers to submit multiple BIND messages before SYNC. One of the big benefits of using Extended Queries is that you can efficiently send a batch of executions without waiting for round trips for each execution. Pairing that with prepared statements and some simplifications: you send a single PARSE, a bunch of BIND/EXECUTE and a SYNC to find out how things went.

In other words, you’d be able to support something like the following without needing 4 entire round trips. (I’m not recommending this API since it would be a terrible breaking change.)

await client.query({
  name: 'my_query',
  text: 'insert sometable (id, val) values ($1, $2)'
  values: [
    [ 1, 'asdf' ],
    [ 2, 'fdsa' ],
    [ 3, 'qwer' ],
    [ 4, 'uytr' ]
  ]
})

For more information, check out how the JDBC Postgres driver handles a batched execution. There are a few layers to dig through, but this appears to be the core of the code that sends a batch of messages and subsequently sends a single SYNC. NOTE: their driver imposes a limit of 128 records per batch as (apparently) further batching does not improve performance.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
brianccommented, Sep 8, 2021

yeah a proper “batched query” would be nice. Probably a separate object you pass to client.query or pool.query that was like

const batch = new BatchQuery({
  name: 'optional',
  text: 'INSERT INTO foo (bar) VALUES ($1)',
  values: [
    ['first'],
    ['second']
  ]
})
const result = client.query(batch)
for (const res of result) {
  for (const row of res) {
  }
}

Then the batch query execution could throw if this is false for some validation:

for (const row of config.values) {
  if (!Array.isArray(config.values)) {
    throw new Error('Batch commands require each set of values to be an array. e.g. values: any[][]')
  }
}

something like that. Then it would be explicit.

0reactions
abenhamdinecommented, Nov 7, 2022

for pipelining, see the experiment in https://github.com/brianc/node-postgres/pull/2706

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Batch Processing: A Comprehensive Guide 101 - Learn
Batch Processing operations are supported by SQL Server using a variety of ways. SQL Server is a relational database management system from ...
Read more >
Executing Batches - ODBC API Reference - Microsoft Learn
Batches of statements are executed through SQLExecute or SQLExecDirect. For example, the following call executes an explicit batch of statements ...
Read more >
31.5. Batch mode and query pipelining
Batch mode and query pipelining. libpq supports queueing up mulitiple queries into a pipeline to be executed as a batch on the server....
Read more >
batch-get-query-execution — AWS CLI 2.7.12 Command ...
Currently the only supported canned ACL is BUCKET_OWNER_FULL_CONTROL . If a query runs in a workgroup and the workgroup overrides client-side settings, then...
Read more >
Executing SQL queries and SQL batches - LLBLGen Pro
The SQL Editor tab supports both single line comments starting with -- and also multi-line comments wrapped with \* *\ . Comments are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found