Skip to content

Commit bf8a662

Browse files
committed
Introduce a new GUC_REPORT setting "in_hot_standby".
Aside from being queriable via SHOW, this value is sent to the client immediately at session startup, and again later on if the server gets promoted to primary during the session. The immediate report will be used in an upcoming patch to avoid an extra round trip when trying to connect to a primary server. Haribabu Kommi, Greg Nancarrow, 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
1 parent 47b2ed1 commit bf8a662

File tree

6 files changed

+91
-11
lines changed

6 files changed

+91
-11
lines changed

doc/src/sgml/config.sgml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9600,13 +9600,14 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
96009600
<title>Preset Options</title>
96019601

96029602
<para>
9603-
The following <quote>parameters</quote> are read-only, and are determined
9604-
when <productname>PostgreSQL</productname> is compiled or when it is
9605-
installed. As such, they have been excluded from the sample
9603+
The following <quote>parameters</quote> are read-only.
9604+
As such, they have been excluded from the sample
96069605
<filename>postgresql.conf</filename> file. These options report
96079606
various aspects of <productname>PostgreSQL</productname> behavior
96089607
that might be of interest to certain applications, particularly
96099608
administrative front-ends.
9609+
Most of them are determined when <productname>PostgreSQL</productname>
9610+
is compiled or when it is installed.
96109611
</para>
96119612

96129613
<variablelist>
@@ -9651,10 +9652,11 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
96519652
</term>
96529653
<listitem>
96539654
<para>
9654-
On Unix systems this parameter reports the permissions of the data
9655-
directory defined by (<xref linkend="guc-data-directory"/>) at startup.
9655+
On Unix systems this parameter reports the permissions the data
9656+
directory (defined by <xref linkend="guc-data-directory"/>)
9657+
had at server startup.
96569658
(On Microsoft Windows this parameter will always display
9657-
<literal>0700</literal>). See
9659+
<literal>0700</literal>.) See
96589660
<xref linkend="app-initdb-allow-group-access"/> for more information.
96599661
</para>
96609662
</listitem>
@@ -9695,6 +9697,23 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
96959697
</listitem>
96969698
</varlistentry>
96979699

9700+
<varlistentry id="guc-in-hot-standby" xreflabel="in_hot_standby">
9701+
<term><varname>in_hot_standby</varname> (<type>boolean</type>)
9702+
<indexterm>
9703+
<primary><varname>in_hot_standby</varname> configuration parameter</primary>
9704+
</indexterm>
9705+
</term>
9706+
<listitem>
9707+
<para>
9708+
Reports whether the server is currently in hot standby mode. When
9709+
this is <literal>on</literal>, all transactions are forced to be
9710+
read-only. Within a session, this can change only if the server is
9711+
promoted to be primary. See <xref linkend="hot-standby"/> for more
9712+
information.
9713+
</para>
9714+
</listitem>
9715+
</varlistentry>
9716+
96989717
<varlistentry id="guc-lc-collate" xreflabel="lc_collate">
96999718
<term><varname>lc_collate</varname> (<type>string</type>)
97009719
<indexterm>

doc/src/sgml/high-availability.sgml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,8 +1859,11 @@ if (!triggered)
18591859
</para>
18601860

18611861
<para>
1862-
Users will be able to tell whether their session is read-only by
1863-
issuing <command>SHOW transaction_read_only</command>. In addition, a set of
1862+
Users can determine whether hot standby is currently active for their
1863+
session by issuing <command>SHOW in_hot_standby</command>.
1864+
(In server versions before 14, the <varname>in_hot_standby</varname>
1865+
parameter did not exist; a workable substitute method for older servers
1866+
is <command>SHOW transaction_read_only</command>.) In addition, a set of
18641867
functions (<xref linkend="functions-recovery-info-table"/>) allow users to
18651868
access information about the standby server. These allow you to write
18661869
programs that are aware of the current state of the database. These

doc/src/sgml/libpq.sgml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,7 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName);
21502150
<varname>server_encoding</varname>,
21512151
<varname>client_encoding</varname>,
21522152
<varname>application_name</varname>,
2153+
<varname>in_hot_standby</varname>,
21532154
<varname>is_superuser</varname>,
21542155
<varname>session_authorization</varname>,
21552156
<varname>DateStyle</varname>,
@@ -2162,7 +2163,10 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName);
21622163
<varname>standard_conforming_strings</varname> was not reported by releases
21632164
before 8.1;
21642165
<varname>IntervalStyle</varname> was not reported by releases before 8.4;
2165-
<varname>application_name</varname> was not reported by releases before 9.0.)
2166+
<varname>application_name</varname> was not reported by releases before
2167+
9.0;
2168+
<varname>in_hot_standby</varname> was not reported by releases before
2169+
14.)
21662170
Note that
21672171
<varname>server_version</varname>,
21682172
<varname>server_encoding</varname> and

