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 278 : page = BufferGetPage(buffer);
34 278 : GinPageGetOpaque(page)->flags &= ~GIN_INCOMPLETE_SPLIT;
35 :
36 278 : PageSetLSN(page, lsn);
37 278 : 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 47216 : ginRedoInsertEntry(Buffer buffer, bool isLeaf, BlockNumber rightblkno, void *rdata)
72 : {
73 47216 : Page page = BufferGetPage(buffer);
74 47216 : ginxlogInsertEntry *data = (ginxlogInsertEntry *) rdata;
75 47216 : OffsetNumber offset = data->offset;
76 : IndexTuple itup;
77 :
78 47216 : if (rightblkno != InvalidBlockNumber)
79 : {
80 : /* update link to right page after split */
81 : Assert(!GinPageIsLeaf(page));
82 : Assert(offset >= FirstOffsetNumber && offset <= PageGetMaxOffsetNumber(page));
83 258 : itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offset));
84 258 : GinSetDownlink(itup, rightblkno);
85 : }
86 :
87 47216 : if (data->isDelete)
88 : {
89 : Assert(GinPageIsLeaf(page));
90 : Assert(offset >= FirstOffsetNumber && offset <= PageGetMaxOffsetNumber(page));
91 7248 : PageIndexTupleDelete(page, offset);
92 : }
93 :
94 47216 : itup = &data->tuple;
95 :
96 47216 : if (PageAddItem(page, 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 47216 : }
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 6678 : 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 6678 : 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 6678 : 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 6678 : oldseg = GinDataLeafPageGetPostingList(page);
167 6678 : writePtr = (Pointer) oldseg;
168 6678 : segmentend = (Pointer) oldseg + GinDataLeafPageGetPostingListSize(page);
169 6678 : segno = 0;
170 :
171 6678 : walbuf = ((char *) data) + sizeof(ginxlogRecompressDataLeaf);
172 13404 : for (actionno = 0; actionno < data->nactions; actionno++)
173 : {
174 6726 : uint8 a_segno = *((uint8 *) (walbuf++));
175 6726 : uint8 a_action = *((uint8 *) (walbuf++));
176 6726 : GinPostingList *newseg = NULL;
177 6726 : int newsegsize = 0;
178 6726 : ItemPointerData *items = NULL;
179 6726 : 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 6726 : if (a_action == GIN_SEGMENT_INSERT ||
188 : a_action == GIN_SEGMENT_REPLACE)
189 : {
190 62 : newseg = (GinPostingList *) walbuf;
191 62 : newsegsize = SizeOfGinPostingList(newseg);
192 62 : walbuf += SHORTALIGN(newsegsize);
193 : }
194 :
195 6726 : if (a_action == GIN_SEGMENT_ADDITEMS)
196 : {
197 6640 : memcpy(&nitems, walbuf, sizeof(uint16));
198 6640 : walbuf += sizeof(uint16);
199 6640 : items = (ItemPointerData *) walbuf;
200 6640 : walbuf += nitems * sizeof(ItemPointerData);
201 : }
202 :
203 : /* Skip to the segment that this action concerns */
204 : Assert(segno <= a_segno);
205 119296 : 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 112570 : segsize = SizeOfGinPostingList(oldseg);
212 112570 : if (tailCopy)
213 : {
214 : Assert(writePtr + segsize < PageGetSpecialPointer(page));
215 0 : memcpy(writePtr, (Pointer) oldseg, segsize);
216 : }
217 112570 : writePtr += segsize;
218 112570 : oldseg = GinNextPostingListSegment(oldseg);
219 112570 : 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 6726 : if (a_action == GIN_SEGMENT_ADDITEMS)
228 : {
229 : int npacked;
230 :
231 6640 : olditems = ginPostingListDecode(oldseg, &nolditems);
232 :
233 6640 : newitems = ginMergeItemPointers(items, nitems,
234 : olditems, nolditems,
235 : &nnewitems);
236 : Assert(nnewitems == nolditems + nitems);
237 :
238 6640 : newseg = ginCompressPostingList(newitems, nnewitems,
239 : BLCKSZ, &npacked);
240 : Assert(npacked == nnewitems);
241 :
242 6640 : newsegsize = SizeOfGinPostingList(newseg);
243 6640 : a_action = GIN_SEGMENT_REPLACE;
244 : }
245 :
246 6726 : segptr = (Pointer) oldseg;
247 6726 : if (segptr != segmentend)
248 6682 : 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 6726 : if (!tailCopy && segptr != segmentend)
264 : {
265 6652 : int tailSize = segmentend - segptr;
266 :
267 6652 : tailCopy = (Pointer) palloc(tailSize);
268 6652 : memcpy(tailCopy, segptr, tailSize);
269 6652 : segptr = tailCopy;
270 6652 : oldseg = (GinPostingList *) segptr;
271 6652 : segmentend = segptr + tailSize;
272 : }
273 :
274 6726 : switch (a_action)
275 : {
276 24 : case GIN_SEGMENT_DELETE:
277 24 : segptr += segsize;
278 24 : segno++;
279 24 : 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 6658 : case GIN_SEGMENT_REPLACE:
289 : /* copy the new version of segment in place */
290 : Assert(writePtr + newsegsize <= PageGetSpecialPointer(page));
291 6658 : memcpy(writePtr, newseg, newsegsize);
292 6658 : writePtr += newsegsize;
293 6658 : segptr += segsize;
294 6658 : segno++;
295 6658 : break;
296 :
297 0 : default:
298 0 : elog(ERROR, "unexpected GIN leaf action: %u", a_action);
299 : }
300 6726 : oldseg = (GinPostingList *) segptr;
301 : }
302 :
303 : /* Copy the rest of unmodified segments if any. */
304 6678 : segptr = (Pointer) oldseg;
305 6678 : 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 6678 : totalsize = writePtr - (Pointer) GinDataLeafPageGetPostingList(page);
315 6678 : GinDataPageSetDataSize(page, totalsize);
316 6678 : }
317 :
318 : static void
319 6676 : ginRedoInsertData(Buffer buffer, bool isLeaf, BlockNumber rightblkno, void *rdata)
320 : {
321 6676 : Page page = BufferGetPage(buffer);
322 :
323 6676 : if (isLeaf)
324 : {
325 6672 : ginxlogRecompressDataLeaf *data = (ginxlogRecompressDataLeaf *) rdata;
326 :
327 : Assert(GinPageIsLeaf(page));
328 :
329 6672 : 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 6676 : }
345 :
346 : static void
347 54014 : ginRedoInsert(XLogReaderState *record)
348 : {
349 54014 : XLogRecPtr lsn = record->EndRecPtr;
350 54014 : ginxlogInsert *data = (ginxlogInsert *) XLogRecGetData(record);
351 : Buffer buffer;
352 : #ifdef NOT_USED
353 : BlockNumber leftChildBlkno = InvalidBlockNumber;
354 : #endif
355 54014 : BlockNumber rightChildBlkno = InvalidBlockNumber;
356 54014 : 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 54014 : 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 54014 : if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
376 : {
377 53892 : Page page = BufferGetPage(buffer);
378 : Size len;
379 53892 : char *payload = XLogRecGetBlockData(record, 0, &len);
380 :
381 : /* How to insert the payload is tree-type specific */
382 53892 : if (data->flags & GIN_INSERT_ISDATA)
383 : {
384 : Assert(GinPageIsData(page));
385 6676 : ginRedoInsertData(buffer, isLeaf, rightChildBlkno, payload);
386 : }
387 : else
388 : {
389 : Assert(!GinPageIsData(page));
390 47216 : ginRedoInsertEntry(buffer, isLeaf, rightChildBlkno, payload);
391 : }
392 :
393 53892 : PageSetLSN(page, lsn);
394 53892 : MarkBufferDirty(buffer);
395 : }
396 54014 : if (BufferIsValid(buffer))
397 54014 : UnlockReleaseBuffer(buffer);
398 54014 : }
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 47556 : Page page = BufferGetPage(buffer);
557 : OffsetNumber off;
558 : int i;
559 : Size tupsize;
560 : char *payload;
561 : IndexTuple tuples;
562 : Size totaltupsize;
563 :
564 47556 : payload = XLogRecGetBlockData(record, 1, &totaltupsize);
565 47556 : tuples = (IndexTuple) payload;
566 :
567 47556 : if (PageIsEmpty(page))
568 0 : off = FirstOffsetNumber;
569 : else
570 47556 : off = OffsetNumberNext(PageGetMaxOffsetNumber(page));
571 :
572 190208 : for (i = 0; i < data->ntuples; i++)
573 : {
574 142652 : tupsize = IndexTupleSize(tuples);
575 :
576 142652 : if (PageAddItem(page, tuples, tupsize, off, false, false) == InvalidOffsetNumber)
577 0 : elog(ERROR, "failed to add item to index page");
578 :
579 142652 : tuples = (IndexTuple) (((char *) tuples) + tupsize);
580 :
581 142652 : off++;
582 : }
583 : Assert(payload + totaltupsize == (char *) tuples);
584 :
585 : /*
586 : * Increase counter of heap tuples
587 : */
588 47556 : GinPageGetOpaque(page)->maxoff++;
589 :
590 47556 : PageSetLSN(page, lsn);
591 47556 : MarkBufferDirty(buffer);
592 : }
593 47590 : if (BufferIsValid(buffer))
594 47590 : UnlockReleaseBuffer(buffer);
595 : }
596 376 : else if (data->prevTail != InvalidBlockNumber)
597 : {
598 : /*
599 : * New tail
600 : */
601 350 : if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
602 : {
603 350 : Page page = BufferGetPage(buffer);
604 :
605 350 : GinPageGetOpaque(page)->rightlink = data->newRightlink;
606 :
607 350 : PageSetLSN(page, lsn);
608 350 : MarkBufferDirty(buffer);
609 : }
610 350 : if (BufferIsValid(buffer))
611 350 : UnlockReleaseBuffer(buffer);
612 : }
613 :
614 47966 : UnlockReleaseBuffer(metabuffer);
615 47966 : }
616 :
617 : static void
618 360 : ginRedoInsertListPage(XLogReaderState *record)
619 : {
620 360 : XLogRecPtr lsn = record->EndRecPtr;
621 360 : ginxlogInsertListPage *data = (ginxlogInsertListPage *) XLogRecGetData(record);
622 : Buffer buffer;
623 : Page page;
624 : OffsetNumber l,
625 360 : off = FirstOffsetNumber;
626 : int i,
627 : tupsize;
628 : char *payload;
629 : IndexTuple tuples;
630 : Size totaltupsize;
631 :
632 : /* We always re-initialize the page. */
633 360 : buffer = XLogInitBufferForRedo(record, 0);
634 360 : page = BufferGetPage(buffer);
635 :
636 360 : GinInitBuffer(buffer, GIN_LIST);
637 360 : GinPageGetOpaque(page)->rightlink = data->rightlink;
638 360 : if (data->rightlink == InvalidBlockNumber)
639 : {
640 : /* tail of sublist */
641 360 : GinPageSetFullRow(page);
642 360 : GinPageGetOpaque(page)->maxoff = 1;
643 : }
644 : else
645 : {
646 0 : GinPageGetOpaque(page)->maxoff = 0;
647 : }
648 :
649 360 : payload = XLogRecGetBlockData(record, 0, &totaltupsize);
650 :
651 360 : tuples = (IndexTuple) payload;
652 1434 : for (i = 0; i < data->ntuples; i++)
653 : {
654 1074 : tupsize = IndexTupleSize(tuples);
655 :
656 1074 : l = PageAddItem(page, tuples, tupsize, off, false, false);
657 :
658 1074 : if (l == InvalidOffsetNumber)
659 0 : elog(ERROR, "failed to add item to index page");
660 :
661 1074 : tuples = (IndexTuple) (((char *) tuples) + tupsize);
662 1074 : off++;
663 : }
664 : Assert((char *) tuples == payload + totaltupsize);
665 :
666 360 : PageSetLSN(page, lsn);
667 360 : MarkBufferDirty(buffer);
668 :
669 360 : UnlockReleaseBuffer(buffer);
670 360 : }
671 :
672 : static void
673 26 : ginRedoDeleteListPages(XLogReaderState *record)
674 : {
675 26 : XLogRecPtr lsn = record->EndRecPtr;
676 26 : ginxlogDeleteListPages *data = (ginxlogDeleteListPages *) XLogRecGetData(record);
677 : Buffer metabuffer;
678 : Page metapage;
679 : int i;
680 :
681 26 : metabuffer = XLogInitBufferForRedo(record, 0);
682 : Assert(BufferGetBlockNumber(metabuffer) == GIN_METAPAGE_BLKNO);
683 26 : metapage = BufferGetPage(metabuffer);
684 :
685 26 : GinInitMetabuffer(metabuffer);
686 :
687 26 : memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
688 26 : PageSetLSN(metapage, lsn);
689 26 : MarkBufferDirty(metabuffer);
690 :
691 : /*
692 : * In normal operation, shiftList() takes exclusive lock on all the
693 : * pages-to-be-deleted simultaneously. During replay, however, it should
694 : * be all right to lock them one at a time. This is dependent on the fact
695 : * that we are deleting pages from the head of the list, and that readers
696 : * share-lock the next page before releasing the one they are on. So we
697 : * cannot get past a reader that is on, or due to visit, any page we are
698 : * going to delete. New incoming readers will block behind our metapage
699 : * lock and then see a fully updated page list.
700 : *
701 : * No full-page images are taken of the deleted pages. Instead, they are
702 : * re-initialized as empty, deleted pages. Their right-links don't need to
703 : * be preserved, because no new readers can see the pages, as explained
704 : * above.
705 : */
706 384 : for (i = 0; i < data->ndeleted; i++)
707 : {
708 : Buffer buffer;
709 : Page page;
710 :
711 358 : buffer = XLogInitBufferForRedo(record, i + 1);
712 358 : page = BufferGetPage(buffer);
713 358 : GinInitBuffer(buffer, GIN_DELETED);
714 :
715 358 : PageSetLSN(page, lsn);
716 358 : MarkBufferDirty(buffer);
717 :
718 358 : UnlockReleaseBuffer(buffer);
719 : }
720 26 : UnlockReleaseBuffer(metabuffer);
721 26 : }
722 :
723 : void
724 102668 : gin_redo(XLogReaderState *record)
725 : {
726 102668 : uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
727 : MemoryContext oldCtx;
728 :
729 : /*
730 : * GIN indexes do not require any conflict processing. NB: If we ever
731 : * implement a similar optimization as we have in b-tree, and remove
732 : * killed tuples outside VACUUM, we'll need to handle that here.
733 : */
734 :
735 102668 : oldCtx = MemoryContextSwitchTo(opCtx);
736 102668 : switch (info)
737 : {
738 12 : case XLOG_GIN_CREATE_PTREE:
739 12 : ginRedoCreatePTree(record);
740 12 : break;
741 54014 : case XLOG_GIN_INSERT:
742 54014 : ginRedoInsert(record);
743 54014 : break;
744 284 : case XLOG_GIN_SPLIT:
745 284 : ginRedoSplit(record);
746 284 : break;
747 0 : case XLOG_GIN_VACUUM_PAGE:
748 0 : ginRedoVacuumPage(record);
749 0 : break;
750 6 : case XLOG_GIN_VACUUM_DATA_LEAF_PAGE:
751 6 : ginRedoVacuumDataLeafPage(record);
752 6 : break;
753 0 : case XLOG_GIN_DELETE_PAGE:
754 0 : ginRedoDeletePage(record);
755 0 : break;
756 47966 : case XLOG_GIN_UPDATE_META_PAGE:
757 47966 : ginRedoUpdateMetapage(record);
758 47966 : break;
759 360 : case XLOG_GIN_INSERT_LISTPAGE:
760 360 : ginRedoInsertListPage(record);
761 360 : break;
762 26 : case XLOG_GIN_DELETE_LISTPAGE:
763 26 : ginRedoDeleteListPages(record);
764 26 : break;
765 0 : default:
766 0 : elog(PANIC, "gin_redo: unknown op code %u", info);
767 : }
768 102668 : MemoryContextSwitchTo(oldCtx);
769 102668 : MemoryContextReset(opCtx);
770 102668 : }
771 :
772 : void
773 410 : gin_xlog_startup(void)
774 : {
775 410 : opCtx = AllocSetContextCreate(CurrentMemoryContext,
776 : "GIN recovery temporary context",
777 : ALLOCSET_DEFAULT_SIZES);
778 410 : }
779 :
780 : void
781 296 : gin_xlog_cleanup(void)
782 : {
783 296 : MemoryContextDelete(opCtx);
784 296 : opCtx = NULL;
785 296 : }
786 :
787 : /*
788 : * Mask a GIN page before running consistency checks on it.
789 : */
790 : void
791 301608 : gin_mask(char *pagedata, BlockNumber blkno)
792 : {
793 301608 : Page page = (Page) pagedata;
794 301608 : PageHeader pagehdr = (PageHeader) page;
795 : GinPageOpaque opaque;
796 :
797 301608 : mask_page_lsn_and_checksum(page);
798 301608 : opaque = GinPageGetOpaque(page);
799 :
800 301608 : mask_page_hint_bits(page);
801 :
802 : /*
803 : * For a GIN_DELETED page, the page is initialized to empty. Hence, mask
804 : * the whole page content. For other pages, mask the hole if pd_lower
805 : * appears to have been set correctly.
806 : */
807 301608 : if (opaque->flags & GIN_DELETED)
808 716 : mask_page_content(page);
809 300892 : else if (pagehdr->pd_lower > SizeOfPageHeaderData)
810 300892 : mask_unused_space(page);
811 301608 : }
|