Skip to content

Commit

Permalink
Extend the abilities of libpq's target_session_attrs parameter.
Browse files Browse the repository at this point in the history
In addition to the existing options of "any" and "read-write", we
now support "read-only", "primary", "standby", and "prefer-standby".
"read-write" retains its previous meaning of "transactions are
read-write by default", and "read-only" inverts that.  The other
three modes test specifically for hot-standby status, which is not
quite the same thing.  (Setting default_transaction_read_only on
a primary server renders it read-only to this logic, but not a
standby.)

Furthermore, if talking to a v14 or later server, no extra network
round trip is needed to detect the session's status; the GUC_REPORT
variables delivered by the server are enough.  When talking to an
older server, a SHOW or SELECT query is issued to detect session
read-only-ness or server hot-standby state, as needed.

Haribabu Kommi, Greg Nancarrow, Vignesh C, Tom Lane; reviewed at
various times by Laurenz Albe, Takayuki Tsunakawa, Peter Smith.

Discussion: https://postgr.es/m/CAF3+xM+8-ztOkaV9gHiJ3wfgENTq97QcjXQt+rbFQ6F7oNzt9A@mail.gmail.com
  • Loading branch information
tglsfdc committed Mar 3, 2021
1 parent 57e6db7 commit ee28cac
Show file tree
Hide file tree
Showing 6 changed files with 456 additions and 114 deletions.
79 changes: 68 additions & 11 deletions doc/src/sgml/libpq.sgml
Expand Up @@ -1877,18 +1877,75 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
<term><literal>target_session_attrs</literal></term>
<listitem>
<para>
If this parameter is set to <literal>read-write</literal>, only a
connection in which read-write transactions are accepted by default
is considered acceptable. The query
<literal>SHOW transaction_read_only</literal> will be sent upon any
successful connection; if it returns <literal>on</literal>, the connection
will be closed. If multiple hosts were specified in the connection
string, any remaining servers will be tried just as if the connection
attempt had failed. The default value of this parameter,
<literal>any</literal>, regards all connections as acceptable.
</para>
This option determines whether the session must have certain
properties to be acceptable. It's typically used in combination
with multiple host names to select the first acceptable alternative
among several hosts. There are six modes:

<variablelist>
<varlistentry>
<term><literal>any</literal> (default)</term>
<listitem>
<para>
any successful connection is acceptable
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><literal>read-write</literal></term>
<listitem>
<para>
session must accept read-write transactions by default (that
is, the server must not be in hot standby mode and
the <varname>default_transaction_read_only</varname> parameter
must be <literal>off</literal>)
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><literal>read-only</literal></term>
<listitem>
<para>
session must not accept read-write transactions by default (the
converse)
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><literal>primary</literal></term>
<listitem>
<para>
server must not be in hot standby mode
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><literal>standby</literal></term>
<listitem>
<para>
server must be in hot standby mode
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><literal>prefer-standby</literal></term>
<listitem>
<para>
first try to find a standby server, but if none of the listed
hosts is a standby server, try again in <literal>all</literal>
mode
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</varlistentry>
</variablelist>
</para>
</sect2>
Expand Down

0 comments on commit ee28cac

Please sign in to comment.