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.

concurrent queries in a connection

See original GitHub issue

Hi

What happens when I submit two sequential queries:

client.query("SELECT ...", function() {} )
client.query("SELECT ...", function() {} )

I understand there is a queue and second query will execute only after first is complete. But what does it mean “complete” - will pg send the second query to the server only after all results from the first query came back? Is it the driver behavior or does postgres mandate that only one query should be sent to the server by the client at a given moment?

Thanks, Yaron

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
yaronncommented, Jan 29, 2014

thanks @brianc ! So to finally sum it up see those 3 snippets:

1:

pg.connect(conString, function(err, client, done) {
  client.query("SELECT ...", function(err, result) {})
});
pg.connect(conString, function(err, client, done) {
  client.query("SELECT ...", function(err, result) {})
});

2:

pg.connect(conString, function(err, client, done) {
  client.query("SELECT ...", function(err, result) {})
  client.query("SELECT ...", function(err, result) {})
});

3:

pg.connect(conString, function(err, client, done) {
  client.query("SELECT ...", function(err, result) {
    client.query("SELECT ...", function(err, result) {})
  })
});

snippet 1 is faster than the others (assuming there is an available connection) since the second query will not wait for the first to finish. snippets 2, 3 are the same since the second query will not start until first finishes, which is implicit in 2 and explicit in 3.

right again?

1reaction
brianccommented, Jan 29, 2014

node-postgres maintains an internal queue of queries and sends them serially to the PostgreSQL server backend one at a time because the protocol & server mandate a maximum of 1 in-flight query per connected client. If you don’t care about order execution you can use something like pg-query and async just be aware transactions don’t work at all across multiple clients.

Read more comments on GitHub >

github_iconTop Results From Across the Web

concurrent queries with a single connection in pyodbc
There is a protocol to allow a single connection to have multiple concurrent queries running at the same time. It's called Multiple Active ......
Read more >
Concurrent queries on single connection #738 - GitHub
can't run multiple queries concurrently on a single PostgreSQL connection. It's inherent to the protocol. The protocol does not allow parallel ...
Read more >
Concurrent queries are slow or fail in Watson Query - IBM
There are many different reasons why concurrent queries might be slow or fail. For example, when the connection pool becomes full.
Read more >
Improve database performance with connection pooling
Caching frequently-accessed queries in memory or via a database can optimize write/read performance and reduce network latency, especially for ...
Read more >
Refreshing queries in parallel
For each connection, the number of data providers that can be refreshed in parallel is set to 4 by default. The database administrator...
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