doc/src/sgml/protocol.sgml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,7 @@ SELCT 1/0;<!-- this typo is intentional -->
12781278
<varname>server_encoding</varname>,
12791279
<varname>client_encoding</varname>,
12801280
<varname>application_name</varname>,
1281+
<varname>in_hot_standby</varname>,
12811282
<varname>is_superuser</varname>,
12821283
<varname>session_authorization</varname>,
12831284
<varname>DateStyle</varname>,
@@ -1290,7 +1291,10 @@ SELCT 1/0;<!-- this typo is intentional -->
12901291
<varname>standard_conforming_strings</varname> was not reported by releases
12911292
before 8.1;
12921293
<varname>IntervalStyle</varname> was not reported by releases before 8.4;
1293-
<varname>application_name</varname> was not reported by releases before 9.0.)
1294+
<varname>application_name</varname> was not reported by releases before
1295+
9.0;
1296+
<varname>in_hot_standby</varname> was not reported by releases before
1297+
14.)
12941298
Note that
12951299
<varname>server_version</varname>,
12961300
<varname>server_encoding</varname> and

src/backend/utils/misc/check_guc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
## if an option is valid but shows up in only one file (guc.c but not
1717
## postgresql.conf.sample), it should be listed here so that it
1818
## can be ignored
19-
INTENTIONALLY_NOT_INCLUDED="debug_deadlocks \
19+
INTENTIONALLY_NOT_INCLUDED="debug_deadlocks in_hot_standby \
2020
is_superuser lc_collate lc_ctype lc_messages lc_monetary lc_numeric lc_time \
2121
pre_auth_delay role seed server_encoding server_version server_version_num \
2222
session_authorization trace_lock_oidmin trace_lock_table trace_locks trace_lwlocks \

src/backend/utils/misc/guc.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ static bool check_cluster_name(char **newval, void **extra, GucSource source);
209209
static const char *show_unix_socket_permissions(void);
210210
static const char *show_log_file_mode(void);
211211
static const char *show_data_directory_mode(void);
212+
static const char *show_in_hot_standby(void);
212213
static bool check_backtrace_functions(char **newval, void **extra, GucSource source);
213214
static void assign_backtrace_functions(const char *newval, void *extra);
214215
static bool check_recovery_target_timeline(char **newval, void **extra, GucSource source);
@@ -610,6 +611,7 @@ static int wal_block_size;
610611
static bool data_checksums;
611612
static bool integer_datetimes;
612613
static bool assert_enabled;
614+
static bool in_hot_standby;
613615
static char *recovery_target_timeline_string;
614616
static char *recovery_target_string;
615617
static char *recovery_target_xid_string;
@@ -1844,6 +1846,17 @@ static struct config_bool ConfigureNamesBool[] =
18441846
NULL, NULL, NULL
18451847
},
18461848

1849+
{
1850+
{"in_hot_standby", PGC_INTERNAL, PRESET_OPTIONS,
1851+
gettext_noop("Shows whether hot standby is currently active."),
1852+
NULL,
1853+
GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1854+
},
1855+
&in_hot_standby,
1856+
false,
1857+
NULL, NULL, show_in_hot_standby
1858+
},
1859+
18471860
{
18481861
{"allow_system_table_mods", PGC_SUSET, DEVELOPER_OPTIONS,
18491862
gettext_noop("Allows modifications of the structure of system tables."),
@@ -6248,6 +6261,14 @@ BeginReportingGUCOptions(void)
62486261

62496262
reporting_enabled = true;
62506263

6264+
/*
6265+
* Hack for in_hot_standby: initialize with the value we're about to send.
6266+
* (This could be out of date by the time we actually send it, in which
6267+
* case the next ReportChangedGUCOptions call will send a duplicate
6268+
* report.)
6269+
*/
6270+
in_hot_standby = RecoveryInProgress();
6271+
62516272
/* Transmit initial values of interesting variables */
62526273
for (i = 0; i < num_guc_variables; i++)
62536274
{
@@ -6280,6 +6301,23 @@ ReportChangedGUCOptions(void)
62806301
if (!reporting_enabled)
62816302
return;
62826303

6304+
/*
6305+
* Since in_hot_standby isn't actually changed by normal GUC actions, we
6306+
* need a hack to check whether a new value needs to be reported to the
6307+
* client. For speed, we rely on the assumption that it can never
6308+
* transition from false to true.
6309+
*/
6310+
if (in_hot_standby && !RecoveryInProgress())
6311+
{
6312+
struct config_generic *record;
6313+
6314+
record = find_option("in_hot_standby", false, ERROR);
6315+
Assert(record != NULL);
6316+
record->status |= GUC_NEEDS_REPORT;
6317+
report_needed = true;
6318+
in_hot_standby = false;
6319+
}
6320+
62836321
/* Quick exit if no values have been changed */
62846322
if (!report_needed)
62856323
return;
@@ -11773,6 +11811,18 @@ show_data_directory_mode(void)
1177311811
return buf;
1177411812
}
1177511813

11814+
static const char *
11815+
show_in_hot_standby(void)
11816+
{
11817+
/*
11818+
* We display the actual state based on shared memory, so that this GUC
11819+
* reports up-to-date state if examined intra-query. The underlying
11820+
* variable in_hot_standby changes only when we transmit a new value to
11821+
* the client.
11822+
*/
11823+
return RecoveryInProgress() ? "on" : "off";
11824+
}
11825+
1177611826
/*
1177711827
* We split the input string, where commas separate function names
1177811828
* and certain whitespace chars are ignored, into a \0-separated (and

0 commit comments

Comments
 (0)