7

In Postgres is there a limitation of having just one executing query per connection (and other queries in the connection wait for the first to complete before they start)? I think I am seeing this in one driver so I want to be sure this is a db behavior and not a specific driver limitation.

5
  • Are you looking for somthing like locking ? Commented Jan 29, 2014 at 18:58
  • No, I just want to understand how things work... and what I gain and lose by using a single and not multiple connections Commented Jan 29, 2014 at 19:00
  • 5
    One query per connection. You don't have to block on the client while waiting for it though: postgresql.org/docs/current/static/libpq-async.html Commented Jan 29, 2014 at 19:41
  • thanks @RichardHuxton, you can submit an answer so I will accept Commented Jan 29, 2014 at 20:17
  • In future, please be more specific. PostgreSQL version, driver and driver version, client language, etc. Commented Jan 30, 2014 at 0:53

1 Answer 1

11

In Postgres is there a limitation of having just one executing query per connection

Yes. PostgreSQL doesn't let you suspend and resume transactions, nor does it support background (asynchronous) queries on the server back-end.

You can still run multiple concurrent queries, you just need one connection per concurrent query. You can use threads (one thread per connection) but it's usually better to use asynchronous query interfaces in your client library.

Without knowing what you're trying to achieve, and what what programming language (and thus what client library) you're using it's hard to offer more detailed advice.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for posting on an old question - I see you reference concurrency here, but I wonder whether one can achieve a speedup in this way or if there are some locks that would prevent that? What about when using CREATE TABLE ... AS ...?
@Zeruno Same applies. PostgreSQL does now have parallelisation of read-only queries, but you can't have multiple different statements running concurrently in one session. You need multiple sessions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.