aboutsummaryrefslogtreecommitdiff
path: root/doc/src
diff options
context:
space:
mode:
authorAlexander Korotkov <akorotkov@postgresql.org>2024-08-02 21:13:05 +0300
committerAlexander Korotkov <akorotkov@postgresql.org>2024-08-02 21:16:56 +0300
commit3c5db1d6b01642bcd8dbf5e34b68f034365747bb (patch)
tree57de0927f3e8600509b8f09ea32f39bbb0d3054b /doc/src
parenta83f3088b8f409aaee7a939c2847157d97006193 (diff)
downloadpostgresql-3c5db1d6b01642bcd8dbf5e34b68f034365747bb.tar.gz
postgresql-3c5db1d6b01642bcd8dbf5e34b68f034365747bb.zip
Implement pg_wal_replay_wait() stored procedure
pg_wal_replay_wait() is to be used on standby and specifies waiting for the specific WAL location to be replayed. This option is useful when the user makes some data changes on primary and needs a guarantee to see these changes are on standby. The queue of waiters is stored in the shared memory as an LSN-ordered pairing heap, where the waiter with the nearest LSN stays on the top. During the replay of WAL, waiters whose LSNs have already been replayed are deleted from the shared memory pairing heap and woken up by setting their latches. pg_wal_replay_wait() needs to wait without any snapshot held. Otherwise, the snapshot could prevent the replay of WAL records, implying a kind of self-deadlock. This is why it is only possible to implement pg_wal_replay_wait() as a procedure working without an active snapshot, not a function. Catversion is bumped. Discussion: https://postgr.es/m/eb12f9b03851bb2583adab5df9579b4b%40postgrespro.ru Author: Kartyshov Ivan, Alexander Korotkov Reviewed-by: Michael Paquier, Peter Eisentraut, Dilip Kumar, Amit Kapila Reviewed-by: Alexander Lakhin, Bharath Rupireddy, Euler Taveira Reviewed-by: Heikki Linnakangas, Kyotaro Horiguchi
Diffstat (limited to 'doc/src')
-rw-r--r--doc/src/sgml/func.sgml117
1 files changed, 117 insertions, 0 deletions
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index b39f97dc8de..3cf896b22fa 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -28911,6 +28911,123 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
the pause, the rate of WAL generation and available disk space.
</para>
+ <para>
+ The procedure shown in <xref linkend="recovery-synchronization-procedure-table"/>
+ can be executed only during recovery.
+ </para>
+
+ <table id="recovery-synchronization-procedure-table">
+ <title>Recovery Synchronization Procedure</title>
+ <tgroup cols="1">
+ <thead>
+ <row>
+ <entry role="func_table_entry"><para role="func_signature">
+ Procedure
+ </para>
+ <para>
+ Description
+ </para></entry>
+ </row>
+ </thead>
+
+ <tbody>
+ <row>
+ <entry role="func_table_entry"><para role="func_signature">
+ <indexterm>
+ <primary>pg_wal_replay_wait</primary>
+ </indexterm>
+ <function>pg_wal_replay_wait</function> (
+ <parameter>target_lsn</parameter> <type>pg_lsn</type>,
+ <parameter>timeout</parameter> <type>bigint</type> <literal>DEFAULT</literal> <literal>0</literal>)
+ <returnvalue>void</returnvalue>
+ </para>
+ <para>
+ Waits until recovery replays <literal>target_lsn</literal>.
+ If no <parameter>timeout</parameter> is specified or it is set to
+ zero, this procedure waits indefinitely for the
+ <literal>target_lsn</literal>. If the <parameter>timeout</parameter>
+ is specified (in milliseconds) and is greater than zero, the
+ procedure waits until <literal>target_lsn</literal> is reached or
+ the specified <parameter>timeout</parameter> has elapsed.
+ On timeout, or if the server is promoted before
+ <literal>target_lsn</literal> is reached, an error is emitted.
+ </para></entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </table>
+
+ <para>
+ <function>pg_wal_replay_wait</function> waits till
+ <parameter>target_lsn</parameter> to be replayed on standby.
+ That is, after this function execution, the value returned by
+ <function>pg_last_wal_replay_lsn</function> should be greater or equal
+ to the <parameter>target_lsn</parameter> value. This is useful to achieve
+ read-your-writes-consistency, while using async replica for reads and
+ primary for writes. In that case <acronym>lsn</acronym> of the last
+ modification should be stored on the client application side or the
+ connection pooler side.
+ </para>
+
+ <para>
+ You can use <function>pg_wal_replay_wait</function> to wait for
+ the <type>pg_lsn</type> value. For example, an application could update
+ the <literal>movie</literal> table and get the <acronym>lsn</acronym> after
+ changes just made. This example uses <function>pg_current_wal_insert_lsn</function>
+ on primary server to get the <acronym>lsn</acronym> given that
+ <varname>synchronous_commit</varname> could be set to
+ <literal>off</literal>.
+
+ <programlisting>
+postgres=# UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama';
+UPDATE 100
+postgres=# SELECT pg_current_wal_insert_lsn();
+pg_current_wal_insert_lsn
+--------------------
+0/306EE20
+(1 row)
+ </programlisting>
+
+ Then an application could run <function>pg_wal_replay_wait</function>
+ with the <acronym>lsn</acronym> obtained from primary. After that the
+ changes made of primary should be guaranteed to be visible on replica.
+
+ <programlisting>
+postgres=# CALL pg_wal_replay_wait('0/306EE20');
+CALL
+postgres=# SELECT * FROM movie WHERE genre = 'Drama';
+ genre
+-------
+(0 rows)
+ </programlisting>
+
+ It may also happen that target <acronym>lsn</acronym> is not achieved
+ within the timeout. In that case the error is thrown.
+
+ <programlisting>
+postgres=# CALL pg_wal_replay_wait('0/306EE20', 100);
+ERROR: timed out while waiting for target LSN 0/306EE20 to be replayed; current replay LSN 0/306EA60
+ </programlisting>
+
+ </para>
+
+ <para>
+ <function>pg_wal_replay_wait</function> can't be used within
+ a transaction with an isolation level higher than
+ <literal>READ COMMITTED</literal>, another procedure, or a function.
+ All the cases above imply holding a snapshot, which could prevent
+ WAL records from replaying (see <xref linkend="hot-standby-conflict"/>)
+ and cause an indirect deadlock.
+
+ <programlisting>
+postgres=# BEGIN;
+BEGIN
+postgres=*# CALL pg_wal_replay_wait('0/306EE20');
+ERROR: pg_wal_replay_wait() must be only called without an active or registered snapshot
+DETAIL: Make sure pg_wal_replay_wait() isn't called within a transaction with an isolation level higher than READ COMMITTED, another procedure, or a function.
+ </programlisting>
+
+ </para>
</sect2>
<sect2 id="functions-snapshot-synchronization">