Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * gistxlog.c
4 : : * WAL replay logic for GiST.
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/access/gist/gistxlog.c
12 : : *-------------------------------------------------------------------------
13 : : */
14 : : #include "postgres.h"
15 : :
16 : : #include "access/bufmask.h"
17 : : #include "access/gist_private.h"
18 : : #include "access/gistxlog.h"
19 : : #include "access/transam.h"
20 : : #include "access/xloginsert.h"
21 : : #include "access/xlogutils.h"
22 : : #include "storage/standby.h"
23 : : #include "utils/memutils.h"
24 : : #include "utils/rel.h"
25 : :
26 : : static MemoryContext opCtx; /* working memory for operations */
27 : :
28 : : /*
29 : : * Replay the clearing of F_FOLLOW_RIGHT flag on a child page.
30 : : *
31 : : * Even if the WAL record includes a full-page image, we have to update the
32 : : * follow-right flag, because that change is not included in the full-page
33 : : * image. To be sure that the intermediate state with the wrong flag value is
34 : : * not visible to concurrent Hot Standby queries, this function handles
35 : : * restoring the full-page image as well as updating the flag. (Note that
36 : : * we never need to do anything else to the child page in the current WAL
37 : : * action.)
38 : : */
39 : : static void
40 : 449 : gistRedoClearFollowRight(XLogReaderState *record, uint8 block_id)
41 : : {
42 : 449 : XLogRecPtr lsn = record->EndRecPtr;
43 : : Buffer buffer;
44 : : Page page;
45 : : XLogRedoAction action;
46 : :
47 : : /*
48 : : * Note that we still update the page even if it was restored from a full
49 : : * page image, because the updated NSN is not included in the image.
50 : : */
51 : 449 : action = XLogReadBufferForRedo(record, block_id, &buffer);
52 [ - + - - ]: 449 : if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
53 : : {
54 : 449 : page = BufferGetPage(buffer);
55 : :
56 : 449 : GistPageSetNSN(page, lsn);
57 : 449 : GistClearFollowRight(page);
58 : :
59 : 449 : PageSetLSN(page, lsn);
60 : 449 : MarkBufferDirty(buffer);
61 : : }
62 [ + - ]: 449 : if (BufferIsValid(buffer))
63 : 449 : UnlockReleaseBuffer(buffer);
64 : 449 : }
65 : :
66 : : /*
67 : : * redo any page update (except page split)
68 : : */
69 : : static void
70 : 59317 : gistRedoPageUpdateRecord(XLogReaderState *record)
71 : : {
72 : 59317 : XLogRecPtr lsn = record->EndRecPtr;
73 : 59317 : gistxlogPageUpdate *xldata = (gistxlogPageUpdate *) XLogRecGetData(record);
74 : : Buffer buffer;
75 : : Page page;
76 : :
77 [ + + ]: 59317 : if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
78 : : {
79 : : char *begin;
80 : : char *data;
81 : : Size datalen;
82 : 58950 : int ninserted PG_USED_FOR_ASSERTS_ONLY = 0;
83 : :
84 : 58950 : data = begin = XLogRecGetBlockData(record, 0, &datalen);
85 : :
86 : 58950 : page = BufferGetPage(buffer);
87 : :
88 [ + + + + ]: 58950 : if (xldata->ntodelete == 1 && xldata->ntoinsert == 1)
89 : 21288 : {
90 : : /*
91 : : * When replacing one tuple with one other tuple, we must use
92 : : * PageIndexTupleOverwrite for consistency with gistplacetopage.
93 : : */
94 : 21288 : OffsetNumber offnum = *((OffsetNumber *) data);
95 : : IndexTuple itup;
96 : : Size itupsize;
97 : :
98 : 21288 : data += sizeof(OffsetNumber);
99 : 21288 : itup = (IndexTuple) data;
100 : 21288 : itupsize = IndexTupleSize(itup);
101 [ - + ]: 21288 : if (!PageIndexTupleOverwrite(page, offnum, itup, itupsize))
102 [ # # ]: 0 : elog(ERROR, "failed to add item to GiST index page, size %zu bytes", itupsize);
103 : 21288 : data += itupsize;
104 : : /* should be nothing left after consuming 1 tuple */
105 : : Assert(data - begin == datalen);
106 : : /* update insertion count for assert check below */
107 : 21288 : ninserted++;
108 : : }
109 [ + + ]: 37662 : else if (xldata->ntodelete > 0)
110 : : {
111 : : /* Otherwise, delete old tuples if any */
112 : 456 : OffsetNumber *todelete = (OffsetNumber *) data;
113 : :
114 : 456 : data += sizeof(OffsetNumber) * xldata->ntodelete;
115 : :
116 : 456 : PageIndexMultiDelete(page, todelete, xldata->ntodelete);
117 : : }
118 : :
119 : : /* Add new tuples if any */
120 [ + + ]: 58950 : if (data - begin < datalen)
121 : : {
122 [ + + ]: 37652 : OffsetNumber off = (PageIsEmpty(page)) ? FirstOffsetNumber :
123 : 37572 : OffsetNumberNext(PageGetMaxOffsetNumber(page));
124 : :
125 [ + + ]: 75750 : while (data - begin < datalen)
126 : : {
127 : 38098 : IndexTuple itup = (IndexTuple) data;
128 : 38098 : Size sz = IndexTupleSize(itup);
129 : : OffsetNumber l;
130 : :
131 : 38098 : data += sz;
132 : :
133 : 38098 : l = PageAddItem(page, itup, sz, off, false, false);
134 [ - + ]: 38098 : if (l == InvalidOffsetNumber)
135 [ # # ]: 0 : elog(ERROR, "failed to add item to GiST index page, size %zu bytes", sz);
136 : 38098 : off++;
137 : 38098 : ninserted++;
138 : : }
139 : : }
140 : :
141 : : /* Check that XLOG record contained expected number of tuples */
142 : : Assert(ninserted == xldata->ntoinsert);
143 : :
144 : 58950 : PageSetLSN(page, lsn);
145 : 58950 : MarkBufferDirty(buffer);
146 : : }
147 : :
148 : : /*
149 : : * Fix follow-right data on left child page
150 : : *
151 : : * This must be done while still holding the lock on the target page. Note
152 : : * that even if the target page no longer exists, we still attempt to
153 : : * replay the change on the child page.
154 : : */
155 [ + + + - ]: 59317 : if (XLogRecHasBlockRef(record, 1))
156 : 447 : gistRedoClearFollowRight(record, 1);
157 : :
158 [ + - ]: 59317 : if (BufferIsValid(buffer))
159 : 59317 : UnlockReleaseBuffer(buffer);
160 : 59317 : }
161 : :
162 : :
163 : : /*
164 : : * redo delete on gist index page to remove tuples marked as DEAD during index
165 : : * tuple insertion
166 : : */
167 : : static void
168 : 3 : gistRedoDeleteRecord(XLogReaderState *record)
169 : : {
170 : 3 : XLogRecPtr lsn = record->EndRecPtr;
171 : 3 : gistxlogDelete *xldata = (gistxlogDelete *) XLogRecGetData(record);
172 : : Buffer buffer;
173 : : Page page;
174 : 3 : OffsetNumber *toDelete = xldata->offsets;
175 : :
176 : : /*
177 : : * If we have any conflict processing to do, it must happen before we
178 : : * update the page.
179 : : *
180 : : * GiST delete records can conflict with standby queries. You might think
181 : : * that vacuum records would conflict as well, but we've handled that
182 : : * already. XLOG_HEAP2_PRUNE_VACUUM_SCAN records provide the highest xid
183 : : * cleaned by the vacuum of the heap and so we can resolve any conflicts
184 : : * just once when that arrives. After that we know that no conflicts
185 : : * exist from individual gist vacuum records on that index.
186 : : */
187 [ + - ]: 3 : if (InHotStandby)
188 : : {
189 : : RelFileLocator rlocator;
190 : :
191 : 3 : XLogRecGetBlockTag(record, 0, &rlocator, NULL, NULL);
192 : :
193 : 3 : ResolveRecoveryConflictWithSnapshot(xldata->snapshotConflictHorizon,
194 : 3 : xldata->isCatalogRel,
195 : : rlocator);
196 : : }
197 : :
198 [ + - ]: 3 : if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
199 : : {
200 : 3 : page = BufferGetPage(buffer);
201 : :
202 : 3 : PageIndexMultiDelete(page, toDelete, xldata->ntodelete);
203 : :
204 : 3 : GistClearPageHasGarbage(page);
205 : :
206 : 3 : PageSetLSN(page, lsn);
207 : 3 : MarkBufferDirty(buffer);
208 : : }
209 : :
210 [ + - ]: 3 : if (BufferIsValid(buffer))
211 : 3 : UnlockReleaseBuffer(buffer);
212 : 3 : }
213 : :
214 : : /*
215 : : * Returns an array of index pointers.
216 : : */
217 : : static IndexTuple *
218 : 916 : decodePageSplitRecord(char *begin, int len, int *n)
219 : : {
220 : : char *ptr;
221 : 916 : int i = 0;
222 : : IndexTuple *tuples;
223 : :
224 : : /* extract the number of tuples */
225 : 916 : memcpy(n, begin, sizeof(int));
226 : 916 : ptr = begin + sizeof(int);
227 : :
228 : 916 : tuples = palloc_array(IndexTuple, *n);
229 : :
230 [ + + ]: 79650 : for (i = 0; i < *n; i++)
231 : : {
232 : : Assert(ptr - begin < len);
233 : 78734 : tuples[i] = (IndexTuple) ptr;
234 : 78734 : ptr += IndexTupleSize((IndexTuple) ptr);
235 : : }
236 : : Assert(ptr - begin == len);
237 : :
238 : 916 : return tuples;
239 : : }
240 : :
241 : : static void
242 : 455 : gistRedoPageSplitRecord(XLogReaderState *record)
243 : : {
244 : 455 : XLogRecPtr lsn = record->EndRecPtr;
245 : 455 : gistxlogPageSplit *xldata = (gistxlogPageSplit *) XLogRecGetData(record);
246 : 455 : Buffer firstbuffer = InvalidBuffer;
247 : : Buffer buffer;
248 : : Page page;
249 : : int i;
250 : 455 : bool isrootsplit = false;
251 : :
252 : : /*
253 : : * We must hold lock on the first-listed page throughout the action,
254 : : * including while updating the left child page (if any). We can unlock
255 : : * remaining pages in the list as soon as they've been written, because
256 : : * there is no path for concurrent queries to reach those pages without
257 : : * first visiting the first-listed page.
258 : : */
259 : :
260 : : /* loop around all pages */
261 [ + + ]: 1371 : for (i = 0; i < xldata->npage; i++)
262 : : {
263 : : int flags;
264 : : char *data;
265 : : Size datalen;
266 : : int num;
267 : : BlockNumber blkno;
268 : : IndexTuple *tuples;
269 : :
270 : 916 : XLogRecGetBlockTag(record, i + 1, NULL, NULL, &blkno);
271 [ + + ]: 916 : if (blkno == GIST_ROOT_BLKNO)
272 : : {
273 : : Assert(i == 0);
274 : 6 : isrootsplit = true;
275 : : }
276 : :
277 : 916 : buffer = XLogInitBufferForRedo(record, i + 1);
278 : 916 : page = BufferGetPage(buffer);
279 : 916 : data = XLogRecGetBlockData(record, i + 1, &datalen);
280 : :
281 : 916 : tuples = decodePageSplitRecord(data, datalen, &num);
282 : :
283 : : /* ok, clear buffer */
284 [ + + + + ]: 916 : if (xldata->origleaf && blkno != GIST_ROOT_BLKNO)
285 : 906 : flags = F_LEAF;
286 : : else
287 : 10 : flags = 0;
288 : 916 : GISTInitBuffer(buffer, flags);
289 : :
290 : : /* and fill it */
291 : 916 : gistfillbuffer(page, tuples, num, FirstOffsetNumber);
292 : :
293 [ + + ]: 916 : if (blkno == GIST_ROOT_BLKNO)
294 : : {
295 : 6 : GistPageGetOpaque(page)->rightlink = InvalidBlockNumber;
296 : 6 : GistPageSetNSN(page, xldata->orignsn);
297 : 6 : GistClearFollowRight(page);
298 : : }
299 : : else
300 : : {
301 [ + + ]: 910 : if (i < xldata->npage - 1)
302 : : {
303 : : BlockNumber nextblkno;
304 : :
305 : 455 : XLogRecGetBlockTag(record, i + 2, NULL, NULL, &nextblkno);
306 : 455 : GistPageGetOpaque(page)->rightlink = nextblkno;
307 : : }
308 : : else
309 : 455 : GistPageGetOpaque(page)->rightlink = xldata->origrlink;
310 : 910 : GistPageSetNSN(page, xldata->orignsn);
311 [ + + + + ]: 910 : if (i < xldata->npage - 1 && !isrootsplit &&
312 [ + - ]: 449 : xldata->markfollowright)
313 : 449 : GistMarkFollowRight(page);
314 : : else
315 : 461 : GistClearFollowRight(page);
316 : : }
317 : :
318 : 916 : PageSetLSN(page, lsn);
319 : 916 : MarkBufferDirty(buffer);
320 : :
321 [ + + ]: 916 : if (i == 0)
322 : 455 : firstbuffer = buffer;
323 : : else
324 : 461 : UnlockReleaseBuffer(buffer);
325 : : }
326 : :
327 : : /* Fix follow-right data on left child page, if any */
328 [ + - + + ]: 455 : if (XLogRecHasBlockRef(record, 0))
329 : 2 : gistRedoClearFollowRight(record, 0);
330 : :
331 : : /* Finally, release lock on the first page */
332 : 455 : UnlockReleaseBuffer(firstbuffer);
333 : 455 : }
334 : :
335 : : /* redo page deletion */
336 : : static void
337 : 81 : gistRedoPageDelete(XLogReaderState *record)
338 : : {
339 : 81 : XLogRecPtr lsn = record->EndRecPtr;
340 : 81 : gistxlogPageDelete *xldata = (gistxlogPageDelete *) XLogRecGetData(record);
341 : : Buffer parentBuffer;
342 : : Buffer leafBuffer;
343 : :
344 [ + - ]: 81 : if (XLogReadBufferForRedo(record, 0, &leafBuffer) == BLK_NEEDS_REDO)
345 : : {
346 : 81 : Page page = BufferGetPage(leafBuffer);
347 : :
348 : 81 : GistPageSetDeleted(page, xldata->deleteXid);
349 : :
350 : 81 : PageSetLSN(page, lsn);
351 : 81 : MarkBufferDirty(leafBuffer);
352 : : }
353 : :
354 [ + + ]: 81 : if (XLogReadBufferForRedo(record, 1, &parentBuffer) == BLK_NEEDS_REDO)
355 : : {
356 : 80 : Page page = BufferGetPage(parentBuffer);
357 : :
358 : 80 : PageIndexTupleDelete(page, xldata->downlinkOffset);
359 : :
360 : 80 : PageSetLSN(page, lsn);
361 : 80 : MarkBufferDirty(parentBuffer);
362 : : }
363 : :
364 [ + - ]: 81 : if (BufferIsValid(parentBuffer))
365 : 81 : UnlockReleaseBuffer(parentBuffer);
366 [ + - ]: 81 : if (BufferIsValid(leafBuffer))
367 : 81 : UnlockReleaseBuffer(leafBuffer);
368 : 81 : }
369 : :
370 : : static void
371 : 0 : gistRedoPageReuse(XLogReaderState *record)
372 : : {
373 : 0 : gistxlogPageReuse *xlrec = (gistxlogPageReuse *) XLogRecGetData(record);
374 : :
375 : : /*
376 : : * PAGE_REUSE records exist to provide a conflict point when we reuse
377 : : * pages in the index via the FSM. That's all they do though.
378 : : *
379 : : * snapshotConflictHorizon was the page's deleteXid. The
380 : : * GlobalVisCheckRemovableFullXid(deleteXid) test in gistPageRecyclable()
381 : : * conceptually mirrors the PGPROC->xmin > limitXmin test in
382 : : * GetConflictingVirtualXIDs(). Consequently, one XID value achieves the
383 : : * same exclusion effect on primary and standby.
384 : : */
385 [ # # ]: 0 : if (InHotStandby)
386 : 0 : ResolveRecoveryConflictWithSnapshotFullXid(xlrec->snapshotConflictHorizon,
387 : 0 : xlrec->isCatalogRel,
388 : : xlrec->locator);
389 : 0 : }
390 : :
391 : : void
392 : 59856 : gist_redo(XLogReaderState *record)
393 : : {
394 : 59856 : uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
395 : : MemoryContext oldCxt;
396 : :
397 : : /*
398 : : * GiST indexes do not require any conflict processing. NB: If we ever
399 : : * implement a similar optimization we have in b-tree, and remove killed
400 : : * tuples outside VACUUM, we'll need to handle that here.
401 : : */
402 : :
403 : 59856 : oldCxt = MemoryContextSwitchTo(opCtx);
404 [ + + - + : 59856 : switch (info)
+ - ]
405 : : {
406 : 59317 : case XLOG_GIST_PAGE_UPDATE:
407 : 59317 : gistRedoPageUpdateRecord(record);
408 : 59317 : break;
409 : 3 : case XLOG_GIST_DELETE:
410 : 3 : gistRedoDeleteRecord(record);
411 : 3 : break;
412 : 0 : case XLOG_GIST_PAGE_REUSE:
413 : 0 : gistRedoPageReuse(record);
414 : 0 : break;
415 : 455 : case XLOG_GIST_PAGE_SPLIT:
416 : 455 : gistRedoPageSplitRecord(record);
417 : 455 : break;
418 : 81 : case XLOG_GIST_PAGE_DELETE:
419 : 81 : gistRedoPageDelete(record);
420 : 81 : break;
421 : 0 : default:
422 [ # # ]: 0 : elog(PANIC, "gist_redo: unknown op code %u", info);
423 : : }
424 : :
425 : 59856 : MemoryContextSwitchTo(oldCxt);
426 : 59856 : MemoryContextReset(opCtx);
427 : 59856 : }
428 : :
429 : : void
430 : 230 : gist_xlog_startup(void)
431 : : {
432 : 230 : opCtx = createTempGistContext();
433 : 230 : }
434 : :
435 : : void
436 : 164 : gist_xlog_cleanup(void)
437 : : {
438 : 164 : MemoryContextDelete(opCtx);
439 : 164 : }
440 : :
441 : : /*
442 : : * Mask a Gist page before running consistency checks on it.
443 : : */
444 : : void
445 : 120958 : gist_mask(char *pagedata, BlockNumber blkno)
446 : : {
447 : 120958 : Page page = (Page) pagedata;
448 : :
449 : 120958 : mask_page_lsn_and_checksum(page);
450 : :
451 : 120958 : mask_page_hint_bits(page);
452 : 120958 : mask_unused_space(page);
453 : :
454 : : /*
455 : : * NSN is nothing but a special purpose LSN. Hence, mask it for the same
456 : : * reason as mask_page_lsn_and_checksum.
457 : : */
458 : 120958 : GistPageSetNSN(page, (uint64) MASK_MARKER);
459 : :
460 : : /*
461 : : * We update F_FOLLOW_RIGHT flag on the left child after writing WAL
462 : : * record. Hence, mask this flag. See gistplacetopage() for details.
463 : : */
464 : 120958 : GistMarkFollowRight(page);
465 : :
466 [ + + ]: 120958 : if (GistPageIsLeaf(page))
467 : : {
468 : : /*
469 : : * In gist leaf pages, it is possible to modify the LP_FLAGS without
470 : : * emitting any WAL record. Hence, mask the line pointer flags. See
471 : : * gistkillitems() for details.
472 : : */
473 : 77310 : mask_lp_flags(page);
474 : : }
475 : :
476 : : /*
477 : : * During gist redo, we never mark a page as garbage. Hence, mask it to
478 : : * ignore any differences.
479 : : */
480 : 120958 : GistClearPageHasGarbage(page);
481 : 120958 : }
482 : :
483 : : /*
484 : : * Write WAL record of a page split.
485 : : */
486 : : XLogRecPtr
487 : 2215 : gistXLogSplit(bool page_is_leaf,
488 : : SplitPageLayout *dist,
489 : : BlockNumber origrlink, GistNSN orignsn,
490 : : Buffer leftchildbuf, bool markfollowright)
491 : : {
492 : : gistxlogPageSplit xlrec;
493 : : SplitPageLayout *ptr;
494 : 2215 : int npage = 0;
495 : : XLogRecPtr recptr;
496 : : int i;
497 : :
498 [ + + ]: 6710 : for (ptr = dist; ptr; ptr = ptr->next)
499 : 4495 : npage++;
500 : :
501 : 2215 : xlrec.origrlink = origrlink;
502 : 2215 : xlrec.orignsn = orignsn;
503 : 2215 : xlrec.origleaf = page_is_leaf;
504 : 2215 : xlrec.npage = (uint16) npage;
505 : 2215 : xlrec.markfollowright = markfollowright;
506 : :
507 : 2215 : XLogBeginInsert();
508 : :
509 : : /*
510 : : * Include a full page image of the child buf. (only necessary if a
511 : : * checkpoint happened since the child page was split)
512 : : */
513 [ + + ]: 2215 : if (BufferIsValid(leftchildbuf))
514 : 8 : XLogRegisterBuffer(0, leftchildbuf, REGBUF_STANDARD);
515 : :
516 : : /*
517 : : * NOTE: We register a lot of data. The caller must've called
518 : : * XLogEnsureRecordSpace() to prepare for that. We cannot do it here,
519 : : * because we're already in a critical section. If you change the number
520 : : * of buffer or data registrations here, make sure you modify the
521 : : * XLogEnsureRecordSpace() calls accordingly!
522 : : */
523 : 2215 : XLogRegisterData(&xlrec, sizeof(gistxlogPageSplit));
524 : :
525 : 2215 : i = 1;
526 [ + + ]: 6710 : for (ptr = dist; ptr; ptr = ptr->next)
527 : : {
528 : 4495 : XLogRegisterBuffer(i, ptr->buffer, REGBUF_WILL_INIT);
529 : 4495 : XLogRegisterBufData(i, &(ptr->block.num), sizeof(int));
530 : 4495 : XLogRegisterBufData(i, ptr->list, ptr->lenlist);
531 : 4495 : i++;
532 : : }
533 : :
534 : 2215 : recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_SPLIT);
535 : :
536 : 2215 : return recptr;
537 : : }
538 : :
539 : : /*
540 : : * Write XLOG record describing a page deletion. This also includes removal of
541 : : * downlink from the parent page.
542 : : */
543 : : XLogRecPtr
544 : 324 : gistXLogPageDelete(Buffer buffer, FullTransactionId xid,
545 : : Buffer parentBuffer, OffsetNumber downlinkOffset)
546 : : {
547 : : gistxlogPageDelete xlrec;
548 : : XLogRecPtr recptr;
549 : :
550 : 324 : xlrec.deleteXid = xid;
551 : 324 : xlrec.downlinkOffset = downlinkOffset;
552 : :
553 : 324 : XLogBeginInsert();
554 : 324 : XLogRegisterData(&xlrec, SizeOfGistxlogPageDelete);
555 : :
556 : 324 : XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
557 : 324 : XLogRegisterBuffer(1, parentBuffer, REGBUF_STANDARD);
558 : :
559 : 324 : recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_DELETE);
560 : :
561 : 324 : return recptr;
562 : : }
563 : :
564 : : /*
565 : : * Write XLOG record about reuse of a deleted page.
566 : : */
567 : : void
568 : 0 : gistXLogPageReuse(Relation rel, Relation heaprel,
569 : : BlockNumber blkno, FullTransactionId deleteXid)
570 : : {
571 : : gistxlogPageReuse xlrec_reuse;
572 : :
573 : : /*
574 : : * Note that we don't register the buffer with the record, because this
575 : : * operation doesn't modify the page. This record only exists to provide a
576 : : * conflict point for Hot Standby.
577 : : */
578 : :
579 : : /* XLOG stuff */
580 [ # # # # : 0 : xlrec_reuse.isCatalogRel = RelationIsAccessibleInLogicalDecoding(heaprel);
# # # # #
# # # # #
# # # # #
# # # ]
581 : 0 : xlrec_reuse.locator = rel->rd_locator;
582 : 0 : xlrec_reuse.block = blkno;
583 : 0 : xlrec_reuse.snapshotConflictHorizon = deleteXid;
584 : :
585 : 0 : XLogBeginInsert();
586 : 0 : XLogRegisterData(&xlrec_reuse, SizeOfGistxlogPageReuse);
587 : :
588 : 0 : XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_REUSE);
589 : 0 : }
590 : :
591 : : /*
592 : : * Write XLOG record describing a page update. The update can include any
593 : : * number of deletions and/or insertions of tuples on a single index page.
594 : : *
595 : : * If this update inserts a downlink for a split page, also record that
596 : : * the F_FOLLOW_RIGHT flag on the child page is cleared and NSN set.
597 : : *
598 : : * Note that both the todelete array and the tuples are marked as belonging
599 : : * to the target buffer; they need not be stored in XLOG if XLogInsert decides
600 : : * to log the whole buffer contents instead.
601 : : */
602 : : XLogRecPtr
603 : 313591 : gistXLogUpdate(Buffer buffer,
604 : : OffsetNumber *todelete, int ntodelete,
605 : : IndexTuple *itup, int ituplen,
606 : : Buffer leftchildbuf)
607 : : {
608 : : gistxlogPageUpdate xlrec;
609 : : int i;
610 : : XLogRecPtr recptr;
611 : :
612 : 313591 : xlrec.ntodelete = ntodelete;
613 : 313591 : xlrec.ntoinsert = ituplen;
614 : :
615 : 313591 : XLogBeginInsert();
616 : 313591 : XLogRegisterData(&xlrec, sizeof(gistxlogPageUpdate));
617 : :
618 : 313591 : XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
619 : 313591 : XLogRegisterBufData(0, todelete, sizeof(OffsetNumber) * ntodelete);
620 : :
621 : : /* new tuples */
622 [ + + ]: 628578 : for (i = 0; i < ituplen; i++)
623 : 314987 : XLogRegisterBufData(0, itup[i], IndexTupleSize(itup[i]));
624 : :
625 : : /*
626 : : * Include a full page image of the child buf. (only necessary if a
627 : : * checkpoint happened since the child page was split)
628 : : */
629 [ + + ]: 313591 : if (BufferIsValid(leftchildbuf))
630 : 2142 : XLogRegisterBuffer(1, leftchildbuf, REGBUF_STANDARD);
631 : :
632 : 313591 : recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE);
633 : :
634 : 313591 : return recptr;
635 : : }
636 : :
637 : : /*
638 : : * Write XLOG record describing a delete of leaf index tuples marked as DEAD
639 : : * during new tuple insertion. One may think that this case is already covered
640 : : * by gistXLogUpdate(). But deletion of index tuples might conflict with
641 : : * standby queries and needs special handling.
642 : : */
643 : : XLogRecPtr
644 : 12 : gistXLogDelete(Buffer buffer, OffsetNumber *todelete, int ntodelete,
645 : : TransactionId snapshotConflictHorizon, Relation heaprel)
646 : : {
647 : : gistxlogDelete xlrec;
648 : : XLogRecPtr recptr;
649 : :
650 [ + - - + : 12 : xlrec.isCatalogRel = RelationIsAccessibleInLogicalDecoding(heaprel);
- - - - -
- - - - -
- - - - -
- - - ]
651 : 12 : xlrec.snapshotConflictHorizon = snapshotConflictHorizon;
652 : 12 : xlrec.ntodelete = ntodelete;
653 : :
654 : 12 : XLogBeginInsert();
655 : 12 : XLogRegisterData(&xlrec, SizeOfGistxlogDelete);
656 : :
657 : : /*
658 : : * We need the target-offsets array whether or not we store the whole
659 : : * buffer, to allow us to find the snapshotConflictHorizon on a standby
660 : : * server.
661 : : */
662 : 12 : XLogRegisterData(todelete, ntodelete * sizeof(OffsetNumber));
663 : :
664 : 12 : XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
665 : :
666 : 12 : recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_DELETE);
667 : :
668 : 12 : return recptr;
669 : : }
|