PostgreSQL Source Code git master
Loading...
Searching...
No Matches
wait.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/xlog.h"
#include "access/xlogrecovery.h"
#include "access/xlogwait.h"
#include "catalog/pg_type_d.h"
#include "commands/defrem.h"
#include "commands/wait.h"
#include "executor/executor.h"
#include "parser/parse_node.h"
#include "storage/proc.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/pg_lsn.h"
#include "utils/snapmgr.h"
Include dependency graph for wait.c:

Go to the source code of this file.

Functions

void ExecWaitStmt (ParseState *pstate, WaitStmt *stmt, DestReceiver *dest)
 
TupleDesc WaitStmtResultDesc (WaitStmt *stmt)
 

Function Documentation

◆ ExecWaitStmt()

void ExecWaitStmt ( ParseState pstate,
WaitStmt stmt,
DestReceiver dest 
)

Definition at line 34 of file wait.c.

35{
36 XLogRecPtr lsn;
37 int64 timeout = 0;
39 WaitLSNType lsnType = WAIT_LSN_TYPE_STANDBY_REPLAY; /* default */
40 bool throw = true;
41 TupleDesc tupdesc;
43 const char *result = "<unset>";
44 bool timeout_specified = false;
45 bool no_throw_specified = false;
46 bool mode_specified = false;
47
48 /* Parse and validate the mandatory LSN */
50 CStringGetDatum(stmt->lsn_literal)));
51
52 foreach_node(DefElem, defel, stmt->options)
53 {
54 if (strcmp(defel->defname, "mode") == 0)
55 {
56 char *mode_str;
57
60 mode_specified = true;
61
63
64 if (pg_strcasecmp(mode_str, "standby_replay") == 0)
66 else if (pg_strcasecmp(mode_str, "standby_write") == 0)
68 else if (pg_strcasecmp(mode_str, "standby_flush") == 0)
70 else if (pg_strcasecmp(mode_str, "primary_flush") == 0)
72 else
75 errmsg("unrecognized value for %s option \"%s\": \"%s\"",
76 "WAIT", defel->defname, mode_str),
77 parser_errposition(pstate, defel->location)));
78 }
79 else if (strcmp(defel->defname, "timeout") == 0)
80 {
81 char *timeout_str;
82 const char *hintmsg;
83 double result;
84
87 timeout_specified = true;
88
90
91 if (!parse_real(timeout_str, &result, GUC_UNIT_MS, &hintmsg))
92 {
95 errmsg("invalid timeout value: \"%s\"", timeout_str),
96 hintmsg ? errhint("%s", _(hintmsg)) : 0);
97 }
98
99 /*
100 * Get rid of any fractional part in the input. This is so we
101 * don't fail on just-out-of-range values that would round into
102 * range.
103 */
104 result = rint(result);
105
106 /* Range check */
107 if (unlikely(isnan(result) || !FLOAT8_FITS_IN_INT64(result)))
110 errmsg("timeout value is out of range"));
111
112 if (result < 0)
115 errmsg("timeout cannot be negative"));
116
117 timeout = (int64) result;
118 }
119 else if (strcmp(defel->defname, "no_throw") == 0)
120 {
123
124 no_throw_specified = true;
125
126 throw = !defGetBoolean(defel);
127 }
128 else
129 {
132 errmsg("option \"%s\" not recognized",
133 defel->defname),
134 parser_errposition(pstate, defel->location));
135 }
136 }
137
138 /*
139 * We are going to wait for the LSN. We should first care that we don't
140 * hold a snapshot and correspondingly our MyProc->xmin is invalid.
141 * Otherwise, our snapshot could prevent the replay of WAL records
142 * implying a kind of self-deadlock. This is the reason why WAIT FOR is a
143 * command, not a procedure or function.
144 *
145 * At first, we should check there is no active snapshot. According to
146 * PlannedStmtRequiresSnapshot(), even in an atomic context, CallStmt is
147 * processed with a snapshot. Thankfully, we can pop this snapshot,
148 * because PortalRunUtility() can tolerate this.
149 */
150 if (ActiveSnapshotSet())
152
153 /*
154 * At second, invalidate a catalog snapshot if any. And we should be done
155 * with the preparation.
156 */
158
159 /* Give up if there is still an active or registered snapshot. */
163 errmsg("WAIT FOR must be called without an active or registered snapshot"),
164 errdetail("WAIT FOR cannot be executed from a function or procedure, nor within a transaction with an isolation level higher than READ COMMITTED."));
165
166 /*
167 * As the result we should hold no snapshot, and correspondingly our xmin
168 * should be unset.
169 */
171
172 /*
173 * Validate that the requested mode matches the current server state.
174 * Primary modes can only be used on a primary.
175 */
176 if (lsnType == WAIT_LSN_TYPE_PRIMARY_FLUSH)
177 {
178 if (RecoveryInProgress())
181 errmsg("recovery is in progress"),
182 errhint("Waiting for primary_flush can only be done on a primary server. "
183 "Use standby_flush mode on a standby server.")));
184 }
185
186 /* Now wait for the LSN */
187 waitLSNResult = WaitForLSN(lsnType, lsn, timeout);
188
189 /*
190 * Process the result of WaitForLSN(). Throw appropriate error if needed.
191 */
192 switch (waitLSNResult)
193 {
195 /* Nothing to do on success */
196 result = "success";
197 break;
198
200 if (throw)
201 {
203
204 switch (lsnType)
205 {
209 errmsg("timed out while waiting for target LSN %X/%08X to be replayed; current standby_replay LSN %X/%08X",
210 LSN_FORMAT_ARGS(lsn),
212 break;
213
217 errmsg("timed out while waiting for target LSN %X/%08X to be written; current standby_write LSN %X/%08X",
218 LSN_FORMAT_ARGS(lsn),
220 break;
221
225 errmsg("timed out while waiting for target LSN %X/%08X to be flushed; current standby_flush LSN %X/%08X",
226 LSN_FORMAT_ARGS(lsn),
228 break;
229
233 errmsg("timed out while waiting for target LSN %X/%08X to be flushed; current primary_flush LSN %X/%08X",
234 LSN_FORMAT_ARGS(lsn),
236 break;
237
238 default:
239 elog(ERROR, "unexpected wait LSN type %d", lsnType);
240 }
241 }
242 else
243 result = "timeout";
244 break;
245
247 if (throw)
248 {
249 if (PromoteIsTriggered())
250 {
252
253 switch (lsnType)
254 {
258 errmsg("recovery is not in progress"),
259 errdetail("Recovery ended before target LSN %X/%08X was replayed; last standby_replay LSN %X/%08X.",
260 LSN_FORMAT_ARGS(lsn),
262 break;
263
267 errmsg("recovery is not in progress"),
268 errdetail("Recovery ended before target LSN %X/%08X was written; last standby_write LSN %X/%08X.",
269 LSN_FORMAT_ARGS(lsn),
271 break;
272
276 errmsg("recovery is not in progress"),
277 errdetail("Recovery ended before target LSN %X/%08X was flushed; last standby_flush LSN %X/%08X.",
278 LSN_FORMAT_ARGS(lsn),
280 break;
281
282 default:
283 elog(ERROR, "unexpected wait LSN type %d", lsnType);
284 }
285 }
286 else
287 {
288 switch (lsnType)
289 {
293 errmsg("recovery is not in progress"),
294 errhint("Waiting for the standby_replay LSN can only be executed during recovery."));
295 break;
296
300 errmsg("recovery is not in progress"),
301 errhint("Waiting for the standby_write LSN can only be executed during recovery."));
302 break;
303
307 errmsg("recovery is not in progress"),
308 errhint("Waiting for the standby_flush LSN can only be executed during recovery."));
309 break;
310
311 default:
312 elog(ERROR, "unexpected wait LSN type %d", lsnType);
313 }
314 }
315 }
316 else
317 result = "not in recovery";
318 break;
319 }
320
321 /* need a tuple descriptor representing a single TEXT column */
322 tupdesc = WaitStmtResultDesc(stmt);
323
324 /* prepare for projection of tuples */
326
327 /* Send it */
329
331}
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
#define FLOAT8_FITS_IN_INT64(num)
Definition c.h:1181
#define unlikely(x)
Definition c.h:432
char * defGetString(DefElem *def)
Definition define.c:34
bool defGetBoolean(DefElem *def)
Definition define.c:93
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition define.c:370
int errcode(int sqlerrcode)
Definition elog.c:874
#define _(x)
Definition elog.c:95
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
const TupleTableSlotOps TTSOpsVirtual
Definition execTuples.c:84
void end_tup_output(TupOutputState *tstate)
TupOutputState * begin_tup_output_tupdesc(DestReceiver *dest, TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
#define do_text_output_oneline(tstate, str_to_emit)
Definition executor.h:632
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition guc.c:2865
#define GUC_UNIT_MS
Definition guc.h:239
#define stmt
static char * errmsg
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
#define foreach_node(type, var, lst)
Definition pg_list.h:496
Datum pg_lsn_in(PG_FUNCTION_ARGS)
Definition pg_lsn.c:64
static XLogRecPtr DatumGetLSN(Datum X)
Definition pg_lsn.h:25
int pg_strcasecmp(const char *s1, const char *s2)
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
static int fb(int x)
bool ActiveSnapshotSet(void)
Definition snapmgr.c:812
bool HaveRegisteredOrActiveSnapshot(void)
Definition snapmgr.c:1644
void PopActiveSnapshot(void)
Definition snapmgr.c:775
void InvalidateCatalogSnapshot(void)
Definition snapmgr.c:455
PGPROC * MyProc
Definition proc.c:68
TransactionId xmin
Definition proc.h:239
#define InvalidTransactionId
Definition transam.h:31
TupleDesc WaitStmtResultDesc(WaitStmt *stmt)
Definition wait.c:334
bool RecoveryInProgress(void)
Definition xlog.c:6444
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint64 XLogRecPtr
Definition xlogdefs.h:21
bool PromoteIsTriggered(void)
XLogRecPtr GetCurrentLSNForWaitType(WaitLSNType lsnType)
Definition xlogwait.c:89
WaitLSNResult WaitForLSN(WaitLSNType lsnType, XLogRecPtr targetLSN, int64 timeout)
Definition xlogwait.c:376
WaitLSNResult
Definition xlogwait.h:26
@ WAIT_LSN_RESULT_NOT_IN_RECOVERY
Definition xlogwait.h:28
@ WAIT_LSN_RESULT_TIMEOUT
Definition xlogwait.h:30
@ WAIT_LSN_RESULT_SUCCESS
Definition xlogwait.h:27
WaitLSNType
Definition xlogwait.h:37
@ WAIT_LSN_TYPE_PRIMARY_FLUSH
Definition xlogwait.h:44
@ WAIT_LSN_TYPE_STANDBY_REPLAY
Definition xlogwait.h:39
@ WAIT_LSN_TYPE_STANDBY_FLUSH
Definition xlogwait.h:41
@ WAIT_LSN_TYPE_STANDBY_WRITE
Definition xlogwait.h:40

References _, ActiveSnapshotSet(), Assert, begin_tup_output_tupdesc(), CStringGetDatum(), DatumGetLSN(), defGetBoolean(), defGetString(), DirectFunctionCall1, do_text_output_oneline, elog, end_tup_output(), ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, errorConflictingDefElem(), fb(), FLOAT8_FITS_IN_INT64, foreach_node, GetCurrentLSNForWaitType(), GUC_UNIT_MS, HaveRegisteredOrActiveSnapshot(), InvalidateCatalogSnapshot(), InvalidTransactionId, LSN_FORMAT_ARGS, MyProc, parse_real(), parser_errposition(), pg_lsn_in(), pg_strcasecmp(), PopActiveSnapshot(), PromoteIsTriggered(), RecoveryInProgress(), stmt, TTSOpsVirtual, unlikely, WAIT_LSN_RESULT_NOT_IN_RECOVERY, WAIT_LSN_RESULT_SUCCESS, WAIT_LSN_RESULT_TIMEOUT, WAIT_LSN_TYPE_PRIMARY_FLUSH, WAIT_LSN_TYPE_STANDBY_FLUSH, WAIT_LSN_TYPE_STANDBY_REPLAY, WAIT_LSN_TYPE_STANDBY_WRITE, WaitForLSN(), WaitStmtResultDesc(), and PGPROC::xmin.

Referenced by standard_ProcessUtility().

◆ WaitStmtResultDesc()

TupleDesc WaitStmtResultDesc ( WaitStmt stmt)

Definition at line 334 of file wait.c.

335{
336 TupleDesc tupdesc;
337
338 /* Need a tuple descriptor representing a single TEXT column */
339 tupdesc = CreateTemplateTupleDesc(1);
340 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "status",
341 TEXTOID, -1, 0);
342 TupleDescFinalize(tupdesc);
343 return tupdesc;
344}
int16 AttrNumber
Definition attnum.h:21
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:508
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:897

References CreateTemplateTupleDesc(), fb(), TupleDescFinalize(), and TupleDescInitEntry().

Referenced by ExecWaitStmt(), and UtilityTupleDescriptor().