Re: active queries - Mailing list pgsql-general

From Gregory Stark
Subject Re: active queries
Date
Msg-id 87prr6p8yx.fsf@oxford.xeocode.com
Whole thread Raw
In response to Re: active queries  (Alban Hertroys <dalroi@solfertje.student.utwente.nl>)
List pgsql-general
"Alban Hertroys" <dalroi@solfertje.student.utwente.nl> writes:

> THE 'active' query (not AN) is the query that's currently being  executed on a
> connection. There can be multiple connections with an  active query each. You
> can't execute multiple queries in parallel on  the same connection. You can
> execute them in sequence of course.
>
> What people are trying to tell you is that you apparently have  multiple
> threads that perform queries simultaneously on the same  connection, and that's
> not possible.

FWIW it's not just queries that are a problem. You pretty much can't call
*any* libpq function on a connection while another thread is calling any other
libpq call on the same connection.

It is however safe (assuming you built postgres with the thread-safe option)
to call two libpq calls from different threads on *different* connections. And
it's always safe to call two libpq calls from different threads on the same
connection as long as you ensure that one is finished before the second one
begins. In either case the normal way to guarantee either property is to put a
mutex around either each connection or all of libpq.

You can actually have more than one query simultaneously active in the same
connection using named portals. But if you called PQgetResult() on one you
have to ensure all threads wait until it returns before issuing PQgetResult()
(or any other libpq function) on the other portal.

--
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com
  Get trained by Bruce Momjian - ask me about EnterpriseDB's PostgreSQL training!

pgsql-general by date:

Previous
From: Gregory Stark
Date:
Subject: Re: Open Source CRM - Options?
Next
From: Richard Huxton
Date:
Subject: Revealing the "postgres" user service-account in Windows XP Home