Re: [GENERAL] Querying libpq compile time options - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [GENERAL] Querying libpq compile time options
Date
Msg-id 200605181918.k4IJICV25876@candle.pha.pa.us
Whole thread Raw
Responses Re: [GENERAL] Querying libpq compile time options  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-patches
spaminos-sql@yahoo.com wrote:
> From: Bruce Momjian <pgman@candle.pha.pa.us>
> > spaminos-sql@yahoo.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
strangelockups). 
> 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);

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [PORTS] solaris build problem with Sun compilers
Next
From: "Jim C. Nasby"
Date:
Subject: Re: [PATCH] Compression and on-disk sorting