From 2e5a27473b6c33a0ead839cede2c7e8823d3e047 Mon Sep 17 00:00:00 2001 From: Hari Babu Date: Mon, 25 Mar 2019 17:35:07 +1100 Subject: [PATCH 5/7] New read-only target_session_attrs type With this read-only option type, application can connect to connecting to a read-only server in the list of hosts, in case if there is any read-only servers available, the connection attempt fails. --- doc/src/sgml/libpq.sgml | 7 +++++- src/interfaces/libpq/fe-connect.c | 34 ++++++++++++++++++++------- src/interfaces/libpq/libpq-fe.h | 3 ++- src/interfaces/libpq/libpq-int.h | 2 +- src/test/recovery/t/001_stream_rep.pl | 10 +++++++- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 7918e398c7..79d122d756 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1578,7 +1578,7 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname The supported options for this parameter are, any, - read-write and prefer-read. + read-write, prefer-read and read-only. The default value of this parameter, any, regards all connections as acceptable. If multiple hosts were specified in the connection string, based on the specified value, any remaining servers @@ -1606,6 +1606,11 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname But for servers version 12 or greater uses the transaction_read_only GUC that is reported by the server upon successful connection. + + + If this parameter is set to read-only, only a connection + in which read-only transactions are accepted by default. + diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 4359a3e152..668bb9712d 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -1249,6 +1249,8 @@ connectOptions2(PGconn *conn) conn->requested_session_type = SESSION_TYPE_READ_WRITE; else if (strcmp(conn->target_session_attrs, "prefer-read") == 0) conn->requested_session_type = SESSION_TYPE_PREFER_READ; + else if (strcmp(conn->target_session_attrs, "read-only") == 0) + conn->requested_session_type = SESSION_TYPE_READ_ONLY; else { conn->status = CONNECTION_BAD; @@ -3249,8 +3251,8 @@ keep_going: /* We will come back to here until there is case CONNECTION_CHECK_TARGET: { /* - * If a read-write or prefer-read connection is required, see - * if we have one. + * If a read-write, prefer-read or read-only connection is + * required, see if we have one. * * 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 @@ -3287,7 +3289,8 @@ keep_going: /* We will come back to here until there is else if ((conn->transaction_read_only && conn->requested_session_type == SESSION_TYPE_READ_WRITE) || (!conn->transaction_read_only && - conn->requested_session_type == SESSION_TYPE_PREFER_READ)) + (conn->requested_session_type == SESSION_TYPE_PREFER_READ || + conn->requested_session_type == SESSION_TYPE_READ_ONLY))) { /* Not a requested type; fail this connection. */ const char *displayed_host; @@ -3347,17 +3350,28 @@ keep_going: /* We will come back to here until there is /* * Requested type is prefer-read, then record this host index - * and try the other before considering it later + * and try the other before considering it later. If requested + * type of connection is read-only, ignore this connection. */ - if (conn->requested_session_type == SESSION_TYPE_PREFER_READ && - conn->read_write_host_index != -2) + if (conn->requested_session_type == SESSION_TYPE_PREFER_READ || + conn->requested_session_type == SESSION_TYPE_READ_ONLY) { + /* + * The following scenario is possible only for the + * prefer-read mode for the next pass of the list of + * connections as it couldn't find any servers that are + * default read-only. + */ + if (conn->read_write_host_index == -2) + goto target_accept_connection; + /* Close connection politely. */ conn->status = CONNECTION_OK; sendTerminateConn(conn); /* Record read-write host index */ - if (conn->read_write_host_index == -1) + if (conn->requested_session_type == SESSION_TYPE_PREFER_READ && + conn->read_write_host_index == -1) conn->read_write_host_index = conn->whichhost; /* @@ -3445,12 +3459,14 @@ keep_going: /* We will come back to here until there is * ignore it. Server is read-write and requested mode is * prefer-read, record it for the first time and try to * consume in the next scan (it means no read-only server - * is found in the first scan). + * is found in the first scan). Server is read-write and + * requested mode is read-only, ignore this connection. */ if ((readonly_server && conn->requested_session_type == SESSION_TYPE_READ_WRITE) || (!readonly_server && - conn->requested_session_type == SESSION_TYPE_PREFER_READ)) + (conn->requested_session_type == SESSION_TYPE_PREFER_READ || + conn->requested_session_type == SESSION_TYPE_READ_ONLY))) { /* * The following scenario is possible only for the diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 4b0fc80df2..5d0b885dae 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -74,7 +74,8 @@ typedef enum { SESSION_TYPE_ANY = 0, /* Any session (default) */ SESSION_TYPE_READ_WRITE, /* Read-write session */ - SESSION_TYPE_PREFER_READ /* Prefer read only session */ + SESSION_TYPE_PREFER_READ, /* Prefer read only session */ + SESSION_TYPE_READ_ONLY /* Read only session */ } TargetSessionAttrsType; typedef enum diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index 13f971b5a5..62a1b7bbd4 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -365,7 +365,7 @@ struct pg_conn /* * Type of connection to make. Possible values: any, read-write, - * prefer-read. + * prefer-read and read-only. */ char *target_session_attrs; TargetSessionAttrsType requested_session_type; diff --git a/src/test/recovery/t/001_stream_rep.pl b/src/test/recovery/t/001_stream_rep.pl index 0e398136a5..651107f49b 100644 --- a/src/test/recovery/t/001_stream_rep.pl +++ b/src/test/recovery/t/001_stream_rep.pl @@ -3,7 +3,7 @@ use strict; use warnings; use PostgresNode; use TestLib; -use Test::More tests => 29; +use Test::More tests => 31; # Initialize master node my $node_master = get_new_node('master'); @@ -129,6 +129,14 @@ test_target_session_attrs($node_standby_1, $node_master, $node_standby_1, test_target_session_attrs($node_master, $node_master, $node_master, "prefer-read", 0); +# Connect to standby1 in "read-only" mode with master,standby1 list. +test_target_session_attrs($node_master, $node_standby_1, $node_standby_1, + "read-only", 0); + +# Connect to standby1 in "read-only" mode with standby1,master list. +test_target_session_attrs($node_standby_1, $node_master, $node_standby_1, + "read-only", 0); + note "switching to physical replication slot"; # Switch to using a physical replication slot. We can do this without a new -- 2.20.1.windows.1