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

Re: [GENERAL] Querying libpq compile time options

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: spaminos-sql(at)yahoo(dot)com, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [GENERAL] Querying libpq compile time options
Date: 2006-05-23 22:13:06
Message-ID: 200605232213.k4NMD6I24109@candle.pha.pa.us (view raw or flat)
Thread:
Lists: pgsql-generalpgsql-hackerspgsql-patches
Applied, with updated exports.txt value.

---------------------------------------------------------------------------

Bruce Momjian wrote:
> spaminos-sql(at)yahoo(dot)com wrote:
> > From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
> > > spaminos-sql(at)yahoo(dot)com wrote:
> > > > Hi all
> > > > 
> > > > I am writing an app that uses libpq, but because it is threaded I want to make sure that the dynamic
> > > >  library being used has been compiled with the  right option.
> > > > How do I do this?
> > > > 
> > > > Is there a call such as "bool PQisThreadSafe()" that I can call?
> > 
> > > Is this like detecting of libpq is SSL-enabled?  I see PQgetssl().  Do
> > > we need to add a libpq function to return true/false for threading? 
> > > Slony requires a threaded libpq, so it could do the test to prevent
> > > wrong configurations.  It would be nice to enabled threading by default,
> > > but it is like SSL in that not all operating systems support it.
> > 
> > Yes, this is exactly the issue I have: I want to enforce at runtime that the library being
> > used is correct.
> > btw, I noticed that for some reason the prebuild linux rpms for Fedora Core 3 are compiled
> > without pthread support (and that's how I found out that I had to check the library on startup as I was getting strange lockups).
> > I think the simplest is to add a new call just like the one you described for testing for ssl and tell people
> > to call this function if they need the library to be thread safe.
> 
> Having heard no demand for libpq checking beyond threading, I have
> implemented PQisthreadsafe().  I used PQisnonblocking() as an example.
> 
> One major argument for having a separate function, aside from lack of
> demand for more, is that we are probably nearing the day when a theaded
> libpq will be created by default on platforms that support it, and in
> that case, there might not be a configure flag to check.
> 
> -- 
>   Bruce Momjian   http://candle.pha.pa.us
>   EnterpriseDB    http://www.enterprisedb.com
> 
>   + If your life is a hard drive, Christ can be your backup. +

> Index: doc/src/sgml/libpq.sgml
> ===================================================================
> RCS file: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v
> retrieving revision 1.209
> diff -c -c -r1.209 libpq.sgml
> *** doc/src/sgml/libpq.sgml	17 May 2006 21:50:54 -0000	1.209
> --- doc/src/sgml/libpq.sgml	18 May 2006 18:15:30 -0000
> ***************
> *** 4115,4125 ****
>   system's documentation for information about how to build
>   thread-enabled applications, or look in 
>   <filename>src/Makefile.global</filename> for <literal>PTHREAD_CFLAGS</>
> ! and <literal>PTHREAD_LIBS</>.
>   </para>
>   
>   <para>
> ! One restriction is that no two threads attempt to manipulate the same
>   <structname>PGconn</> object at the same time. In particular, you cannot
>   issue concurrent commands from different threads through the same
>   connection object. (If you need to run concurrent commands, use
> --- 4115,4146 ----
>   system's documentation for information about how to build
>   thread-enabled applications, or look in 
>   <filename>src/Makefile.global</filename> for <literal>PTHREAD_CFLAGS</>
> ! and <literal>PTHREAD_LIBS</>.  This function allows the querying of
> ! <application>libpq</application>'s thread-safe status:
>   </para>
>   
> + <variablelist>
> + <varlistentry>
> + <term><function>PQisthreadsafe</function><indexterm><primary>PQisthreadsafe</></></term>
> + <listitem>
>   <para>
> !        Returns the thread safety status of the <application>libpq</application>
> !        library.
> ! <synopsis>
> ! int PQisthreadsafe();
> ! </synopsis>
> ! </para>
> ! 
> ! <para>
> !        Returns 1 if the <application>libpq</application> is thead-safe and
> !        0 if it is not.
> ! </para>
> ! </listitem>
> ! </varlistentry>
> ! </variablelist>
> ! 
> ! <para>
> ! One thread restriction is that no two threads attempt to manipulate the same
>   <structname>PGconn</> object at the same time. In particular, you cannot
>   issue concurrent commands from different threads through the same
>   connection object. (If you need to run concurrent commands, use
> Index: src/interfaces/libpq/exports.txt
> ===================================================================
> RCS file: /cvsroot/pgsql/src/interfaces/libpq/exports.txt,v
> retrieving revision 1.7
> diff -c -c -r1.7 exports.txt
> *** src/interfaces/libpq/exports.txt	26 Dec 2005 14:58:05 -0000	1.7
> --- src/interfaces/libpq/exports.txt	18 May 2006 18:15:51 -0000
> ***************
> *** 126,128 ****
> --- 126,129 ----
>   PQinitSSL                 124
>   PQregisterThreadLock      125
>   PQencryptPassword         126
> + PQisthreadsafe            127
> Index: src/interfaces/libpq/fe-exec.c
> ===================================================================
> RCS file: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v
> retrieving revision 1.182
> diff -c -c -r1.182 fe-exec.c
> *** src/interfaces/libpq/fe-exec.c	14 Mar 2006 22:48:23 -0000	1.182
> --- src/interfaces/libpq/fe-exec.c	18 May 2006 18:15:53 -0000
> ***************
> *** 2326,2331 ****
> --- 2326,2343 ----
>   	return pqIsnonblocking(conn);
>   }
>   
> + /* libpq is thread-safe? */
> + int
> + PQisthreadsafe(void)
> + {
> + #ifdef ENABLE_THREAD_SAFETY
> + 	return true;
> + #else
> + 	return false;
> + #endif
> + }
> + 
> + 
>   /* try to force data out, really only useful for non-blocking users */
>   int
>   PQflush(PGconn *conn)
> Index: src/interfaces/libpq/libpq-fe.h
> ===================================================================
> RCS file: /cvsroot/pgsql/src/interfaces/libpq/libpq-fe.h,v
> retrieving revision 1.127
> diff -c -c -r1.127 libpq-fe.h
> *** src/interfaces/libpq/libpq-fe.h	27 Apr 2006 00:53:58 -0000	1.127
> --- src/interfaces/libpq/libpq-fe.h	18 May 2006 18:15:54 -0000
> ***************
> *** 366,371 ****
> --- 366,372 ----
>   /* Set blocking/nonblocking connection to the backend */
>   extern int	PQsetnonblocking(PGconn *conn, int arg);
>   extern int	PQisnonblocking(const PGconn *conn);
> + extern int	PQisthreadsafe(void);
>   
>   /* Force the write buffer to be written (or at least try) */
>   extern int	PQflush(PGconn *conn);

> 
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>        choose an index scan if your joining column's datatypes do not
>        match

-- 
  Bruce Momjian   http://candle.pha.pa.us
  EnterpriseDB    http://www.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +

In response to

pgsql-hackers by date

Next:From: Josh BerkusDate: 2006-05-23 22:16:31
Subject: Re: SQL compliant interval implementation
Previous:From: Tom LaneDate: 2006-05-23 21:54:12
Subject: Re: SQL compliant interval implementation

pgsql-patches by date

Next:From: Adam Sj√łgrenDate: 2006-05-24 08:25:07
Subject: plperl - put schema-name in $_TD
Previous:From: Bruce MomjianDate: 2006-05-23 19:38:29
Subject: Re: plperl - make $_TD global

pgsql-general by date

Next:From: Bruce MomjianDate: 2006-05-23 22:15:03
Subject: Re: allow LIMIT in UPDATE and DELETE
Previous:From: Jim C. NasbyDate: 2006-05-23 22:12:00
Subject: Re: allow LIMIT in UPDATE and DELETE

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