Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b438e7e

Browse files
committed
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
1 parent 55d015b commit b438e7e

File tree

2 files changed

+33
-55
lines changed

2 files changed

+33
-55
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ typedef struct _internalPQconninfoOption
170170
char *keyword; /* The keyword of the option */
171171
char *envvar; /* Fallback environment variable name */
172172
char *compiled; /* Fallback compiled in default value */
173-
char *val; /* Option's current value, or NULL */
173+
char *val; /* Option's current value, or NULL */
174174
char *label; /* Label for field in connect dialog */
175175
char *dispchar; /* Indicates how to display this field in a
176176
* connect dialog. Values are: "" Display
@@ -3434,6 +3434,13 @@ PQconnectPoll(PGconn *conn)
34343434
return PGRES_POLLING_WRITING;
34353435
}
34363436

3437+
/* Almost there now ... */
3438+
conn->status = CONNECTION_CHECK_TARGET;
3439+
goto keep_going;
3440+
}
3441+
3442+
case CONNECTION_CHECK_TARGET:
3443+
{
34373444
/*
34383445
* If a read-write connection is required, see if we have one.
34393446
*
@@ -3476,66 +3483,36 @@ PQconnectPoll(PGconn *conn)
34763483
}
34773484

34783485
case CONNECTION_SETENV:
3479-
3480-
/*
3481-
* Do post-connection housekeeping (only needed in protocol 2.0).
3482-
*
3483-
* We pretend that the connection is OK for the duration of these
3484-
* queries.
3485-
*/
3486-
conn->status = CONNECTION_OK;
3487-
3488-
switch (pqSetenvPoll(conn))
34893486
{
3490-
case PGRES_POLLING_OK: /* Success */
3491-
break;
3492-
3493-
case PGRES_POLLING_READING: /* Still going */
3494-
conn->status = CONNECTION_SETENV;
3495-
return PGRES_POLLING_READING;
3487+
/*
3488+
* Do post-connection housekeeping (only needed in protocol 2.0).
3489+
*
3490+
* We pretend that the connection is OK for the duration of these
3491+
* queries.
3492+
*/
3493+
conn->status = CONNECTION_OK;
34963494

3497-
case PGRES_POLLING_WRITING: /* Still going */
3498-
conn->status = CONNECTION_SETENV;
3499-
return PGRES_POLLING_WRITING;
3495+
switch (pqSetenvPoll(conn))
3496+
{
3497+
case PGRES_POLLING_OK: /* Success */
3498+
break;
35003499

3501-
default:
3502-
goto error_return;
3503-
}
3500+
case PGRES_POLLING_READING: /* Still going */
3501+
conn->status = CONNECTION_SETENV;
3502+
return PGRES_POLLING_READING;
35043503

3505-
/*
3506-
* If a read-write connection is required, see if we have one.
3507-
* (This should match the stanza in the CONNECTION_AUTH_OK case
3508-
* above.)
3509-
*
3510-
* Servers before 7.4 lack the transaction_read_only GUC, but by
3511-
* the same token they don't have any read-only mode, so we may
3512-
* just skip the test in that case.
3513-
*/
3514-
if (conn->sversion >= 70400 &&
3515-
conn->target_session_attrs != NULL &&
3516-
strcmp(conn->target_session_attrs, "read-write") == 0)
3517-
{
3518-
if (!saveErrorMessage(conn, &savedMessage))
3519-
goto error_return;
3504+
case PGRES_POLLING_WRITING: /* Still going */
3505+
conn->status = CONNECTION_SETENV;
3506+
return PGRES_POLLING_WRITING;
35203507

3521-
conn->status = CONNECTION_OK;
3522-
if (!PQsendQuery(conn,
3523-
"SHOW transaction_read_only"))
3524-
{
3525-
restoreErrorMessage(conn, &savedMessage);
3526-
goto error_return;
3508+
default:
3509+
goto error_return;
35273510
}
3528-
conn->status = CONNECTION_CHECK_WRITABLE;
3529-
restoreErrorMessage(conn, &savedMessage);
3530-
return PGRES_POLLING_READING;
3531-
}
3532-
3533-
/* We can release the address list now. */
3534-
release_conn_addrinfo(conn);
35353511

3536-
/* We are open for business! */
3537-
conn->status = CONNECTION_OK;
3538-
return PGRES_POLLING_OK;
3512+
/* Almost there now ... */
3513+
conn->status = CONNECTION_CHECK_TARGET;
3514+
goto keep_going;
3515+
}
35393516

35403517
case CONNECTION_CONSUME:
35413518
{

src/interfaces/libpq/libpq-fe.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ typedef enum
6767
* connection. */
6868
CONNECTION_CONSUME, /* Wait for any pending message and consume
6969
* them. */
70-
CONNECTION_GSS_STARTUP /* Negotiating GSSAPI. */
70+
CONNECTION_GSS_STARTUP, /* Negotiating GSSAPI. */
71+
CONNECTION_CHECK_TARGET /* Check if we have a proper target connection */
7172
} ConnStatusType;
7273

7374
typedef enum

0 commit comments

Comments
 (0)