Querying libpq compile time options

Started by Nonameover 19 years ago26 messages
#1Noname
spaminos-sql@yahoo.com

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?

Thanks

Nicolas

#2Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Noname (#1)
Re: [GENERAL] Querying libpq compile time options

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?

[ Email moved to hackers list.]

Good question. This has come up a few times. You can check if the
installation has threading with "pg_config --configure", and I though
there was a way to test at runtime, but looking I don't see anything.

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.

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

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

#3Noname
spaminos-sql@yahoo.com
In reply to: Bruce Momjian (#2)
Re: [GENERAL] Querying libpq compile time options

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 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.

Nicolas

#4Martijn van Oosterhout
kleptog@svana.org
In reply to: Bruce Momjian (#2)
Re: [GENERAL] Querying libpq compile time options

On Fri, May 12, 2006 at 08:38:07PM -0400, Bruce Momjian wrote:

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.

PQgetssl() doesn't tell you if SSL is supported, it tells you if the
*current connection* is using OpenSSL, which is similar but not the
same.

There is AFAIK no way to tell if SSL support is compiled in. Part of
the patch I posted for GnuTLS support answered this question also
(PQgettlsinfo()).

Have a ncie day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

From each according to his ability. To each according to his ability to litigate.

#5Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Martijn van Oosterhout (#4)
1 attachment(s)
Re: [GENERAL] Querying libpq compile time options

Martijn van Oosterhout wrote:
-- Start of PGP signed section.

On Fri, May 12, 2006 at 08:38:07PM -0400, Bruce Momjian wrote:

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.

PQgetssl() doesn't tell you if SSL is supported, it tells you if the
*current connection* is using OpenSSL, which is similar but not the
same.

There is AFAIK no way to tell if SSL support is compiled in. Part of
the patch I posted for GnuTLS support answered this question also
(PQgettlsinfo()).

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to configure. One
idea would be to add this sample to our libpq documentation. The
problem with the example is popen() overhead, pg_config not in $PATH, or
pg_config's version not matching libpq's version.

A more comprehensive idea would be to embed the configure string in
libpq, like we do for pg_config, and allow that to be returned to the
caller so they can do a strstr() to see if a certain flag was used.

Having per-configure flag functions, like for threading, seems like it
could be a mess because there is a lot more application programmers might
care about in addition to threading, like SSL, multi-byte, etc.

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

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

Attachments:

/u/src/tst/tst1/tst1.ctext/plainDownload
#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#5)
Re: [GENERAL] Querying libpq compile time options

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to configure. One
idea would be to add this sample to our libpq documentation. The
problem with the example is popen() overhead, pg_config not in $PATH, or
pg_config's version not matching libpq's version.

Yeah, the last point seems like a killer objection :-(. It'd be better
to add some sort of libpq function to handle the issue.

regards, tom lane

#7Larry Rosenman
lrosenman@pervasive.com
In reply to: Tom Lane (#6)
Re: [GENERAL] Querying libpq compile time options

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to configure.
One idea would be to add this sample to our libpq documentation. The
problem with the example is popen() overhead, pg_config not in
$PATH, or pg_config's version not matching libpq's version.

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

--
Larry Rosenman
Database Support Engineer

PERVASIVE SOFTWARE. INC.
12365B RIATA TRACE PKWY
3015
AUSTIN TX 78727-6531

Tel: 512.231.6173
Fax: 512.231.6597
Email: Larry.Rosenman@pervasive.com
Web: www.pervasive.com

#8Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Larry Rosenman (#7)
Re: [GENERAL] Querying libpq compile time options

Larry Rosenman wrote:

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to configure.
One idea would be to add this sample to our libpq documentation. The
problem with the example is popen() overhead, pg_config not in
$PATH, or pg_config's version not matching libpq's version.

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

No idea, what is the URL of your proposal. Keep in mind this is not
option-specific.

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

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

#9Larry Rosenman
lrosenman@pervasive.com
In reply to: Bruce Momjian (#8)
Re: [GENERAL] Querying libpq compile time options

Bruce Momjian wrote:

Larry Rosenman wrote:

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to configure.
One idea would be to add this sample to our libpq documentation.
The problem with the example is popen() overhead, pg_config not in
$PATH, or pg_config's version not matching libpq's version.

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

No idea, what is the URL of your proposal. Keep in mind this is not
option-specific.

I had made a proposal to expose the path used for pg_service.conf.

this was back a month or so ago.

Would it be better to make a structure that has ALL the options, and
return
that from ONE function?

If so, I can code it up. I have time available.

--
Larry Rosenman
Database Support Engineer

PERVASIVE SOFTWARE. INC.
12365B RIATA TRACE PKWY
3015
AUSTIN TX 78727-6531

Tel: 512.231.6173
Fax: 512.231.6597
Email: Larry.Rosenman@pervasive.com
Web: www.pervasive.com

#10Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#8)
Re: [GENERAL] Querying libpq compile time options

Bruce Momjian <pgman@candle.pha.pa.us> writes:

Tom Lane wrote:

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

No idea, what is the URL of your proposal. Keep in mind this is not
option-specific.

Hm, I was thinking of something like "bool PQisThreadSafe()". It sounds
like Bruce is thinking of something that'd return a string literal
containing configure flags and then apps would have to try to inspect
that to determine what they want to know. That seems fairly messy to
me; for one thing because it would imply wiring assumptions about
default configure flags into apps, and that's something that could
change over time.

regards, tom lane

#11Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Larry Rosenman (#9)
Re: [GENERAL] Querying libpq compile time options

Larry Rosenman wrote:

Bruce Momjian wrote:

Larry Rosenman wrote:

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to configure.
One idea would be to add this sample to our libpq documentation.
The problem with the example is popen() overhead, pg_config not in
$PATH, or pg_config's version not matching libpq's version.

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

No idea, what is the URL of your proposal. Keep in mind this is not
option-specific.

I had made a proposal to expose the path used for pg_service.conf.

Why would an application programmer care to know the location of
pg_service.conf?

Would it be better to make a structure that has ALL the options, and
return
that from ONE function?

I can't think of an easy way to do that.

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

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

#12Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Tom Lane (#10)
Re: [GENERAL] Querying libpq compile time options

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

Tom Lane wrote:

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

No idea, what is the URL of your proposal. Keep in mind this is not
option-specific.

Hm, I was thinking of something like "bool PQisThreadSafe()". It sounds
like Bruce is thinking of something that'd return a string literal
containing configure flags and then apps would have to try to inspect
that to determine what they want to know. That seems fairly messy to
me; for one thing because it would imply wiring assumptions about
default configure flags into apps, and that's something that could
change over time.

True, but if you go per-option, I can see adding a lot of them. That
seemed more messy.

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

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

#13Tom Lane
tgl@sss.pgh.pa.us
In reply to: Larry Rosenman (#9)
Re: [GENERAL] Querying libpq compile time options

"Larry Rosenman" <lrosenman@pervasive.com> writes:

I had made a proposal to expose the path used for pg_service.conf.

I don't remember that anymore, but my question about it would be "what's
the use case?" I don't see a particularly good reason why an app would
need to know that, whereas it's pretty clear why a thread-dependent app
might wish to defend itself against a thread-unsafe libpq.

regards, tom lane

#14Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#12)
Re: [GENERAL] Querying libpq compile time options

Bruce Momjian <pgman@candle.pha.pa.us> writes:

True, but if you go per-option, I can see adding a lot of them. That
seemed more messy.

If there actually were a lot of options being proposed for addition,
maybe, but I only see one on the table.

regards, tom lane

#15Larry Rosenman
lrosenman@pervasive.com
In reply to: Tom Lane (#14)
Re: [GENERAL] Querying libpq compile time options

Bruce Momjian wrote:

Larry Rosenman wrote:

Bruce Momjian wrote:

Larry Rosenman wrote:

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to
configure. One idea would be to add this sample to our libpq
documentation. The problem with the example is popen() overhead,
pg_config not in $PATH, or pg_config's version not matching
libpq's version.

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

No idea, what is the URL of your proposal. Keep in mind this is not
option-specific.

I had made a proposal to expose the path used for pg_service.conf.

Why would an application programmer care to know the location of
pg_service.conf?

The admin needs to know it to use it. Currently there is no
way to get what is compiled into a specific libpq.

Would it be better to make a structure that has ALL the options, and
return that from ONE function?

I can't think of an easy way to do that.

I guess I'll just crawl under my rock again.

--
Larry Rosenman
Database Support Engineer

PERVASIVE SOFTWARE. INC.
12365B RIATA TRACE PKWY
3015
AUSTIN TX 78727-6531

Tel: 512.231.6173
Fax: 512.231.6597
Email: Larry.Rosenman@pervasive.com
Web: www.pervasive.com

#16Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Tom Lane (#14)
Re: [GENERAL] Querying libpq compile time options

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

True, but if you go per-option, I can see adding a lot of them. That
seemed more messy.

If there actually were a lot of options being proposed for addition,
maybe, but I only see one on the table.

Well, SSL is one, multibyte is another. I can see it expanding.
Locale? NLS? If we think it just threading, then that is easy, just
one function.

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

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

#17Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Larry Rosenman (#15)
Re: [GENERAL] Querying libpq compile time options

Larry Rosenman wrote:

Bruce Momjian wrote:

Larry Rosenman wrote:

Bruce Momjian wrote:

Larry Rosenman wrote:

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to
configure. One idea would be to add this sample to our libpq
documentation. The problem with the example is popen() overhead,
pg_config not in $PATH, or pg_config's version not matching
libpq's version.

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

No idea, what is the URL of your proposal. Keep in mind this is not
option-specific.

I had made a proposal to expose the path used for pg_service.conf.

Why would an application programmer care to know the location of
pg_service.conf?

The admin needs to know it to use it. Currently there is no
way to get what is compiled into a specific libpq.

Uh, it is an _admin_ function, not an application programmer function.

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

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

#18Larry Rosenman
lrosenman@pervasive.com
In reply to: Bruce Momjian (#17)
Re: [GENERAL] Querying libpq compile time options

Bruce Momjian wrote:

Larry Rosenman wrote:

Bruce Momjian wrote:

Larry Rosenman wrote:

Bruce Momjian wrote:

Larry Rosenman wrote:

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I thought about this. Attached is a patch you can use to
popen("pg_config") and then look for the thread flag to
configure. One idea would be to add this sample to our libpq
documentation. The problem with the example is popen()
overhead, pg_config not in $PATH, or pg_config's version not
matching libpq's version.

Yeah, the last point seems like a killer objection :-(. It'd be
better to add some sort of libpq function to handle the issue.

and when I've proposed libpq functions to expose compile-time
constants, I've been shot down.

How is this different?

No idea, what is the URL of your proposal. Keep in mind this is
not option-specific.

I had made a proposal to expose the path used for pg_service.conf.

Why would an application programmer care to know the location of
pg_service.conf?

The admin needs to know it to use it. Currently there is no
way to get what is compiled into a specific libpq.

Uh, it is an _admin_ function, not an application programmer function.

but libpq is the only thing that knows where it is, and I had proposed a
way
for psql to use the function to get it.

However, I'm going to forget about it, as obviously I won't get approval
for it.

--
Larry Rosenman
Database Support Engineer

PERVASIVE SOFTWARE. INC.
12365B RIATA TRACE PKWY
3015
AUSTIN TX 78727-6531

Tel: 512.231.6173
Fax: 512.231.6597
Email: Larry.Rosenman@pervasive.com
Web: www.pervasive.com

#19Tom Lane
tgl@sss.pgh.pa.us
In reply to: Larry Rosenman (#18)
Re: [GENERAL] Querying libpq compile time options

"Larry Rosenman" <lrosenman@pervasive.com> writes:

Uh, it is an _admin_ function, not an application programmer
function.

but libpq is the only thing that knows where it is, and I had proposed a
way for psql to use the function to get it.

It'd make more sense for pg_config to expose this as one of the
available information bits. The difference from the thread-support
case is that you'd typically want to get the pg_service.conf location
manually, and that's exactly what pg_config is designed for. Verifying
thread support, on the other hand, is something that a program would
want to do.

regards, tom lane

#20Larry Rosenman
lrosenman@pervasive.com
In reply to: Tom Lane (#19)
Re: [GENERAL] Querying libpq compile time options

Tom Lane wrote:

"Larry Rosenman" <lrosenman@pervasive.com> writes:

Uh, it is an _admin_ function, not an application programmer
function.

but libpq is the only thing that knows where it is, and I had
proposed a way for psql to use the function to get it.

It'd make more sense for pg_config to expose this as one of the
available information bits. The difference from the thread-support
case is that you'd typically want to get the pg_service.conf location
manually, and that's exactly what pg_config is designed for.
Verifying thread support, on the other hand, is something that a
program would want to do.

It still gets into the messiness of pg_config doesn't load libpq, and
there could be a mis-match.

--
Larry Rosenman
Database Support Engineer

PERVASIVE SOFTWARE. INC.
12365B RIATA TRACE PKWY
3015
AUSTIN TX 78727-6531

Tel: 512.231.6173
Fax: 512.231.6597
Email: Larry.Rosenman@pervasive.com
Web: www.pervasive.com

#21Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Tom Lane (#19)
Re: [GENERAL] Querying libpq compile time options

Tom Lane wrote:

"Larry Rosenman" <lrosenman@pervasive.com> writes:

Uh, it is an _admin_ function, not an application programmer
function.

but libpq is the only thing that knows where it is, and I had proposed a
way for psql to use the function to get it.

It'd make more sense for pg_config to expose this as one of the
available information bits. The difference from the thread-support

We already do expose it:

$ pg_config --sysconfdir
/usr/var/local/postgres/etc

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

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

#22Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Larry Rosenman (#20)
Re: [GENERAL] Querying libpq compile time options

Larry Rosenman wrote:

Tom Lane wrote:

"Larry Rosenman" <lrosenman@pervasive.com> writes:

Uh, it is an _admin_ function, not an application programmer
function.

but libpq is the only thing that knows where it is, and I had
proposed a way for psql to use the function to get it.

It'd make more sense for pg_config to expose this as one of the
available information bits. The difference from the thread-support
case is that you'd typically want to get the pg_service.conf location
manually, and that's exactly what pg_config is designed for.
Verifying thread support, on the other hand, is something that a
program would want to do.

It still gets into the messiness of pg_config doesn't load libpq, and
there could be a mis-match.

That is the administrator's job, to make sure they match. Applications
programmers can't do that.

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

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

#23Andrew Dunstan
andrew@dunslane.net
In reply to: Bruce Momjian (#21)
Re: [GENERAL] Querying libpq compile time options

Bruce Momjian wrote:

We already do expose it:

$ pg_config --sysconfdir
/usr/var/local/postgres/etc

Speaking of this item, what do we want to do about the Windows situation
where if the directory doesn't exist it reports nothing at all? I am
inclined to send back the output from GetFullPathName() instead of
GetShortPathName(). This should be fixed - reporting an empty string is
fairly unsatisfactory.

cheers

andrew

#24Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Andrew Dunstan (#23)
Re: [GENERAL] Querying libpq compile time options

Andrew Dunstan wrote:

Bruce Momjian wrote:

We already do expose it:

$ pg_config --sysconfdir
/usr/var/local/postgres/etc

Speaking of this item, what do we want to do about the Windows situation
where if the directory doesn't exist it reports nothing at all? I am
inclined to send back the output from GetFullPathName() instead of
GetShortPathName(). This should be fixed - reporting an empty string is
fairly unsatisfactory.

I was researching that and will report back.

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

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

#25Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Noname (#3)
1 attachment(s)
Re: [GENERAL] Querying libpq compile time options

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 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. +

Attachments:

/pgpatches/threadtext/plainDownload
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);
#26Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Bruce Momjian (#25)
Re: [GENERAL] Querying libpq compile time options

Applied, with updated exports.txt value.

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

Bruce Momjian wrote:

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 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. +