Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * ginxlog.c
4 : * WAL replay logic for inverted index.
5 : *
6 : *
7 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 : * Portions Copyright (c) 1994, Regents of the University of California
9 : *
10 : * IDENTIFICATION
11 : * src/backend/access/gin/ginxlog.c
12 : *-------------------------------------------------------------------------
13 : */
14 : #include "postgres.h"
15 :
16 : #include "access/bufmask.h"
17 : #include "access/gin_private.h"
18 : #include "access/ginxlog.h"
19 : #include "access/xlogutils.h"
20 : #include "utils/memutils.h"
21 :
22 : static MemoryContext opCtx; /* working memory for operations */
23 :
24 : static void
25 278 : ginRedoClearIncompleteSplit(XLogReaderState *record, uint8 block_id)
26 : {
27 278 : XLogRecPtr lsn = record->EndRecPtr;
28 : Buffer buffer;
29 : Page page;
30 :
31 278 : if (XLogReadBufferForRedo(record, block_id, &buffer) == BLK_NEEDS_REDO)
32 : {
33 276 : page = BufferGetPage(buffer);
34 276 : GinPageGetOpaque(page)->flags &= ~GIN_INCOMPLETE_SPLIT;
35 :
36 276 : PageSetLSN(page, lsn);
37 276 : MarkBufferDirty(buffer);
38 : }
39 278 : if (BufferIsValid(buffer))
40 278 : UnlockReleaseBuffer(buffer);
41 278 : }
42 :
43 : static void
44 12 : ginRedoCreatePTree(XLogReaderState *record)
45 : {
46 12 : XLogRecPtr lsn = record->EndRecPtr;
47 12 : ginxlogCreatePostingTree *data = (ginxlogCreatePostingTree *) XLogRecGetData(record);
48 : char *ptr;
49 : Buffer buffer;
50 : Page page;
51 :
52 12 : buffer = XLogInitBufferForRedo(record, 0);
53 12 : page = BufferGetPage(buffer);
54 :
55 12 : GinInitBuffer(buffer, GIN_DATA | GIN_LEAF | GIN_COMPRESSED);
56 :
57 12 : ptr = XLogRecGetData(record) + sizeof(ginxlogCreatePostingTree);
58 :
59 : /* Place page data */
60 12 : memcpy(GinDataLeafPageGetPostingList(page), ptr, data->size);
61 :
62 12 : GinDataPageSetDataSize(page, data->size);
63 :
64 12 : PageSetLSN(page, lsn);
65 :
66 12 : MarkBufferDirty(buffer);
67 12 : UnlockReleaseBuffer(buffer);
68 12 : }
69 :
70 : static void
71 47232 : ginRedoInsertEntry(Buffer buffer, bool isLeaf, BlockNumber rightblkno, void *rdata)
72 : {
73 47232 : Page page = BufferGetPage(buffer);
74 47232 : ginxlogInsertEntry *data = (ginxlogInsertEntry *) rdata;
75 47232 : OffsetNumber offset = data->offset;
76 : IndexTuple itup;
77 :
78 47232 : if (rightblkno != InvalidBlockNumber)
79 : {
80 : /* update link to right page after split */
81 : Assert(!GinPageIsLeaf(page));
82 : Assert(offset >= FirstOffsetNumber && offset <= PageGetMaxOffsetNumber(page));
83 260 : itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offset));
84 260 : GinSetDownlink(itup, rightblkno);
85 : }
86 :
87 47232 : if (data->isDelete)
88 : {
89 : Assert(GinPageIsLeaf(page));
90 : Assert(offset >= FirstOffsetNumber && offset <= PageGetMaxOffsetNumber(page));
91 7258 : PageIndexTupleDelete(page, offset);
92 : }
93 :
94 47232 : itup = &data->tuple;
95 :
96 47232 : if (PageAddItem(page, (Item) itup, IndexTupleSize(itup), offset, false, false) == InvalidOffsetNumber)
97 : {
98 : RelFileLocator locator;
99 : ForkNumber forknum;
100 : BlockNumber blknum;
101 :
102 0 : BufferGetTag(buffer, &locator, &forknum, &blknum);
103 0 : elog(ERROR, "failed to add item to index page in %u/%u/%u",
104 : locator.spcOid, locator.dbOid, locator.relNumber);
105 : }
106 47232 : }
107 :
108 : /*
109 : * Redo recompression of posting list. Doing all the changes in-place is not
110 : * always possible, because it might require more space than we've on the page.
111 : * Instead, once modification is required we copy unprocessed tail of the page
112 : * into separately allocated chunk of memory for further reading original
113 : * versions of segments. Thanks to that we don't bother about moving page data
114 : * in-place.
115 : */
116 : static void
117 6680 : ginRedoRecompress(Page page, ginxlogRecompressDataLeaf *data)
118 : {
119 : int actionno;
120 : int segno;
121 : GinPostingList *oldseg;
122 : Pointer segmentend;
123 : char *walbuf;
124 : int totalsize;
125 6680 : Pointer tailCopy = NULL;
126 : Pointer writePtr;
127 : Pointer segptr;
128 :
129 : /*
130 : * If the page is in pre-9.4 format, convert to new format first.
131 : */
132 6680 : if (!GinPageIsCompressed(page))
133 : {
134 0 : ItemPointer uncompressed = (ItemPointer) GinDataPageGetData(page);
135 0 : int nuncompressed = GinPageGetOpaque(page)->maxoff;
136 : int npacked;
137 :
138 : /*
139 : * Empty leaf pages are deleted as part of vacuum, but leftmost and
140 : * rightmost pages are never deleted. So, pg_upgrade'd from pre-9.4
141 : * instances might contain empty leaf pages, and we need to handle
142 : * them correctly.
143 : */
144 0 : if (nuncompressed > 0)
145 : {
146 : GinPostingList *plist;
147 :
148 0 : plist = ginCompressPostingList(uncompressed, nuncompressed,
149 : BLCKSZ, &npacked);
150 0 : totalsize = SizeOfGinPostingList(plist);
151 :
152 : Assert(npacked == nuncompressed);
153 :
154 0 : memcpy(GinDataLeafPageGetPostingList(page), plist, totalsize);
155 : }
156 : else
157 : {
158 0 : totalsize = 0;
159 : }
160 :
161 0 : GinDataPageSetDataSize(page, totalsize);
162 0 : GinPageSetCompressed(page);
163 0 : GinPageGetOpaque(page)->maxoff = InvalidOffsetNumber;
164 : }
165 :
166 6680 : oldseg = GinDataLeafPageGetPostingList(page);
167 6680 : writePtr = (Pointer) oldseg;
168 6680 : segmentend = (Pointer) oldseg + GinDataLeafPageGetPostingListSize(page);
169 6680 : segno = 0;
170 :
171 6680 : walbuf = ((char *) data) + sizeof(ginxlogRecompressDataLeaf);
172 13390 : for (actionno = 0; actionno < data->nactions; actionno++)
173 : {
174 6710 : uint8 a_segno = *((uint8 *) (walbuf++));
175 6710 : uint8 a_action = *((uint8 *) (walbuf++));
176 6710 : GinPostingList *newseg = NULL;
177 6710 : int newsegsize = 0;
178 6710 : ItemPointerData *items = NULL;
179 6710 : uint16 nitems = 0;
180 : ItemPointerData *olditems;
181 : int nolditems;
182 : ItemPointerData *newitems;
183 : int nnewitems;
184 : int segsize;
185 :
186 : /* Extract all the information we need from the WAL record */
187 6710 : if (a_action == GIN_SEGMENT_INSERT ||
188 : a_action == GIN_SEGMENT_REPLACE)
189 : {
190 56 : newseg = (GinPostingList *) walbuf;
191 56 : newsegsize = SizeOfGinPostingList(newseg);
192 56 : walbuf += SHORTALIGN(newsegsize);
193 : }
194 :
195 6710 : if (a_action == GIN_SEGMENT_ADDITEMS)
196 : {
197 6642 : memcpy(&nitems, walbuf, sizeof(uint16));
198 6642 : walbuf += sizeof(uint16);
199 6642 : items = (ItemPointerData *) walbuf;
200 6642 : walbuf += nitems * sizeof(ItemPointerData);
201 : }
202 :
203 : /* Skip to the segment that this action concerns */
204 : Assert(segno <= a_segno);
205 119346 : while (segno < a_segno)
206 : {
207 : /*
208 : * Once modification is started and page tail is copied, we've to
209 : * copy unmodified segments.
210 : */
211 112636 : segsize = SizeOfGinPostingList(oldseg);
212 112636 : if (tailCopy)
213 : {
214 : Assert(writePtr + segsize < PageGetSpecialPointer(page));
215 0 : memcpy(writePtr, (Pointer) oldseg, segsize);
216 : }
217 112636 : writePtr += segsize;
218 112636 : oldseg = GinNextPostingListSegment(oldseg);
219 112636 : segno++;
220 : }
221 :
222 : /*
223 : * ADDITEMS action is handled like REPLACE, but the new segment to
224 : * replace the old one is reconstructed using the old segment from
225 : * disk and the new items from the WAL record.
226 : */
227 6710 : if (a_action == GIN_SEGMENT_ADDITEMS)
228 : {
229 : int npacked;
230 :
231 6642 : olditems = ginPostingListDecode(oldseg, &nolditems);
232 :
233 6642 : newitems = ginMergeItemPointers(items, nitems,
234 : olditems, nolditems,
235 : &nnewitems);
236 : Assert(nnewitems == nolditems + nitems);
237 :
238 6642 : newseg = ginCompressPostingList(newitems, nnewitems,
239 : BLCKSZ, &npacked);
240 : Assert(npacked == nnewitems);
241 :
242 6642 : newsegsize = SizeOfGinPostingList(newseg);
243 6642 : a_action = GIN_SEGMENT_REPLACE;
244 : }
245 :
246 6710 : segptr = (Pointer) oldseg;
247 6710 : if (segptr != segmentend)
248 6666 : segsize = SizeOfGinPostingList(oldseg);
249 : else
250 : {
251 : /*
252 : * Positioned after the last existing segment. Only INSERTs
253 : * expected here.
254 : */
255 : Assert(a_action == GIN_SEGMENT_INSERT);
256 44 : segsize = 0;
257 : }
258 :
259 : /*
260 : * We're about to start modification of the page. So, copy tail of
261 : * the page if it's not done already.
262 : */
263 6710 : if (!tailCopy && segptr != segmentend)
264 : {
265 6654 : int tailSize = segmentend - segptr;
266 :
267 6654 : tailCopy = (Pointer) palloc(tailSize);
268 6654 : memcpy(tailCopy, segptr, tailSize);
269 6654 : segptr = tailCopy;
270 6654 : oldseg = (GinPostingList *) segptr;
271 6654 : segmentend = segptr + tailSize;
272 : }
273 :
274 6710 : switch (a_action)
275 : {
276 12 : case GIN_SEGMENT_DELETE:
277 12 : segptr += segsize;
278 12 : segno++;
279 12 : break;
280 :
281 44 : case GIN_SEGMENT_INSERT:
282 : /* copy the new segment in place */
283 : Assert(writePtr + newsegsize <= PageGetSpecialPointer(page));
284 44 : memcpy(writePtr, newseg, newsegsize);
285 44 : writePtr += newsegsize;
286 44 : break;
287 :
288 6654 : case GIN_SEGMENT_REPLACE:
289 : /* copy the new version of segment in place */
290 : Assert(writePtr + newsegsize <= PageGetSpecialPointer(page));
291 6654 : memcpy(writePtr, newseg, newsegsize);
292 6654 : writePtr += newsegsize;
293 6654 : segptr += segsize;
294 6654 : segno++;
295 6654 : break;
296 :
297 0 : default:
298 0 : elog(ERROR, "unexpected GIN leaf action: %u", a_action);
299 : }
300 6710 : oldseg = (GinPostingList *) segptr;
301 : }
302 :
303 : /* Copy the rest of unmodified segments if any. */
304 6680 : segptr = (Pointer) oldseg;
305 6680 : if (segptr != segmentend && tailCopy)
306 : {
307 6 : int restSize = segmentend - segptr;
308 :
309 : Assert(writePtr + restSize <= PageGetSpecialPointer(page));
310 6 : memcpy(writePtr, segptr, restSize);
311 6 : writePtr += restSize;
312 : }
313 :
314 6680 : totalsize = writePtr - (Pointer) GinDataLeafPageGetPostingList(page);
315 6680 : GinDataPageSetDataSize(page, totalsize);
316 6680 : }
317 :
318 : static void
319 6678 : ginRedoInsertData(Buffer buffer, bool isLeaf, BlockNumber rightblkno, void *rdata)
320 : {
321 6678 : Page page = BufferGetPage(buffer);
322 :
323 6678 : if (isLeaf)
324 : {
325 6674 : ginxlogRecompressDataLeaf *data = (ginxlogRecompressDataLeaf *) rdata;
326 :
327 : Assert(GinPageIsLeaf(page));
328 :
329 6674 : ginRedoRecompress(page, data);
330 : }
331 : else
332 : {
333 4 : ginxlogInsertDataInternal *data = (ginxlogInsertDataInternal *) rdata;
334 : PostingItem *oldpitem;
335 :
336 : Assert(!GinPageIsLeaf(page));
337 :
338 : /* update link to right page after split */
339 4 : oldpitem = GinDataPageGetPostingItem(page, data->offset);
340 4 : PostingItemSetBlockNumber(oldpitem, rightblkno);
341 :
342 4 : GinDataPageAddPostingItem(page, &data->newitem, data->offset);
343 : }
344 6678 : }
345 :
346 : static void
347 54024 : ginRedoInsert(XLogReaderState *record)
348 : {
349 54024 : XLogRecPtr lsn = record->EndRecPtr;
350 54024 : ginxlogInsert *data = (ginxlogInsert *) XLogRecGetData(record);
351 : Buffer buffer;
352 : #ifdef NOT_USED
353 : BlockNumber leftChildBlkno = InvalidBlockNumber;
354 : #endif
355 54024 : BlockNumber rightChildBlkno = InvalidBlockNumber;
356 54024 : bool isLeaf = (data->flags & GIN_INSERT_ISLEAF) != 0;
357 :
358 : /*
359 : * First clear incomplete-split flag on child page if this finishes a
360 : * split.
361 : */
362 54024 : if (!isLeaf)
363 : {
364 278 : char *payload = XLogRecGetData(record) + sizeof(ginxlogInsert);
365 :
366 : #ifdef NOT_USED
367 : leftChildBlkno = BlockIdGetBlockNumber((BlockId) payload);
368 : #endif
369 278 : payload += sizeof(BlockIdData);
370 278 : rightChildBlkno = BlockIdGetBlockNumber((BlockId) payload);
371 :
372 278 : ginRedoClearIncompleteSplit(record, 1);
373 : }
374 :
375 54024 : if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
376 : {
377 53910 : Page page = BufferGetPage(buffer);
378 : Size len;
379 53910 : char *payload = XLogRecGetBlockData(record, 0, &len);
380 :
381 : /* How to insert the payload is tree-type specific */
382 53910 : if (data->flags & GIN_INSERT_ISDATA)
383 : {
384 : Assert(GinPageIsData(page));
385 6678 : ginRedoInsertData(buffer, isLeaf, rightChildBlkno, payload);
386 : }
387 : else
388 : {
389 : Assert(!GinPageIsData(page));
390 47232 : ginRedoInsertEntry(buffer, isLeaf, rightChildBlkno, payload);
391 : }
392 :
393 53910 : PageSetLSN(page, lsn);
394 53910 : MarkBufferDirty(buffer);
395 : }
396 54024 : if (BufferIsValid(buffer))
397 54024 : UnlockReleaseBuffer(buffer);
398 54024 : }
399 :
400 : static void
401 284 : ginRedoSplit(XLogReaderState *record)
402 : {
403 284 : ginxlogSplit *data = (ginxlogSplit *) XLogRecGetData(record);
404 : Buffer lbuffer,
405 : rbuffer,
406 : rootbuf;
407 284 : bool isLeaf = (data->flags & GIN_INSERT_ISLEAF) != 0;
408 284 : bool isRoot = (data->flags & GIN_SPLIT_ROOT) != 0;
409 :
410 : /*
411 : * First clear incomplete-split flag on child page if this finishes a
412 : * split
413 : */
414 284 : if (!isLeaf)
415 0 : ginRedoClearIncompleteSplit(record, 3);
416 :
417 284 : if (XLogReadBufferForRedo(record, 0, &lbuffer) != BLK_RESTORED)
418 0 : elog(ERROR, "GIN split record did not contain a full-page image of left page");
419 :
420 284 : if (XLogReadBufferForRedo(record, 1, &rbuffer) != BLK_RESTORED)
421 0 : elog(ERROR, "GIN split record did not contain a full-page image of right page");
422 :
423 284 : if (isRoot)
424 : {
425 6 : if (XLogReadBufferForRedo(record, 2, &rootbuf) != BLK_RESTORED)
426 0 : elog(ERROR, "GIN split record did not contain a full-page image of root page");
427 6 : UnlockReleaseBuffer(rootbuf);
428 : }
429 :
430 284 : UnlockReleaseBuffer(rbuffer);
431 284 : UnlockReleaseBuffer(lbuffer);
432 284 : }
433 :
434 : /*
435 : * VACUUM_PAGE record contains simply a full image of the page, similar to
436 : * an XLOG_FPI record.
437 : */
438 : static void
439 0 : ginRedoVacuumPage(XLogReaderState *record)
440 : {
441 : Buffer buffer;
442 :
443 0 : if (XLogReadBufferForRedo(record, 0, &buffer) != BLK_RESTORED)
444 : {
445 0 : elog(ERROR, "replay of gin entry tree page vacuum did not restore the page");
446 : }
447 0 : UnlockReleaseBuffer(buffer);
448 0 : }
449 :
450 : static void
451 6 : ginRedoVacuumDataLeafPage(XLogReaderState *record)
452 : {
453 6 : XLogRecPtr lsn = record->EndRecPtr;
454 : Buffer buffer;
455 :
456 6 : if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
457 : {
458 6 : Page page = BufferGetPage(buffer);
459 : Size len;
460 : ginxlogVacuumDataLeafPage *xlrec;
461 :
462 6 : xlrec = (ginxlogVacuumDataLeafPage *) XLogRecGetBlockData(record, 0, &len);
463 :
464 : Assert(GinPageIsLeaf(page));
465 : Assert(GinPageIsData(page));
466 :
467 6 : ginRedoRecompress(page, &xlrec->data);
468 6 : PageSetLSN(page, lsn);
469 6 : MarkBufferDirty(buffer);
470 : }
471 6 : if (BufferIsValid(buffer))
472 6 : UnlockReleaseBuffer(buffer);
473 6 : }
474 :
475 : static void
476 0 : ginRedoDeletePage(XLogReaderState *record)
477 : {
478 0 : XLogRecPtr lsn = record->EndRecPtr;
479 0 : ginxlogDeletePage *data = (ginxlogDeletePage *) XLogRecGetData(record);
480 : Buffer dbuffer;
481 : Buffer pbuffer;
482 : Buffer lbuffer;
483 : Page page;
484 :
485 : /*
486 : * Lock left page first in order to prevent possible deadlock with
487 : * ginStepRight().
488 : */
489 0 : if (XLogReadBufferForRedo(record, 2, &lbuffer) == BLK_NEEDS_REDO)
490 : {
491 0 : page = BufferGetPage(lbuffer);
492 : Assert(GinPageIsData(page));
493 0 : GinPageGetOpaque(page)->rightlink = data->rightLink;
494 0 : PageSetLSN(page, lsn);
495 0 : MarkBufferDirty(lbuffer);
496 : }
497 :
498 0 : if (XLogReadBufferForRedo(record, 0, &dbuffer) == BLK_NEEDS_REDO)
499 : {
500 0 : page = BufferGetPage(dbuffer);
501 : Assert(GinPageIsData(page));
502 0 : GinPageSetDeleted(page);
503 0 : GinPageSetDeleteXid(page, data->deleteXid);
504 0 : PageSetLSN(page, lsn);
505 0 : MarkBufferDirty(dbuffer);
506 : }
507 :
508 0 : if (XLogReadBufferForRedo(record, 1, &pbuffer) == BLK_NEEDS_REDO)
509 : {
510 0 : page = BufferGetPage(pbuffer);
511 : Assert(GinPageIsData(page));
512 : Assert(!GinPageIsLeaf(page));
513 0 : GinPageDeletePostingItem(page, data->parentOffset);
514 0 : PageSetLSN(page, lsn);
515 0 : MarkBufferDirty(pbuffer);
516 : }
517 :
518 0 : if (BufferIsValid(lbuffer))
519 0 : UnlockReleaseBuffer(lbuffer);
520 0 : if (BufferIsValid(pbuffer))
521 0 : UnlockReleaseBuffer(pbuffer);
522 0 : if (BufferIsValid(dbuffer))
523 0 : UnlockReleaseBuffer(dbuffer);
524 0 : }
525 :
526 : static void
527 47966 : ginRedoUpdateMetapage(XLogReaderState *record)
528 : {
529 47966 : XLogRecPtr lsn = record->EndRecPtr;
530 47966 : ginxlogUpdateMeta *data = (ginxlogUpdateMeta *) XLogRecGetData(record);
531 : Buffer metabuffer;
532 : Page metapage;
533 : Buffer buffer;
534 :
535 : /*
536 : * Restore the metapage. This is essentially the same as a full-page
537 : * image, so restore the metapage unconditionally without looking at the
538 : * LSN, to avoid torn page hazards.
539 : */
540 47966 : metabuffer = XLogInitBufferForRedo(record, 0);
541 : Assert(BufferGetBlockNumber(metabuffer) == GIN_METAPAGE_BLKNO);
542 47966 : metapage = BufferGetPage(metabuffer);
543 :
544 47966 : GinInitMetabuffer(metabuffer);
545 47966 : memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
546 47966 : PageSetLSN(metapage, lsn);
547 47966 : MarkBufferDirty(metabuffer);
548 :
549 47966 : if (data->ntuples > 0)
550 : {
551 : /*
552 : * insert into tail page
553 : */
554 47590 : if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
555 : {
556 47560 : Page page = BufferGetPage(buffer);
557 : OffsetNumber off;
558 : int i;
559 : Size tupsize;
560 : char *payload;
561 : IndexTuple tuples;
562 : Size totaltupsize;
563 :
564 47560 : payload = XLogRecGetBlockData(record, 1, &totaltupsize);
565 47560 : tuples = (IndexTuple) payload;
566 :
567 47560 : if (PageIsEmpty(page))
568 0 : off = FirstOffsetNumber;
569 : else
570 47560 : off = OffsetNumberNext(PageGetMaxOffsetNumber(page));
571 :
572 190224 : for (i = 0; i < data->ntuples; i++)
573 : {
574 142664 : tupsize = IndexTupleSize(tuples);
575 :
576 142664 : if (PageAddItem(page, (Item) tuples, tupsize, off,
577 : false, false) == InvalidOffsetNumber)
578 0 : elog(ERROR, "failed to add item to index page");
579 :
580 142664 : tuples = (IndexTuple) (((char *) tuples) + tupsize);
581 :
582 142664 : off++;
583 : }
584 : Assert(payload + totaltupsize == (char *) tuples);
585 :
586 : /*
587 : * Increase counter of heap tuples
588 : */
589 47560 : GinPageGetOpaque(page)->maxoff++;
590 :
591 47560 : PageSetLSN(page, lsn);
592 47560 : MarkBufferDirty(buffer);
593 : }
594 47590 : if (BufferIsValid(buffer))
595 47590 : UnlockReleaseBuffer(buffer);
596 : }
597 376 : else if (data->prevTail != InvalidBlockNumber)
598 : {
599 : /*
600 : * New tail
601 : */
602 350 : if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
603 : {
604 348 : Page page = BufferGetPage(buffer);
605 :
606 348 : GinPageGetOpaque(page)->rightlink = data->newRightlink;
607 :
608 348 : PageSetLSN(page, lsn);
609 348 : MarkBufferDirty(buffer);
610 : }
611 350 : if (BufferIsValid(buffer))
612 350 : UnlockReleaseBuffer(buffer);
613 : }
614 :
615 47966 : UnlockReleaseBuffer(metabuffer);
616 47966 : }
617 :
618 : static void
619 360 : ginRedoInsertListPage(XLogReaderState *record)
620 : {
621 360 : XLogRecPtr lsn = record->EndRecPtr;
622 360 : ginxlogInsertListPage *data = (ginxlogInsertListPage *) XLogRecGetData(record);
623 : Buffer buffer;
624 : Page page;
625 : OffsetNumber l,
626 360 : off = FirstOffsetNumber;
627 : int i,
628 : tupsize;
629 : char *payload;
630 : IndexTuple tuples;
631 : Size totaltupsize;
632 :
633 : /* We always re-initialize the page. */
634 360 : buffer = XLogInitBufferForRedo(record, 0);
635 360 : page = BufferGetPage(buffer);
636 :
637 360 : GinInitBuffer(buffer, GIN_LIST);
638 360 : GinPageGetOpaque(page)->rightlink = data->rightlink;
639 360 : if (data->rightlink == InvalidBlockNumber)
640 : {
641 : /* tail of sublist */
642 360 : GinPageSetFullRow(page);
643 360 : GinPageGetOpaque(page)->maxoff = 1;
644 : }
645 : else
646 : {
647 0 : GinPageGetOpaque(page)->maxoff = 0;
648 : }
649 :
650 360 : payload = XLogRecGetBlockData(record, 0, &totaltupsize);
651 :
652 360 : tuples = (IndexTuple) payload;
653 1434 : for (i = 0; i < data->ntuples; i++)
654 : {
655 1074 : tupsize = IndexTupleSize(tuples);
656 :
657 1074 : l = PageAddItem(page, (Item) tuples, tupsize, off, false, false);
658 :
659 1074 : if (l == InvalidOffsetNumber)
660 0 : elog(ERROR, "failed to add item to index page");
661 :
662 1074 : tuples = (IndexTuple) (((char *) tuples) + tupsize);
663 1074 : off++;
664 : }
665 : Assert((char *) tuples == payload + totaltupsize);
666 :
667 360 : PageSetLSN(page, lsn);
668 360 : MarkBufferDirty(buffer);
669 :
670 360 : UnlockReleaseBuffer(buffer);
671 360 : }
672 :
673 : static void
674 28 : ginRedoDeleteListPages(XLogReaderState *record)
675 : {
676 28 : XLogRecPtr lsn = record->EndRecPtr;
677 28 : ginxlogDeleteListPages *data = (ginxlogDeleteListPages *) XLogRecGetData(record);
678 : Buffer metabuffer;
679 : Page metapage;
680 : int i;
681 :
682 28 : metabuffer = XLogInitBufferForRedo(record, 0);
683 : Assert(BufferGetBlockNumber(metabuffer) == GIN_METAPAGE_BLKNO);
684 28 : metapage = BufferGetPage(metabuffer);
685 :
686 28 : GinInitMetabuffer(metabuffer);
687 :
688 28 : memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
689 28 : PageSetLSN(metapage, lsn);
690 28 : MarkBufferDirty(metabuffer);
691 :
692 : /*
693 : * In normal operation, shiftList() takes exclusive lock on all the
694 : * pages-to-be-deleted simultaneously. During replay, however, it should
695 : * be all right to lock them one at a time. This is dependent on the fact
696 : * that we are deleting pages from the head of the list, and that readers
697 : * share-lock the next page before releasing the one they are on. So we
698 : * cannot get past a reader that is on, or due to visit, any page we are
699 : * going to delete. New incoming readers will block behind our metapage
700 : * lock and then see a fully updated page list.
701 : *
702 : * No full-page images are taken of the deleted pages. Instead, they are
703 : * re-initialized as empty, deleted pages. Their right-links don't need to
704 : * be preserved, because no new readers can see the pages, as explained
705 : * above.
706 : */
707 388 : for (i = 0; i < data->ndeleted; i++)
708 : {
709 : Buffer buffer;
710 : Page page;
711 :
712 360 : buffer = XLogInitBufferForRedo(record, i + 1);
713 360 : page = BufferGetPage(buffer);
714 360 : GinInitBuffer(buffer, GIN_DELETED);
715 :
716 360 : PageSetLSN(page, lsn);
717 360 : MarkBufferDirty(buffer);
718 :
719 360 : UnlockReleaseBuffer(buffer);
720 : }
721 28 : UnlockReleaseBuffer(metabuffer);
722 28 : }
723 :
724 : void
725 102680 : gin_redo(XLogReaderState *record)
726 : {
727 102680 : uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
728 : MemoryContext oldCtx;
729 :
730 : /*
731 : * GIN indexes do not require any conflict processing. NB: If we ever
732 : * implement a similar optimization as we have in b-tree, and remove
733 : * killed tuples outside VACUUM, we'll need to handle that here.
734 : */
735 :
736 102680 : oldCtx = MemoryContextSwitchTo(opCtx);
737 102680 : switch (info)
738 : {
739 12 : case XLOG_GIN_CREATE_PTREE:
740 12 : ginRedoCreatePTree(record);
741 12 : break;
742 54024 : case XLOG_GIN_INSERT:
743 54024 : ginRedoInsert(record);
744 54024 : break;
745 284 : case XLOG_GIN_SPLIT:
746 284 : ginRedoSplit(record);
747 284 : break;
748 0 : case XLOG_GIN_VACUUM_PAGE:
749 0 : ginRedoVacuumPage(record);
750 0 : break;
751 6 : case XLOG_GIN_VACUUM_DATA_LEAF_PAGE:
752 6 : ginRedoVacuumDataLeafPage(record);
753 6 : break;
754 0 : case XLOG_GIN_DELETE_PAGE:
755 0 : ginRedoDeletePage(record);
756 0 : break;
757 47966 : case XLOG_GIN_UPDATE_META_PAGE:
758 47966 : ginRedoUpdateMetapage(record);
759 47966 : break;
760 360 : case XLOG_GIN_INSERT_LISTPAGE:
761 360 : ginRedoInsertListPage(record);
762 360 : break;
763 28 : case XLOG_GIN_DELETE_LISTPAGE:
764 28 : ginRedoDeleteListPages(record);
765 28 : break;
766 0 : default:
767 0 : elog(PANIC, "gin_redo: unknown op code %u", info);
768 : }
769 102680 : MemoryContextSwitchTo(oldCtx);
770 102680 : MemoryContextReset(opCtx);
771 102680 : }
772 :
773 : void
774 408 : gin_xlog_startup(void)
775 : {
776 408 : opCtx = AllocSetContextCreate(CurrentMemoryContext,
777 : "GIN recovery temporary context",
778 : ALLOCSET_DEFAULT_SIZES);
779 408 : }
780 :
781 : void
782 292 : gin_xlog_cleanup(void)
783 : {
784 292 : MemoryContextDelete(opCtx);
785 292 : opCtx = NULL;
786 292 : }
787 :
788 : /*
789 : * Mask a GIN page before running consistency checks on it.
790 : */
791 : void
792 301652 : gin_mask(char *pagedata, BlockNumber blkno)
793 : {
794 301652 : Page page = (Page) pagedata;
795 301652 : PageHeader pagehdr = (PageHeader) page;
796 : GinPageOpaque opaque;
797 :
798 301652 : mask_page_lsn_and_checksum(page);
799 301652 : opaque = GinPageGetOpaque(page);
800 :
801 301652 : mask_page_hint_bits(page);
802 :
803 : /*
804 : * For a GIN_DELETED page, the page is initialized to empty. Hence, mask
805 : * the whole page content. For other pages, mask the hole if pd_lower
806 : * appears to have been set correctly.
807 : */
808 301652 : if (opaque->flags & GIN_DELETED)
809 720 : mask_page_content(page);
810 300932 : else if (pagehdr->pd_lower > SizeOfPageHeaderData)
811 300932 : mask_unused_space(page);
812 301652 : }
|