Commit b438e7e7 authored by Alvaro Herrera's avatar Alvaro Herrera

Restructure libpq code to remove some duplicity

There was some duplicate code to run SHOW transaction_read_only to
determine whether the server is read-write or read-only.  Reduce it by
adding another state to the state machine.

Author: Hari Babu Kommi
Reviewed-by: Takayuki Tsunakawa, Álvaro Herrera
Discussion: https://postgr.es/m/CAJrrPGe_qgdbbN+yBgEVpd+YLHXXjTruzk6RmTMhqrFig+32ag@mail.gmail.com
parent 55d015bd
...@@ -3434,6 +3434,13 @@ keep_going: /* We will come back to here until there is ...@@ -3434,6 +3434,13 @@ keep_going: /* We will come back to here until there is
return PGRES_POLLING_WRITING; return PGRES_POLLING_WRITING;
} }
/* Almost there now ... */
conn->status = CONNECTION_CHECK_TARGET;
goto keep_going;
}
case CONNECTION_CHECK_TARGET:
{
/* /*
* If a read-write connection is required, see if we have one. * If a read-write connection is required, see if we have one.
* *
...@@ -3476,7 +3483,7 @@ keep_going: /* We will come back to here until there is ...@@ -3476,7 +3483,7 @@ keep_going: /* We will come back to here until there is
} }
case CONNECTION_SETENV: case CONNECTION_SETENV:
{
/* /*
* Do post-connection housekeeping (only needed in protocol 2.0). * Do post-connection housekeeping (only needed in protocol 2.0).
* *
...@@ -3502,41 +3509,11 @@ keep_going: /* We will come back to here until there is ...@@ -3502,41 +3509,11 @@ keep_going: /* We will come back to here until there is
goto error_return; goto error_return;
} }
/* /* Almost there now ... */
* If a read-write connection is required, see if we have one. conn->status = CONNECTION_CHECK_TARGET;
* (This should match the stanza in the CONNECTION_AUTH_OK case goto keep_going;
* above.)
*
* Servers before 7.4 lack the transaction_read_only GUC, but by
* the same token they don't have any read-only mode, so we may
* just skip the test in that case.
*/
if (conn->sversion >= 70400 &&
conn->target_session_attrs != NULL &&
strcmp(conn->target_session_attrs, "read-write") == 0)
{
if (!saveErrorMessage(conn, &savedMessage))
goto error_return;
conn->status = CONNECTION_OK;
if (!PQsendQuery(conn,
"SHOW transaction_read_only"))
{
restoreErrorMessage(conn, &savedMessage);
goto error_return;
}
conn->status = CONNECTION_CHECK_WRITABLE;
restoreErrorMessage(conn, &savedMessage);
return PGRES_POLLING_READING;
} }
/* We can release the address list now. */
release_conn_addrinfo(conn);
/* We are open for business! */
conn->status = CONNECTION_OK;
return PGRES_POLLING_OK;
case CONNECTION_CONSUME: case CONNECTION_CONSUME:
{ {
conn->status = CONNECTION_OK; conn->status = CONNECTION_OK;
......
...@@ -67,7 +67,8 @@ typedef enum ...@@ -67,7 +67,8 @@ typedef enum
* connection. */ * connection. */
CONNECTION_CONSUME, /* Wait for any pending message and consume CONNECTION_CONSUME, /* Wait for any pending message and consume
* them. */ * them. */
CONNECTION_GSS_STARTUP /* Negotiating GSSAPI. */ CONNECTION_GSS_STARTUP, /* Negotiating GSSAPI. */
CONNECTION_CHECK_TARGET /* Check if we have a proper target connection */
} ConnStatusType; } ConnStatusType;
typedef enum typedef enum
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment