Re: libpq on windows

Lists: pgsql-interfaces
From: Gustavo Lopes <contratempo(at)gmail(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: libpq on windows
Date: 2005-05-19 13:52:13
Message-ID: 2b5c484b05051906526f8bb577@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Hi
I'm having trouble using libpq.dll (implicit linking with C programs).
I'm using libpq.dll v8.0.3.5131 (timestamp Wed May 11 11:12:58 2005)
and PostgreSQL 8.0.3 on i686-pc-mingw32, compiled by GCC gcc.exe (GCC)
3.4.2 (mingw-special) v8.0.3.5131. I used both Borland C++ 5.5.1 and
Microsoft (R) 32-bit C/C++ Standard Compiler Version 12.00.8168.
The problem seems to occurr only when the server sends a hint or the
dll generates one. This can happen when the connection to the server
cannot be established because the server is not running, a nonexistant
postgres function exist is called, when one attempts to drop an index
upon which a constraint depends, etc. (the program doesn't go beyond
PQconnectdb, PQexec or PQexecparam). Since my debugging skills are
very poor I cannot give any accurate description on what is causing
the exception.
The problem can be very easily reproduced by creating a C program
which calls PQconnectdb with a host parameter that points to a machine
that is not running postgres.
Running the same program under linux (although I used older versions
of the interface and the server) does not cause any problems.

Gustavo Lopes


From: John DeSoi <desoi(at)pgedit(dot)com>
To: Gustavo Lopes <contratempo(at)gmail(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq on windows
Date: 2005-05-19 14:58:47
Message-ID: 192bc8571d77abfaa3f193ebfb3c10e5@pgedit.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces


On May 19, 2005, at 9:52 AM, Gustavo Lopes wrote:

> The problem can be very easily reproduced by creating a C program
> which calls PQconnectdb with a host parameter that points to a machine
> that is not running postgres.
> Running the same program under linux (although I used older versions
> of the interface and the server) does not cause any problems.

Try comparing what you are doing in this case to the psql source. It
uses libpq and I run tests regularly that try connecting to an invalid
server. I have not seen any problems.

John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL


From: Gustavo Lopes <contratempo(at)gmail(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq on windows
Date: 2005-05-19 23:52:47
Message-ID: 2b5c484b05051916527e953873@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

psql is too complex for me to analyze and I believe it's multithreaded
but anyway Ic ouldn't fathom anything substantially different when the
function PQsetdbLogin() is called (file startup.c).
It would probably be better to say what I've doing because it's
possible it's a mistake in some elementary aspect I haven't grasped.
Here is a sample program:

#include <libpq-fe.h>
#pragma comment(lib,"libpq.lib")
int main(void) {
PGconn *conn;
conn = PQconnectdb("host=localhost");
return 0;
}

The import library was created with "impdef -f -a libpq.lib libpq.dll"
(borland) and impdef/implib (vc++).
This program works only when postgresql is running.

Thanks in advance

Gustavo Lopes

On 19/05/05, John DeSoi <desoi(at)pgedit(dot)com> wrote:
>
> On May 19, 2005, at 9:52 AM, Gustavo Lopes wrote:
>
> > The problem can be very easily reproduced by creating a C program
> > which calls PQconnectdb with a host parameter that points to a machine
> > that is not running postgres.
> > Running the same program under linux (although I used older versions
> > of the interface and the server) does not cause any problems.
>
> Try comparing what you are doing in this case to the psql source. It
> uses libpq and I run tests regularly that try connecting to an invalid
> server. I have not seen any problems.
>
>
> John DeSoi, Ph.D.
> http://pgedit.com/
> Power Tools for PostgreSQL
>
>

Attachment Content-Type Size
libpq.rar application/octet-stream 7.0 KB

From: jtv(at)xs4all(dot)nl
To: "Gustavo Lopes" <contratempo(at)gmail(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq on windows
Date: 2005-05-20 06:32:21
Message-ID: 20704.202.47.227.25.1116570741.squirrel@202.47.227.25
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Gustavo Lopes <contratempo(at)gmail(dot)com> wrote:

> The problem seems to occurr only when the server sends a hint or the
> dll generates one. This can happen when the connection to the server
> cannot be established because the server is not running, a nonexistant
> postgres function exist is called, when one attempts to drop an index
> upon which a constraint depends, etc. (the program doesn't go beyond
> PQconnectdb, PQexec or PQexecparam). Since my debugging skills are
> very poor I cannot give any accurate description on what is causing
> the exception.
> The problem can be very easily reproduced by creating a C program
> which calls PQconnectdb with a host parameter that points to a machine
> that is not running postgres.
> Running the same program under linux (although I used older versions
> of the interface and the server) does not cause any problems.

It could be the notice processor that crashes. A "notice processor" is a
callback that you can register with libpq that handles error messages.
The default is to print them to the console, but I'm not sure you can
always do that in a Windows program. Or since you're apparently using
different compilers for libpq and the application, maybe the default
notice processor gets linked to a different standard library than it
expects and fails because of that.

Notice processors are documented here:

http://www.postgresql.org/docs/8.0/interactive/libpq-notice-processing.html

To find out if this is what's wrong, try creating an empty function (with
C-style calling convention, not a regular C++ function) and setting that
as the notice processor:

extern "C" { /* (this line only needed in C++) */
void emptynoticeprocessor(void *, const char *)
{
}
} /* (this line only needed in C++) */

Now in your code, just after you opened your connection (call it "c"):

PQsetNoticeProcessor(c,emptynoticeprocessor,NULL);

Of course that will mean that error messages are not displayed, so if this
solves your crashing problem then your next step is to implement something
here that displays the given message!

Jeroen


From: Gustavo Lopes <contratempo(at)gmail(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq on windows
Date: 2005-05-20 13:20:58
Message-ID: 2b5c484b05052006206c301de@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

No, I'd say the notice processor/receiver is not the problem since
I've been able to implement a notice receiver that would display the
received notices without any complications.
The problem seems to arise only when a message contains a HINT
attached (I don't even know whether messages of type NOTICE can
cointain a hint field, but anyway that doesn't seem relevant to this
issue).

Gustavio Lopes

On 20/05/05, jtv(at)xs4all(dot)nl <jtv(at)xs4all(dot)nl> wrote:
> Gustavo Lopes <contratempo(at)gmail(dot)com> wrote:
>
> > The problem seems to occurr only when the server sends a hint or the
> > dll generates one. This can happen when the connection to the server
> > cannot be established because the server is not running, a nonexistant
> > postgres function exist is called, when one attempts to drop an index
> > upon which a constraint depends, etc. (the program doesn't go beyond
> > PQconnectdb, PQexec or PQexecparam). Since my debugging skills are
> > very poor I cannot give any accurate description on what is causing
> > the exception.
> > The problem can be very easily reproduced by creating a C program
> > which calls PQconnectdb with a host parameter that points to a machine
> > that is not running postgres.
> > Running the same program under linux (although I used older versions
> > of the interface and the server) does not cause any problems.
>
> It could be the notice processor that crashes. A "notice processor" is a
> callback that you can register with libpq that handles error messages.
> The default is to print them to the console, but I'm not sure you can
> always do that in a Windows program. Or since you're apparently using
> different compilers for libpq and the application, maybe the default
> notice processor gets linked to a different standard library than it
> expects and fails because of that.
>
> Notice processors are documented here:
>
> http://www.postgresql.org/docs/8.0/interactive/libpq-notice-processing.html
>
> To find out if this is what's wrong, try creating an empty function (with
> C-style calling convention, not a regular C++ function) and setting that
> as the notice processor:
>
> extern "C" { /* (this line only needed in C++) */
> void emptynoticeprocessor(void *, const char *)
> {
> }
> } /* (this line only needed in C++) */
>
> Now in your code, just after you opened your connection (call it "c"):
>
> PQsetNoticeProcessor(c,emptynoticeprocessor,NULL);
>
> Of course that will mean that error messages are not displayed, so if this
> solves your crashing problem then your next step is to implement something
> here that displays the given message!
>
> Jeroen
>
>


From: Gustavo Lopes <contratempo(at)gmail(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq on windows
Date: 2005-05-20 16:54:32
Message-ID: 2b5c484b05052009545a310cd7@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Actually it seems the hints are not the problem. I see no pattern now.
For instance, if table "j" doesn't exist, "drop table j" is ok but not
"drop table".

Gustavo Lopes

On 20/05/05, Gustavo Lopes <contratempo(at)gmail(dot)com> wrote:
> No, I'd say the notice processor/receiver is not the problem since
> I've been able to implement a notice receiver that would display the
> received notices without any complications.
> The problem seems to arise only when a message contains a HINT
> attached (I don't even know whether messages of type NOTICE can
> cointain a hint field, but anyway that doesn't seem relevant to this
> issue).
>
> Gustavio Lopes
>
> On 20/05/05, jtv(at)xs4all(dot)nl <jtv(at)xs4all(dot)nl> wrote:
> > Gustavo Lopes <contratempo(at)gmail(dot)com> wrote:
> >
> > > The problem seems to occurr only when the server sends a hint or the
> > > dll generates one. This can happen when the connection to the server
> > > cannot be established because the server is not running, a nonexistant
> > > postgres function exist is called, when one attempts to drop an index
> > > upon which a constraint depends, etc. (the program doesn't go beyond
> > > PQconnectdb, PQexec or PQexecparam). Since my debugging skills are
> > > very poor I cannot give any accurate description on what is causing
> > > the exception.
> > > The problem can be very easily reproduced by creating a C program
> > > which calls PQconnectdb with a host parameter that points to a machine
> > > that is not running postgres.
> > > Running the same program under linux (although I used older versions
> > > of the interface and the server) does not cause any problems.
> >
> > It could be the notice processor that crashes. A "notice processor" is a
> > callback that you can register with libpq that handles error messages.
> > The default is to print them to the console, but I'm not sure you can
> > always do that in a Windows program. Or since you're apparently using
> > different compilers for libpq and the application, maybe the default
> > notice processor gets linked to a different standard library than it
> > expects and fails because of that.
> >
> > Notice processors are documented here:
> >
> > http://www.postgresql.org/docs/8.0/interactive/libpq-notice-processing.html
> >
> > To find out if this is what's wrong, try creating an empty function (with
> > C-style calling convention, not a regular C++ function) and setting that
> > as the notice processor:
> >
> > extern "C" { /* (this line only needed in C++) */
> > void emptynoticeprocessor(void *, const char *)
> > {
> > }
> > } /* (this line only needed in C++) */
> >
> > Now in your code, just after you opened your connection (call it "c"):
> >
> > PQsetNoticeProcessor(c,emptynoticeprocessor,NULL);
> >
> > Of course that will mean that error messages are not displayed, so if this
> > solves your crashing problem then your next step is to implement something
> > here that displays the given message!
> >
> > Jeroen
> >
> >
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Gustavo Lopes <contratempo(at)gmail(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq on windows
Date: 2005-05-20 18:51:25
Message-ID: 27068.1116615085@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Gustavo Lopes <contratempo(at)gmail(dot)com> writes:
> Actually it seems the hints are not the problem. I see no pattern now.
> For instance, if table "j" doesn't exist, "drop table j" is ok but not
> "drop table".

Well, the latter generates a syntax complaint:

regression=# drop table j;
ERROR: table "j" does not exist
regression=# drop table ;
ERROR: syntax error at or near ";" at character 12
LINE 1: drop table ;
^
regression=#

Maybe the pattern is "any multi-line error message causes a problem"?

regards, tom lane


From: Gustavo Lopes <contratempo(at)gmail(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq on windows
Date: 2005-05-20 23:43:56
Message-ID: 2b5c484b0505201643d628432@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Is there any easy (ie, no C postgres functions) way to generate
multi-line error messages so that I can explore that possibility?

Gustavo Lopes

On 20/05/05, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Gustavo Lopes <contratempo(at)gmail(dot)com> writes:
> > Actually it seems the hints are not the problem. I see no pattern now.
> > For instance, if table "j" doesn't exist, "drop table j" is ok but not
> > "drop table".
>
> Well, the latter generates a syntax complaint:
>
> regression=# drop table j;
> ERROR: table "j" does not exist
> regression=# drop table ;
> ERROR: syntax error at or near ";" at character 12
> LINE 1: drop table ;
> ^
> regression=#
>
> Maybe the pattern is "any multi-line error message causes a problem"?
>
> regards, tom lane
>


From: Volkan YAZICI <volkan(dot)yazici(at)gmail(dot)com>
To: Gustavo Lopes <contratempo(at)gmail(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq on windows
Date: 2005-05-22 07:22:24
Message-ID: 7104a737050522002267997cef@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Hi,

On 5/21/05, Gustavo Lopes <contratempo(at)gmail(dot)com> wrote:
> Is there any easy (ie, no C postgres functions) way to generate
> multi-line error messages so that I can explore that possibility?

One way of generating mult-line error messages could be increasing
verbosity level:

=> \set verbosity verbose

=> CREATE TABLE del_me_1 (id1 integer PRIMARY KEY);
=> CREATE TABLE del_me_2 (id2 integer REFERENCES del_me_1 (id1) );

=> DROP TABLE del_me_1;
NOTICE: constraint del_me_2_id2_fkey on table del_me_2 depends on
table del_me_1
ERROR: cannot drop table del_me_1 because other objects depend on it
HINT: Use DROP ... CASCADE to drop the dependent objects too.

HTH.
Regards.