Thread

  1. Checking PQstatus (libpq) returns CONNECTION_OK when it is not.

    PostgreSQL Bugs List <pgsql-bugs@postgresql.org> — 2001-03-14T05:16:23Z

    Philip (philip.howell@cit.act.edu.au) reports a bug with a severity of 2
    The lower the number the more severe it is.
    
    Short Description
    Checking PQstatus (libpq) returns CONNECTION_OK when it is not.
    
    Long Description
    Hi
    I'm having problems with the PQstatus function in the libpq library.  It returns CONNECTION_OK when I know that it is not.  I used the test program attached bellow and the following method to test it:
    
    1. I compiled the program:
    
        gcc -Wall -o test test.c -lpq
    
    2. With the postgreSQL server (161.50.16.173) up and running I executed the test program:
    
        ./test
    
    3. I hit <return> a few times to get the following output
    
    >Connection status... OK
    >
    >Connection status... OK
    >
    >Connection status... OK
    >
    >Connection status... OK
    
    4. I then sutdown the network service on the postgreSQL server, to break the connection
    
    5. However when I hit <return> I still get
    
    >Connection status... OK
    
    When I would expect:
    
    >Connection status... BAD
    
    The call to PQstatus(conn) on line 16 of the attached code never returns CONNECTION_BAD.
    
    SYSTEM INFORMATION:
    ===================
    
    postgreSQL Server:
    Hardware: i386
    OS Linux RedHat 6.2
    Postgres was installed from RPMs version 7.0.3-2
    
    Client Machine
    Hardware: i386
    OS: Linux RedHat 6.2
    RPMs installed:
         postgresql-devel-7.0.3-2
         postgresql-7.0.3-2
    
    Regards
    Phil
    
    
    Sample Code
    #include <stdlib.h>
    #include <stdio.h>
    #include "pgsql/libpq-fe.h"
    
    PGconn *make_conn();
    
    int main (int argc, char *argv[])
    {
        PGconn      *conn;
    
        conn = make_conn();
    
        while (fgetc(stdin) != 'q')
        {
            printf("Connection status... ");
            if (PQstatus(conn) != CONNECTION_OK) {
                printf("BAD\n");
                PQreset(conn);
            } else {
                printf("OK\n");
            }
    
        }
        return(0);
    }
    
    PGconn *make_conn()
    {
        PGconn      *conn;
    
        conn = PQconnectdb("hostaddr = 161.50.16.173 dbname = squid user = squid password = squid");
        if (PQstatus(conn) == CONNECTION_BAD)
        {
            fprintf(stderr,"Failed to connect to database\n");
            fprintf(stderr,"%s\n",PQerrorMessage(conn));
            PQfinish(conn);
            exit(1);
        }
        return(conn);
    }
    
    
    No file was uploaded with this report