Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * xlogreader.c
4 : : * Generic XLog reading facility
5 : : *
6 : : * Portions Copyright (c) 2013-2025, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/backend/access/transam/xlogreader.c
10 : : *
11 : : * NOTES
12 : : * See xlogreader.h for more notes on this facility.
13 : : *
14 : : * This file is compiled as both front-end and backend code, so it
15 : : * may not use ereport, server-defined static variables, etc.
16 : : *-------------------------------------------------------------------------
17 : : */
18 : : #include "postgres.h"
19 : :
20 : : #include <unistd.h>
21 : : #ifdef USE_LZ4
22 : : #include <lz4.h>
23 : : #endif
24 : : #ifdef USE_ZSTD
25 : : #include <zstd.h>
26 : : #endif
27 : :
28 : : #include "access/transam.h"
29 : : #include "access/xlog_internal.h"
30 : : #include "access/xlogreader.h"
31 : : #include "access/xlogrecord.h"
32 : : #include "catalog/pg_control.h"
33 : : #include "common/pg_lzcompress.h"
34 : : #include "replication/origin.h"
35 : :
36 : : #ifndef FRONTEND
37 : : #include "pgstat.h"
38 : : #include "storage/bufmgr.h"
39 : : #else
40 : : #include "common/logging.h"
41 : : #endif
42 : :
43 : : static void report_invalid_record(XLogReaderState *state, const char *fmt,...)
44 : : pg_attribute_printf(2, 3);
45 : : static void allocate_recordbuf(XLogReaderState *state, uint32 reclength);
46 : : static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr,
47 : : int reqLen);
48 : : static void XLogReaderInvalReadState(XLogReaderState *state);
49 : : static XLogPageReadResult XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking);
50 : : static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
51 : : XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess);
52 : : static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record,
53 : : XLogRecPtr recptr);
54 : : static void ResetDecoder(XLogReaderState *state);
55 : : static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt,
56 : : int segsize, const char *waldir);
57 : :
58 : : /* size of the buffer allocated for error message. */
59 : : #define MAX_ERRORMSG_LEN 1000
60 : :
61 : : /*
62 : : * Default size; large enough that typical users of XLogReader won't often need
63 : : * to use the 'oversized' memory allocation code path.
64 : : */
65 : : #define DEFAULT_DECODE_BUFFER_SIZE (64 * 1024)
66 : :
67 : : /*
68 : : * Construct a string in state->errormsg_buf explaining what's wrong with
69 : : * the current record being read.
70 : : */
71 : : static void
4616 alvherre@alvh.no-ip. 72 :CBC 240 : report_invalid_record(XLogReaderState *state, const char *fmt,...)
73 : : {
74 : : va_list args;
75 : :
76 : 240 : fmt = _(fmt);
77 : :
78 : 240 : va_start(args, fmt);
79 : 240 : vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
80 : 240 : va_end(args);
81 : :
1268 tmunro@postgresql.or 82 : 240 : state->errormsg_deferred = true;
83 : 240 : }
84 : :
85 : : /*
86 : : * Set the size of the decoding buffer. A pointer to a caller supplied memory
87 : : * region may also be passed in, in which case non-oversized records will be
88 : : * decoded there.
89 : : */
90 : : void
91 : 887 : XLogReaderSetDecodeBuffer(XLogReaderState *state, void *buffer, size_t size)
92 : : {
93 [ - + ]: 887 : Assert(state->decode_buffer == NULL);
94 : :
95 : 887 : state->decode_buffer = buffer;
96 : 887 : state->decode_buffer_size = size;
97 : 887 : state->decode_buffer_tail = buffer;
98 : 887 : state->decode_buffer_head = buffer;
4616 alvherre@alvh.no-ip. 99 : 887 : }
100 : :
101 : : /*
102 : : * Allocate and initialize a new XLogReader.
103 : : *
104 : : * Returns NULL if the xlogreader couldn't be allocated.
105 : : */
106 : : XLogReaderState *
2174 107 : 2688 : XLogReaderAllocate(int wal_segment_size, const char *waldir,
108 : : XLogReaderRoutine *routine, void *private_data)
109 : : {
110 : : XLogReaderState *state;
111 : :
112 : : state = (XLogReaderState *)
3809 fujii@postgresql.org 113 : 2688 : palloc_extended(sizeof(XLogReaderState),
114 : : MCXT_ALLOC_NO_OOM | MCXT_ALLOC_ZERO);
115 [ - + ]: 2688 : if (!state)
3809 fujii@postgresql.org 116 :UBC 0 : return NULL;
117 : :
118 : : /* initialize caller-provided support functions */
1580 tmunro@postgresql.or 119 :CBC 2688 : state->routine = *routine;
120 : :
121 : : /*
122 : : * Permanently allocate readBuf. We do it this way, rather than just
123 : : * making a static array, for two reasons: (1) no need to waste the
124 : : * storage in most instantiations of the backend; (2) a static char array
125 : : * isn't guaranteed to have any particular alignment, whereas
126 : : * palloc_extended() will provide MAXALIGN'd storage.
127 : : */
3809 fujii@postgresql.org 128 : 2688 : state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
129 : : MCXT_ALLOC_NO_OOM);
130 [ - + ]: 2688 : if (!state->readBuf)
131 : : {
3809 fujii@postgresql.org 132 :UBC 0 : pfree(state);
133 : 0 : return NULL;
134 : : }
135 : :
136 : : /* Initialize segment info. */
2174 alvherre@alvh.no-ip. 137 :CBC 2688 : WALOpenSegmentInit(&state->seg, &state->segcxt, wal_segment_size,
138 : : waldir);
139 : :
140 : : /* system_identifier initialized to zeroes above */
1580 tmunro@postgresql.or 141 : 2688 : state->private_data = private_data;
142 : : /* ReadRecPtr, EndRecPtr and readLen initialized to zeroes above */
3809 fujii@postgresql.org 143 : 2688 : state->errormsg_buf = palloc_extended(MAX_ERRORMSG_LEN + 1,
144 : : MCXT_ALLOC_NO_OOM);
145 [ - + ]: 2688 : if (!state->errormsg_buf)
146 : : {
3809 fujii@postgresql.org 147 :UBC 0 : pfree(state->readBuf);
148 : 0 : pfree(state);
149 : 0 : return NULL;
150 : : }
4616 alvherre@alvh.no-ip. 151 :CBC 2688 : state->errormsg_buf[0] = '\0';
152 : :
153 : : /*
154 : : * Allocate an initial readRecordBuf of minimal size, which can later be
155 : : * enlarged if necessary.
156 : : */
704 michael@paquier.xyz 157 : 2688 : allocate_recordbuf(state, 0);
4616 alvherre@alvh.no-ip. 158 : 2688 : return state;
159 : : }
160 : :
161 : : void
162 : 2171 : XLogReaderFree(XLogReaderState *state)
163 : : {
1580 tmunro@postgresql.or 164 [ + + ]: 2171 : if (state->seg.ws_file != -1)
165 : 1257 : state->routine.segment_close(state);
166 : :
1268 167 [ + + + - ]: 2171 : if (state->decode_buffer && state->free_decode_buffer)
168 : 2131 : pfree(state->decode_buffer);
169 : :
3943 heikki.linnakangas@i 170 : 2171 : pfree(state->errormsg_buf);
4616 alvherre@alvh.no-ip. 171 [ + - ]: 2171 : if (state->readRecordBuf)
3943 heikki.linnakangas@i 172 : 2171 : pfree(state->readRecordBuf);
173 : 2171 : pfree(state->readBuf);
174 : 2171 : pfree(state);
4616 alvherre@alvh.no-ip. 175 : 2171 : }
176 : :
177 : : /*
178 : : * Allocate readRecordBuf to fit a record of at least the given length.
179 : : *
180 : : * readRecordBufSize is set to the new buffer size.
181 : : *
182 : : * To avoid useless small increases, round its size to a multiple of
183 : : * XLOG_BLCKSZ, and make sure it's at least 5*Max(BLCKSZ, XLOG_BLCKSZ) to start
184 : : * with. (That is enough for all "normal" records, but very large commit or
185 : : * abort records might need more space.)
186 : : *
187 : : * Note: This routine should *never* be called for xl_tot_len until the header
188 : : * of the record has been fully validated.
189 : : */
190 : : static void
191 : 2720 : allocate_recordbuf(XLogReaderState *state, uint32 reclength)
192 : : {
193 : 2720 : uint32 newSize = reclength;
194 : :
195 : 2720 : newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
196 : 2720 : newSize = Max(newSize, 5 * Max(BLCKSZ, XLOG_BLCKSZ));
197 : :
198 [ + + ]: 2720 : if (state->readRecordBuf)
3943 heikki.linnakangas@i 199 : 32 : pfree(state->readRecordBuf);
704 michael@paquier.xyz 200 : 2720 : state->readRecordBuf = (char *) palloc(newSize);
4616 alvherre@alvh.no-ip. 201 : 2720 : state->readRecordBufSize = newSize;
202 : 2720 : }
203 : :
204 : : /*
205 : : * Initialize the passed segment structs.
206 : : */
207 : : static void
2174 208 : 2688 : WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt,
209 : : int segsize, const char *waldir)
210 : : {
211 : 2688 : seg->ws_file = -1;
212 : 2688 : seg->ws_segno = 0;
213 : 2688 : seg->ws_tli = 0;
214 : :
215 : 2688 : segcxt->ws_segsize = segsize;
216 [ + + ]: 2688 : if (waldir)
217 : 109 : snprintf(segcxt->ws_dir, MAXPGPATH, "%s", waldir);
218 : 2688 : }
219 : :
220 : : /*
221 : : * Begin reading WAL at 'RecPtr'.
222 : : *
223 : : * 'RecPtr' should point to the beginning of a valid WAL record. Pointing at
224 : : * the beginning of a page is also OK, if there is a new record right after
225 : : * the page header, i.e. not a continuation.
226 : : *
227 : : * This does not make any attempt to read the WAL yet, and hence cannot fail.
228 : : * If the starting address is not correct, the first call to XLogReadRecord()
229 : : * will error out.
230 : : */
231 : : void
2050 heikki.linnakangas@i 232 : 6203 : XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
233 : : {
234 [ - + ]: 6203 : Assert(!XLogRecPtrIsInvalid(RecPtr));
235 : :
236 : 6203 : ResetDecoder(state);
237 : :
238 : : /* Begin at the passed-in record pointer. */
239 : 6203 : state->EndRecPtr = RecPtr;
1268 tmunro@postgresql.or 240 : 6203 : state->NextRecPtr = RecPtr;
2050 heikki.linnakangas@i 241 : 6203 : state->ReadRecPtr = InvalidXLogRecPtr;
1268 tmunro@postgresql.or 242 : 6203 : state->DecodeRecPtr = InvalidXLogRecPtr;
243 : 6203 : }
244 : :
245 : : /*
246 : : * Release the last record that was returned by XLogNextRecord(), if any, to
247 : : * free up space. Returns the LSN past the end of the record.
248 : : */
249 : : XLogRecPtr
250 : 10852707 : XLogReleasePreviousRecord(XLogReaderState *state)
251 : : {
252 : : DecodedXLogRecord *record;
253 : : XLogRecPtr next_lsn;
254 : :
255 [ + + ]: 10852707 : if (!state->record)
1094 256 : 5435814 : return InvalidXLogRecPtr;
257 : :
258 : : /*
259 : : * Remove it from the decoded record queue. It must be the oldest item
260 : : * decoded, decode_queue_head.
261 : : */
1268 262 : 5416893 : record = state->record;
1094 263 : 5416893 : next_lsn = record->next_lsn;
1268 264 [ - + ]: 5416893 : Assert(record == state->decode_queue_head);
265 : 5416893 : state->record = NULL;
266 : 5416893 : state->decode_queue_head = record->next;
267 : :
268 : : /* It might also be the newest item decoded, decode_queue_tail. */
269 [ + + ]: 5416893 : if (state->decode_queue_tail == record)
270 : 2710898 : state->decode_queue_tail = NULL;
271 : :
272 : : /* Release the space. */
273 [ + + ]: 5416893 : if (unlikely(record->oversized))
274 : : {
275 : : /* It's not in the decode buffer, so free it to release space. */
276 : 54 : pfree(record);
277 : : }
278 : : else
279 : : {
280 : : /* It must be the head (oldest) record in the decode buffer. */
281 [ - + ]: 5416839 : Assert(state->decode_buffer_head == (char *) record);
282 : :
283 : : /*
284 : : * We need to update head to point to the next record that is in the
285 : : * decode buffer, if any, being careful to skip oversized ones
286 : : * (they're not in the decode buffer).
287 : : */
288 : 5416839 : record = record->next;
289 [ + + - + : 5416839 : while (unlikely(record && record->oversized))
- + ]
1268 tmunro@postgresql.or 290 :UBC 0 : record = record->next;
291 : :
1268 tmunro@postgresql.or 292 [ + + ]:CBC 5416839 : if (record)
293 : : {
294 : : /* Adjust head to release space up to the next record. */
295 : 2705995 : state->decode_buffer_head = (char *) record;
296 : : }
297 : : else
298 : : {
299 : : /*
300 : : * Otherwise we might as well just reset head and tail to the
301 : : * start of the buffer space, because we're empty. This means
302 : : * we'll keep overwriting the same piece of memory if we're not
303 : : * doing any prefetching.
304 : : */
305 : 2710844 : state->decode_buffer_head = state->decode_buffer;
306 : 2710844 : state->decode_buffer_tail = state->decode_buffer;
307 : : }
308 : : }
309 : :
1094 310 : 5416893 : return next_lsn;
311 : : }
312 : :
313 : : /*
314 : : * Attempt to read an XLOG record.
315 : : *
316 : : * XLogBeginRead() or XLogFindNextRecord() and then XLogReadAhead() must be
317 : : * called before the first call to XLogNextRecord(). This functions returns
318 : : * records and errors that were put into an internal queue by XLogReadAhead().
319 : : *
320 : : * On success, a record is returned.
321 : : *
322 : : * The returned record (or *errormsg) points to an internal buffer that's
323 : : * valid until the next call to XLogNextRecord.
324 : : */
325 : : DecodedXLogRecord *
1268 326 : 5426235 : XLogNextRecord(XLogReaderState *state, char **errormsg)
327 : : {
328 : : /* Release the last record returned by XLogNextRecord(). */
329 : 5426235 : XLogReleasePreviousRecord(state);
330 : :
331 [ + + ]: 5426235 : if (state->decode_queue_head == NULL)
332 : : {
333 : 3790 : *errormsg = NULL;
334 [ + + ]: 3790 : if (state->errormsg_deferred)
335 : : {
336 [ + + ]: 239 : if (state->errormsg_buf[0] != '\0')
337 : 236 : *errormsg = state->errormsg_buf;
338 : 239 : state->errormsg_deferred = false;
339 : : }
340 : :
341 : : /*
342 : : * state->EndRecPtr is expected to have been set by the last call to
343 : : * XLogBeginRead() or XLogNextRecord(), and is the location of the
344 : : * error.
345 : : */
346 [ - + ]: 3790 : Assert(!XLogRecPtrIsInvalid(state->EndRecPtr));
347 : :
348 : 3790 : return NULL;
349 : : }
350 : :
351 : : /*
352 : : * Record this as the most recent record returned, so that we'll release
353 : : * it next time. This also exposes it to the traditional
354 : : * XLogRecXXX(xlogreader) macros, which work with the decoder rather than
355 : : * the record for historical reasons.
356 : : */
357 : 5422445 : state->record = state->decode_queue_head;
358 : :
359 : : /*
360 : : * Update the pointers to the beginning and one-past-the-end of this
361 : : * record, again for the benefit of historical code that expected the
362 : : * decoder to track this rather than accessing these fields of the record
363 : : * itself.
364 : : */
365 : 5422445 : state->ReadRecPtr = state->record->lsn;
366 : 5422445 : state->EndRecPtr = state->record->next_lsn;
367 : :
368 : 5422445 : *errormsg = NULL;
369 : :
370 : 5422445 : return state->record;
371 : : }
372 : :
373 : : /*
374 : : * Attempt to read an XLOG record.
375 : : *
376 : : * XLogBeginRead() or XLogFindNextRecord() must be called before the first call
377 : : * to XLogReadRecord().
378 : : *
379 : : * If the page_read callback fails to read the requested data, NULL is
380 : : * returned. The callback is expected to have reported the error; errormsg
381 : : * is set to NULL.
382 : : *
383 : : * If the reading fails for some other reason, NULL is also returned, and
384 : : * *errormsg is set to a string with details of the failure.
385 : : *
386 : : * The returned pointer (or *errormsg) points to an internal buffer that's
387 : : * valid until the next call to XLogReadRecord.
388 : : */
389 : : XLogRecord *
1580 390 : 2688625 : XLogReadRecord(XLogReaderState *state, char **errormsg)
391 : : {
392 : : DecodedXLogRecord *decoded;
393 : :
394 : : /*
395 : : * Release last returned record, if there is one. We need to do this so
396 : : * that we can check for empty decode queue accurately.
397 : : */
1268 398 : 2688625 : XLogReleasePreviousRecord(state);
399 : :
400 : : /*
401 : : * Call XLogReadAhead() in blocking mode to make sure there is something
402 : : * in the queue, though we don't use the result.
403 : : */
404 [ + - ]: 2688625 : if (!XLogReaderHasQueuedRecordOrError(state))
405 : 2688625 : XLogReadAhead(state, false /* nonblocking */ );
406 : :
407 : : /* Consume the head record or error. */
408 : 2688440 : decoded = XLogNextRecord(state, errormsg);
409 [ + + ]: 2688440 : if (decoded)
410 : : {
411 : : /*
412 : : * This function returns a pointer to the record's header, not the
413 : : * actual decoded record. The caller will access the decoded record
414 : : * through the XLogRecGetXXX() macros, which reach the decoded
415 : : * recorded as xlogreader->record.
416 : : */
417 [ - + ]: 2684904 : Assert(state->record == decoded);
418 : 2684904 : return &decoded->header;
419 : : }
420 : :
421 : 3536 : return NULL;
422 : : }
423 : :
424 : : /*
425 : : * Allocate space for a decoded record. The only member of the returned
426 : : * object that is initialized is the 'oversized' flag, indicating that the
427 : : * decoded record wouldn't fit in the decode buffer and must eventually be
428 : : * freed explicitly.
429 : : *
430 : : * The caller is responsible for adjusting decode_buffer_tail with the real
431 : : * size after successfully decoding a record into this space. This way, if
432 : : * decoding fails, then there is nothing to undo unless the 'oversized' flag
433 : : * was set and pfree() must be called.
434 : : *
435 : : * Return NULL if there is no space in the decode buffer and allow_oversized
436 : : * is false, or if memory allocation fails for an oversized buffer.
437 : : */
438 : : static DecodedXLogRecord *
439 : 5429168 : XLogReadRecordAlloc(XLogReaderState *state, size_t xl_tot_len, bool allow_oversized)
440 : : {
441 : 5429168 : size_t required_space = DecodeXLogRecordRequiredSpace(xl_tot_len);
442 : 5429168 : DecodedXLogRecord *decoded = NULL;
443 : :
444 : : /* Allocate a circular decode buffer if we don't have one already. */
445 [ + + ]: 5429168 : if (unlikely(state->decode_buffer == NULL))
446 : : {
447 [ + + ]: 2395 : if (state->decode_buffer_size == 0)
448 : 1508 : state->decode_buffer_size = DEFAULT_DECODE_BUFFER_SIZE;
449 : 2395 : state->decode_buffer = palloc(state->decode_buffer_size);
450 : 2395 : state->decode_buffer_head = state->decode_buffer;
451 : 2395 : state->decode_buffer_tail = state->decode_buffer;
452 : 2395 : state->free_decode_buffer = true;
453 : : }
454 : :
455 : : /* Try to allocate space in the circular decode buffer. */
456 [ + + ]: 5429168 : if (state->decode_buffer_tail >= state->decode_buffer_head)
457 : : {
458 : : /* Empty, or tail is to the right of head. */
638 459 : 4548356 : if (required_space <=
460 : 4548356 : state->decode_buffer_size -
461 [ + + ]: 4548356 : (state->decode_buffer_tail - state->decode_buffer))
462 : : {
463 : : /*-
464 : : * There is space between tail and end.
465 : : *
466 : : * +-----+--------------------+-----+
467 : : * | |////////////////////|here!|
468 : : * +-----+--------------------+-----+
469 : : * ^ ^
470 : : * | |
471 : : * h t
472 : : */
1268 473 : 4529675 : decoded = (DecodedXLogRecord *) state->decode_buffer_tail;
474 : 4529675 : decoded->oversized = false;
475 : 4529675 : return decoded;
476 : : }
638 477 : 18681 : else if (required_space <
478 [ + + ]: 18681 : state->decode_buffer_head - state->decode_buffer)
479 : : {
480 : : /*-
481 : : * There is space between start and head.
482 : : *
483 : : * +-----+--------------------+-----+
484 : : * |here!|////////////////////| |
485 : : * +-----+--------------------+-----+
486 : : * ^ ^
487 : : * | |
488 : : * h t
489 : : */
1268 490 : 16096 : decoded = (DecodedXLogRecord *) state->decode_buffer;
491 : 16096 : decoded->oversized = false;
492 : 16096 : return decoded;
493 : : }
494 : : }
495 : : else
496 : : {
497 : : /* Tail is to the left of head. */
638 498 : 880812 : if (required_space <
499 [ + + ]: 880812 : state->decode_buffer_head - state->decode_buffer_tail)
500 : : {
501 : : /*-
502 : : * There is space between tail and head.
503 : : *
504 : : * +-----+--------------------+-----+
505 : : * |/////|here! |/////|
506 : : * +-----+--------------------+-----+
507 : : * ^ ^
508 : : * | |
509 : : * t h
510 : : */
1268 511 : 880707 : decoded = (DecodedXLogRecord *) state->decode_buffer_tail;
512 : 880707 : decoded->oversized = false;
513 : 880707 : return decoded;
514 : : }
515 : : }
516 : :
517 : : /* Not enough space in the decode buffer. Are we allowed to allocate? */
518 [ + + ]: 2690 : if (allow_oversized)
519 : : {
704 michael@paquier.xyz 520 : 54 : decoded = palloc(required_space);
1268 tmunro@postgresql.or 521 : 54 : decoded->oversized = true;
522 : 54 : return decoded;
523 : : }
524 : :
525 : 2636 : return NULL;
526 : : }
527 : :
528 : : static XLogPageReadResult
529 : 5441304 : XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
530 : : {
531 : : XLogRecPtr RecPtr;
532 : : XLogRecord *record;
533 : : XLogRecPtr targetPagePtr;
534 : : bool randAccess;
535 : : uint32 len,
536 : : total_len;
537 : : uint32 targetRecOff;
538 : : uint32 pageHeaderSize;
539 : : bool assembled;
540 : : bool gotheader;
541 : : int readOff;
542 : : DecodedXLogRecord *decoded;
543 : : char *errormsg; /* not used */
544 : :
545 : : /*
546 : : * randAccess indicates whether to verify the previous-record pointer of
547 : : * the record we're reading. We only do this if we're reading
548 : : * sequentially, which is what we initially assume.
549 : : */
1580 550 : 5441304 : randAccess = false;
551 : :
552 : : /* reset error state */
553 : 5441304 : state->errormsg_buf[0] = '\0';
1268 554 : 5441304 : decoded = NULL;
555 : :
1438 alvherre@alvh.no-ip. 556 : 5441304 : state->abortedRecPtr = InvalidXLogRecPtr;
557 : 5441304 : state->missingContrecPtr = InvalidXLogRecPtr;
558 : :
1268 tmunro@postgresql.or 559 : 5441304 : RecPtr = state->NextRecPtr;
560 : :
561 [ + + ]: 5441304 : if (state->DecodeRecPtr != InvalidXLogRecPtr)
562 : : {
563 : : /* read the record after the one we just read */
564 : :
565 : : /*
566 : : * NextRecPtr is pointing to end+1 of the previous WAL record. If
567 : : * we're at a page boundary, no more records can fit on the current
568 : : * page. We must skip over the page header, but we can't do that until
569 : : * we've read in the page, since the header size is variable.
570 : : */
571 : : }
572 : : else
573 : : {
574 : : /*
575 : : * Caller supplied a position to start at.
576 : : *
577 : : * In this case, NextRecPtr should already be pointing either to a
578 : : * valid record starting position or alternatively to the beginning of
579 : : * a page. See the header comments for XLogBeginRead.
580 : : */
1115 rhaas@postgresql.org 581 [ + - - + ]: 6170 : Assert(RecPtr % XLOG_BLCKSZ == 0 || XRecOffIsValid(RecPtr));
1580 tmunro@postgresql.or 582 : 6170 : randAccess = true;
583 : : }
584 : :
1438 alvherre@alvh.no-ip. 585 : 5441304 : restart:
1268 tmunro@postgresql.or 586 : 5441305 : state->nonblocking = nonblocking;
1580 587 : 5441305 : state->currRecPtr = RecPtr;
1438 alvherre@alvh.no-ip. 588 : 5441305 : assembled = false;
589 : :
1580 tmunro@postgresql.or 590 : 5441305 : targetPagePtr = RecPtr - (RecPtr % XLOG_BLCKSZ);
591 : 5441305 : targetRecOff = RecPtr % XLOG_BLCKSZ;
592 : :
593 : : /*
594 : : * Read the page containing the record into state->readBuf. Request enough
595 : : * byte to cover the whole record header, or at least the part of it that
596 : : * fits on the same page.
597 : : */
598 : 5441305 : readOff = ReadPageInternal(state, targetPagePtr,
599 : 5441305 : Min(targetRecOff + SizeOfXLogRecord, XLOG_BLCKSZ));
1268 600 [ + + ]: 5441069 : if (readOff == XLREAD_WOULDBLOCK)
601 : 8169 : return XLREAD_WOULDBLOCK;
602 [ + + ]: 5432900 : else if (readOff < 0)
1580 603 : 3569 : goto err;
604 : :
605 : : /*
606 : : * ReadPageInternal always returns at least the page header, so we can
607 : : * examine it now.
608 : : */
609 [ + + ]: 5429331 : pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
610 [ + + ]: 5429331 : if (targetRecOff == 0)
611 : : {
612 : : /*
613 : : * At page start, so skip over page header.
614 : : */
615 : 5493 : RecPtr += pageHeaderSize;
616 : 5493 : targetRecOff = pageHeaderSize;
617 : : }
618 [ - + ]: 5423838 : else if (targetRecOff < pageHeaderSize)
619 : : {
61 alvherre@kurilemu.de 620 :UNC 0 : report_invalid_record(state, "invalid record offset at %X/%08X: expected at least %u, got %u",
919 peter@eisentraut.org 621 :UBC 0 : LSN_FORMAT_ARGS(RecPtr),
622 : : pageHeaderSize, targetRecOff);
1580 tmunro@postgresql.or 623 : 0 : goto err;
624 : : }
625 : :
1580 tmunro@postgresql.or 626 [ + + - + ]:CBC 5429331 : if ((((XLogPageHeader) state->readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) &&
627 : : targetRecOff == pageHeaderSize)
628 : : {
61 alvherre@kurilemu.de 629 :UNC 0 : report_invalid_record(state, "contrecord is requested by %X/%08X",
1580 tmunro@postgresql.or 630 :UBC 0 : LSN_FORMAT_ARGS(RecPtr));
631 : 0 : goto err;
632 : : }
633 : :
634 : : /* ReadPageInternal has verified the page header */
1580 tmunro@postgresql.or 635 [ - + ]:CBC 5429331 : Assert(pageHeaderSize <= readOff);
636 : :
637 : : /*
638 : : * Read the record length.
639 : : *
640 : : * NB: Even though we use an XLogRecord pointer here, the whole record
641 : : * header might not fit on this page. xl_tot_len is the first field of the
642 : : * struct, so it must be on this page (the records are MAXALIGNed), but we
643 : : * cannot access any other fields until we've verified that we got the
644 : : * whole header.
645 : : */
646 : 5429331 : record = (XLogRecord *) (state->readBuf + RecPtr % XLOG_BLCKSZ);
647 : 5429331 : total_len = record->xl_tot_len;
648 : :
649 : : /*
650 : : * If the whole record header is on this page, validate it immediately.
651 : : * Otherwise do just a basic sanity check on xl_tot_len, and validate the
652 : : * rest of the header after reading it from the next page. The xl_tot_len
653 : : * check is necessary here to ensure that we enter the "Need to reassemble
654 : : * record" code path below; otherwise we might fail to apply
655 : : * ValidXLogRecordHeader at all.
656 : : */
657 [ + + ]: 5429331 : if (targetRecOff <= XLOG_BLCKSZ - SizeOfXLogRecord)
658 : : {
1268 659 [ + + ]: 5418596 : if (!ValidXLogRecordHeader(state, RecPtr, state->DecodeRecPtr, record,
660 : : randAccess))
1580 661 : 216 : goto err;
662 : 5418380 : gotheader = true;
663 : : }
664 : : else
665 : : {
666 : : /* There may be no next page if it's too small. */
711 667 [ + + ]: 10735 : if (total_len < SizeOfXLogRecord)
668 : : {
669 : 1 : report_invalid_record(state,
670 : : "invalid record length at %X/%08X: expected at least %u, got %u",
671 : 1 : LSN_FORMAT_ARGS(RecPtr),
672 : : (uint32) SizeOfXLogRecord, total_len);
673 : 1 : goto err;
674 : : }
675 : : /* We'll validate the header once we have the next page. */
1580 676 : 10734 : gotheader = false;
677 : : }
678 : :
679 : : /*
680 : : * Try to find space to decode this record, if we can do so without
681 : : * calling palloc. If we can't, we'll try again below after we've
682 : : * validated that total_len isn't garbage bytes from a recycled WAL page.
683 : : */
1268 684 : 5429114 : decoded = XLogReadRecordAlloc(state,
685 : : total_len,
686 : : false /* allow_oversized */ );
714 687 [ + + + + ]: 5429114 : if (decoded == NULL && nonblocking)
688 : : {
689 : : /*
690 : : * There is no space in the circular decode buffer, and the caller is
691 : : * only reading ahead. The caller should consume existing records to
692 : : * make space.
693 : : */
694 : 2570 : return XLREAD_WOULDBLOCK;
695 : : }
696 : :
1580 697 : 5426544 : len = XLOG_BLCKSZ - RecPtr % XLOG_BLCKSZ;
698 [ + + ]: 5426544 : if (total_len > len)
699 : : {
700 : : /* Need to reassemble record */
701 : : char *contdata;
702 : : XLogPageHeader pageHeader;
703 : : char *buffer;
704 : : uint32 gotlen;
705 : :
1438 alvherre@alvh.no-ip. 706 : 1423247 : assembled = true;
707 : :
708 : : /*
709 : : * We always have space for a couple of pages, enough to validate a
710 : : * boundary-spanning record header.
711 : : */
714 tmunro@postgresql.or 712 [ - + ]: 1423247 : Assert(state->readRecordBufSize >= XLOG_BLCKSZ * 2);
713 [ - + ]: 1423247 : Assert(state->readRecordBufSize >= len);
714 : :
715 : : /* Copy the first fragment of the record from the first page. */
1580 716 : 1423247 : memcpy(state->readRecordBuf,
717 : 1423247 : state->readBuf + RecPtr % XLOG_BLCKSZ, len);
718 : 1423247 : buffer = state->readRecordBuf + len;
719 : 1423247 : gotlen = len;
720 : :
721 : : do
722 : : {
723 : : /* Calculate pointer to beginning of next page */
724 : 1453601 : targetPagePtr += XLOG_BLCKSZ;
725 : :
726 : : /*
727 : : * Read the page header before processing the record data, so we
728 : : * can handle the case where the previous record ended as being a
729 : : * partial one.
730 : : */
49 akorotkov@postgresql 731 : 1453601 : readOff = ReadPageInternal(state, targetPagePtr, SizeOfXLogShortPHD);
1268 tmunro@postgresql.or 732 [ + + ]: 1453600 : if (readOff == XLREAD_WOULDBLOCK)
733 : 4004 : return XLREAD_WOULDBLOCK;
734 [ + + ]: 1449596 : else if (readOff < 0)
1580 735 : 18 : goto err;
736 : :
737 [ - + ]: 1449578 : Assert(SizeOfXLogShortPHD <= readOff);
738 : :
739 : 1449578 : pageHeader = (XLogPageHeader) state->readBuf;
740 : :
741 : : /*
742 : : * If we were expecting a continuation record and got an
743 : : * "overwrite contrecord" flag, that means the continuation record
744 : : * was overwritten with a different record. Restart the read by
745 : : * assuming the address to read is the location where we found
746 : : * this flag; but keep track of the LSN of the record we were
747 : : * reading, for later verification.
748 : : */
1438 alvherre@alvh.no-ip. 749 [ + + ]: 1449578 : if (pageHeader->xlp_info & XLP_FIRST_IS_OVERWRITE_CONTRECORD)
750 : : {
1380 751 : 1 : state->overwrittenRecPtr = RecPtr;
1438 752 : 1 : RecPtr = targetPagePtr;
753 : 1 : goto restart;
754 : : }
755 : :
756 : : /* Check that the continuation on next page looks valid */
1580 tmunro@postgresql.or 757 [ + + ]: 1449577 : if (!(pageHeader->xlp_info & XLP_FIRST_IS_CONTRECORD))
758 : : {
759 : 1 : report_invalid_record(state,
760 : : "there is no contrecord flag at %X/%08X",
761 : 1 : LSN_FORMAT_ARGS(RecPtr));
762 : 1 : goto err;
763 : : }
764 : :
765 : : /*
766 : : * Cross-check that xlp_rem_len agrees with how much of the record
767 : : * we expect there to be left.
768 : : */
769 [ + - ]: 1449576 : if (pageHeader->xlp_rem_len == 0 ||
770 [ + + ]: 1449576 : total_len != (pageHeader->xlp_rem_len + gotlen))
771 : : {
772 : 2 : report_invalid_record(state,
773 : : "invalid contrecord length %u (expected %lld) at %X/%08X",
774 : : pageHeader->xlp_rem_len,
775 : 2 : ((long long) total_len) - gotlen,
776 : 2 : LSN_FORMAT_ARGS(RecPtr));
777 : 2 : goto err;
778 : : }
779 : :
780 : : /* Wait for the next page to become available */
49 akorotkov@postgresql 781 : 1449574 : readOff = ReadPageInternal(state, targetPagePtr,
782 : 1449574 : Min(total_len - gotlen + SizeOfXLogShortPHD,
783 : : XLOG_BLCKSZ));
784 [ - + ]: 1449574 : if (readOff == XLREAD_WOULDBLOCK)
49 akorotkov@postgresql 785 :UBC 0 : return XLREAD_WOULDBLOCK;
49 akorotkov@postgresql 786 [ - + ]:CBC 1449574 : else if (readOff < 0)
49 akorotkov@postgresql 787 :UBC 0 : goto err;
788 : :
789 : : /* Append the continuation from this page to the buffer */
1580 tmunro@postgresql.or 790 [ + + ]:CBC 1449574 : pageHeaderSize = XLogPageHeaderSize(pageHeader);
791 : :
792 [ - + ]: 1449574 : if (readOff < pageHeaderSize)
1580 tmunro@postgresql.or 793 :UBC 0 : readOff = ReadPageInternal(state, targetPagePtr,
794 : : pageHeaderSize);
795 : :
1580 tmunro@postgresql.or 796 [ - + ]:CBC 1449574 : Assert(pageHeaderSize <= readOff);
797 : :
798 : 1449574 : contdata = (char *) state->readBuf + pageHeaderSize;
799 : 1449574 : len = XLOG_BLCKSZ - pageHeaderSize;
800 [ + + ]: 1449574 : if (pageHeader->xlp_rem_len < len)
801 : 1419206 : len = pageHeader->xlp_rem_len;
802 : :
803 [ - + ]: 1449574 : if (readOff < pageHeaderSize + len)
1580 tmunro@postgresql.or 804 :UBC 0 : readOff = ReadPageInternal(state, targetPagePtr,
805 : 0 : pageHeaderSize + len);
806 : :
206 peter@eisentraut.org 807 :CBC 1449574 : memcpy(buffer, contdata, len);
1580 tmunro@postgresql.or 808 : 1449574 : buffer += len;
809 : 1449574 : gotlen += len;
810 : :
811 : : /* If we just reassembled the record header, validate it. */
812 [ + + ]: 1449574 : if (!gotheader)
813 : : {
814 : 10704 : record = (XLogRecord *) state->readRecordBuf;
1268 815 [ - + ]: 10704 : if (!ValidXLogRecordHeader(state, RecPtr, state->DecodeRecPtr,
816 : : record, randAccess))
4616 alvherre@alvh.no-ip. 817 :UBC 0 : goto err;
1580 tmunro@postgresql.or 818 :CBC 10704 : gotheader = true;
819 : : }
820 : :
821 : : /*
822 : : * We might need a bigger buffer. We have validated the record
823 : : * header, in the case that it split over a page boundary. We've
824 : : * also cross-checked total_len against xlp_rem_len on the second
825 : : * page, and verified xlp_pageaddr on both.
826 : : */
714 827 [ + + ]: 1449574 : if (total_len > state->readRecordBufSize)
828 : : {
829 : : char save_copy[XLOG_BLCKSZ * 2];
830 : :
831 : : /*
832 : : * Save and restore the data we already had. It can't be more
833 : : * than two pages.
834 : : */
835 [ - + ]: 32 : Assert(gotlen <= lengthof(save_copy));
836 [ - + ]: 32 : Assert(gotlen <= state->readRecordBufSize);
837 : 32 : memcpy(save_copy, state->readRecordBuf, gotlen);
704 michael@paquier.xyz 838 : 32 : allocate_recordbuf(state, total_len);
714 tmunro@postgresql.or 839 : 32 : memcpy(state->readRecordBuf, save_copy, gotlen);
840 : 32 : buffer = state->readRecordBuf + gotlen;
841 : : }
842 [ + + ]: 1449574 : } while (gotlen < total_len);
1580 843 [ - + ]: 1419220 : Assert(gotheader);
844 : :
845 : 1419220 : record = (XLogRecord *) state->readRecordBuf;
846 [ - + ]: 1419220 : if (!ValidXLogRecord(state, record, RecPtr))
1580 tmunro@postgresql.or 847 :UBC 0 : goto err;
848 : :
1580 tmunro@postgresql.or 849 [ + + ]:CBC 1419220 : pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
1268 850 : 1419220 : state->DecodeRecPtr = RecPtr;
851 : 1419220 : state->NextRecPtr = targetPagePtr + pageHeaderSize
1580 852 : 1419220 : + MAXALIGN(pageHeader->xlp_rem_len);
853 : : }
854 : : else
855 : : {
856 : : /* Wait for the record data to become available */
857 : 4003297 : readOff = ReadPageInternal(state, targetPagePtr,
858 : 4003297 : Min(targetRecOff + total_len, XLOG_BLCKSZ));
1268 859 [ - + ]: 4003297 : if (readOff == XLREAD_WOULDBLOCK)
1268 tmunro@postgresql.or 860 :UBC 0 : return XLREAD_WOULDBLOCK;
1268 tmunro@postgresql.or 861 [ - + ]:CBC 4003297 : else if (readOff < 0)
1580 tmunro@postgresql.or 862 :UBC 0 : goto err;
863 : :
864 : : /* Record does not cross a page boundary */
1580 tmunro@postgresql.or 865 [ + + ]:CBC 4003297 : if (!ValidXLogRecord(state, record, RecPtr))
866 : 1 : goto err;
867 : :
1268 868 : 4003296 : state->NextRecPtr = RecPtr + MAXALIGN(total_len);
869 : :
870 : 4003296 : state->DecodeRecPtr = RecPtr;
871 : : }
872 : :
873 : : /*
874 : : * Special processing if it's an XLOG SWITCH record
875 : : */
1612 876 [ + + ]: 5422516 : if (record->xl_rmid == RM_XLOG_ID &&
877 [ + + ]: 71956 : (record->xl_info & ~XLR_INFO_MASK) == XLOG_SWITCH)
878 : : {
879 : : /* Pretend it extends to end of segment */
1268 880 : 484 : state->NextRecPtr += state->segcxt.ws_segsize - 1;
881 : 484 : state->NextRecPtr -= XLogSegmentOffset(state->NextRecPtr, state->segcxt.ws_segsize);
882 : : }
883 : :
884 : : /*
885 : : * If we got here without a DecodedXLogRecord, it means we needed to
886 : : * validate total_len before trusting it, but by now we've done that.
887 : : */
714 888 [ + + ]: 5422516 : if (decoded == NULL)
889 : : {
890 [ - + ]: 54 : Assert(!nonblocking);
891 : 54 : decoded = XLogReadRecordAlloc(state,
892 : : total_len,
893 : : true /* allow_oversized */ );
894 : : /* allocation should always happen under allow_oversized */
704 michael@paquier.xyz 895 [ - + ]: 54 : Assert(decoded != NULL);
896 : : }
897 : :
1268 tmunro@postgresql.or 898 [ + - ]: 5422516 : if (DecodeXLogRecord(state, decoded, record, RecPtr, &errormsg))
899 : : {
900 : : /* Record the location of the next record. */
901 : 5422516 : decoded->next_lsn = state->NextRecPtr;
902 : :
903 : : /*
904 : : * If it's in the decode buffer, mark the decode buffer space as
905 : : * occupied.
906 : : */
907 [ + + ]: 5422516 : if (!decoded->oversized)
908 : : {
909 : : /* The new decode buffer head must be MAXALIGNed. */
910 [ - + ]: 5422462 : Assert(decoded->size == MAXALIGN(decoded->size));
911 [ + + ]: 5422462 : if ((char *) decoded == state->decode_buffer)
912 : 2732449 : state->decode_buffer_tail = state->decode_buffer + decoded->size;
913 : : else
914 : 2690013 : state->decode_buffer_tail += decoded->size;
915 : : }
916 : :
917 : : /* Insert it into the queue of decoded records. */
918 [ - + ]: 5422516 : Assert(state->decode_queue_tail != decoded);
919 [ + + ]: 5422516 : if (state->decode_queue_tail)
920 : 2706066 : state->decode_queue_tail->next = decoded;
921 : 5422516 : state->decode_queue_tail = decoded;
922 [ + + ]: 5422516 : if (!state->decode_queue_head)
923 : 2716450 : state->decode_queue_head = decoded;
924 : 5422516 : return XLREAD_SUCCESS;
925 : : }
926 : :
4616 alvherre@alvh.no-ip. 927 :UBC 0 : err:
1438 alvherre@alvh.no-ip. 928 [ + + ]:CBC 3808 : if (assembled)
929 : : {
930 : : /*
931 : : * We get here when a record that spans multiple pages needs to be
932 : : * assembled, but something went wrong -- perhaps a contrecord piece
933 : : * was lost. If caller is WAL replay, it will know where the aborted
934 : : * record was and where to direct followup WAL to be written, marking
935 : : * the next piece with XLP_FIRST_IS_OVERWRITE_CONTRECORD, which will
936 : : * in turn signal downstream WAL consumers that the broken WAL record
937 : : * is to be ignored.
938 : : */
939 : 21 : state->abortedRecPtr = RecPtr;
940 : 21 : state->missingContrecPtr = targetPagePtr;
941 : :
942 : : /*
943 : : * If we got here without reporting an error, make sure an error is
944 : : * queued so that XLogPrefetcherReadRecord() doesn't bring us back a
945 : : * second time and clobber the above state.
946 : : */
796 tmunro@postgresql.or 947 : 21 : state->errormsg_deferred = true;
948 : : }
949 : :
1268 950 [ + + - + ]: 3808 : if (decoded && decoded->oversized)
1268 tmunro@postgresql.or 951 :UBC 0 : pfree(decoded);
952 : :
953 : : /*
954 : : * Invalidate the read state. We might read from a different source after
955 : : * failure.
956 : : */
3447 alvherre@alvh.no-ip. 957 :CBC 3808 : XLogReaderInvalReadState(state);
958 : :
959 : : /*
960 : : * If an error was written to errormsg_buf, it'll be returned to the
961 : : * caller of XLogReadRecord() after all successfully decoded records from
962 : : * the read queue.
963 : : */
964 : :
1268 tmunro@postgresql.or 965 : 3808 : return XLREAD_FAIL;
966 : : }
967 : :
968 : : /*
969 : : * Try to decode the next available record, and return it. The record will
970 : : * also be returned to XLogNextRecord(), which must be called to 'consume'
971 : : * each record.
972 : : *
973 : : * If nonblocking is true, may return NULL due to lack of data or WAL decoding
974 : : * space.
975 : : */
976 : : DecodedXLogRecord *
977 : 5441304 : XLogReadAhead(XLogReaderState *state, bool nonblocking)
978 : : {
979 : : XLogPageReadResult result;
980 : :
981 [ - + ]: 5441304 : if (state->errormsg_deferred)
1268 tmunro@postgresql.or 982 :UBC 0 : return NULL;
983 : :
1268 tmunro@postgresql.or 984 :CBC 5441304 : result = XLogDecodeNextRecord(state, nonblocking);
985 [ + + ]: 5441067 : if (result == XLREAD_SUCCESS)
986 : : {
987 [ - + ]: 5422516 : Assert(state->decode_queue_tail != NULL);
988 : 5422516 : return state->decode_queue_tail;
989 : : }
990 : :
1580 991 : 18551 : return NULL;
992 : : }
993 : :
994 : : /*
995 : : * Read a single xlog page including at least [pageptr, reqLen] of valid data
996 : : * via the page_read() callback.
997 : : *
998 : : * Returns XLREAD_FAIL if the required page cannot be read for some
999 : : * reason; errormsg_buf is set in that case (unless the error occurs in the
1000 : : * page_read callback).
1001 : : *
1002 : : * Returns XLREAD_WOULDBLOCK if the requested data can't be read without
1003 : : * waiting. This can be returned only if the installed page_read callback
1004 : : * respects the state->nonblocking flag, and cannot read the requested data
1005 : : * immediately.
1006 : : *
1007 : : * We fetch the page from a reader-local cache if we know we have the required
1008 : : * data and if there hasn't been any error since caching the data.
1009 : : */
1010 : : static int
1011 : 12347947 : ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
1012 : : {
1013 : : int readLen;
1014 : : uint32 targetPageOff;
1015 : : XLogSegNo targetSegNo;
1016 : : XLogPageHeader hdr;
1017 : :
1018 [ - + ]: 12347947 : Assert((pageptr % XLOG_BLCKSZ) == 0);
1019 : :
1020 : 12347947 : XLByteToSeg(pageptr, targetSegNo, state->segcxt.ws_segsize);
1021 : 12347947 : targetPageOff = XLogSegmentOffset(pageptr, state->segcxt.ws_segsize);
1022 : :
1023 : : /* check whether we have all the requested data already */
1024 [ + + ]: 12347947 : if (targetSegNo == state->seg.ws_segno &&
1025 [ + + + + ]: 12340500 : targetPageOff == state->segoff && reqLen <= state->readLen)
1026 : 10859197 : return state->readLen;
1027 : :
1028 : : /*
1029 : : * Invalidate contents of internal buffer before read attempt. Just set
1030 : : * the length to 0, rather than a full XLogReaderInvalReadState(), so we
1031 : : * don't forget the segment we last successfully read.
1032 : : */
1099 1033 : 1488750 : state->readLen = 0;
1034 : :
1035 : : /*
1036 : : * Data is not in our buffer.
1037 : : *
1038 : : * Every time we actually read the segment, even if we looked at parts of
1039 : : * it before, we need to do verification as the page_read callback might
1040 : : * now be rereading data from a different source.
1041 : : *
1042 : : * Whenever switching to a new WAL segment, we read the first page of the
1043 : : * file and validate its header, even if that's not where the target
1044 : : * record is. This is so that we can check the additional identification
1045 : : * info that is present in the first page's "long" header.
1046 : : */
1580 1047 [ + + + + ]: 1488750 : if (targetSegNo != state->seg.ws_segno && targetPageOff != 0)
1048 : : {
1049 : 5592 : XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
1050 : :
1051 : 5592 : readLen = state->routine.page_read(state, targetSegmentPtr, XLOG_BLCKSZ,
1052 : : state->currRecPtr,
1053 : : state->readBuf);
1268 1054 [ - + ]: 5584 : if (readLen == XLREAD_WOULDBLOCK)
1268 tmunro@postgresql.or 1055 :UBC 0 : return XLREAD_WOULDBLOCK;
1268 tmunro@postgresql.or 1056 [ - + ]:CBC 5584 : else if (readLen < 0)
1580 tmunro@postgresql.or 1057 :UBC 0 : goto err;
1058 : :
1059 : : /* we can be sure to have enough WAL available, we scrolled back */
1580 tmunro@postgresql.or 1060 [ - + ]:CBC 5584 : Assert(readLen == XLOG_BLCKSZ);
1061 : :
1062 [ - + ]: 5584 : if (!XLogReaderValidatePageHeader(state, targetSegmentPtr,
1063 : : state->readBuf))
1580 tmunro@postgresql.or 1064 :UBC 0 : goto err;
1065 : : }
1066 : :
1067 : : /*
1068 : : * First, read the requested data length, but at least a short page header
1069 : : * so that we can validate it.
1070 : : */
1580 tmunro@postgresql.or 1071 :CBC 1488742 : readLen = state->routine.page_read(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
1072 : : state->currRecPtr,
1073 : : state->readBuf);
1268 1074 [ + + ]: 1488513 : if (readLen == XLREAD_WOULDBLOCK)
1075 : 12173 : return XLREAD_WOULDBLOCK;
1076 [ + + ]: 1476340 : else if (readLen < 0)
1580 1077 : 3572 : goto err;
1078 : :
1079 [ - + ]: 1472768 : Assert(readLen <= XLOG_BLCKSZ);
1080 : :
1081 : : /* Do we have enough data to check the header length? */
1082 [ - + ]: 1472768 : if (readLen <= SizeOfXLogShortPHD)
1580 tmunro@postgresql.or 1083 :UBC 0 : goto err;
1084 : :
1580 tmunro@postgresql.or 1085 [ - + ]:CBC 1472768 : Assert(readLen >= reqLen);
1086 : :
1087 : 1472768 : hdr = (XLogPageHeader) state->readBuf;
1088 : :
1089 : : /* still not enough */
1090 [ + + - + ]: 1472768 : if (readLen < XLogPageHeaderSize(hdr))
1091 : : {
1580 tmunro@postgresql.or 1092 [ # # ]:UBC 0 : readLen = state->routine.page_read(state, pageptr, XLogPageHeaderSize(hdr),
1093 : : state->currRecPtr,
1094 : : state->readBuf);
1268 1095 [ # # ]: 0 : if (readLen == XLREAD_WOULDBLOCK)
1096 : 0 : return XLREAD_WOULDBLOCK;
1097 [ # # ]: 0 : else if (readLen < 0)
1580 1098 : 0 : goto err;
1099 : : }
1100 : :
1101 : : /*
1102 : : * Now that we know we have the full header, validate it.
1103 : : */
1580 tmunro@postgresql.or 1104 [ + + ]:CBC 1472768 : if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr))
1105 : 15 : goto err;
1106 : :
1107 : : /* update read state information */
1108 : 1472753 : state->seg.ws_segno = targetSegNo;
1109 : 1472753 : state->segoff = targetPageOff;
1110 : 1472753 : state->readLen = readLen;
1111 : :
1112 : 1472753 : return readLen;
1113 : :
1114 : 3587 : err:
1099 1115 : 3587 : XLogReaderInvalReadState(state);
1116 : :
1268 1117 : 3587 : return XLREAD_FAIL;
1118 : : }
1119 : :
1120 : : /*
1121 : : * Invalidate the xlogreader's read state to force a re-read.
1122 : : */
1123 : : static void
3447 alvherre@alvh.no-ip. 1124 : 7395 : XLogReaderInvalReadState(XLogReaderState *state)
1125 : : {
1580 tmunro@postgresql.or 1126 : 7395 : state->seg.ws_segno = 0;
1127 : 7395 : state->segoff = 0;
1128 : 7395 : state->readLen = 0;
4616 alvherre@alvh.no-ip. 1129 : 7395 : }
1130 : :
1131 : : /*
1132 : : * Validate an XLOG record header.
1133 : : *
1134 : : * This is just a convenience subroutine to avoid duplicated code in
1135 : : * XLogReadRecord. It's not intended for use from anywhere else.
1136 : : */
1137 : : static bool
1138 : 5429300 : ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
1139 : : XLogRecPtr PrevRecPtr, XLogRecord *record,
1140 : : bool randAccess)
1141 : : {
3943 heikki.linnakangas@i 1142 [ + + ]: 5429300 : if (record->xl_tot_len < SizeOfXLogRecord)
1143 : : {
4616 alvherre@alvh.no-ip. 1144 : 213 : report_invalid_record(state,
1145 : : "invalid record length at %X/%08X: expected at least %u, got %u",
1656 peter@eisentraut.org 1146 : 213 : LSN_FORMAT_ARGS(RecPtr),
1147 : : (uint32) SizeOfXLogRecord, record->xl_tot_len);
4616 alvherre@alvh.no-ip. 1148 : 213 : return false;
1149 : : }
1248 jdavis@postgresql.or 1150 [ + + - + ]: 5429087 : if (!RmgrIdIsValid(record->xl_rmid))
1151 : : {
4616 alvherre@alvh.no-ip. 1152 :UBC 0 : report_invalid_record(state,
1153 : : "invalid resource manager ID %u at %X/%08X",
1656 peter@eisentraut.org 1154 : 0 : record->xl_rmid, LSN_FORMAT_ARGS(RecPtr));
4616 alvherre@alvh.no-ip. 1155 : 0 : return false;
1156 : : }
1580 tmunro@postgresql.or 1157 [ + + ]:CBC 5429087 : if (randAccess)
1158 : : {
1159 : : /*
1160 : : * We can't exactly verify the prev-link, but surely it should be less
1161 : : * than the record's own address.
1162 : : */
4616 alvherre@alvh.no-ip. 1163 [ - + ]: 6170 : if (!(record->xl_prev < RecPtr))
1164 : : {
4616 alvherre@alvh.no-ip. 1165 :UBC 0 : report_invalid_record(state,
1166 : : "record with incorrect prev-link %X/%08X at %X/%08X",
1656 peter@eisentraut.org 1167 : 0 : LSN_FORMAT_ARGS(record->xl_prev),
1168 : 0 : LSN_FORMAT_ARGS(RecPtr));
4616 alvherre@alvh.no-ip. 1169 : 0 : return false;
1170 : : }
1171 : : }
1172 : : else
1173 : : {
1174 : : /*
1175 : : * Record's prev-link should exactly match our previous location. This
1176 : : * check guards against torn WAL pages where a stale but valid-looking
1177 : : * WAL record starts on a sector boundary.
1178 : : */
4616 alvherre@alvh.no-ip. 1179 [ + + ]:CBC 5422917 : if (record->xl_prev != PrevRecPtr)
1180 : : {
1181 : 3 : report_invalid_record(state,
1182 : : "record with incorrect prev-link %X/%08X at %X/%08X",
1656 peter@eisentraut.org 1183 : 3 : LSN_FORMAT_ARGS(record->xl_prev),
1184 : 3 : LSN_FORMAT_ARGS(RecPtr));
4616 alvherre@alvh.no-ip. 1185 : 3 : return false;
1186 : : }
1187 : : }
1188 : :
1189 : 5429084 : return true;
1190 : : }
1191 : :
1192 : :
1193 : : /*
1194 : : * CRC-check an XLOG record. We do not believe the contents of an XLOG
1195 : : * record (other than to the minimal extent of computing the amount of
1196 : : * data to read in) until we've checked the CRCs.
1197 : : *
1198 : : * We assume all of the record (that is, xl_tot_len bytes) has been read
1199 : : * into memory at *record. Also, ValidXLogRecordHeader() has accepted the
1200 : : * record's header, which means in particular that xl_tot_len is at least
1201 : : * SizeOfXLogRecord.
1202 : : */
1203 : : static bool
1204 : 5422517 : ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
1205 : : {
1206 : : pg_crc32c crc;
1207 : :
711 tmunro@postgresql.or 1208 [ - + ]: 5422517 : Assert(record->xl_tot_len >= SizeOfXLogRecord);
1209 : :
1210 : : /* Calculate the CRC */
3959 heikki.linnakangas@i 1211 : 5422517 : INIT_CRC32C(crc);
3943 1212 : 5422517 : COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
1213 : : /* include the record header last */
3959 1214 : 5422517 : COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
1215 : 5422517 : FIN_CRC32C(crc);
1216 : :
1217 [ + + ]: 5422517 : if (!EQ_CRC32C(record->xl_crc, crc))
1218 : : {
4616 alvherre@alvh.no-ip. 1219 : 1 : report_invalid_record(state,
1220 : : "incorrect resource manager data checksum in record at %X/%08X",
1656 peter@eisentraut.org 1221 : 1 : LSN_FORMAT_ARGS(recptr));
4616 alvherre@alvh.no-ip. 1222 : 1 : return false;
1223 : : }
1224 : :
1225 : 5422516 : return true;
1226 : : }
1227 : :
1228 : : /*
1229 : : * Validate a page header.
1230 : : *
1231 : : * Check if 'phdr' is valid as the header of the XLog page at position
1232 : : * 'recptr'.
1233 : : */
1234 : : bool
2681 heikki.linnakangas@i 1235 : 1479654 : XLogReaderValidatePageHeader(XLogReaderState *state, XLogRecPtr recptr,
1236 : : char *phdr)
1237 : : {
1238 : : XLogSegNo segno;
1239 : : int32 offset;
1240 : 1479654 : XLogPageHeader hdr = (XLogPageHeader) phdr;
1241 : :
4616 alvherre@alvh.no-ip. 1242 [ - + ]: 1479654 : Assert((recptr % XLOG_BLCKSZ) == 0);
1243 : :
2174 1244 : 1479654 : XLByteToSeg(recptr, segno, state->segcxt.ws_segsize);
1245 : 1479654 : offset = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1246 : :
4616 1247 [ + + ]: 1479654 : if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
1248 : : {
1249 : : char fname[MAXFNAMELEN];
1250 : :
2174 1251 : 12 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1252 : :
4616 1253 : 12 : report_invalid_record(state,
1254 : : "invalid magic number %04X in WAL segment %s, LSN %X/%08X, offset %u",
1255 : 12 : hdr->xlp_magic,
1256 : : fname,
1006 michael@paquier.xyz 1257 : 12 : LSN_FORMAT_ARGS(recptr),
1258 : : offset);
4616 alvherre@alvh.no-ip. 1259 : 12 : return false;
1260 : : }
1261 : :
1262 [ + + ]: 1479642 : if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
1263 : : {
1264 : : char fname[MAXFNAMELEN];
1265 : :
2174 1266 : 1 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1267 : :
4616 1268 : 1 : report_invalid_record(state,
1269 : : "invalid info bits %04X in WAL segment %s, LSN %X/%08X, offset %u",
1270 : 1 : hdr->xlp_info,
1271 : : fname,
1006 michael@paquier.xyz 1272 : 1 : LSN_FORMAT_ARGS(recptr),
1273 : : offset);
4616 alvherre@alvh.no-ip. 1274 : 1 : return false;
1275 : : }
1276 : :
1277 [ + + ]: 1479641 : if (hdr->xlp_info & XLP_LONG_HEADER)
1278 : : {
1279 : 8410 : XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
1280 : :
1281 [ + + ]: 8410 : if (state->system_identifier &&
1282 [ - + ]: 3548 : longhdr->xlp_sysid != state->system_identifier)
1283 : : {
4616 alvherre@alvh.no-ip. 1284 :UBC 0 : report_invalid_record(state,
1285 : : "WAL file is from different database system: WAL file database system identifier is %" PRIu64 ", pg_control database system identifier is %" PRIu64,
1286 : : longhdr->xlp_sysid,
1287 : : state->system_identifier);
1288 : 0 : return false;
1289 : : }
2174 alvherre@alvh.no-ip. 1290 [ - + ]:CBC 8410 : else if (longhdr->xlp_seg_size != state->segcxt.ws_segsize)
1291 : : {
4616 alvherre@alvh.no-ip. 1292 :UBC 0 : report_invalid_record(state,
1293 : : "WAL file is from different database system: incorrect segment size in page header");
1294 : 0 : return false;
1295 : : }
4616 alvherre@alvh.no-ip. 1296 [ - + ]:CBC 8410 : else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
1297 : : {
4616 alvherre@alvh.no-ip. 1298 :UBC 0 : report_invalid_record(state,
1299 : : "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
1300 : 0 : return false;
1301 : : }
1302 : : }
4616 alvherre@alvh.no-ip. 1303 [ - + ]:CBC 1471231 : else if (offset == 0)
1304 : : {
1305 : : char fname[MAXFNAMELEN];
1306 : :
2174 alvherre@alvh.no-ip. 1307 :UBC 0 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1308 : :
1309 : : /* hmm, first page of file doesn't have a long header? */
4616 1310 : 0 : report_invalid_record(state,
1311 : : "invalid info bits %04X in WAL segment %s, LSN %X/%08X, offset %u",
1312 : 0 : hdr->xlp_info,
1313 : : fname,
1006 michael@paquier.xyz 1314 : 0 : LSN_FORMAT_ARGS(recptr),
1315 : : offset);
4616 alvherre@alvh.no-ip. 1316 : 0 : return false;
1317 : : }
1318 : :
1319 : : /*
1320 : : * Check that the address on the page agrees with what we expected. This
1321 : : * check typically fails when an old WAL segment is recycled, and hasn't
1322 : : * yet been overwritten with new data yet.
1323 : : */
1060 michael@paquier.xyz 1324 [ + + ]:CBC 1479641 : if (hdr->xlp_pageaddr != recptr)
1325 : : {
1326 : : char fname[MAXFNAMELEN];
1327 : :
2174 alvherre@alvh.no-ip. 1328 : 6 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1329 : :
4616 1330 : 6 : report_invalid_record(state,
1331 : : "unexpected pageaddr %X/%08X in WAL segment %s, LSN %X/%08X, offset %u",
1656 peter@eisentraut.org 1332 : 6 : LSN_FORMAT_ARGS(hdr->xlp_pageaddr),
1333 : : fname,
1006 michael@paquier.xyz 1334 : 6 : LSN_FORMAT_ARGS(recptr),
1335 : : offset);
4616 alvherre@alvh.no-ip. 1336 : 6 : return false;
1337 : : }
1338 : :
1339 : : /*
1340 : : * Since child timelines are always assigned a TLI greater than their
1341 : : * immediate parent's TLI, we should never see TLI go backwards across
1342 : : * successive pages of a consistent WAL sequence.
1343 : : *
1344 : : * Sometimes we re-read a segment that's already been (partially) read. So
1345 : : * we only verify TLIs for pages that are later than the last remembered
1346 : : * LSN.
1347 : : */
1348 [ + + ]: 1479635 : if (recptr > state->latestPagePtr)
1349 : : {
1350 [ - + ]: 1459742 : if (hdr->xlp_tli < state->latestPageTLI)
1351 : : {
1352 : : char fname[MAXFNAMELEN];
1353 : :
2174 alvherre@alvh.no-ip. 1354 :UBC 0 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1355 : :
4616 1356 : 0 : report_invalid_record(state,
1357 : : "out-of-sequence timeline ID %u (after %u) in WAL segment %s, LSN %X/%08X, offset %u",
1358 : : hdr->xlp_tli,
1359 : : state->latestPageTLI,
1360 : : fname,
1006 michael@paquier.xyz 1361 : 0 : LSN_FORMAT_ARGS(recptr),
1362 : : offset);
4616 alvherre@alvh.no-ip. 1363 : 0 : return false;
1364 : : }
1365 : : }
4616 alvherre@alvh.no-ip. 1366 :CBC 1479635 : state->latestPagePtr = recptr;
1367 : 1479635 : state->latestPageTLI = hdr->xlp_tli;
1368 : :
1369 : 1479635 : return true;
1370 : : }
1371 : :
1372 : : /*
1373 : : * Forget about an error produced by XLogReaderValidatePageHeader().
1374 : : */
1375 : : void
1099 tmunro@postgresql.or 1376 : 4 : XLogReaderResetError(XLogReaderState *state)
1377 : : {
1378 : 4 : state->errormsg_buf[0] = '\0';
1379 : 4 : state->errormsg_deferred = false;
1380 : 4 : }
1381 : :
1382 : : /*
1383 : : * Find the first record with an lsn >= RecPtr.
1384 : : *
1385 : : * This is different from XLogBeginRead() in that RecPtr doesn't need to point
1386 : : * to a valid record boundary. Useful for checking whether RecPtr is a valid
1387 : : * xlog address for reading, and to find the first valid address after some
1388 : : * address when dumping records for debugging purposes.
1389 : : *
1390 : : * This positions the reader, like XLogBeginRead(), so that the next call to
1391 : : * XLogReadRecord() will read the next valid record.
1392 : : */
1393 : : XLogRecPtr
1580 1394 : 85 : XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
1395 : : {
1396 : : XLogRecPtr tmpRecPtr;
1397 : 85 : XLogRecPtr found = InvalidXLogRecPtr;
1398 : : XLogPageHeader header;
1399 : : char *errormsg;
1400 : :
1401 [ - + ]: 85 : Assert(!XLogRecPtrIsInvalid(RecPtr));
1402 : :
1403 : : /* Make sure ReadPageInternal() can't return XLREAD_WOULDBLOCK. */
1268 1404 : 85 : state->nonblocking = false;
1405 : :
1406 : : /*
1407 : : * skip over potential continuation data, keeping in mind that it may span
1408 : : * multiple pages
1409 : : */
1580 1410 : 85 : tmpRecPtr = RecPtr;
1411 : : while (true)
3295 fujii@postgresql.org 1412 :UBC 0 : {
1413 : : XLogRecPtr targetPagePtr;
1414 : : int targetRecOff;
1415 : : uint32 pageHeaderSize;
1416 : : int readLen;
1417 : :
1418 : : /*
1419 : : * Compute targetRecOff. It should typically be equal or greater than
1420 : : * short page-header since a valid record can't start anywhere before
1421 : : * that, except when caller has explicitly specified the offset that
1422 : : * falls somewhere there or when we are skipping multi-page
1423 : : * continuation record. It doesn't matter though because
1424 : : * ReadPageInternal() is prepared to handle that and will read at
1425 : : * least short page-header worth of data
1426 : : */
1580 tmunro@postgresql.or 1427 :CBC 85 : targetRecOff = tmpRecPtr % XLOG_BLCKSZ;
1428 : :
1429 : : /* scroll back to page boundary */
1430 : 85 : targetPagePtr = tmpRecPtr - targetRecOff;
1431 : :
1432 : : /* Read the page containing the record */
1433 : 85 : readLen = ReadPageInternal(state, targetPagePtr, targetRecOff);
1434 [ - + ]: 85 : if (readLen < 0)
3295 fujii@postgresql.org 1435 :UBC 0 : goto err;
1436 : :
1580 tmunro@postgresql.or 1437 :CBC 85 : header = (XLogPageHeader) state->readBuf;
1438 : :
3295 fujii@postgresql.org 1439 [ + + ]: 85 : pageHeaderSize = XLogPageHeaderSize(header);
1440 : :
1441 : : /* make sure we have enough data for the page header */
1580 tmunro@postgresql.or 1442 : 85 : readLen = ReadPageInternal(state, targetPagePtr, pageHeaderSize);
1443 [ - + ]: 85 : if (readLen < 0)
1580 tmunro@postgresql.or 1444 :UBC 0 : goto err;
1445 : :
1446 : : /* skip over potential continuation data */
3295 fujii@postgresql.org 1447 [ + + ]:CBC 85 : if (header->xlp_info & XLP_FIRST_IS_CONTRECORD)
1448 : : {
1449 : : /*
1450 : : * If the length of the remaining continuation data is more than
1451 : : * what can fit in this page, the continuation record crosses over
1452 : : * this page. Read the next page and try again. xlp_rem_len in the
1453 : : * next page header will contain the remaining length of the
1454 : : * continuation data
1455 : : *
1456 : : * Note that record headers are MAXALIGN'ed
1457 : : */
2130 1458 [ - + ]: 30 : if (MAXALIGN(header->xlp_rem_len) >= (XLOG_BLCKSZ - pageHeaderSize))
1580 tmunro@postgresql.or 1459 :UBC 0 : tmpRecPtr = targetPagePtr + XLOG_BLCKSZ;
1460 : : else
1461 : : {
1462 : : /*
1463 : : * The previous continuation record ends in this page. Set
1464 : : * tmpRecPtr to point to the first valid record
1465 : : */
1580 tmunro@postgresql.or 1466 :CBC 30 : tmpRecPtr = targetPagePtr + pageHeaderSize
3295 fujii@postgresql.org 1467 : 30 : + MAXALIGN(header->xlp_rem_len);
1468 : 30 : break;
1469 : : }
1470 : : }
1471 : : else
1472 : : {
1580 tmunro@postgresql.or 1473 : 55 : tmpRecPtr = targetPagePtr + pageHeaderSize;
3295 fujii@postgresql.org 1474 : 55 : break;
1475 : : }
1476 : : }
1477 : :
1478 : : /*
1479 : : * we know now that tmpRecPtr is an address pointing to a valid XLogRecord
1480 : : * because either we're at the first record after the beginning of a page
1481 : : * or we just jumped over the remaining data of a continuation.
1482 : : */
1580 tmunro@postgresql.or 1483 : 85 : XLogBeginRead(state, tmpRecPtr);
1484 [ + - ]: 801 : while (XLogReadRecord(state, &errormsg) != NULL)
1485 : : {
1486 : : /* past the record we've found, break out */
1487 [ + + ]: 801 : if (RecPtr <= state->ReadRecPtr)
1488 : : {
1489 : : /* Rewind the reader to the beginning of the last record. */
1490 : 85 : found = state->ReadRecPtr;
1491 : 85 : XLogBeginRead(state, found);
1492 : 85 : return found;
1493 : : }
1494 : : }
1495 : :
4616 alvherre@alvh.no-ip. 1496 :UBC 0 : err:
1580 tmunro@postgresql.or 1497 : 0 : XLogReaderInvalReadState(state);
1498 : :
1499 : 0 : return InvalidXLogRecPtr;
1500 : : }
1501 : :
1502 : : /*
1503 : : * Helper function to ease writing of XLogReaderRoutine->page_read callbacks.
1504 : : * If this function is used, caller must supply a segment_open callback in
1505 : : * 'state', as that is used here.
1506 : : *
1507 : : * Read 'count' bytes into 'buf', starting at location 'startptr', from WAL
1508 : : * fetched from timeline 'tli'.
1509 : : *
1510 : : * Returns true if succeeded, false if an error occurs, in which case
1511 : : * 'errinfo' receives error details.
1512 : : */
1513 : : bool
1947 alvherre@alvh.no-ip. 1514 :CBC 146246 : WALRead(XLogReaderState *state,
1515 : : char *buf, XLogRecPtr startptr, Size count, TimeLineID tli,
1516 : : WALReadError *errinfo)
1517 : : {
1518 : : char *p;
1519 : : XLogRecPtr recptr;
1520 : : Size nbytes;
1521 : : #ifndef FRONTEND
1522 : : instr_time io_start;
1523 : : #endif
1524 : :
2112 1525 : 146246 : p = buf;
1526 : 146246 : recptr = startptr;
1527 : 146246 : nbytes = count;
1528 : :
1529 [ + + ]: 292876 : while (nbytes > 0)
1530 : : {
1531 : : uint32 startoff;
1532 : : int segbytes;
1533 : : int readbytes;
1534 : :
1942 1535 : 146631 : startoff = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1536 : :
1537 : : /*
1538 : : * If the data we want is not in a segment we have open, close what we
1539 : : * have (if anything) and open the next one, using the caller's
1540 : : * provided segment_open callback.
1541 : : */
1542 [ + + ]: 146631 : if (state->seg.ws_file < 0 ||
1543 [ + + ]: 144930 : !XLByteInSeg(recptr, state->seg.ws_segno, state->segcxt.ws_segsize) ||
1544 [ - + ]: 140909 : tli != state->seg.ws_tli)
1545 : : {
1546 : : XLogSegNo nextSegNo;
1547 : :
1548 [ + + ]: 5722 : if (state->seg.ws_file >= 0)
1580 tmunro@postgresql.or 1549 : 4021 : state->routine.segment_close(state);
1550 : :
1942 alvherre@alvh.no-ip. 1551 : 5722 : XLByteToSeg(recptr, nextSegNo, state->segcxt.ws_segsize);
1580 tmunro@postgresql.or 1552 : 5722 : state->routine.segment_open(state, nextSegNo, &tli);
1553 : :
1554 : : /* This shouldn't happen -- indicates a bug in segment_open */
1942 alvherre@alvh.no-ip. 1555 [ - + ]: 5721 : Assert(state->seg.ws_file >= 0);
1556 : :
1557 : : /* Update the current segment info. */
1558 : 5721 : state->seg.ws_tli = tli;
1559 : 5721 : state->seg.ws_segno = nextSegNo;
1560 : : }
1561 : :
1562 : : /* How many bytes are within this segment? */
1563 [ + + ]: 146630 : if (nbytes > (state->segcxt.ws_segsize - startoff))
1564 : 385 : segbytes = state->segcxt.ws_segsize - startoff;
1565 : : else
2112 1566 : 146245 : segbytes = nbytes;
1567 : :
1568 : : #ifndef FRONTEND
1569 : : /* Measure I/O timing when reading segment */
192 michael@paquier.xyz 1570 : 127751 : io_start = pgstat_prepare_io_time(track_wal_io_timing);
1571 : :
2112 alvherre@alvh.no-ip. 1572 : 127751 : pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
1573 : : #endif
1574 : :
1575 : : /* Reset errno first; eases reporting non-errno-affecting errors */
1576 : 146630 : errno = 0;
1073 tmunro@postgresql.or 1577 : 146630 : readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1578 : :
1579 : : #ifndef FRONTEND
2112 alvherre@alvh.no-ip. 1580 : 127751 : pgstat_report_wait_end();
1581 : :
214 michael@paquier.xyz 1582 : 127751 : pgstat_count_io_op_time(IOOBJECT_WAL, IOCONTEXT_NORMAL, IOOP_READ,
1583 : : io_start, 1, readbytes);
1584 : : #endif
1585 : :
2112 alvherre@alvh.no-ip. 1586 [ - + ]: 146630 : if (readbytes <= 0)
1587 : : {
2112 alvherre@alvh.no-ip. 1588 :UBC 0 : errinfo->wre_errno = errno;
1589 : 0 : errinfo->wre_req = segbytes;
1590 : 0 : errinfo->wre_read = readbytes;
1591 : 0 : errinfo->wre_off = startoff;
1942 1592 : 0 : errinfo->wre_seg = state->seg;
2112 1593 : 0 : return false;
1594 : : }
1595 : :
1596 : : /* Update state for read */
2112 alvherre@alvh.no-ip. 1597 :CBC 146630 : recptr += readbytes;
1598 : 146630 : nbytes -= readbytes;
1599 : 146630 : p += readbytes;
1600 : : }
1601 : :
1602 : 146245 : return true;
1603 : : }
1604 : :
1605 : : /* ----------------------------------------
1606 : : * Functions for decoding the data and block references in a record.
1607 : : * ----------------------------------------
1608 : : */
1609 : :
1610 : : /*
1611 : : * Private function to reset the state, forgetting all decoded records, if we
1612 : : * are asked to move to a new read position.
1613 : : */
1614 : : static void
3943 heikki.linnakangas@i 1615 : 6203 : ResetDecoder(XLogReaderState *state)
1616 : : {
1617 : : DecodedXLogRecord *r;
1618 : :
1619 : : /* Reset the decoded record queue, freeing any oversized records. */
1268 tmunro@postgresql.or 1620 [ + + ]: 16110 : while ((r = state->decode_queue_head) != NULL)
1621 : : {
1622 : 3704 : state->decode_queue_head = r->next;
1623 [ + - ]: 3704 : if (r->oversized)
1268 tmunro@postgresql.or 1624 :UBC 0 : pfree(r);
1625 : : }
1268 tmunro@postgresql.or 1626 :CBC 6203 : state->decode_queue_tail = NULL;
1627 : 6203 : state->decode_queue_head = NULL;
1628 : 6203 : state->record = NULL;
1629 : :
1630 : : /* Reset the decode buffer to empty. */
1631 : 6203 : state->decode_buffer_tail = state->decode_buffer;
1632 : 6203 : state->decode_buffer_head = state->decode_buffer;
1633 : :
1634 : : /* Clear error state. */
1635 : 6203 : state->errormsg_buf[0] = '\0';
1636 : 6203 : state->errormsg_deferred = false;
3943 heikki.linnakangas@i 1637 : 6203 : }
1638 : :
1639 : : /*
1640 : : * Compute the maximum possible amount of padding that could be required to
1641 : : * decode a record, given xl_tot_len from the record's header. This is the
1642 : : * amount of output buffer space that we need to decode a record, though we
1643 : : * might not finish up using it all.
1644 : : *
1645 : : * This computation is pessimistic and assumes the maximum possible number of
1646 : : * blocks, due to lack of better information.
1647 : : */
1648 : : size_t
1268 tmunro@postgresql.or 1649 : 10870349 : DecodeXLogRecordRequiredSpace(size_t xl_tot_len)
1650 : : {
1651 : 10870349 : size_t size = 0;
1652 : :
1653 : : /* Account for the fixed size part of the decoded record struct. */
1654 : 10870349 : size += offsetof(DecodedXLogRecord, blocks[0]);
1655 : : /* Account for the flexible blocks array of maximum possible size. */
1656 : 10870349 : size += sizeof(DecodedBkpBlock) * (XLR_MAX_BLOCK_ID + 1);
1657 : : /* Account for all the raw main and block data. */
1658 : 10870349 : size += xl_tot_len;
1659 : : /* We might insert padding before main_data. */
1660 : 10870349 : size += (MAXIMUM_ALIGNOF - 1);
1661 : : /* We might insert padding before each block's data. */
1662 : 10870349 : size += (MAXIMUM_ALIGNOF - 1) * (XLR_MAX_BLOCK_ID + 1);
1663 : : /* We might insert padding at the end. */
1664 : 10870349 : size += (MAXIMUM_ALIGNOF - 1);
1665 : :
1666 : 10870349 : return size;
1667 : : }
1668 : :
1669 : : /*
1670 : : * Decode a record. "decoded" must point to a MAXALIGNed memory area that has
1671 : : * space for at least DecodeXLogRecordRequiredSpace(record) bytes. On
1672 : : * success, decoded->size contains the actual space occupied by the decoded
1673 : : * record, which may turn out to be less.
1674 : : *
1675 : : * Only decoded->oversized member must be initialized already, and will not be
1676 : : * modified. Other members will be initialized as required.
1677 : : *
1678 : : * On error, a human-readable error message is returned in *errormsg, and
1679 : : * the return value is false.
1680 : : */
1681 : : bool
1682 : 5422516 : DecodeXLogRecord(XLogReaderState *state,
1683 : : DecodedXLogRecord *decoded,
1684 : : XLogRecord *record,
1685 : : XLogRecPtr lsn,
1686 : : char **errormsg)
1687 : : {
1688 : : /*
1689 : : * read next _size bytes from record buffer, but check for overrun first.
1690 : : */
1691 : : #define COPY_HEADER_FIELD(_dst, _size) \
1692 : : do { \
1693 : : if (remaining < _size) \
1694 : : goto shortdata_err; \
1695 : : memcpy(_dst, ptr, _size); \
1696 : : ptr += _size; \
1697 : : remaining -= _size; \
1698 : : } while(0)
1699 : :
1700 : : char *ptr;
1701 : : char *out;
1702 : : uint32 remaining;
1703 : : uint32 datatotal;
1158 rhaas@postgresql.org 1704 : 5422516 : RelFileLocator *rlocator = NULL;
1705 : : uint8 block_id;
1706 : :
1268 tmunro@postgresql.or 1707 : 5422516 : decoded->header = *record;
1708 : 5422516 : decoded->lsn = lsn;
1709 : 5422516 : decoded->next = NULL;
1710 : 5422516 : decoded->record_origin = InvalidRepOriginId;
1711 : 5422516 : decoded->toplevel_xid = InvalidTransactionId;
1712 : 5422516 : decoded->main_data = NULL;
1713 : 5422516 : decoded->main_data_len = 0;
1714 : 5422516 : decoded->max_block_id = -1;
3943 heikki.linnakangas@i 1715 : 5422516 : ptr = (char *) record;
1716 : 5422516 : ptr += SizeOfXLogRecord;
1717 : 5422516 : remaining = record->xl_tot_len - SizeOfXLogRecord;
1718 : :
1719 : : /* Decode the headers */
1720 : 5422516 : datatotal = 0;
1721 [ + + ]: 11170955 : while (remaining > datatotal)
1722 : : {
1723 [ - + ]: 11102410 : COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1724 : :
1725 [ + + ]: 11102410 : if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1726 : : {
1727 : : /* XLogRecordDataHeaderShort */
1728 : : uint8 main_data_len;
1729 : :
1730 [ - + ]: 5343043 : COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1731 : :
1268 tmunro@postgresql.or 1732 : 5343043 : decoded->main_data_len = main_data_len;
3943 heikki.linnakangas@i 1733 : 5343043 : datatotal += main_data_len;
1734 : 5343043 : break; /* by convention, the main data fragment is
1735 : : * always last */
1736 : : }
1737 [ + + ]: 5759367 : else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1738 : : {
1739 : : /* XLogRecordDataHeaderLong */
1740 : : uint32 main_data_len;
1741 : :
1742 [ - + ]: 10928 : COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1268 tmunro@postgresql.or 1743 : 10928 : decoded->main_data_len = main_data_len;
3943 heikki.linnakangas@i 1744 : 10928 : datatotal += main_data_len;
1745 : 10928 : break; /* by convention, the main data fragment is
1746 : : * always last */
1747 : : }
3783 andres@anarazel.de 1748 [ + + ]: 5748439 : else if (block_id == XLR_BLOCK_ID_ORIGIN)
1749 : : {
1268 tmunro@postgresql.or 1750 [ - + ]: 12928 : COPY_HEADER_FIELD(&decoded->record_origin, sizeof(RepOriginId));
1751 : : }
1874 akapila@postgresql.o 1752 [ + + ]: 5735511 : else if (block_id == XLR_BLOCK_ID_TOPLEVEL_XID)
1753 : : {
1268 tmunro@postgresql.or 1754 [ - + ]: 643 : COPY_HEADER_FIELD(&decoded->toplevel_xid, sizeof(TransactionId));
1755 : : }
3943 heikki.linnakangas@i 1756 [ + - ]: 5734868 : else if (block_id <= XLR_MAX_BLOCK_ID)
1757 : : {
1758 : : /* XLogRecordBlockHeader */
1759 : : DecodedBkpBlock *blk;
1760 : : uint8 fork_flags;
1761 : :
1762 : : /* mark any intervening block IDs as not in use */
1268 tmunro@postgresql.or 1763 [ + + ]: 5737831 : for (int i = decoded->max_block_id + 1; i < block_id; ++i)
1764 : 2963 : decoded->blocks[i].in_use = false;
1765 : :
1766 [ - + ]: 5734868 : if (block_id <= decoded->max_block_id)
1767 : : {
3943 heikki.linnakangas@i 1768 :UBC 0 : report_invalid_record(state,
1769 : : "out-of-order block_id %u at %X/%08X",
1770 : : block_id,
1656 peter@eisentraut.org 1771 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3943 heikki.linnakangas@i 1772 : 0 : goto err;
1773 : : }
1268 tmunro@postgresql.or 1774 :CBC 5734868 : decoded->max_block_id = block_id;
1775 : :
1776 : 5734868 : blk = &decoded->blocks[block_id];
3943 heikki.linnakangas@i 1777 : 5734868 : blk->in_use = true;
3132 rhaas@postgresql.org 1778 : 5734868 : blk->apply_image = false;
1779 : :
3943 heikki.linnakangas@i 1780 [ - + ]: 5734868 : COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1781 : 5734868 : blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1782 : 5734868 : blk->flags = fork_flags;
1783 : 5734868 : blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1784 : 5734868 : blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1785 : :
1248 tmunro@postgresql.or 1786 : 5734868 : blk->prefetch_buffer = InvalidBuffer;
1787 : :
3943 heikki.linnakangas@i 1788 [ - + ]: 5734868 : COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1789 : : /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1790 [ + + - + ]: 5734868 : if (blk->has_data && blk->data_len == 0)
1791 : : {
3943 heikki.linnakangas@i 1792 :UBC 0 : report_invalid_record(state,
1793 : : "BKPBLOCK_HAS_DATA set, but no data included at %X/%08X",
1656 peter@eisentraut.org 1794 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3834 fujii@postgresql.org 1795 : 0 : goto err;
1796 : : }
3943 heikki.linnakangas@i 1797 [ + + - + ]:CBC 5734868 : if (!blk->has_data && blk->data_len != 0)
1798 : : {
3943 heikki.linnakangas@i 1799 :UBC 0 : report_invalid_record(state,
1800 : : "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%08X",
1801 : 0 : (unsigned int) blk->data_len,
1656 peter@eisentraut.org 1802 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3834 fujii@postgresql.org 1803 : 0 : goto err;
1804 : : }
3943 heikki.linnakangas@i 1805 :CBC 5734868 : datatotal += blk->data_len;
1806 : :
1807 [ + + ]: 5734868 : if (blk->has_image)
1808 : : {
3832 fujii@postgresql.org 1809 [ - + ]: 2414066 : COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
3943 heikki.linnakangas@i 1810 [ - + ]: 2414066 : COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
3832 fujii@postgresql.org 1811 [ - + ]: 2414066 : COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1812 : :
3132 rhaas@postgresql.org 1813 : 2414066 : blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1814 : :
1530 michael@paquier.xyz 1815 [ - + ]: 2414066 : if (BKPIMAGE_COMPRESSED(blk->bimg_info))
1816 : : {
3832 fujii@postgresql.org 1817 [ # # ]:UBC 0 : if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1818 [ # # ]: 0 : COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1819 : : else
1820 : 0 : blk->hole_length = 0;
1821 : : }
1822 : : else
3832 fujii@postgresql.org 1823 :CBC 2414066 : blk->hole_length = BLCKSZ - blk->bimg_len;
1824 : 2414066 : datatotal += blk->bimg_len;
1825 : :
1826 : : /*
1827 : : * cross-check that hole_offset > 0, hole_length > 0 and
1828 : : * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1829 : : */
1830 [ + + ]: 2414066 : if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1831 [ + - ]: 2393055 : (blk->hole_offset == 0 ||
1832 [ + - ]: 2393055 : blk->hole_length == 0 ||
1833 [ - + ]: 2393055 : blk->bimg_len == BLCKSZ))
1834 : : {
3832 fujii@postgresql.org 1835 :UBC 0 : report_invalid_record(state,
1836 : : "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%08X",
1837 : 0 : (unsigned int) blk->hole_offset,
1838 : 0 : (unsigned int) blk->hole_length,
1839 : 0 : (unsigned int) blk->bimg_len,
1656 peter@eisentraut.org 1840 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3832 fujii@postgresql.org 1841 : 0 : goto err;
1842 : : }
1843 : :
1844 : : /*
1845 : : * cross-check that hole_offset == 0 and hole_length == 0 if
1846 : : * the HAS_HOLE flag is not set.
1847 : : */
3832 fujii@postgresql.org 1848 [ + + ]:CBC 2414066 : if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1849 [ + - - + ]: 21011 : (blk->hole_offset != 0 || blk->hole_length != 0))
1850 : : {
3832 fujii@postgresql.org 1851 :UBC 0 : report_invalid_record(state,
1852 : : "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%08X",
1853 : 0 : (unsigned int) blk->hole_offset,
1854 : 0 : (unsigned int) blk->hole_length,
1656 peter@eisentraut.org 1855 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3832 fujii@postgresql.org 1856 : 0 : goto err;
1857 : : }
1858 : :
1859 : : /*
1860 : : * Cross-check that bimg_len < BLCKSZ if it is compressed.
1861 : : */
1530 michael@paquier.xyz 1862 [ - + ]:CBC 2414066 : if (BKPIMAGE_COMPRESSED(blk->bimg_info) &&
3832 fujii@postgresql.org 1863 [ # # ]:UBC 0 : blk->bimg_len == BLCKSZ)
1864 : : {
1865 : 0 : report_invalid_record(state,
1866 : : "BKPIMAGE_COMPRESSED set, but block image length %u at %X/%08X",
1867 : 0 : (unsigned int) blk->bimg_len,
1656 peter@eisentraut.org 1868 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3832 fujii@postgresql.org 1869 : 0 : goto err;
1870 : : }
1871 : :
1872 : : /*
1873 : : * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE is
1874 : : * set nor COMPRESSED().
1875 : : */
3832 fujii@postgresql.org 1876 [ + + ]:CBC 2414066 : if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1530 michael@paquier.xyz 1877 [ + - ]: 21011 : !BKPIMAGE_COMPRESSED(blk->bimg_info) &&
3832 fujii@postgresql.org 1878 [ - + ]: 21011 : blk->bimg_len != BLCKSZ)
1879 : : {
3832 fujii@postgresql.org 1880 :UBC 0 : report_invalid_record(state,
1881 : : "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_COMPRESSED set, but block image length is %u at %X/%08X",
1882 : 0 : (unsigned int) blk->data_len,
1656 peter@eisentraut.org 1883 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3832 fujii@postgresql.org 1884 : 0 : goto err;
1885 : : }
1886 : : }
3943 heikki.linnakangas@i 1887 [ + + ]:CBC 5734868 : if (!(fork_flags & BKPBLOCK_SAME_REL))
1888 : : {
1158 rhaas@postgresql.org 1889 [ - + ]: 5290485 : COPY_HEADER_FIELD(&blk->rlocator, sizeof(RelFileLocator));
1890 : 5290485 : rlocator = &blk->rlocator;
1891 : : }
1892 : : else
1893 : : {
1894 [ - + ]: 444383 : if (rlocator == NULL)
1895 : : {
3943 heikki.linnakangas@i 1896 :UBC 0 : report_invalid_record(state,
1897 : : "BKPBLOCK_SAME_REL set but no previous rel at %X/%08X",
1656 peter@eisentraut.org 1898 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3943 heikki.linnakangas@i 1899 : 0 : goto err;
1900 : : }
1901 : :
1158 rhaas@postgresql.org 1902 :CBC 444383 : blk->rlocator = *rlocator;
1903 : : }
3943 heikki.linnakangas@i 1904 [ - + ]: 5734868 : COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1905 : : }
1906 : : else
1907 : : {
3943 heikki.linnakangas@i 1908 :UBC 0 : report_invalid_record(state,
1909 : : "invalid block_id %u at %X/%08X",
1656 peter@eisentraut.org 1910 : 0 : block_id, LSN_FORMAT_ARGS(state->ReadRecPtr));
3943 heikki.linnakangas@i 1911 : 0 : goto err;
1912 : : }
1913 : : }
1914 : :
3943 heikki.linnakangas@i 1915 [ - + ]:CBC 5422516 : if (remaining != datatotal)
3943 heikki.linnakangas@i 1916 :UBC 0 : goto shortdata_err;
1917 : :
1918 : : /*
1919 : : * Ok, we've parsed the fragment headers, and verified that the total
1920 : : * length of the payload in the fragments is equal to the amount of data
1921 : : * left. Copy the data of each fragment to contiguous space after the
1922 : : * blocks array, inserting alignment padding before the data fragments so
1923 : : * they can be cast to struct pointers by REDO routines.
1924 : : */
1268 tmunro@postgresql.or 1925 :CBC 5422516 : out = ((char *) decoded) +
1926 : 5422516 : offsetof(DecodedXLogRecord, blocks) +
1927 : 5422516 : sizeof(decoded->blocks[0]) * (decoded->max_block_id + 1);
1928 : :
1929 : : /* block data first */
1930 [ + + ]: 11160347 : for (block_id = 0; block_id <= decoded->max_block_id; block_id++)
1931 : : {
1932 : 5737831 : DecodedBkpBlock *blk = &decoded->blocks[block_id];
1933 : :
3943 heikki.linnakangas@i 1934 [ + + ]: 5737831 : if (!blk->in_use)
1935 : 2963 : continue;
1936 : :
3132 rhaas@postgresql.org 1937 [ + + - + ]: 5734868 : Assert(blk->has_image || !blk->apply_image);
1938 : :
3943 heikki.linnakangas@i 1939 [ + + ]: 5734868 : if (blk->has_image)
1940 : : {
1941 : : /* no need to align image */
1268 tmunro@postgresql.or 1942 : 2414066 : blk->bkp_image = out;
1943 : 2414066 : memcpy(out, ptr, blk->bimg_len);
3832 fujii@postgresql.org 1944 : 2414066 : ptr += blk->bimg_len;
1268 tmunro@postgresql.or 1945 : 2414066 : out += blk->bimg_len;
1946 : : }
3943 heikki.linnakangas@i 1947 [ + + ]: 5734868 : if (blk->has_data)
1948 : : {
1268 tmunro@postgresql.or 1949 : 4288669 : out = (char *) MAXALIGN(out);
1950 : 4288669 : blk->data = out;
3943 heikki.linnakangas@i 1951 : 4288669 : memcpy(blk->data, ptr, blk->data_len);
1952 : 4288669 : ptr += blk->data_len;
1268 tmunro@postgresql.or 1953 : 4288669 : out += blk->data_len;
1954 : : }
1955 : : }
1956 : :
1957 : : /* and finally, the main data */
1958 [ + + ]: 5422516 : if (decoded->main_data_len > 0)
1959 : : {
1960 : 5353971 : out = (char *) MAXALIGN(out);
1961 : 5353971 : decoded->main_data = out;
1962 : 5353971 : memcpy(decoded->main_data, ptr, decoded->main_data_len);
1963 : 5353971 : ptr += decoded->main_data_len;
1964 : 5353971 : out += decoded->main_data_len;
1965 : : }
1966 : :
1967 : : /* Report the actual size we used. */
1968 : 5422516 : decoded->size = MAXALIGN(out - (char *) decoded);
1969 [ - + ]: 5422516 : Assert(DecodeXLogRecordRequiredSpace(record->xl_tot_len) >=
1970 : : decoded->size);
1971 : :
3943 heikki.linnakangas@i 1972 : 5422516 : return true;
1973 : :
3943 heikki.linnakangas@i 1974 :UBC 0 : shortdata_err:
1975 : 0 : report_invalid_record(state,
1976 : : "record with invalid length at %X/%08X",
1656 peter@eisentraut.org 1977 : 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
3943 heikki.linnakangas@i 1978 : 0 : err:
1979 : 0 : *errormsg = state->errormsg_buf;
1980 : :
1981 : 0 : return false;
1982 : : }
1983 : :
1984 : : /*
1985 : : * Returns information about the block that a block reference refers to.
1986 : : *
1987 : : * This is like XLogRecGetBlockTagExtended, except that the block reference
1988 : : * must exist and there's no access to prefetch_buffer.
1989 : : */
1990 : : void
3943 heikki.linnakangas@i 1991 :CBC 3008162 : XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id,
1992 : : RelFileLocator *rlocator, ForkNumber *forknum,
1993 : : BlockNumber *blknum)
1994 : : {
1158 rhaas@postgresql.org 1995 [ - + ]: 3008162 : if (!XLogRecGetBlockTagExtended(record, block_id, rlocator, forknum,
1996 : : blknum, NULL))
1997 : : {
1998 : : #ifndef FRONTEND
1078 peter@eisentraut.org 1999 [ # # ]:UBC 0 : elog(ERROR, "could not locate backup block with ID %d in WAL record",
2000 : : block_id);
2001 : : #else
2002 : 0 : pg_fatal("could not locate backup block with ID %d in WAL record",
2003 : : block_id);
2004 : : #endif
2005 : : }
1248 tmunro@postgresql.or 2006 :CBC 3008162 : }
2007 : :
2008 : : /*
2009 : : * Returns information about the block that a block reference refers to,
2010 : : * optionally including the buffer that the block may already be in.
2011 : : *
2012 : : * If the WAL record contains a block reference with the given ID, *rlocator,
2013 : : * *forknum, *blknum and *prefetch_buffer are filled in (if not NULL), and
2014 : : * returns true. Otherwise returns false.
2015 : : */
2016 : : bool
2017 : 8905407 : XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id,
2018 : : RelFileLocator *rlocator, ForkNumber *forknum,
2019 : : BlockNumber *blknum,
2020 : : Buffer *prefetch_buffer)
2021 : : {
2022 : : DecodedBkpBlock *bkpb;
2023 : :
1244 tgl@sss.pgh.pa.us 2024 [ + + + + ]: 8905407 : if (!XLogRecHasBlockRef(record, block_id))
3943 heikki.linnakangas@i 2025 : 42162 : return false;
2026 : :
1268 tmunro@postgresql.or 2027 : 8863245 : bkpb = &record->record->blocks[block_id];
1158 rhaas@postgresql.org 2028 [ + + ]: 8863245 : if (rlocator)
2029 : 8803500 : *rlocator = bkpb->rlocator;
3943 heikki.linnakangas@i 2030 [ + + ]: 8863245 : if (forknum)
2031 : 5800395 : *forknum = bkpb->forknum;
2032 [ + + ]: 8863245 : if (blknum)
2033 : 7615477 : *blknum = bkpb->blkno;
1248 tmunro@postgresql.or 2034 [ + + ]: 8863245 : if (prefetch_buffer)
2035 : 2888467 : *prefetch_buffer = bkpb->prefetch_buffer;
3943 heikki.linnakangas@i 2036 : 8863245 : return true;
2037 : : }
2038 : :
2039 : : /*
2040 : : * Returns the data associated with a block reference, or NULL if there is
2041 : : * no data (e.g. because a full-page image was taken instead). The returned
2042 : : * pointer points to a MAXALIGNed buffer.
2043 : : */
2044 : : char *
2045 : 3228961 : XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
2046 : : {
2047 : : DecodedBkpBlock *bkpb;
2048 : :
1268 tmunro@postgresql.or 2049 [ + - ]: 3228961 : if (block_id > record->record->max_block_id ||
2050 [ - + ]: 3228961 : !record->record->blocks[block_id].in_use)
3943 heikki.linnakangas@i 2051 :UBC 0 : return NULL;
2052 : :
1268 tmunro@postgresql.or 2053 :CBC 3228961 : bkpb = &record->record->blocks[block_id];
2054 : :
3943 heikki.linnakangas@i 2055 [ + + ]: 3228961 : if (!bkpb->has_data)
2056 : : {
2057 [ + - ]: 228 : if (len)
2058 : 228 : *len = 0;
2059 : 228 : return NULL;
2060 : : }
2061 : : else
2062 : : {
2063 [ + + ]: 3228733 : if (len)
2064 : 3224716 : *len = bkpb->data_len;
2065 : 3228733 : return bkpb->data;
2066 : : }
2067 : : }
2068 : :
2069 : : /*
2070 : : * Restore a full-page image from a backup block attached to an XLOG record.
2071 : : *
2072 : : * Returns true if a full-page image is restored, and false on failure with
2073 : : * an error to be consumed by the caller.
2074 : : */
2075 : : bool
2076 : 2374794 : RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
2077 : : {
2078 : : DecodedBkpBlock *bkpb;
2079 : : char *ptr;
2080 : : PGAlignedBlock tmp;
2081 : :
1268 tmunro@postgresql.or 2082 [ + - ]: 2374794 : if (block_id > record->record->max_block_id ||
2083 [ - + ]: 2374794 : !record->record->blocks[block_id].in_use)
2084 : : {
1093 michael@paquier.xyz 2085 :UBC 0 : report_invalid_record(record,
2086 : : "could not restore image at %X/%08X with invalid block %d specified",
2087 : 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2088 : : block_id);
3943 heikki.linnakangas@i 2089 : 0 : return false;
2090 : : }
1268 tmunro@postgresql.or 2091 [ - + ]:CBC 2374794 : if (!record->record->blocks[block_id].has_image)
2092 : : {
61 alvherre@kurilemu.de 2093 :UNC 0 : report_invalid_record(record, "could not restore image at %X/%08X with invalid state, block %d",
1093 michael@paquier.xyz 2094 :UBC 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2095 : : block_id);
3943 heikki.linnakangas@i 2096 : 0 : return false;
2097 : : }
2098 : :
1268 tmunro@postgresql.or 2099 :CBC 2374794 : bkpb = &record->record->blocks[block_id];
3832 fujii@postgresql.org 2100 : 2374794 : ptr = bkpb->bkp_image;
2101 : :
1530 michael@paquier.xyz 2102 [ - + ]: 2374794 : if (BKPIMAGE_COMPRESSED(bkpb->bimg_info))
2103 : : {
2104 : : /* If a backup block image is compressed, decompress it */
1530 michael@paquier.xyz 2105 :UBC 0 : bool decomp_success = true;
2106 : :
2107 [ # # ]: 0 : if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
2108 : : {
2109 [ # # ]: 0 : if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
2110 : 0 : BLCKSZ - bkpb->hole_length, true) < 0)
2111 : 0 : decomp_success = false;
2112 : : }
2113 [ # # ]: 0 : else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
2114 : : {
2115 : : #ifdef USE_LZ4
2116 [ # # ]: 0 : if (LZ4_decompress_safe(ptr, tmp.data,
2117 : 0 : bkpb->bimg_len, BLCKSZ - bkpb->hole_length) <= 0)
2118 : 0 : decomp_success = false;
2119 : : #else
2120 : : report_invalid_record(record, "could not restore image at %X/%08X compressed with %s not supported by build, block %d",
2121 : : LSN_FORMAT_ARGS(record->ReadRecPtr),
2122 : : "LZ4",
2123 : : block_id);
2124 : : return false;
2125 : : #endif
2126 : : }
1275 2127 [ # # ]: 0 : else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_ZSTD) != 0)
2128 : : {
2129 : : #ifdef USE_ZSTD
2130 : 0 : size_t decomp_result = ZSTD_decompress(tmp.data,
2131 : 0 : BLCKSZ - bkpb->hole_length,
2132 : 0 : ptr, bkpb->bimg_len);
2133 : :
2134 [ # # ]: 0 : if (ZSTD_isError(decomp_result))
2135 : 0 : decomp_success = false;
2136 : : #else
2137 : : report_invalid_record(record, "could not restore image at %X/%08X compressed with %s not supported by build, block %d",
2138 : : LSN_FORMAT_ARGS(record->ReadRecPtr),
2139 : : "zstd",
2140 : : block_id);
2141 : : return false;
2142 : : #endif
2143 : : }
2144 : : else
2145 : : {
61 alvherre@kurilemu.de 2146 :UNC 0 : report_invalid_record(record, "could not restore image at %X/%08X compressed with unknown method, block %d",
1520 michael@paquier.xyz 2147 :UBC 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2148 : : block_id);
1530 2149 : 0 : return false;
2150 : : }
2151 : :
2152 [ # # ]: 0 : if (!decomp_success)
2153 : : {
61 alvherre@kurilemu.de 2154 :UNC 0 : report_invalid_record(record, "could not decompress image at %X/%08X, block %d",
1656 peter@eisentraut.org 2155 :UBC 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2156 : : block_id);
3832 fujii@postgresql.org 2157 : 0 : return false;
2158 : : }
2159 : :
2562 tgl@sss.pgh.pa.us 2160 : 0 : ptr = tmp.data;
2161 : : }
2162 : :
2163 : : /* generate page, taking into account hole if necessary */
3943 heikki.linnakangas@i 2164 [ + + ]:CBC 2374794 : if (bkpb->hole_length == 0)
2165 : : {
3832 fujii@postgresql.org 2166 : 17084 : memcpy(page, ptr, BLCKSZ);
2167 : : }
2168 : : else
2169 : : {
2170 : 2357710 : memcpy(page, ptr, bkpb->hole_offset);
2171 : : /* must zero-fill the hole */
3943 heikki.linnakangas@i 2172 [ + + + - : 12303666 : MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
+ - + + +
+ ]
2173 : 2357710 : memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
3832 fujii@postgresql.org 2174 : 2357710 : ptr + bkpb->hole_offset,
3943 heikki.linnakangas@i 2175 : 2357710 : BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
2176 : : }
2177 : :
2178 : 2374794 : return true;
2179 : : }
2180 : :
2181 : : #ifndef FRONTEND
2182 : :
2183 : : /*
2184 : : * Extract the FullTransactionId from a WAL record.
2185 : : */
2186 : : FullTransactionId
2245 tmunro@postgresql.or 2187 :UBC 0 : XLogRecGetFullXid(XLogReaderState *record)
2188 : : {
2189 : : /*
2190 : : * This function is only safe during replay, because it depends on the
2191 : : * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
2192 : : */
2193 [ # # # # ]: 0 : Assert(AmStartupProcess() || !IsUnderPostmaster);
2194 : :
224 noah@leadboat.com 2195 : 0 : return FullTransactionIdFromAllowableAt(TransamVariables->nextXid,
2196 : 0 : XLogRecGetXid(record));
2197 : : }
2198 : :
2199 : : #endif
|