Hello,

This this is my first posting to the mailing list.

I am interested in multiple hosts of libpq [1], then I found the bug in this 
feature. 
When I set "target_session_attrs" to "any" and call PQsendQuery, my application 
is succeeded.
However, when I set "target_session_attrs" to "read-write" and call 
PQsendQuery, "another command is already in progress" is occurred. 
I attached the test application to reproduce this problem. 

I think this is because PQgetResult is not called until PQgetResult has 
returned a null pointer. 
So, I attached the patch for fix this. 

[1] 
https://www.postgresql.org/message-id/flat/20150818041850.ga5...@wagner.pp.ru#20150818041850.ga5...@wagner.pp.ru

Regards, 
Daisuke Higuchi

/*
 * gcc multiple_hosts_test.c -lpq -I/home/postgres/work/pgsql10/include 
-L/home/postgres/work/pgsql10/lib
 * if target_session_attrs=read-write,  "another command is already in 
progress" is occured
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <winsock2.h>
#endif
#include "libpq-fe.h"

int main( int argc, char *argv[])
{
        int i=0,rtn;
        PGconn *conn;
        PGresult *r;
        const char *keywords[5];
        const char *values[5];

        /*
        ** Connect to postgres
        */
        keywords[0] = "dbname";
        values[0] = "postgres";
        keywords[1] = "port";
        values[1] = (char *)getenv("PGPORT"); /* export PGPORT=5432,5433 */
        keywords[1] = "host";
        values[1] = (char *)getenv("PGHOST"); /* export 
PGHOST=localhost,localhost */
        keywords[2] = "target_session_attrs";
        values[2] = "read-write";
        keywords[3] = NULL;
        values[3] = NULL;
        conn = PQconnectdbParams((const char * const*)&keywords, (const char * 
const*)&values, 1);
        if (PQstatus(conn) != CONNECTION_OK)
        {
                printf("Connection to database failed: %s 
\n",PQerrorMessage(conn));
                goto ERR;
        }
        printf("host=%s,port=%s\n",PQhost(conn),PQport(conn));

        /*
        ** Asynchronous statement
        */
        printf("-- SHOW transaction_read_only --\n");
        rtn = PQsendQuery(conn, "SHOW transaction_read_only");
        if (rtn != 1)
        {
                printf( "SHOW command failed: %s", PQerrorMessage(conn));
                goto ERR;
        }
        r = PQgetResult(conn);
        printf("SHOW transaction_read_only:%s\n",PQgetvalue(r, 0, 0));
        r = PQgetResult(conn);
        PQclear(r);

        /*
        ** Disconnect
        */
        PQfinish(conn);
        printf("Success!\n");

        return(0);

ERR:
        PQfinish(conn);
        printf("Failed!\n");

        return(1);
}


Attachment: PQsendQuery_for_target_session_attrs_v1.patch
Description: PQsendQuery_for_target_session_attrs_v1.patch

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to