|
| 1 | +<!-- |
| 2 | +doc/src/sgml/ref/wait_for.sgml |
| 3 | +PostgreSQL documentation |
| 4 | +--> |
| 5 | + |
| 6 | +<refentry id="sql-wait-for"> |
| 7 | + <indexterm zone="sql-wait-for"> |
| 8 | + <primary>WAIT FOR</primary> |
| 9 | + </indexterm> |
| 10 | + |
| 11 | + <refmeta> |
| 12 | + <refentrytitle>WAIT FOR</refentrytitle> |
| 13 | + <manvolnum>7</manvolnum> |
| 14 | + <refmiscinfo>SQL - Language Statements</refmiscinfo> |
| 15 | + </refmeta> |
| 16 | + |
| 17 | + <refnamediv> |
| 18 | + <refname>WAIT FOR</refname> |
| 19 | + <refpurpose>wait for target <acronym>LSN</acronym> to be replayed, optionally with a timeout</refpurpose> |
| 20 | + </refnamediv> |
| 21 | + |
| 22 | + <refsynopsisdiv> |
| 23 | +<synopsis> |
| 24 | +WAIT FOR LSN '<replaceable class="parameter">lsn</replaceable>' [ WITH ( <replaceable class="parameter">option</replaceable> [, ...] ) ] |
| 25 | + |
| 26 | +<phrase>where <replaceable class="parameter">option</replaceable> can be:</phrase> |
| 27 | + |
| 28 | + TIMEOUT '<replaceable class="parameter">timeout</replaceable>' |
| 29 | + NO_THROW |
| 30 | +</synopsis> |
| 31 | + </refsynopsisdiv> |
| 32 | + |
| 33 | + <refsect1> |
| 34 | + <title>Description</title> |
| 35 | + |
| 36 | + <para> |
| 37 | + Waits until recovery replays <parameter>lsn</parameter>. |
| 38 | + If no <parameter>timeout</parameter> is specified or it is set to |
| 39 | + zero, this command waits indefinitely for the |
| 40 | + <parameter>lsn</parameter>. |
| 41 | + On timeout, or if the server is promoted before |
| 42 | + <parameter>lsn</parameter> is reached, an error is emitted, |
| 43 | + unless <literal>NO_THROW</literal> is specified in the WITH clause. |
| 44 | + If <parameter>NO_THROW</parameter> is specified, then the command |
| 45 | + doesn't throw errors. |
| 46 | + </para> |
| 47 | + |
| 48 | + <para> |
| 49 | + The possible return values are <literal>success</literal>, |
| 50 | + <literal>timeout</literal>, and <literal>not in recovery</literal>. |
| 51 | + </para> |
| 52 | + </refsect1> |
| 53 | + |
| 54 | + <refsect1> |
| 55 | + <title>Parameters</title> |
| 56 | + |
| 57 | + <variablelist> |
| 58 | + <varlistentry> |
| 59 | + <term><replaceable class="parameter">lsn</replaceable></term> |
| 60 | + <listitem> |
| 61 | + <para> |
| 62 | + Specifies the target <acronym>LSN</acronym> to wait for. |
| 63 | + </para> |
| 64 | + </listitem> |
| 65 | + </varlistentry> |
| 66 | + |
| 67 | + <varlistentry> |
| 68 | + <term><literal>WITH ( <replaceable class="parameter">option</replaceable> [, ...] )</literal></term> |
| 69 | + <listitem> |
| 70 | + <para> |
| 71 | + This clause specifies optional parameters for the wait operation. |
| 72 | + The following parameters are supported: |
| 73 | + |
| 74 | + <variablelist> |
| 75 | + <varlistentry> |
| 76 | + <term><literal>TIMEOUT</literal> '<replaceable class="parameter">timeout</replaceable>'</term> |
| 77 | + <listitem> |
| 78 | + <para> |
| 79 | + When specified and <parameter>timeout</parameter> is greater than zero, |
| 80 | + the command waits until <parameter>lsn</parameter> is reached or |
| 81 | + the specified <parameter>timeout</parameter> has elapsed. |
| 82 | + </para> |
| 83 | + <para> |
| 84 | + The <parameter>timeout</parameter> might be given as integer number of |
| 85 | + milliseconds. Also it might be given as string literal with |
| 86 | + integer number of milliseconds or a number with unit |
| 87 | + (see <xref linkend="config-setting-names-values"/>). |
| 88 | + </para> |
| 89 | + </listitem> |
| 90 | + </varlistentry> |
| 91 | + |
| 92 | + <varlistentry> |
| 93 | + <term><literal>NO_THROW</literal></term> |
| 94 | + <listitem> |
| 95 | + <para> |
| 96 | + Specify to not throw an error in the case of timeout or |
| 97 | + running on the primary. In this case the result status can be get from |
| 98 | + the return value. |
| 99 | + </para> |
| 100 | + </listitem> |
| 101 | + </varlistentry> |
| 102 | + </variablelist> |
| 103 | + </para> |
| 104 | + </listitem> |
| 105 | + </varlistentry> |
| 106 | + </variablelist> |
| 107 | + </refsect1> |
| 108 | + |
| 109 | + <refsect1> |
| 110 | + <title>Outputs</title> |
| 111 | + |
| 112 | + <variablelist> |
| 113 | + <varlistentry> |
| 114 | + <term><literal>success</literal></term> |
| 115 | + <listitem> |
| 116 | + <para> |
| 117 | + This return value denotes that we have successfully reached |
| 118 | + the target <parameter>lsn</parameter>. |
| 119 | + </para> |
| 120 | + </listitem> |
| 121 | + </varlistentry> |
| 122 | + |
| 123 | + <varlistentry> |
| 124 | + <term><literal>timeout</literal></term> |
| 125 | + <listitem> |
| 126 | + <para> |
| 127 | + This return value denotes that the timeout happened before reaching |
| 128 | + the target <parameter>lsn</parameter>. |
| 129 | + </para> |
| 130 | + </listitem> |
| 131 | + </varlistentry> |
| 132 | + |
| 133 | + <varlistentry> |
| 134 | + <term><literal>not in recovery</literal></term> |
| 135 | + <listitem> |
| 136 | + <para> |
| 137 | + This return value denotes that the database server is not in a recovery |
| 138 | + state. This might mean either the database server was not in recovery |
| 139 | + at the moment of receiving the command, or it was promoted before |
| 140 | + reaching the target <parameter>lsn</parameter>. |
| 141 | + </para> |
| 142 | + </listitem> |
| 143 | + </varlistentry> |
| 144 | + </variablelist> |
| 145 | + </refsect1> |
| 146 | + |
| 147 | + <refsect1> |
| 148 | + <title>Notes</title> |
| 149 | + |
| 150 | + <para> |
| 151 | + <command>WAIT FOR</command> command waits till |
| 152 | + <parameter>lsn</parameter> to be replayed on standby. |
| 153 | + That is, after this command execution, the value returned by |
| 154 | + <function>pg_last_wal_replay_lsn</function> should be greater or equal |
| 155 | + to the <parameter>lsn</parameter> value. This is useful to achieve |
| 156 | + read-your-writes-consistency, while using async replica for reads and |
| 157 | + primary for writes. In that case, the <acronym>lsn</acronym> of the last |
| 158 | + modification should be stored on the client application side or the |
| 159 | + connection pooler side. |
| 160 | + </para> |
| 161 | + |
| 162 | + <para> |
| 163 | + <command>WAIT FOR</command> command should be called on standby. |
| 164 | + If a user runs <command>WAIT FOR</command> on primary, it |
| 165 | + will error out unless <parameter>NO_THROW</parameter> is specified in the WITH clause. |
| 166 | + However, if <command>WAIT FOR</command> is |
| 167 | + called on primary promoted from standby and <literal>lsn</literal> |
| 168 | + was already replayed, then the <command>WAIT FOR</command> command just |
| 169 | + exits immediately. |
| 170 | + </para> |
| 171 | + |
| 172 | +</refsect1> |
| 173 | + |
| 174 | + <refsect1> |
| 175 | + <title>Examples</title> |
| 176 | + |
| 177 | + <para> |
| 178 | + You can use <command>WAIT FOR</command> command to wait for |
| 179 | + the <type>pg_lsn</type> value. For example, an application could update |
| 180 | + the <literal>movie</literal> table and get the <acronym>lsn</acronym> after |
| 181 | + changes just made. This example uses <function>pg_current_wal_insert_lsn</function> |
| 182 | + on primary server to get the <acronym>lsn</acronym> given that |
| 183 | + <varname>synchronous_commit</varname> could be set to |
| 184 | + <literal>off</literal>. |
| 185 | + |
| 186 | + <programlisting> |
| 187 | +postgres=# UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama'; |
| 188 | +UPDATE 100 |
| 189 | +postgres=# SELECT pg_current_wal_insert_lsn(); |
| 190 | +pg_current_wal_insert_lsn |
| 191 | +-------------------- |
| 192 | +0/306EE20 |
| 193 | +(1 row) |
| 194 | +</programlisting> |
| 195 | + |
| 196 | + Then an application could run <command>WAIT FOR</command> |
| 197 | + with the <parameter>lsn</parameter> obtained from primary. After that the |
| 198 | + changes made on primary should be guaranteed to be visible on replica. |
| 199 | + |
| 200 | +<programlisting> |
| 201 | +postgres=# WAIT FOR LSN '0/306EE20'; |
| 202 | + status |
| 203 | +-------- |
| 204 | + success |
| 205 | +(1 row) |
| 206 | +postgres=# SELECT * FROM movie WHERE genre = 'Drama'; |
| 207 | + genre |
| 208 | +------- |
| 209 | +(0 rows) |
| 210 | +</programlisting> |
| 211 | + </para> |
| 212 | + |
| 213 | + <para> |
| 214 | + If the target LSN is not reached before the timeout, the error is thrown. |
| 215 | + |
| 216 | +<programlisting> |
| 217 | +postgres=# WAIT FOR LSN '0/306EE20' WITH (TIMEOUT '0.1s'); |
| 218 | +ERROR: timed out while waiting for target LSN 0/306EE20 to be replayed; current replay LSN 0/306EA60 |
| 219 | +</programlisting> |
| 220 | + </para> |
| 221 | + |
| 222 | + <para> |
| 223 | + The same example uses <command>WAIT FOR</command> with |
| 224 | + <parameter>NO_THROW</parameter> option. |
| 225 | +<programlisting> |
| 226 | +postgres=# WAIT FOR LSN '0/306EE20' WITH (TIMEOUT '100ms', NO_THROW); |
| 227 | + status |
| 228 | +-------- |
| 229 | + timeout |
| 230 | +(1 row) |
| 231 | +</programlisting> |
| 232 | + </para> |
| 233 | + </refsect1> |
| 234 | +</refentry> |
0 commit comments