Age Owner Branch data TLA 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
3995 heikki.linnakangas@i 25 :CBC 139 : ginRedoClearIncompleteSplit(XLogReaderState *record, uint8 block_id)
26 : : {
27 : 139 : XLogRecPtr lsn = record->EndRecPtr;
28 : : Buffer buffer;
29 : : Page page;
30 : :
31 [ + - ]: 139 : if (XLogReadBufferForRedo(record, block_id, &buffer) == BLK_NEEDS_REDO)
32 : : {
60 peter@eisentraut.org 33 :GNC 139 : page = BufferGetPage(buffer);
4353 heikki.linnakangas@i 34 :CBC 139 : GinPageGetOpaque(page)->flags &= ~GIN_INCOMPLETE_SPLIT;
35 : :
36 : 139 : PageSetLSN(page, lsn);
37 : 139 : MarkBufferDirty(buffer);
38 : : }
4094 39 [ + - ]: 139 : if (BufferIsValid(buffer))
40 : 139 : UnlockReleaseBuffer(buffer);
7119 teodor@sigaev.ru 41 : 139 : }
42 : :
43 : : static void
3995 heikki.linnakangas@i 44 : 6 : ginRedoCreatePTree(XLogReaderState *record)
45 : : {
46 : 6 : XLogRecPtr lsn = record->EndRecPtr;
6964 bruce@momjian.us 47 : 6 : ginxlogCreatePostingTree *data = (ginxlogCreatePostingTree *) XLogRecGetData(record);
48 : : char *ptr;
49 : : Buffer buffer;
50 : : Page page;
51 : :
3995 heikki.linnakangas@i 52 : 6 : buffer = XLogInitBufferForRedo(record, 0);
60 peter@eisentraut.org 53 :GNC 6 : page = BufferGetPage(buffer);
54 : :
4297 heikki.linnakangas@i 55 :CBC 6 : GinInitBuffer(buffer, GIN_DATA | GIN_LEAF | GIN_COMPRESSED);
56 : :
57 : 6 : ptr = XLogRecGetData(record) + sizeof(ginxlogCreatePostingTree);
58 : :
59 : : /* Place page data */
60 : 6 : memcpy(GinDataLeafPageGetPostingList(page), ptr, data->size);
61 : :
4215 62 [ - + ]: 6 : GinDataPageSetDataSize(page, data->size);
63 : :
7119 teodor@sigaev.ru 64 : 6 : PageSetLSN(page, lsn);
65 : :
66 : 6 : MarkBufferDirty(buffer);
67 : 6 : UnlockReleaseBuffer(buffer);
68 : 6 : }
69 : :
70 : : static void
4297 heikki.linnakangas@i 71 : 23616 : ginRedoInsertEntry(Buffer buffer, bool isLeaf, BlockNumber rightblkno, void *rdata)
72 : : {
3478 kgrittn@postgresql.o 73 : 23616 : Page page = BufferGetPage(buffer);
4353 heikki.linnakangas@i 74 : 23616 : ginxlogInsertEntry *data = (ginxlogInsertEntry *) rdata;
4297 75 : 23616 : OffsetNumber offset = data->offset;
76 : : IndexTuple itup;
77 : :
4353 78 [ + + ]: 23616 : if (rightblkno != InvalidBlockNumber)
79 : : {
80 : : /* update link to right page after split */
81 [ - + ]: 130 : Assert(!GinPageIsLeaf(page));
82 [ + - - + ]: 130 : Assert(offset >= FirstOffsetNumber && offset <= PageGetMaxOffsetNumber(page));
83 : 130 : itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offset));
84 : 130 : GinSetDownlink(itup, rightblkno);
85 : : }
86 : :
87 [ + + ]: 23616 : if (data->isDelete)
88 : : {
89 [ - + ]: 3629 : Assert(GinPageIsLeaf(page));
90 [ + - - + ]: 3629 : Assert(offset >= FirstOffsetNumber && offset <= PageGetMaxOffsetNumber(page));
91 : 3629 : PageIndexTupleDelete(page, offset);
92 : : }
93 : :
94 : 23616 : itup = &data->tuple;
95 : :
1 peter@eisentraut.org 96 [ - + ]:GNC 23616 : if (PageAddItem(page, itup, IndexTupleSize(itup), offset, false, false) == InvalidOffsetNumber)
97 : : {
98 : : RelFileLocator locator;
99 : : ForkNumber forknum;
100 : : BlockNumber blknum;
101 : :
1210 rhaas@postgresql.org 102 :UBC 0 : BufferGetTag(buffer, &locator, &forknum, &blknum);
1126 103 [ # # ]: 0 : elog(ERROR, "failed to add item to index page in %u/%u/%u",
104 : : locator.spcOid, locator.dbOid, locator.relNumber);
105 : : }
4353 heikki.linnakangas@i 106 :CBC 23616 : }
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
4297 117 : 3343 : ginRedoRecompress(Page page, ginxlogRecompressDataLeaf *data)
118 : : {
119 : : int actionno;
120 : : int segno;
121 : : GinPostingList *oldseg;
122 : : Pointer segmentend;
123 : : char *walbuf;
124 : : int totalsize;
2606 akorotkov@postgresql 125 : 3343 : 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 : : */
4229 heikki.linnakangas@i 132 [ - + ]: 3343 : if (!GinPageIsCompressed(page))
133 : : {
4229 heikki.linnakangas@i 134 :UBC 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 : : */
2658 akorotkov@postgresql 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 [ # # ]: 0 : Assert(npacked == nuncompressed);
153 : :
154 : 0 : memcpy(GinDataLeafPageGetPostingList(page), plist, totalsize);
155 : : }
156 : : else
157 : : {
158 : 0 : totalsize = 0;
159 : : }
160 : :
4215 heikki.linnakangas@i 161 [ # # ]: 0 : GinDataPageSetDataSize(page, totalsize);
4229 162 : 0 : GinPageSetCompressed(page);
163 : 0 : GinPageGetOpaque(page)->maxoff = InvalidOffsetNumber;
164 : : }
165 : :
4229 heikki.linnakangas@i 166 :CBC 3343 : oldseg = GinDataLeafPageGetPostingList(page);
2606 akorotkov@postgresql 167 : 3343 : writePtr = (Pointer) oldseg;
4229 heikki.linnakangas@i 168 : 3343 : segmentend = (Pointer) oldseg + GinDataLeafPageGetPostingListSize(page);
169 : 3343 : segno = 0;
170 : :
171 : 3343 : walbuf = ((char *) data) + sizeof(ginxlogRecompressDataLeaf);
172 [ + + ]: 6710 : for (actionno = 0; actionno < data->nactions; actionno++)
173 : : {
174 : 3367 : uint8 a_segno = *((uint8 *) (walbuf++));
175 : 3367 : uint8 a_action = *((uint8 *) (walbuf++));
176 : 3367 : GinPostingList *newseg = NULL;
177 : 3367 : int newsegsize = 0;
178 : 3367 : ItemPointerData *items = NULL;
179 : 3367 : 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 [ + + + + ]: 3367 : if (a_action == GIN_SEGMENT_INSERT ||
188 : : a_action == GIN_SEGMENT_REPLACE)
189 : : {
190 : 31 : newseg = (GinPostingList *) walbuf;
191 : 31 : newsegsize = SizeOfGinPostingList(newseg);
192 : 31 : walbuf += SHORTALIGN(newsegsize);
193 : : }
194 : :
195 [ + + ]: 3367 : if (a_action == GIN_SEGMENT_ADDITEMS)
196 : : {
197 : 3324 : memcpy(&nitems, walbuf, sizeof(uint16));
198 : 3324 : walbuf += sizeof(uint16);
199 : 3324 : items = (ItemPointerData *) walbuf;
200 : 3324 : walbuf += nitems * sizeof(ItemPointerData);
201 : : }
202 : :
203 : : /* Skip to the segment that this action concerns */
204 [ - + ]: 3367 : Assert(segno <= a_segno);
205 [ + + ]: 59725 : while (segno < a_segno)
206 : : {
207 : : /*
208 : : * Once modification is started and page tail is copied, we've to
209 : : * copy unmodified segments.
210 : : */
2606 akorotkov@postgresql 211 : 56358 : segsize = SizeOfGinPostingList(oldseg);
212 [ - + ]: 56358 : if (tailCopy)
213 : : {
2606 akorotkov@postgresql 214 [ # # ]:UBC 0 : Assert(writePtr + segsize < PageGetSpecialPointer(page));
215 : 0 : memcpy(writePtr, (Pointer) oldseg, segsize);
216 : : }
2606 akorotkov@postgresql 217 :CBC 56358 : writePtr += segsize;
4229 heikki.linnakangas@i 218 : 56358 : oldseg = GinNextPostingListSegment(oldseg);
219 : 56358 : 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 [ + + ]: 3367 : if (a_action == GIN_SEGMENT_ADDITEMS)
228 : : {
229 : : int npacked;
230 : :
231 : 3324 : olditems = ginPostingListDecode(oldseg, &nolditems);
232 : :
233 : 3324 : newitems = ginMergeItemPointers(items, nitems,
234 : : olditems, nolditems,
235 : : &nnewitems);
236 [ - + ]: 3324 : Assert(nnewitems == nolditems + nitems);
237 : :
238 : 3324 : newseg = ginCompressPostingList(newitems, nnewitems,
239 : : BLCKSZ, &npacked);
240 [ - + ]: 3324 : Assert(npacked == nnewitems);
241 : :
242 : 3324 : newsegsize = SizeOfGinPostingList(newseg);
243 : 3324 : a_action = GIN_SEGMENT_REPLACE;
244 : : }
245 : :
246 : 3367 : segptr = (Pointer) oldseg;
247 [ + + ]: 3367 : if (segptr != segmentend)
248 : 3345 : segsize = SizeOfGinPostingList(oldseg);
249 : : else
250 : : {
251 : : /*
252 : : * Positioned after the last existing segment. Only INSERTs
253 : : * expected here.
254 : : */
255 [ - + ]: 22 : Assert(a_action == GIN_SEGMENT_INSERT);
256 : 22 : 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 : : */
2606 akorotkov@postgresql 263 [ + + + + ]: 3367 : if (!tailCopy && segptr != segmentend)
264 : : {
2351 tgl@sss.pgh.pa.us 265 : 3330 : int tailSize = segmentend - segptr;
266 : :
2606 akorotkov@postgresql 267 : 3330 : tailCopy = (Pointer) palloc(tailSize);
268 : 3330 : memcpy(tailCopy, segptr, tailSize);
269 : 3330 : segptr = tailCopy;
270 : 3330 : oldseg = (GinPostingList *) segptr;
271 : 3330 : segmentend = segptr + tailSize;
272 : : }
273 : :
4229 heikki.linnakangas@i 274 [ + + + - ]: 3367 : switch (a_action)
275 : : {
276 : 12 : case GIN_SEGMENT_DELETE:
2606 akorotkov@postgresql 277 : 12 : segptr += segsize;
4229 heikki.linnakangas@i 278 : 12 : segno++;
279 : 12 : break;
280 : :
281 : 22 : case GIN_SEGMENT_INSERT:
282 : : /* copy the new segment in place */
2606 akorotkov@postgresql 283 [ - + ]: 22 : Assert(writePtr + newsegsize <= PageGetSpecialPointer(page));
284 : 22 : memcpy(writePtr, newseg, newsegsize);
285 : 22 : writePtr += newsegsize;
4229 heikki.linnakangas@i 286 : 22 : break;
287 : :
288 : 3333 : case GIN_SEGMENT_REPLACE:
289 : : /* copy the new version of segment in place */
2606 akorotkov@postgresql 290 [ - + ]: 3333 : Assert(writePtr + newsegsize <= PageGetSpecialPointer(page));
291 : 3333 : memcpy(writePtr, newseg, newsegsize);
292 : 3333 : writePtr += newsegsize;
293 : 3333 : segptr += segsize;
4229 heikki.linnakangas@i 294 : 3333 : segno++;
295 : 3333 : break;
296 : :
4229 heikki.linnakangas@i 297 :UBC 0 : default:
298 [ # # ]: 0 : elog(ERROR, "unexpected GIN leaf action: %u", a_action);
299 : : }
4229 heikki.linnakangas@i 300 :CBC 3367 : oldseg = (GinPostingList *) segptr;
301 : : }
302 : :
303 : : /* Copy the rest of unmodified segments if any. */
2606 akorotkov@postgresql 304 : 3343 : segptr = (Pointer) oldseg;
305 [ + + + - ]: 3343 : if (segptr != segmentend && tailCopy)
306 : : {
2351 tgl@sss.pgh.pa.us 307 : 3 : int restSize = segmentend - segptr;
308 : :
2606 akorotkov@postgresql 309 [ - + ]: 3 : Assert(writePtr + restSize <= PageGetSpecialPointer(page));
310 : 3 : memcpy(writePtr, segptr, restSize);
311 : 3 : writePtr += restSize;
312 : : }
313 : :
314 : 3343 : totalsize = writePtr - (Pointer) GinDataLeafPageGetPostingList(page);
4215 heikki.linnakangas@i 315 [ - + ]: 3343 : GinDataPageSetDataSize(page, totalsize);
4297 316 : 3343 : }
317 : :
318 : : static void
319 : 3342 : ginRedoInsertData(Buffer buffer, bool isLeaf, BlockNumber rightblkno, void *rdata)
320 : : {
3478 kgrittn@postgresql.o 321 : 3342 : Page page = BufferGetPage(buffer);
322 : :
4297 heikki.linnakangas@i 323 [ + + ]: 3342 : if (isLeaf)
324 : : {
325 : 3340 : ginxlogRecompressDataLeaf *data = (ginxlogRecompressDataLeaf *) rdata;
326 : :
327 [ - + ]: 3340 : Assert(GinPageIsLeaf(page));
328 : :
329 : 3340 : ginRedoRecompress(page, data);
330 : : }
331 : : else
332 : : {
333 : 2 : ginxlogInsertDataInternal *data = (ginxlogInsertDataInternal *) rdata;
334 : : PostingItem *oldpitem;
335 : :
336 [ - + ]: 2 : Assert(!GinPageIsLeaf(page));
337 : :
338 : : /* update link to right page after split */
339 : 2 : oldpitem = GinDataPageGetPostingItem(page, data->offset);
4353 340 : 2 : PostingItemSetBlockNumber(oldpitem, rightblkno);
341 : :
4297 342 : 2 : GinDataPageAddPostingItem(page, &data->newitem, data->offset);
343 : : }
4353 344 : 3342 : }
345 : :
346 : : static void
3995 347 : 27012 : ginRedoInsert(XLogReaderState *record)
348 : : {
349 : 27012 : XLogRecPtr lsn = record->EndRecPtr;
4353 350 : 27012 : ginxlogInsert *data = (ginxlogInsert *) XLogRecGetData(record);
351 : : Buffer buffer;
352 : : #ifdef NOT_USED
353 : : BlockNumber leftChildBlkno = InvalidBlockNumber;
354 : : #endif
355 : 27012 : BlockNumber rightChildBlkno = InvalidBlockNumber;
356 : 27012 : 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 [ + + ]: 27012 : if (!isLeaf)
363 : : {
3995 364 : 139 : char *payload = XLogRecGetData(record) + sizeof(ginxlogInsert);
365 : :
366 : : #ifdef NOT_USED
367 : : leftChildBlkno = BlockIdGetBlockNumber((BlockId) payload);
368 : : #endif
4353 369 : 139 : payload += sizeof(BlockIdData);
370 : 139 : rightChildBlkno = BlockIdGetBlockNumber((BlockId) payload);
371 : :
3995 372 : 139 : ginRedoClearIncompleteSplit(record, 1);
373 : : }
374 : :
375 [ + + ]: 27012 : if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
376 : : {
3478 kgrittn@postgresql.o 377 : 26958 : Page page = BufferGetPage(buffer);
378 : : Size len;
3995 heikki.linnakangas@i 379 : 26958 : char *payload = XLogRecGetBlockData(record, 0, &len);
380 : :
381 : : /* How to insert the payload is tree-type specific */
4353 382 [ + + ]: 26958 : if (data->flags & GIN_INSERT_ISDATA)
383 : : {
5496 tgl@sss.pgh.pa.us 384 [ - + ]: 3342 : Assert(GinPageIsData(page));
4297 heikki.linnakangas@i 385 : 3342 : ginRedoInsertData(buffer, isLeaf, rightChildBlkno, payload);
386 : : }
387 : : else
388 : : {
5496 tgl@sss.pgh.pa.us 389 [ - + ]: 23616 : Assert(!GinPageIsData(page));
4297 heikki.linnakangas@i 390 : 23616 : ginRedoInsertEntry(buffer, isLeaf, rightChildBlkno, payload);
391 : : }
392 : :
4353 393 : 26958 : PageSetLSN(page, lsn);
394 : 26958 : MarkBufferDirty(buffer);
395 : : }
4094 396 [ + - ]: 27012 : if (BufferIsValid(buffer))
397 : 27012 : UnlockReleaseBuffer(buffer);
4353 398 : 27012 : }
399 : :
400 : : static void
3995 401 : 142 : ginRedoSplit(XLogReaderState *record)
402 : : {
6964 bruce@momjian.us 403 : 142 : ginxlogSplit *data = (ginxlogSplit *) XLogRecGetData(record);
404 : : Buffer lbuffer,
405 : : rbuffer,
406 : : rootbuf;
4353 heikki.linnakangas@i 407 : 142 : bool isLeaf = (data->flags & GIN_INSERT_ISLEAF) != 0;
408 : 142 : 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 [ - + ]: 142 : if (!isLeaf)
3995 heikki.linnakangas@i 415 :UBC 0 : ginRedoClearIncompleteSplit(record, 3);
416 : :
3995 heikki.linnakangas@i 417 [ - + ]:CBC 142 : if (XLogReadBufferForRedo(record, 0, &lbuffer) != BLK_RESTORED)
3995 heikki.linnakangas@i 418 [ # # ]:UBC 0 : elog(ERROR, "GIN split record did not contain a full-page image of left page");
419 : :
3995 heikki.linnakangas@i 420 [ - + ]:CBC 142 : if (XLogReadBufferForRedo(record, 1, &rbuffer) != BLK_RESTORED)
3995 heikki.linnakangas@i 421 [ # # ]:UBC 0 : elog(ERROR, "GIN split record did not contain a full-page image of right page");
422 : :
4353 heikki.linnakangas@i 423 [ + + ]:CBC 142 : if (isRoot)
424 : : {
3995 425 [ - + ]: 3 : if (XLogReadBufferForRedo(record, 2, &rootbuf) != BLK_RESTORED)
3995 heikki.linnakangas@i 426 [ # # ]:UBC 0 : elog(ERROR, "GIN split record did not contain a full-page image of root page");
3995 heikki.linnakangas@i 427 :CBC 3 : UnlockReleaseBuffer(rootbuf);
428 : : }
429 : :
7119 teodor@sigaev.ru 430 : 142 : UnlockReleaseBuffer(rbuffer);
431 : 142 : UnlockReleaseBuffer(lbuffer);
432 : 142 : }
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
3995 heikki.linnakangas@i 439 :UBC 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 : : }
4297 447 : 0 : UnlockReleaseBuffer(buffer);
448 : 0 : }
449 : :
450 : : static void
3995 heikki.linnakangas@i 451 :CBC 3 : ginRedoVacuumDataLeafPage(XLogReaderState *record)
452 : : {
453 : 3 : XLogRecPtr lsn = record->EndRecPtr;
454 : : Buffer buffer;
455 : :
456 [ + - ]: 3 : if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
457 : : {
3478 kgrittn@postgresql.o 458 : 3 : Page page = BufferGetPage(buffer);
459 : : Size len;
460 : : ginxlogVacuumDataLeafPage *xlrec;
461 : :
3995 heikki.linnakangas@i 462 : 3 : xlrec = (ginxlogVacuumDataLeafPage *) XLogRecGetBlockData(record, 0, &len);
463 : :
4094 464 [ - + ]: 3 : Assert(GinPageIsLeaf(page));
465 [ - + ]: 3 : Assert(GinPageIsData(page));
466 : :
4297 467 : 3 : ginRedoRecompress(page, &xlrec->data);
5496 tgl@sss.pgh.pa.us 468 : 3 : PageSetLSN(page, lsn);
469 : 3 : MarkBufferDirty(buffer);
470 : : }
4094 heikki.linnakangas@i 471 [ + - ]: 3 : if (BufferIsValid(buffer))
472 : 3 : UnlockReleaseBuffer(buffer);
7119 teodor@sigaev.ru 473 : 3 : }
474 : :
475 : : static void
3995 heikki.linnakangas@i 476 :UBC 0 : ginRedoDeletePage(XLogReaderState *record)
477 : : {
478 : 0 : XLogRecPtr lsn = record->EndRecPtr;
6964 bruce@momjian.us 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 : : */
2511 akorotkov@postgresql 489 [ # # ]: 0 : if (XLogReadBufferForRedo(record, 2, &lbuffer) == BLK_NEEDS_REDO)
490 : : {
491 : 0 : page = BufferGetPage(lbuffer);
492 [ # # ]: 0 : Assert(GinPageIsData(page));
493 : 0 : GinPageGetOpaque(page)->rightlink = data->rightLink;
494 : 0 : PageSetLSN(page, lsn);
495 : 0 : MarkBufferDirty(lbuffer);
496 : : }
497 : :
3995 heikki.linnakangas@i 498 [ # # ]: 0 : if (XLogReadBufferForRedo(record, 0, &dbuffer) == BLK_NEEDS_REDO)
499 : : {
3478 kgrittn@postgresql.o 500 : 0 : page = BufferGetPage(dbuffer);
4094 heikki.linnakangas@i 501 [ # # ]: 0 : Assert(GinPageIsData(page));
2170 akorotkov@postgresql 502 : 0 : GinPageSetDeleted(page);
2511 503 : 0 : GinPageSetDeleteXid(page, data->deleteXid);
4094 heikki.linnakangas@i 504 : 0 : PageSetLSN(page, lsn);
505 : 0 : MarkBufferDirty(dbuffer);
506 : : }
507 : :
3995 508 [ # # ]: 0 : if (XLogReadBufferForRedo(record, 1, &pbuffer) == BLK_NEEDS_REDO)
509 : : {
3478 kgrittn@postgresql.o 510 : 0 : page = BufferGetPage(pbuffer);
4094 heikki.linnakangas@i 511 [ # # ]: 0 : Assert(GinPageIsData(page));
512 [ # # ]: 0 : 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);
4733 tgl@sss.pgh.pa.us 520 [ # # ]: 0 : if (BufferIsValid(pbuffer))
521 : 0 : UnlockReleaseBuffer(pbuffer);
522 [ # # ]: 0 : if (BufferIsValid(dbuffer))
523 : 0 : UnlockReleaseBuffer(dbuffer);
7119 teodor@sigaev.ru 524 : 0 : }
525 : :
526 : : static void
3995 heikki.linnakangas@i 527 :CBC 23983 : ginRedoUpdateMetapage(XLogReaderState *record)
528 : : {
529 : 23983 : XLogRecPtr lsn = record->EndRecPtr;
5983 bruce@momjian.us 530 : 23983 : 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 : : */
3995 heikki.linnakangas@i 540 : 23983 : metabuffer = XLogInitBufferForRedo(record, 0);
541 [ - + ]: 23983 : Assert(BufferGetBlockNumber(metabuffer) == GIN_METAPAGE_BLKNO);
3478 kgrittn@postgresql.o 542 : 23983 : metapage = BufferGetPage(metabuffer);
543 : :
2917 tgl@sss.pgh.pa.us 544 : 23983 : GinInitMetabuffer(metabuffer);
4248 heikki.linnakangas@i 545 : 23983 : memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
546 : 23983 : PageSetLSN(metapage, lsn);
547 : 23983 : MarkBufferDirty(metabuffer);
548 : :
5983 bruce@momjian.us 549 [ + + ]: 23983 : if (data->ntuples > 0)
550 : : {
551 : : /*
552 : : * insert into tail page
553 : : */
3995 heikki.linnakangas@i 554 [ + + ]: 23795 : if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
555 : : {
3478 kgrittn@postgresql.o 556 : 23777 : Page page = BufferGetPage(buffer);
557 : : OffsetNumber off;
558 : : int i;
559 : : Size tupsize;
560 : : char *payload;
561 : : IndexTuple tuples;
562 : : Size totaltupsize;
563 : :
3995 heikki.linnakangas@i 564 : 23777 : payload = XLogRecGetBlockData(record, 1, &totaltupsize);
565 : 23777 : tuples = (IndexTuple) payload;
566 : :
4094 567 [ - + ]: 23777 : if (PageIsEmpty(page))
4094 heikki.linnakangas@i 568 :UBC 0 : off = FirstOffsetNumber;
569 : : else
4094 heikki.linnakangas@i 570 :CBC 23777 : off = OffsetNumberNext(PageGetMaxOffsetNumber(page));
571 : :
572 [ + + ]: 95101 : for (i = 0; i < data->ntuples; i++)
573 : : {
574 : 71324 : tupsize = IndexTupleSize(tuples);
575 : :
1 peter@eisentraut.org 576 [ - + ]:GNC 71324 : if (PageAddItem(page, tuples, tupsize, off, false, false) == InvalidOffsetNumber)
4094 heikki.linnakangas@i 577 [ # # ]:UBC 0 : elog(ERROR, "failed to add item to index page");
578 : :
4094 heikki.linnakangas@i 579 :CBC 71324 : tuples = (IndexTuple) (((char *) tuples) + tupsize);
580 : :
581 : 71324 : off++;
582 : : }
3995 583 [ - + ]: 23777 : Assert(payload + totaltupsize == (char *) tuples);
584 : :
585 : : /*
586 : : * Increase counter of heap tuples
587 : : */
4094 588 : 23777 : GinPageGetOpaque(page)->maxoff++;
589 : :
590 : 23777 : PageSetLSN(page, lsn);
591 : 23777 : MarkBufferDirty(buffer);
592 : : }
593 [ + - ]: 23795 : if (BufferIsValid(buffer))
594 : 23795 : UnlockReleaseBuffer(buffer);
595 : : }
5983 bruce@momjian.us 596 [ + + ]: 188 : else if (data->prevTail != InvalidBlockNumber)
597 : : {
598 : : /*
599 : : * New tail
600 : : */
3995 heikki.linnakangas@i 601 [ + - ]: 175 : if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
602 : : {
3478 kgrittn@postgresql.o 603 : 175 : Page page = BufferGetPage(buffer);
604 : :
4094 heikki.linnakangas@i 605 : 175 : GinPageGetOpaque(page)->rightlink = data->newRightlink;
606 : :
607 : 175 : PageSetLSN(page, lsn);
608 : 175 : MarkBufferDirty(buffer);
609 : : }
610 [ + - ]: 175 : if (BufferIsValid(buffer))
611 : 175 : UnlockReleaseBuffer(buffer);
612 : : }
613 : :
6062 tgl@sss.pgh.pa.us 614 : 23983 : UnlockReleaseBuffer(metabuffer);
615 : 23983 : }
616 : :
617 : : static void
3995 heikki.linnakangas@i 618 : 180 : ginRedoInsertListPage(XLogReaderState *record)
619 : : {
620 : 180 : XLogRecPtr lsn = record->EndRecPtr;
5983 bruce@momjian.us 621 : 180 : ginxlogInsertListPage *data = (ginxlogInsertListPage *) XLogRecGetData(record);
622 : : Buffer buffer;
623 : : Page page;
624 : : OffsetNumber l,
625 : 180 : off = FirstOffsetNumber;
626 : : int i,
627 : : tupsize;
628 : : char *payload;
629 : : IndexTuple tuples;
630 : : Size totaltupsize;
631 : :
632 : : /* We always re-initialize the page. */
3995 heikki.linnakangas@i 633 : 180 : buffer = XLogInitBufferForRedo(record, 0);
3478 kgrittn@postgresql.o 634 : 180 : page = BufferGetPage(buffer);
635 : :
6062 tgl@sss.pgh.pa.us 636 : 180 : GinInitBuffer(buffer, GIN_LIST);
637 : 180 : GinPageGetOpaque(page)->rightlink = data->rightlink;
5983 bruce@momjian.us 638 [ + - ]: 180 : if (data->rightlink == InvalidBlockNumber)
639 : : {
640 : : /* tail of sublist */
6062 tgl@sss.pgh.pa.us 641 : 180 : GinPageSetFullRow(page);
642 : 180 : GinPageGetOpaque(page)->maxoff = 1;
643 : : }
644 : : else
645 : : {
6062 tgl@sss.pgh.pa.us 646 :UBC 0 : GinPageGetOpaque(page)->maxoff = 0;
647 : : }
648 : :
3995 heikki.linnakangas@i 649 :CBC 180 : payload = XLogRecGetBlockData(record, 0, &totaltupsize);
650 : :
651 : 180 : tuples = (IndexTuple) payload;
5983 bruce@momjian.us 652 [ + + ]: 717 : for (i = 0; i < data->ntuples; i++)
653 : : {
6062 tgl@sss.pgh.pa.us 654 : 537 : tupsize = IndexTupleSize(tuples);
655 : :
1 peter@eisentraut.org 656 :GNC 537 : l = PageAddItem(page, tuples, tupsize, off, false, false);
657 : :
6062 tgl@sss.pgh.pa.us 658 [ - + ]:CBC 537 : if (l == InvalidOffsetNumber)
6062 tgl@sss.pgh.pa.us 659 [ # # ]:UBC 0 : elog(ERROR, "failed to add item to index page");
660 : :
5983 bruce@momjian.us 661 :CBC 537 : tuples = (IndexTuple) (((char *) tuples) + tupsize);
4201 heikki.linnakangas@i 662 : 537 : off++;
663 : : }
3995 664 [ - + ]: 180 : Assert((char *) tuples == payload + totaltupsize);
665 : :
6062 tgl@sss.pgh.pa.us 666 : 180 : PageSetLSN(page, lsn);
667 : 180 : MarkBufferDirty(buffer);
668 : :
669 : 180 : UnlockReleaseBuffer(buffer);
670 : 180 : }
671 : :
672 : : static void
3995 heikki.linnakangas@i 673 : 14 : ginRedoDeleteListPages(XLogReaderState *record)
674 : : {
675 : 14 : XLogRecPtr lsn = record->EndRecPtr;
5983 bruce@momjian.us 676 : 14 : ginxlogDeleteListPages *data = (ginxlogDeleteListPages *) XLogRecGetData(record);
677 : : Buffer metabuffer;
678 : : Page metapage;
679 : : int i;
680 : :
3995 heikki.linnakangas@i 681 : 14 : metabuffer = XLogInitBufferForRedo(record, 0);
682 [ - + ]: 14 : Assert(BufferGetBlockNumber(metabuffer) == GIN_METAPAGE_BLKNO);
3478 kgrittn@postgresql.o 683 : 14 : metapage = BufferGetPage(metabuffer);
684 : :
2917 tgl@sss.pgh.pa.us 685 : 14 : GinInitMetabuffer(metabuffer);
686 : :
4248 heikki.linnakangas@i 687 : 14 : memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
688 : 14 : PageSetLSN(metapage, lsn);
689 : 14 : 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 : : */
5983 bruce@momjian.us 706 [ + + ]: 194 : for (i = 0; i < data->ndeleted; i++)
707 : : {
708 : : Buffer buffer;
709 : : Page page;
710 : :
3995 heikki.linnakangas@i 711 : 180 : buffer = XLogInitBufferForRedo(record, i + 1);
3478 kgrittn@postgresql.o 712 : 180 : page = BufferGetPage(buffer);
4191 heikki.linnakangas@i 713 : 180 : GinInitBuffer(buffer, GIN_DELETED);
714 : :
715 : 180 : PageSetLSN(page, lsn);
716 : 180 : MarkBufferDirty(buffer);
717 : :
718 : 180 : UnlockReleaseBuffer(buffer);
719 : : }
6062 tgl@sss.pgh.pa.us 720 : 14 : UnlockReleaseBuffer(metabuffer);
721 : 14 : }
722 : :
723 : : void
3995 heikki.linnakangas@i 724 : 51340 : gin_redo(XLogReaderState *record)
725 : : {
726 : 51340 : 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 : :
4353 735 : 51340 : oldCtx = MemoryContextSwitchTo(opCtx);
6964 bruce@momjian.us 736 [ + + + - : 51340 : switch (info)
+ - + + +
- ]
737 : : {
738 : 6 : case XLOG_GIN_CREATE_PTREE:
3995 heikki.linnakangas@i 739 : 6 : ginRedoCreatePTree(record);
7119 teodor@sigaev.ru 740 : 6 : break;
6964 bruce@momjian.us 741 : 27012 : case XLOG_GIN_INSERT:
3995 heikki.linnakangas@i 742 : 27012 : ginRedoInsert(record);
7119 teodor@sigaev.ru 743 : 27012 : break;
6964 bruce@momjian.us 744 : 142 : case XLOG_GIN_SPLIT:
3995 heikki.linnakangas@i 745 : 142 : ginRedoSplit(record);
7119 teodor@sigaev.ru 746 : 142 : break;
6964 bruce@momjian.us 747 :UBC 0 : case XLOG_GIN_VACUUM_PAGE:
3995 heikki.linnakangas@i 748 : 0 : ginRedoVacuumPage(record);
7119 teodor@sigaev.ru 749 : 0 : break;
4297 heikki.linnakangas@i 750 :CBC 3 : case XLOG_GIN_VACUUM_DATA_LEAF_PAGE:
3995 751 : 3 : ginRedoVacuumDataLeafPage(record);
4297 752 : 3 : break;
6964 bruce@momjian.us 753 :UBC 0 : case XLOG_GIN_DELETE_PAGE:
3995 heikki.linnakangas@i 754 : 0 : ginRedoDeletePage(record);
7119 teodor@sigaev.ru 755 : 0 : break;
6062 tgl@sss.pgh.pa.us 756 :CBC 23983 : case XLOG_GIN_UPDATE_META_PAGE:
3995 heikki.linnakangas@i 757 : 23983 : ginRedoUpdateMetapage(record);
6062 tgl@sss.pgh.pa.us 758 : 23983 : break;
759 : 180 : case XLOG_GIN_INSERT_LISTPAGE:
3995 heikki.linnakangas@i 760 : 180 : ginRedoInsertListPage(record);
6062 tgl@sss.pgh.pa.us 761 : 180 : break;
5983 bruce@momjian.us 762 : 14 : case XLOG_GIN_DELETE_LISTPAGE:
3995 heikki.linnakangas@i 763 : 14 : ginRedoDeleteListPages(record);
6062 tgl@sss.pgh.pa.us 764 : 14 : break;
7119 teodor@sigaev.ru 765 :UBC 0 : default:
766 [ # # ]: 0 : elog(PANIC, "gin_redo: unknown op code %u", info);
767 : : }
4353 heikki.linnakangas@i 768 :CBC 51340 : MemoryContextSwitchTo(oldCtx);
7119 teodor@sigaev.ru 769 : 51340 : MemoryContextReset(opCtx);
770 : 51340 : }
771 : :
772 : : void
6964 bruce@momjian.us 773 : 197 : gin_xlog_startup(void)
774 : : {
7119 teodor@sigaev.ru 775 : 197 : opCtx = AllocSetContextCreate(CurrentMemoryContext,
776 : : "GIN recovery temporary context",
777 : : ALLOCSET_DEFAULT_SIZES);
778 : 197 : }
779 : :
780 : : void
6964 bruce@momjian.us 781 : 143 : gin_xlog_cleanup(void)
782 : : {
7119 teodor@sigaev.ru 783 : 143 : MemoryContextDelete(opCtx);
3349 tgl@sss.pgh.pa.us 784 : 143 : opCtx = NULL;
7022 785 : 143 : }
786 : :
787 : : /*
788 : : * Mask a GIN page before running consistency checks on it.
789 : : */
790 : : void
3184 rhaas@postgresql.org 791 : 150830 : gin_mask(char *pagedata, BlockNumber blkno)
792 : : {
793 : 150830 : Page page = (Page) pagedata;
2917 tgl@sss.pgh.pa.us 794 : 150830 : PageHeader pagehdr = (PageHeader) page;
795 : : GinPageOpaque opaque;
796 : :
2958 rhaas@postgresql.org 797 : 150830 : mask_page_lsn_and_checksum(page);
3184 798 : 150830 : opaque = GinPageGetOpaque(page);
799 : :
800 : 150830 : 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 : : */
2917 tgl@sss.pgh.pa.us 807 [ + + ]: 150830 : if (opaque->flags & GIN_DELETED)
808 : 360 : mask_page_content(page);
809 [ + - ]: 150470 : else if (pagehdr->pd_lower > SizeOfPageHeaderData)
810 : 150470 : mask_unused_space(page);
3184 rhaas@postgresql.org 811 : 150830 : }
|