Skip to content

Commit 1de0a4e

Browse files
committed
libpq: Make target_session_attrs=read-write consume empty result.
Otherwise, the leftover empty result can cause problems in some situations. Michael Paquier and Ashutosh Bapat, per a report from Higuchi Daisuke
1 parent fbe7a3f commit 1de0a4e

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ PQconnectPoll(PGconn *conn)
18961896
case CONNECTION_SSL_STARTUP:
18971897
case CONNECTION_NEEDED:
18981898
case CONNECTION_CHECK_WRITABLE:
1899+
case CONNECTION_CONSUME:
18991900
break;
19001901

19011902
default:
@@ -2935,6 +2936,34 @@ PQconnectPoll(PGconn *conn)
29352936
conn->status = CONNECTION_OK;
29362937
return PGRES_POLLING_OK;
29372938

2939+
case CONNECTION_CONSUME:
2940+
{
2941+
conn->status = CONNECTION_OK;
2942+
if (!PQconsumeInput(conn))
2943+
goto error_return;
2944+
2945+
if (PQisBusy(conn))
2946+
{
2947+
conn->status = CONNECTION_CONSUME;
2948+
restoreErrorMessage(conn, &savedMessage);
2949+
return PGRES_POLLING_READING;
2950+
}
2951+
2952+
/*
2953+
* Call PQgetResult() again to consume NULL result.
2954+
*/
2955+
res = PQgetResult(conn);
2956+
if (res != NULL)
2957+
{
2958+
PQclear(res);
2959+
conn->status = CONNECTION_CONSUME;
2960+
goto keep_going;
2961+
}
2962+
2963+
/* We are open for business! */
2964+
conn->status = CONNECTION_OK;
2965+
return PGRES_POLLING_OK;
2966+
}
29382967
case CONNECTION_CHECK_WRITABLE:
29392968
{
29402969
if (!saveErrorMessage(conn, &savedMessage))
@@ -2994,9 +3023,12 @@ PQconnectPoll(PGconn *conn)
29943023
/* We can release the address lists now. */
29953024
release_all_addrinfo(conn);
29963025

2997-
/* We are open for business! */
2998-
conn->status = CONNECTION_OK;
2999-
return PGRES_POLLING_OK;
3026+
/*
3027+
* Finish reading any remaining messages before
3028+
* being considered as ready.
3029+
*/
3030+
conn->status = CONNECTION_CONSUME;
3031+
goto keep_going;
30003032
}
30013033

30023034
/*

src/interfaces/libpq/libpq-fe.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ typedef enum
6363
CONNECTION_SETENV, /* Negotiating environment. */
6464
CONNECTION_SSL_STARTUP, /* Negotiating SSL. */
6565
CONNECTION_NEEDED, /* Internal state: connect() needed */
66-
CONNECTION_CHECK_WRITABLE /* Check if we could make a writable
66+
CONNECTION_CHECK_WRITABLE, /* Check if we could make a writable
6767
* connection. */
68+
CONNECTION_CONSUME /* Wait for any pending message and
69+
* consume them. */
6870
} ConnStatusType;
6971

7072
typedef enum

0 commit comments

Comments
 (0)