@@ -29000,176 +29000,6 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
2900029000 the pause, the rate of WAL generation and available disk space.
2900129001 </para>
2900229002
29003- <para>
29004- The procedure shown in <xref linkend="recovery-synchronization-procedure-table"/>
29005- can be executed only during recovery.
29006- </para>
29007-
29008- <table id="recovery-synchronization-procedure-table">
29009- <title>Recovery Synchronization Procedure and Function</title>
29010- <tgroup cols="1">
29011- <thead>
29012- <row>
29013- <entry role="func_table_entry"><para role="func_signature">
29014- Procedure or Function
29015- </para>
29016- <para>
29017- Type
29018- </para>
29019- <para>
29020- Description
29021- </para></entry>
29022- </row>
29023- </thead>
29024-
29025- <tbody>
29026- <row>
29027- <entry role="func_table_entry"><para role="func_signature">
29028- <indexterm>
29029- <primary>pg_wal_replay_wait</primary>
29030- </indexterm>
29031- <function>pg_wal_replay_wait</function> (
29032- <parameter>target_lsn</parameter> <type>pg_lsn</type>,
29033- <parameter>timeout</parameter> <type>bigint</type> <literal>DEFAULT</literal> <literal>0</literal>,
29034- <parameter>no_error</parameter> <type>bool</type> <literal>DEFAULT</literal> <literal>false</literal>)
29035- </para>
29036- <para>
29037- Procedure
29038- </para>
29039- <para>
29040- Waits until recovery replays <literal>target_lsn</literal>.
29041- If no <parameter>timeout</parameter> is specified or it is set to
29042- zero, this procedure waits indefinitely for the
29043- <literal>target_lsn</literal>. If the <parameter>timeout</parameter>
29044- is specified (in milliseconds) and is greater than zero, the
29045- procedure waits until <literal>target_lsn</literal> is reached or
29046- the specified <parameter>timeout</parameter> has elapsed.
29047- On timeout, or if the server is promoted before
29048- <literal>target_lsn</literal> is reached, an error is emitted,
29049- as soon as <parameter>no_error</parameter> is false.
29050- If <parameter>no_error</parameter> is set to true, then the procedure
29051- doesn't throw errors. The last result status could be read
29052- with <function>pg_wal_replay_wait_status</function>.
29053- </para></entry>
29054- </row>
29055-
29056- <row>
29057- <entry role="func_table_entry"><para role="func_signature">
29058- <indexterm>
29059- <primary>pg_wal_replay_wait_status</primary>
29060- </indexterm>
29061- <function>pg_wal_replay_wait_status</function> ()
29062- <returnvalue>text</returnvalue>
29063- </para>
29064- <para>
29065- Function
29066- </para>
29067- <para>
29068- Returns the last result status for
29069- <function>pg_wal_replay_wait</function> procedure. The possible
29070- values are <literal>success</literal>, <literal>timeout</literal>,
29071- and <literal>not in recovery</literal>.
29072- </para></entry>
29073- </row>
29074- </tbody>
29075- </tgroup>
29076- </table>
29077-
29078- <para>
29079- <function>pg_wal_replay_wait</function> waits till
29080- <parameter>target_lsn</parameter> to be replayed on standby.
29081- That is, after this function execution, the value returned by
29082- <function>pg_last_wal_replay_lsn</function> should be greater or equal
29083- to the <parameter>target_lsn</parameter> value. This is useful to achieve
29084- read-your-writes-consistency, while using async replica for reads and
29085- primary for writes. In that case <acronym>lsn</acronym> of the last
29086- modification should be stored on the client application side or the
29087- connection pooler side.
29088- </para>
29089-
29090- <para>
29091- <function>pg_wal_replay_wait</function> should be called on standby.
29092- If a user calls <function>pg_wal_replay_wait</function> on primary, it
29093- will error out as soon as <parameter>no_error</parameter> is false.
29094- However, if <function>pg_wal_replay_wait</function> is
29095- called on primary promoted from standby and <literal>target_lsn</literal>
29096- was already replayed, then <function>pg_wal_replay_wait</function> just
29097- exits immediately.
29098- </para>
29099-
29100- <para>
29101- You can use <function>pg_wal_replay_wait</function> to wait for
29102- the <type>pg_lsn</type> value. For example, an application could update
29103- the <literal>movie</literal> table and get the <acronym>lsn</acronym> after
29104- changes just made. This example uses <function>pg_current_wal_insert_lsn</function>
29105- on primary server to get the <acronym>lsn</acronym> given that
29106- <varname>synchronous_commit</varname> could be set to
29107- <literal>off</literal>.
29108-
29109- <programlisting>
29110- postgres=# UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama';
29111- UPDATE 100
29112- postgres=# SELECT pg_current_wal_insert_lsn();
29113- pg_current_wal_insert_lsn
29114- --------------------
29115- 0/306EE20
29116- (1 row)
29117- </programlisting>
29118-
29119- Then an application could run <function>pg_wal_replay_wait</function>
29120- with the <acronym>lsn</acronym> obtained from primary. After that the
29121- changes made on primary should be guaranteed to be visible on replica.
29122-
29123- <programlisting>
29124- postgres=# CALL pg_wal_replay_wait('0/306EE20');
29125- CALL
29126- postgres=# SELECT * FROM movie WHERE genre = 'Drama';
29127- genre
29128- -------
29129- (0 rows)
29130- </programlisting>
29131-
29132- It may also happen that target <acronym>lsn</acronym> is not reached
29133- within the timeout. In that case the error is thrown.
29134-
29135- <programlisting>
29136- postgres=# CALL pg_wal_replay_wait('0/306EE20', 100);
29137- ERROR: timed out while waiting for target LSN 0/306EE20 to be replayed; current replay LSN 0/306EA60
29138- </programlisting>
29139-
29140- The same example uses <function>pg_wal_replay_wait</function> with
29141- <parameter>no_error</parameter> set to true. In this case, the result
29142- status must be read with <function>pg_wal_replay_wait_status</function>.
29143-
29144- <programlisting>
29145- postgres=# CALL pg_wal_replay_wait('0/306EE20', 100, true);
29146- CALL
29147- postgres=# SELECT pg_wal_replay_wait_status();
29148- pg_wal_replay_wait_status
29149- ---------------------------
29150- timeout
29151- (1 row)
29152- </programlisting>
29153-
29154- </para>
29155-
29156- <para>
29157- <function>pg_wal_replay_wait</function> can't be used within
29158- a transaction with an isolation level higher than
29159- <literal>READ COMMITTED</literal>, another procedure, or a function.
29160- All the cases above imply holding a snapshot, which could prevent
29161- WAL records from replaying (see <xref linkend="hot-standby-conflict"/>)
29162- and cause an indirect deadlock.
29163-
29164- <programlisting>
29165- postgres=# BEGIN;
29166- BEGIN
29167- postgres=*# CALL pg_wal_replay_wait('0/306EE20');
29168- ERROR: pg_wal_replay_wait() must be only called without an active or registered snapshot
29169- 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.
29170- </programlisting>
29171-
29172- </para>
2917329003 </sect2>
2917429004
2917529005 <sect2 id="functions-snapshot-synchronization">
0 commit comments