Skip site navigation (1) Skip section navigation (2)

Re: [INTERFACES] threads and libpq

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: dave(at)aersoft(dot)ie
Cc: pgsql-general(at)postgreSQL(dot)org, pgsql-interfaces(at)postgreSQL(dot)org
Subject: Re: [INTERFACES] threads and libpq
Date: 1999-03-11 15:14:10
Message-ID: 20754.921165250@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-generalpgsql-interfaces
"David O'Farrell" <dave(dot)ofarrell(at)aersoft(dot)ie> writes:
> "The libpq library allows a single frontend to make multiple
> connections to backend processes.  However, the frontend application
> is still a single-threaded process."

> could some clarify to me if this means that a frontend CANNOT open
> several connections from different threads with the current
> implementation of libpq.

It just means that libpq doesn't somehow magically convert a
single-threaded application into a multi-threaded one just becase you
opened more than one connection.

PQconnectdb() is not currently multithreadable, because it relies on a
global variable to hold connection options.  (Which is part of the API
for the routine, so I can't easily fix it without risking breaking
applications :-(.)  But as far as I know, all the rest of libpq is
multithreadable, by which I mean that different threads can be working
on different connection objects concurrently.

libpq does not know anything about threads, so it does not contain any
interlocking that would make it safe for more than one thread to touch a
given connection object or result object.  If you need that, you'll have
to provide a lock associated with each object at the application level.
(Realistically, you'd have to do that anyway.  Even if libpq contained
code to serialize access to a connection object, it could only interlock
on a function-call-by-function-call basis.  But almost certainly, each
thread will want to lock down the state of the connection object for a
series of operations.  For instance, you'd not want another thread to
issue a new PQexec till you'd got done examining the result of the
previous one.  libpq couldn't enforce that for you.)

If you need to make connections concurrently, just use PQsetdbLogin()
rather than PQconnectdb().

BTW this all assumes that you have a thread-safe libc; if malloc is
not thread-safe then all bets are off...

> Is there anyone working on multi threading for libpq ?

I don't really see a need for libpq to be thread-aware.  

			regards, tom lane

pgsql-interfaces by date

Next:From: Tom LaneDate: 1999-03-11 15:21:15
Subject: Re: [INTERFACES] DBD-Pg-0.73 make failed
Previous:From: Tom LaneDate: 1999-03-11 14:50:34
Subject: Re: [INTERFACES] libpq problem

pgsql-general by date

Next:From: Stefan WehnerDate: 1999-03-11 17:08:16
Subject: Re: [GENERAL] Bug with sequences in 6.4.2
Previous:From: Tim RöstermundtDate: 1999-03-11 15:04:43
Subject: Binary data in postgres

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group