/* * 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 #include #include #ifdef WIN32 #include #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); }