1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-24 14:22:24 +03:00

Introduce a new GUC 'standby_slot_names'.

This patch provides a way to ensure that physical standbys that are
potential failover candidates have received and flushed changes before
the primary server making them visible to subscribers. Doing so guarantees
that the promoted standby server is not lagging behind the subscribers
when a failover is necessary.

The logical walsender now guarantees that all local changes are sent and
flushed to the standby servers corresponding to the replication slots
specified in 'standby_slot_names' before sending those changes to the
subscriber.

Additionally, the SQL functions pg_logical_slot_get_changes,
pg_logical_slot_peek_changes and pg_replication_slot_advance are modified
to ensure that they process changes for failover slots only after physical
slots specified in 'standby_slot_names' have confirmed WAL receipt for those.

Author: Hou Zhijie and Shveta Malik
Reviewed-by: Masahiko Sawada, Peter Smith, Bertrand Drouvot, Ajin Cherian, Nisha Moond, Amit Kapila
Discussion: https://postgr.es/m/514f6f2f-6833-4539-39f1-96cd1e011f23@enterprisedb.com
This commit is contained in:
Amit Kapila
2024-03-08 08:10:45 +05:30
parent 453c468737
commit bf279ddd1c
18 changed files with 918 additions and 27 deletions

View File

@ -4559,6 +4559,45 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
</listitem>
</varlistentry>
<varlistentry id="guc-standby-slot-names" xreflabel="standby_slot_names">
<term><varname>standby_slot_names</varname> (<type>string</type>)
<indexterm>
<primary><varname>standby_slot_names</varname> configuration parameter</primary>
</indexterm>
</term>
<listitem>
<para>
A comma-separated list of streaming replication standby server slot names
that logical WAL sender processes will wait for. Logical WAL sender processes
will send decoded changes to plugins only after the specified replication
slots confirm receiving WAL. This guarantees that logical replication
failover slots do not consume changes until those changes are received
and flushed to corresponding physical standbys. If a
logical replication connection is meant to switch to a physical standby
after the standby is promoted, the physical replication slot for the
standby should be listed here. Note that logical replication will not
proceed if the slots specified in the
<varname>standby_slot_names</varname> do not exist or are invalidated.
Additionally, the replication management functions
<link linkend="pg-replication-slot-advance">
<function>pg_replication_slot_advance</function></link>,
<link linkend="pg-logical-slot-get-changes">
<function>pg_logical_slot_get_changes</function></link>, and
<link linkend="pg-logical-slot-peek-changes">
<function>pg_logical_slot_peek_changes</function></link>,
when used with logical failover slots, will block until all
physical slots specified in <varname>standby_slot_names</varname> have
confirmed WAL receipt.
</para>
<para>
The standbys corresponding to the physical replication slots in
<varname>standby_slot_names</varname> must configure
<literal>sync_replication_slots = true</literal> so they can receive
logical failover slot changes from the primary.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect2>

View File

@ -28150,7 +28150,7 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<entry id="pg-logical-slot-get-changes" role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_logical_slot_get_changes</primary>
</indexterm>
@ -28173,11 +28173,15 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
the specified value. Note, however, that the actual number of
rows returned may be larger, since this limit is only checked after
adding the rows produced when decoding each new transaction commit.
If the specified slot is a logical failover slot then the function will
not return until all physical slots specified in
<link linkend="guc-standby-slot-names"><varname>standby_slot_names</varname></link>
have confirmed WAL receipt.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<entry id="pg-logical-slot-peek-changes" role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_logical_slot_peek_changes</primary>
</indexterm>
@ -28232,7 +28236,7 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<entry id="pg-replication-slot-advance" role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_replication_slot_advance</primary>
</indexterm>
@ -28248,7 +28252,11 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
the name of the slot and the actual position that it was advanced to.
The updated slot position information is written out at the next
checkpoint if any advancing is done. So in the event of a crash, the
slot may return to an earlier position.
slot may return to an earlier position. If the specified slot is a
logical failover slot then the function will not return until all
physical slots specified in
<link linkend="guc-standby-slot-names"><varname>standby_slot_names</varname></link>
have confirmed WAL receipt.
</para></entry>
</row>

View File

@ -384,6 +384,18 @@ postgres=# select * from pg_logical_slot_get_changes('regression_slot', NULL, NU
must be enabled on the standby. It is also necessary to specify a valid
<literal>dbname</literal> in the
<link linkend="guc-primary-conninfo"><varname>primary_conninfo</varname></link>.
It's highly recommended that the said physical replication slot is named in
<link linkend="guc-standby-slot-names"><varname>standby_slot_names</varname></link>
list on the primary, to prevent the subscriber from consuming changes
faster than the hot standby. Even when correctly configured, some latency
is expected when sending changes to logical subscribers due to the waiting
on slots named in
<link linkend="guc-standby-slot-names"><varname>standby_slot_names</varname></link>.
When <varname>standby_slot_names</varname> is utilized, the
primary server will not completely shut down until the corresponding
standbys, associated with the physical replication slots specified
in <varname>standby_slot_names</varname>, have confirmed
receiving the WAL up to the latest flushed position on the primary server.
</para>
<para>