PostgreSQL Source Code git master
Loading...
Searching...
No Matches
wait.h File Reference
#include "nodes/parsenodes.h"
#include "parser/parse_node.h"
#include "tcop/dest.h"
Include dependency graph for wait.h:
This graph shows which files directly or indirectly include this file:

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 
)
extern

Definition at line 33 of file wait.c.

34{
35 XLogRecPtr lsn;
36 int64 timeout = 0;
38 WaitLSNType lsnType = WAIT_LSN_TYPE_STANDBY_REPLAY; /* default */
39 bool throw = true;
40 TupleDesc tupdesc;
42 const char *result = "<unset>";
43 bool timeout_specified = false;
44 bool no_throw_specified = false;
45 bool mode_specified = false;
46
47 /* Parse and validate the mandatory LSN */
49 CStringGetDatum(stmt->lsn_literal)));
50
51 foreach_node(DefElem, defel, stmt->options)
52 {
53 if (strcmp(defel->defname, "mode") == 0)
54 {
55 char *mode_str;
56
59 mode_specified = true;
60
62
63 if (pg_strcasecmp(mode_str, "standby_replay") == 0)
65 else if (pg_strcasecmp(mode_str, "standby_write") == 0)
67 else if (pg_strcasecmp(mode_str, "standby_flush") == 0)
69 else if (pg_strcasecmp(mode_str, "primary_flush") == 0)
71 else
74 errmsg("unrecognized value for %s option \"%s\": \"%s\"",
75 "WAIT", defel->defname, mode_str),
76 parser_errposition(pstate, defel->location)));
77 }
78 else if (strcmp(defel->defname, "timeout") == 0)
79 {
80 char *timeout_str;
81 const char *hintmsg;
82 double result;
83
86 timeout_specified = true;
87
89
90 if (!parse_real(timeout_str, &result, GUC_UNIT_MS, &hintmsg))
91 {
94 errmsg("invalid timeout value: \"%s\"", timeout_str),
95 hintmsg ? errhint("%s", _(hintmsg)) : 0);
96 }
97
98 /*
99 * Get rid of any fractional part in the input. This is so we
100 * don't fail on just-out-of-range values that would round into
101 * range.
102 */
103 result = rint(result);
104
105 /* Range check */
106 if (unlikely(isnan(result) || !FLOAT8_FITS_IN_INT64(result)))
109 errmsg("timeout value is out of range"));
110
111 if (result < 0)
114 errmsg("timeout cannot be negative"));
115
116 timeout = (int64) result;
117 }
118 else if (strcmp(defel->defname, "no_throw") == 0)
119 {
122
123 no_throw_specified = true;
124
125 throw = !defGetBoolean(defel);
126 }
127 else
128 {
131 errmsg("option \"%s\" not recognized",
132 defel->defname),
133 parser_errposition(pstate, defel->location));
134 }
135 }
136
137 /*
138 * We are going to wait for the LSN. We should first care that we don't
139 * hold a snapshot and correspondingly our MyProc->xmin is invalid.
140 * Otherwise, our snapshot could prevent the replay of WAL records
141 * implying a kind of self-deadlock. This is the reason why WAIT FOR is a
142 * command, not a procedure or function.
143 *
144 * At first, we should check there is no active snapshot. According to
145 * PlannedStmtRequiresSnapshot(), even in an atomic context, CallStmt is
146 * processed with a snapshot. Thankfully, we can pop this snapshot,
147 * because PortalRunUtility() can tolerate this.
148 */
149 if (ActiveSnapshotSet())
151
152 /*
153 * At second, invalidate a catalog snapshot if any. And we should be done
154 * with the preparation.
155 */
157
158 /* Give up if there is still an active or registered snapshot. */
162 errmsg("WAIT FOR must be called without an active or registered snapshot"),
163 errdetail("WAIT FOR cannot be executed from a function or procedure, nor within a transaction with an isolation level higher than READ COMMITTED."));
164
165 /*
166 * As the result we should hold no snapshot, and correspondingly our xmin
167 * should be unset.
168 */
170
171 /*
172 * Validate that the requested mode matches the current server state.
173 * Primary modes can only be used on a primary.
174 */
175 if (lsnType == WAIT_LSN_TYPE_PRIMARY_FLUSH)
176 {
177 if (RecoveryInProgress())
180 errmsg("recovery is in progress"),
181 errhint("Waiting for primary_flush can only be done on a primary server. "
182 "Use standby_flush mode on a standby server.")));
183 }
184
185 /* Now wait for the LSN */
186 waitLSNResult = WaitForLSN(lsnType, lsn, timeout);
187
188 /*
189 * Process the result of WaitForLSN(). Throw appropriate error if needed.
190 */
191 switch (waitLSNResult)
192 {
194 /* Nothing to do on success */
195 result = "success";
196 break;
197
199 if (throw)
200 {
202
203 switch (lsnType)
204 {
208 errmsg("timed out while waiting for target LSN %X/%08X to be replayed; current standby_replay LSN %X/%08X",
209 LSN_FORMAT_ARGS(lsn),
211 break;
212
216 errmsg("timed out while waiting for target LSN %X/%08X to be written; current standby_write LSN %X/%08X",
217 LSN_FORMAT_ARGS(lsn),
219 break;
220
224 errmsg("timed out while waiting for target LSN %X/%08X to be flushed; current standby_flush LSN %X/%08X",
225 LSN_FORMAT_ARGS(lsn),
227 break;
228
232 errmsg("timed out while waiting for target LSN %X/%08X to be flushed; current primary_flush LSN %X/%08X",
233 LSN_FORMAT_ARGS(lsn),
235 break;
236
237 default:
238 elog(ERROR, "unexpected wait LSN type %d", lsnType);
239 }
240 }
241 else
242 result = "timeout";
243 break;
244
246 if (throw)
247 {
248 if (PromoteIsTriggered())
249 {
251
252 switch (lsnType)
253 {
257 errmsg("recovery is not in progress"),
258 errdetail("Recovery ended before target LSN %X/%08X was replayed; last standby_replay LSN %X/%08X.",
259 LSN_FORMAT_ARGS(lsn),
261 break;
262
266 errmsg("recovery is not in progress"),
267 errdetail("Recovery ended before target LSN %X/%08X was written; last standby_write LSN %X/%08X.",
268 LSN_FORMAT_ARGS(lsn),
270 break;
271
275 errmsg("recovery is not in progress"),
276 errdetail("Recovery ended before target LSN %X/%08X was flushed; last standby_flush LSN %X/%08X.",
277 LSN_FORMAT_ARGS(lsn),
279 break;
280
281 default:
282 elog(ERROR, "unexpected wait LSN type %d", lsnType);
283 }
284 }
285 else
286 {
287 switch (lsnType)
288 {
292 errmsg("recovery is not in progress"),
293 errhint("Waiting for the standby_replay LSN can only be executed during recovery."));
294 break;
295
299 errmsg("recovery is not in progress"),
300 errhint("Waiting for the standby_write LSN can only be executed during recovery."));
301 break;
302
306 errmsg("recovery is not in progress"),
307 errhint("Waiting for the standby_flush LSN can only be executed during recovery."));
308 break;
309
310 default:
311 elog(ERROR, "unexpected wait LSN type %d", lsnType);
312 }
313 }
314 }
315 else
316 result = "not in recovery";
317 break;
318 }
319
320 /* need a tuple descriptor representing a single TEXT column */
321 tupdesc = WaitStmtResultDesc(stmt);
322
323 /* prepare for projection of tuples */
325
326 /* Send it */
328
330}
#define Assert(condition)
Definition c.h:873
int64_t int64
Definition c.h:543
#define FLOAT8_FITS_IN_INT64(num)
Definition c.h:1085
#define unlikely(x)
Definition c.h:412
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 errdetail(const char *fmt,...)
Definition elog.c:1216
int errhint(const char *fmt,...)
Definition elog.c:1330
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define _(x)
Definition elog.c:91
#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:628
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition guc.c:2833
#define GUC_UNIT_MS
Definition guc.h:239
#define stmt
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:380
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:67
TransactionId xmin
Definition proc.h:194
#define InvalidTransactionId
Definition transam.h:31
TupleDesc WaitStmtResultDesc(WaitStmt *stmt)
Definition wait.c:333
bool RecoveryInProgress(void)
Definition xlog.c:6460
#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:88
WaitLSNResult WaitForLSN(WaitLSNType lsnType, XLogRecPtr targetLSN, int64 timeout)
Definition xlogwait.c:375
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)
extern

Definition at line 333 of file wait.c.

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

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

Referenced by ExecWaitStmt(), and UtilityTupleDescriptor().