Debian Codegraph @

src/backend/access/hash/hash_xlog.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * hash_xlog.c
4 * WAL replay logic for hash 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/hash/hash_xlog.c
12 *
13 *-------------------------------------------------------------------------
14 */
15 #include "postgres.h"
16
17 #include "access/bufmask.h"
18 #include "access/hash.h"
19 #include "access/hash_xlog.h"
20 #include "access/xlogutils.h"
21 #include "storage/standby.h"
22
23 /*
24 * replay a hash index meta page
25 */
26 static void
27 hash_xlog_init_meta_page(XLogReaderState *record)
28 {
29 XLogRecPtr lsn = record->EndRecPtr;
30 Page page;
31 Buffer metabuf;
32
33 xl_hash_init_meta_page *xlrec = (xl_hash_init_meta_page *) XLogRecGetData(record);
34
35 /* create the index' metapage */
36 metabuf = XLogInitBufferForRedo(record, 0);
37 Assert(BufferIsValid(metabuf));
38 _hash_init_metabuffer(metabuf, xlrec->num_tuples, xlrec->procid,
39 xlrec->ffactor, true);
40 page = (Page) BufferGetPage(metabuf);
41 PageSetLSN(page, lsn);
42 MarkBufferDirty(metabuf);
43 XLogFlushBufferForRedoIfInit(record, 0, metabuf);
44
45 /* all done */
46 UnlockReleaseBuffer(metabuf);
47 }
48
49 /*
50 * replay a hash index bitmap page
51 */
52 static void
53 hash_xlog_init_bitmap_page(XLogReaderState *record)
54 {
55 XLogRecPtr lsn = record->EndRecPtr;
56 Buffer bitmapbuf;
57 Buffer metabuf;
58 Page page;
59 HashMetaPage metap;
60 uint32 num_buckets;
61
62 xl_hash_init_bitmap_page *xlrec = (xl_hash_init_bitmap_page *) XLogRecGetData(record);
63
64 /*
65 * Initialize bitmap page
66 */
67 bitmapbuf = XLogInitBufferForRedo(record, 0);
68 _hash_initbitmapbuffer(bitmapbuf, xlrec->bmsize, true);
69 PageSetLSN(BufferGetPage(bitmapbuf), lsn);
70 MarkBufferDirty(bitmapbuf);
71 XLogFlushBufferForRedoIfInit(record, 0, bitmapbuf);
72 UnlockReleaseBuffer(bitmapbuf);
73
74 /* add the new bitmap page to the metapage's list of bitmaps */
75 if (XLogReadBufferForRedo(record, 1, &metabuf) == BLK_NEEDS_REDO)
76 {
77 /*
78 * Note: in normal operation, we'd update the metapage while still
79 * holding lock on the bitmap page. But during replay it's not
80 * necessary to hold that lock, since nobody can see it yet; the
81 * creating transaction hasn't yet committed.
82 */
83 page = BufferGetPage(metabuf);
84 metap = HashPageGetMeta(page);
85
86 num_buckets = metap->hashm_maxbucket + 1;
87 metap->hashm_mapp[metap->hashm_nmaps] = num_buckets + 1;
88 metap->hashm_nmaps++;
89
90 PageSetLSN(page, lsn);
91 MarkBufferDirty(metabuf);
92 XLogFlushBufferForRedoIfInit(record, 1, metabuf);
93 }
94 if (BufferIsValid(metabuf))
95 UnlockReleaseBuffer(metabuf);
96 }
97
98 /*
99 * replay a hash index insert without split
100 */
101 static void
102 hash_xlog_insert(XLogReaderState *record)
103 {
104 HashMetaPage metap;
105 XLogRecPtr lsn = record->EndRecPtr;
106 xl_hash_insert *xlrec = (xl_hash_insert *) XLogRecGetData(record);
107 Buffer buffer;
108 Page page;
109
110 if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
111 {
112 Size datalen;
113 char *datapos = XLogRecGetBlockData(record, 0, &datalen);
114
115 page = BufferGetPage(buffer);
116
117 if (PageAddItem(page, (Item) datapos, datalen, xlrec->offnum,
118 false, false) == InvalidOffsetNumber)
119 elog(PANIC, "hash_xlog_insert: failed to add item");
120
121 PageSetLSN(page, lsn);
122 MarkBufferDirty(buffer);
123 }
124 if (BufferIsValid(buffer))
125 UnlockReleaseBuffer(buffer);
126
127 if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
128 {
129 /*
130 * Note: in normal operation, we'd update the metapage while still
131 * holding lock on the page we inserted into. But during replay it's
132 * not necessary to hold that lock, since no other index updates can
133 * be happening concurrently.
134 */
135 page = BufferGetPage(buffer);
136 metap = HashPageGetMeta(page);
137 metap->hashm_ntuples += 1;
138
139 PageSetLSN(page, lsn);
140 MarkBufferDirty(buffer);
141 }
142 if (BufferIsValid(buffer))
143 UnlockReleaseBuffer(buffer);
144 }
145
146 /*
147 * replay addition of overflow page for hash index
148 */
149 static void
150 hash_xlog_add_ovfl_page(XLogReaderState *record)
151 {
152 XLogRecPtr lsn = record->EndRecPtr;
153 xl_hash_add_ovfl_page *xlrec = (xl_hash_add_ovfl_page *) XLogRecGetData(record);
154 Buffer leftbuf;
155 Buffer ovflbuf;
156 Buffer metabuf;
157 BlockNumber leftblk;
158 BlockNumber rightblk;
159 BlockNumber newmapblk = InvalidBlockNumber;
160 Page ovflpage;
161 HashPageOpaque ovflopaque;
162 uint32 *num_bucket;
163 char *data;
164 Size datalen PG_USED_FOR_ASSERTS_ONLY;
165 bool new_bmpage = false;
166
167 XLogRecGetBlockTag(record, 0, NULL, NULL, &rightblk);
168 XLogRecGetBlockTag(record, 1, NULL, NULL, &leftblk);
169
170 ovflbuf = XLogInitBufferForRedo(record, 0);
171 Assert(BufferIsValid(ovflbuf));
172
173 data = XLogRecGetBlockData(record, 0, &datalen);
174 num_bucket = (uint32 *) data;
175 Assert(datalen == sizeof(uint32));
176 _hash_initbuf(ovflbuf, InvalidBlockNumber, *num_bucket, LH_OVERFLOW_PAGE,
177 true);
178 /* update backlink */
179 ovflpage = BufferGetPage(ovflbuf);
180 ovflopaque = HashPageGetOpaque(ovflpage);
181 ovflopaque->hasho_prevblkno = leftblk;
182
183 PageSetLSN(ovflpage, lsn);
184 MarkBufferDirty(ovflbuf);
185
186 if (XLogReadBufferForRedo(record, 1, &leftbuf) == BLK_NEEDS_REDO)
187 {
188 Page leftpage;
189 HashPageOpaque leftopaque;
190
191 leftpage = BufferGetPage(leftbuf);
192 leftopaque = HashPageGetOpaque(leftpage);
193 leftopaque->hasho_nextblkno = rightblk;
194
195 PageSetLSN(leftpage, lsn);
196 MarkBufferDirty(leftbuf);
197 }
198
199 if (BufferIsValid(leftbuf))
200 UnlockReleaseBuffer(leftbuf);
201 UnlockReleaseBuffer(ovflbuf);
202
203 /*
204 * Note: in normal operation, we'd update the bitmap and meta page while
205 * still holding lock on the overflow pages. But during replay it's not
206 * necessary to hold those locks, since no other index updates can be
207 * happening concurrently.
208 */
209 if (XLogRecHasBlockRef(record, 2))
210 {
211 Buffer mapbuffer;
212
213 if (XLogReadBufferForRedo(record, 2, &mapbuffer) == BLK_NEEDS_REDO)
214 {
215 Page mappage = (Page) BufferGetPage(mapbuffer);
216 uint32 *freep = NULL;
217 uint32 *bitmap_page_bit;
218
219 freep = HashPageGetBitmap(mappage);
220
221 data = XLogRecGetBlockData(record, 2, &datalen);
222 bitmap_page_bit = (uint32 *) data;
223
224 SETBIT(freep, *bitmap_page_bit);
225
226 PageSetLSN(mappage, lsn);
227 MarkBufferDirty(mapbuffer);
228 }
229 if (BufferIsValid(mapbuffer))
230 UnlockReleaseBuffer(mapbuffer);
231 }
232
233 if (XLogRecHasBlockRef(record, 3))
234 {
235 Buffer newmapbuf;
236
237 newmapbuf = XLogInitBufferForRedo(record, 3);
238
239 _hash_initbitmapbuffer(newmapbuf, xlrec->bmsize, true);
240
241 new_bmpage = true;
242 newmapblk = BufferGetBlockNumber(newmapbuf);
243
244 MarkBufferDirty(newmapbuf);
245 PageSetLSN(BufferGetPage(newmapbuf), lsn);
246
247 UnlockReleaseBuffer(newmapbuf);
248 }
249
250 if (XLogReadBufferForRedo(record, 4, &metabuf) == BLK_NEEDS_REDO)
251 {
252 HashMetaPage metap;
253 Page page;
254 uint32 *firstfree_ovflpage;
255
256 data = XLogRecGetBlockData(record, 4, &datalen);
257 firstfree_ovflpage = (uint32 *) data;
258
259 page = BufferGetPage(metabuf);
260 metap = HashPageGetMeta(page);
261 metap->hashm_firstfree = *firstfree_ovflpage;
262
263 if (!xlrec->bmpage_found)
264 {
265 metap->hashm_spares[metap->hashm_ovflpoint]++;
266
267 if (new_bmpage)
268 {
269 Assert(BlockNumberIsValid(newmapblk));
270
271 metap->hashm_mapp[metap->hashm_nmaps] = newmapblk;
272 metap->hashm_nmaps++;
273 metap->hashm_spares[metap->hashm_ovflpoint]++;
274 }
275 }
276
277 PageSetLSN(page, lsn);
278 MarkBufferDirty(metabuf);
279 }
280 if (BufferIsValid(metabuf))
281 UnlockReleaseBuffer(metabuf);
282 }
283
284 /*
285 * replay allocation of page for split operation
286 */
287 static void
288 hash_xlog_split_allocate_page(XLogReaderState *record)
289 {
290 XLogRecPtr lsn = record->EndRecPtr;
291 xl_hash_split_allocate_page *xlrec = (xl_hash_split_allocate_page *) XLogRecGetData(record);
292 Buffer oldbuf;
293 Buffer newbuf;
294 Buffer metabuf;
295 Size datalen PG_USED_FOR_ASSERTS_ONLY;
296 char *data;
297 XLogRedoAction action;
298
299 /*
300 * To be consistent with normal operation, here we take cleanup locks on
301 * both the old and new buckets even though there can't be any concurrent
302 * inserts.
303 */
304
305 /* replay the record for old bucket */
306 action = XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &oldbuf);
307
308 /*
309 * Note that we still update the page even if it was restored from a full
310 * page image, because the special space is not included in the image.
311 */
312 if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
313 {
314 Page oldpage;
315 HashPageOpaque oldopaque;
316
317 oldpage = BufferGetPage(oldbuf);
318 oldopaque = HashPageGetOpaque(oldpage);
319
320 oldopaque->hasho_flag = xlrec->old_bucket_flag;
321 oldopaque->hasho_prevblkno = xlrec->new_bucket;
322
323 PageSetLSN(oldpage, lsn);
324 MarkBufferDirty(oldbuf);
325 }
326
327 /* replay the record for new bucket */
328 XLogReadBufferForRedoExtended(record, 1, RBM_ZERO_AND_CLEANUP_LOCK, true,
329 &newbuf);
330 _hash_initbuf(newbuf, xlrec->new_bucket, xlrec->new_bucket,
331 xlrec->new_bucket_flag, true);
332 MarkBufferDirty(newbuf);
333 PageSetLSN(BufferGetPage(newbuf), lsn);
334
335 /*
336 * We can release the lock on old bucket early as well but doing here to
337 * consistent with normal operation.
338 */
339 if (BufferIsValid(oldbuf))
340 UnlockReleaseBuffer(oldbuf);
341 if (BufferIsValid(newbuf))
342 UnlockReleaseBuffer(newbuf);
343
344 /*
345 * Note: in normal operation, we'd update the meta page while still
346 * holding lock on the old and new bucket pages. But during replay it's
347 * not necessary to hold those locks, since no other bucket splits can be
348 * happening concurrently.
349 */
350
351 /* replay the record for metapage changes */
352 if (XLogReadBufferForRedo(record, 2, &metabuf) == BLK_NEEDS_REDO)
353 {
354 Page page;
355 HashMetaPage metap;
356
357 page = BufferGetPage(metabuf);
358 metap = HashPageGetMeta(page);
359 metap->hashm_maxbucket = xlrec->new_bucket;
360
361 data = XLogRecGetBlockData(record, 2, &datalen);
362
363 if (xlrec->flags & XLH_SPLIT_META_UPDATE_MASKS)
364 {
365 uint32 lowmask;
366 uint32 *highmask;
367
368 /* extract low and high masks. */
369 memcpy(&lowmask, data, sizeof(uint32));
370 highmask = (uint32 *) ((char *) data + sizeof(uint32));
371
372 /* update metapage */
373 metap->hashm_lowmask = lowmask;
374 metap->hashm_highmask = *highmask;
375
376 data += sizeof(uint32) * 2;
377 }
378
379 if (xlrec->flags & XLH_SPLIT_META_UPDATE_SPLITPOINT)
380 {
381 uint32 ovflpoint;
382 uint32 *ovflpages;
383
384 /* extract information of overflow pages. */
385 memcpy(&ovflpoint, data, sizeof(uint32));
386 ovflpages = (uint32 *) ((char *) data + sizeof(uint32));
387
388 /* update metapage */
389 metap->hashm_spares[ovflpoint] = *ovflpages;
390 metap->hashm_ovflpoint = ovflpoint;
391 }
392
393 MarkBufferDirty(metabuf);
394 PageSetLSN(BufferGetPage(metabuf), lsn);
395 }
396
397 if (BufferIsValid(metabuf))
398 UnlockReleaseBuffer(metabuf);
399 }
400
401 /*
402 * replay of split operation
403 */
404 static void
405 hash_xlog_split_page(XLogReaderState *record)
406 {
407 Buffer buf;
408
409 if (XLogReadBufferForRedo(record, 0, &buf) != BLK_RESTORED)
410 elog(ERROR, "Hash split record did not contain a full-page image");
411
412 UnlockReleaseBuffer(buf);
413 }
414
415 /*
416 * replay completion of split operation
417 */
418 static void
419 hash_xlog_split_complete(XLogReaderState *record)
420 {
421 XLogRecPtr lsn = record->EndRecPtr;
422 xl_hash_split_complete *xlrec = (xl_hash_split_complete *) XLogRecGetData(record);
423 Buffer oldbuf;
424 Buffer newbuf;
425 XLogRedoAction action;
426
427 /* replay the record for old bucket */
428 action = XLogReadBufferForRedo(record, 0, &oldbuf);
429
430 /*
431 * Note that we still update the page even if it was restored from a full
432 * page image, because the bucket flag is not included in the image.
433 */
434 if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
435 {
436 Page oldpage;
437 HashPageOpaque oldopaque;
438
439 oldpage = BufferGetPage(oldbuf);
440 oldopaque = HashPageGetOpaque(oldpage);
441
442 oldopaque->hasho_flag = xlrec->old_bucket_flag;
443
444 PageSetLSN(oldpage, lsn);
445 MarkBufferDirty(oldbuf);
446 }
447 if (BufferIsValid(oldbuf))
448 UnlockReleaseBuffer(oldbuf);
449
450 /* replay the record for new bucket */
451 action = XLogReadBufferForRedo(record, 1, &newbuf);
452
453 /*
454 * Note that we still update the page even if it was restored from a full
455 * page image, because the bucket flag is not included in the image.
456 */
457 if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
458 {
459 Page newpage;
460 HashPageOpaque nopaque;
461
462 newpage = BufferGetPage(newbuf);
463 nopaque = HashPageGetOpaque(newpage);
464
465 nopaque->hasho_flag = xlrec->new_bucket_flag;
466
467 PageSetLSN(newpage, lsn);
468 MarkBufferDirty(newbuf);
469 }
470 if (BufferIsValid(newbuf))
471 UnlockReleaseBuffer(newbuf);
472 }
473
474 /*
475 * replay move of page contents for squeeze operation of hash index
476 */
477 static void
478 hash_xlog_move_page_contents(XLogReaderState *record)
479 {
480 XLogRecPtr lsn = record->EndRecPtr;
481 xl_hash_move_page_contents *xldata = (xl_hash_move_page_contents *) XLogRecGetData(record);
482 Buffer bucketbuf = InvalidBuffer;
483 Buffer writebuf = InvalidBuffer;
484 Buffer deletebuf = InvalidBuffer;
485 XLogRedoAction action;
486
487 /*
488 * Ensure we have a cleanup lock on primary bucket page before we start
489 * with the actual replay operation. This is to ensure that neither a
490 * scan can start nor a scan can be already-in-progress during the replay
491 * of this operation. If we allow scans during this operation, then they
492 * can miss some records or show the same record multiple times.
493 */
494 if (xldata->is_prim_bucket_same_wrt)
495 action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &writebuf);
496 else
497 {
498 /*
499 * we don't care for return value as the purpose of reading bucketbuf
500 * is to ensure a cleanup lock on primary bucket page.
501 */
502 (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
503
504 action = XLogReadBufferForRedo(record, 1, &writebuf);
505 }
506
507 /* replay the record for adding entries in overflow buffer */
508 if (action == BLK_NEEDS_REDO)
509 {
510 Page writepage;
511 char *begin;
512 char *data;
513 Size datalen;
514 uint16 ninserted = 0;
515
516 data = begin = XLogRecGetBlockData(record, 1, &datalen);
517
518 writepage = (Page) BufferGetPage(writebuf);
519
520 if (xldata->ntups > 0)
521 {
522 OffsetNumber *towrite = (OffsetNumber *) data;
523
524 data += sizeof(OffsetNumber) * xldata->ntups;
525
526 while (data - begin < datalen)
527 {
528 IndexTuple itup = (IndexTuple) data;
529 Size itemsz;
530 OffsetNumber l;
531
532 itemsz = IndexTupleSize(itup);
533 itemsz = MAXALIGN(itemsz);
534
535 data += itemsz;
536
537 l = PageAddItem(writepage, (Item) itup, itemsz, towrite[ninserted], false, false);
538 if (l == InvalidOffsetNumber)
539 elog(ERROR, "hash_xlog_move_page_contents: failed to add item to hash index page, size %d bytes",
540 (int) itemsz);
541
542 ninserted++;
543 }
544 }
545
546 /*
547 * number of tuples inserted must be same as requested in REDO record.
548 */
549 Assert(ninserted == xldata->ntups);
550
551 PageSetLSN(writepage, lsn);
552 MarkBufferDirty(writebuf);
553 }
554
555 /* replay the record for deleting entries from overflow buffer */
556 if (XLogReadBufferForRedo(record, 2, &deletebuf) == BLK_NEEDS_REDO)
557 {
558 Page page;
559 char *ptr;
560 Size len;
561
562 ptr = XLogRecGetBlockData(record, 2, &len);
563
564 page = (Page) BufferGetPage(deletebuf);
565
566 if (len > 0)
567 {
568 OffsetNumber *unused;
569 OffsetNumber *unend;
570
571 unused = (OffsetNumber *) ptr;
572 unend = (OffsetNumber *) ((char *) ptr + len);
573
574 if ((unend - unused) > 0)
575 PageIndexMultiDelete(page, unused, unend - unused);
576 }
577
578 PageSetLSN(page, lsn);
579 MarkBufferDirty(deletebuf);
580 }
581
582 /*
583 * Replay is complete, now we can release the buffers. We release locks at
584 * end of replay operation to ensure that we hold lock on primary bucket
585 * page till end of operation. We can optimize by releasing the lock on
586 * write buffer as soon as the operation for same is complete, if it is
587 * not same as primary bucket page, but that doesn't seem to be worth
588 * complicating the code.
589 */
590 if (BufferIsValid(deletebuf))
591 UnlockReleaseBuffer(deletebuf);
592
593 if (BufferIsValid(writebuf))
594 UnlockReleaseBuffer(writebuf);
595
596 if (BufferIsValid(bucketbuf))
597 UnlockReleaseBuffer(bucketbuf);
598 }
599
600 /*
601 * replay squeeze page operation of hash index
602 */
603 static void
604 hash_xlog_squeeze_page(XLogReaderState *record)
605 {
606 XLogRecPtr lsn = record->EndRecPtr;
607 xl_hash_squeeze_page *xldata = (xl_hash_squeeze_page *) XLogRecGetData(record);
608 Buffer bucketbuf = InvalidBuffer;
609 Buffer writebuf = InvalidBuffer;
610 Buffer ovflbuf;
611 Buffer prevbuf = InvalidBuffer;
612 Buffer mapbuf;
613 XLogRedoAction action;
614
615 /*
616 * Ensure we have a cleanup lock on primary bucket page before we start
617 * with the actual replay operation. This is to ensure that neither a
618 * scan can start nor a scan can be already-in-progress during the replay
619 * of this operation. If we allow scans during this operation, then they
620 * can miss some records or show the same record multiple times.
621 */
622 if (xldata->is_prim_bucket_same_wrt)
623 action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &writebuf);
624 else
625 {
626 /*
627 * we don't care for return value as the purpose of reading bucketbuf
628 * is to ensure a cleanup lock on primary bucket page.
629 */
630 (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
631
632 if (xldata->ntups > 0 || xldata->is_prev_bucket_same_wrt)
633 action = XLogReadBufferForRedo(record, 1, &writebuf);
634 else
635 action = BLK_NOTFOUND;
636 }
637
638 /* replay the record for adding entries in overflow buffer */
639 if (action == BLK_NEEDS_REDO)
640 {
641 Page writepage;
642 char *begin;
643 char *data;
644 Size datalen;
645 uint16 ninserted = 0;
646 bool mod_wbuf = false;
647
648 data = begin = XLogRecGetBlockData(record, 1, &datalen);
649
650 writepage = (Page) BufferGetPage(writebuf);
651
652 if (xldata->ntups > 0)
653 {
654 OffsetNumber *towrite = (OffsetNumber *) data;
655
656 data += sizeof(OffsetNumber) * xldata->ntups;
657
658 while (data - begin < datalen)
659 {
660 IndexTuple itup = (IndexTuple) data;
661 Size itemsz;
662 OffsetNumber l;
663
664 itemsz = IndexTupleSize(itup);
665 itemsz = MAXALIGN(itemsz);
666
667 data += itemsz;
668
669 l = PageAddItem(writepage, (Item) itup, itemsz, towrite[ninserted], false, false);
670 if (l == InvalidOffsetNumber)
671 elog(ERROR, "hash_xlog_squeeze_page: failed to add item to hash index page, size %d bytes",
672 (int) itemsz);
673
674 ninserted++;
675 }
676
677 mod_wbuf = true;
678 }
679 else
680 {
681 /*
682 * Ensure that the required flags are set when there are no
683 * tuples. See _hash_freeovflpage().
684 */
685 Assert(xldata->is_prim_bucket_same_wrt ||
686 xldata->is_prev_bucket_same_wrt);
687 }
688
689 /*
690 * number of tuples inserted must be same as requested in REDO record.
691 */
692 Assert(ninserted == xldata->ntups);
693
694 /*
695 * if the page on which are adding tuples is a page previous to freed
696 * overflow page, then update its nextblkno.
697 */
698 if (xldata->is_prev_bucket_same_wrt)
699 {
700 HashPageOpaque writeopaque = HashPageGetOpaque(writepage);
701
702 writeopaque->hasho_nextblkno = xldata->nextblkno;
703 mod_wbuf = true;
704 }
705
706 /* Set LSN and mark writebuf dirty iff it is modified */
707 if (mod_wbuf)
708 {
709 PageSetLSN(writepage, lsn);
710 MarkBufferDirty(writebuf);
711 }
712 }
713
714 /* replay the record for initializing overflow buffer */
715 if (XLogReadBufferForRedo(record, 2, &ovflbuf) == BLK_NEEDS_REDO)
716 {
717 Page ovflpage;
718 HashPageOpaque ovflopaque;
719
720 ovflpage = BufferGetPage(ovflbuf);
721
722 _hash_pageinit(ovflpage, BufferGetPageSize(ovflbuf));
723
724 ovflopaque = HashPageGetOpaque(ovflpage);
725
726 ovflopaque->hasho_prevblkno = InvalidBlockNumber;
727 ovflopaque->hasho_nextblkno = InvalidBlockNumber;
728 ovflopaque->hasho_bucket = InvalidBucket;
729 ovflopaque->hasho_flag = LH_UNUSED_PAGE;
730 ovflopaque->hasho_page_id = HASHO_PAGE_ID;
731
732 PageSetLSN(ovflpage, lsn);
733 MarkBufferDirty(ovflbuf);
734 }
735 if (BufferIsValid(ovflbuf))
736 UnlockReleaseBuffer(ovflbuf);
737
738 /* replay the record for page previous to the freed overflow page */
739 if (!xldata->is_prev_bucket_same_wrt &&
740 XLogReadBufferForRedo(record, 3, &prevbuf) == BLK_NEEDS_REDO)
741 {
742 Page prevpage = BufferGetPage(prevbuf);
743 HashPageOpaque prevopaque = HashPageGetOpaque(prevpage);
744
745 prevopaque->hasho_nextblkno = xldata->nextblkno;
746
747 PageSetLSN(prevpage, lsn);
748 MarkBufferDirty(prevbuf);
749 }
750 if (BufferIsValid(prevbuf))
751 UnlockReleaseBuffer(prevbuf);
752
753 /* replay the record for page next to the freed overflow page */
754 if (XLogRecHasBlockRef(record, 4))
755 {
756 Buffer nextbuf;
757
758 if (XLogReadBufferForRedo(record, 4, &nextbuf) == BLK_NEEDS_REDO)
759 {
760 Page nextpage = BufferGetPage(nextbuf);
761 HashPageOpaque nextopaque = HashPageGetOpaque(nextpage);
762
763 nextopaque->hasho_prevblkno = xldata->prevblkno;
764
765 PageSetLSN(nextpage, lsn);
766 MarkBufferDirty(nextbuf);
767 }
768 if (BufferIsValid(nextbuf))
769 UnlockReleaseBuffer(nextbuf);
770 }
771
772 if (BufferIsValid(writebuf))
773 UnlockReleaseBuffer(writebuf);
774
775 if (BufferIsValid(bucketbuf))
776 UnlockReleaseBuffer(bucketbuf);
777
778 /*
779 * Note: in normal operation, we'd update the bitmap and meta page while
780 * still holding lock on the primary bucket page and overflow pages. But
781 * during replay it's not necessary to hold those locks, since no other
782 * index updates can be happening concurrently.
783 */
784 /* replay the record for bitmap page */
785 if (XLogReadBufferForRedo(record, 5, &mapbuf) == BLK_NEEDS_REDO)
786 {
787 Page mappage = (Page) BufferGetPage(mapbuf);
788 uint32 *freep = NULL;
789 char *data;
790 uint32 *bitmap_page_bit;
791 Size datalen;
792
793 freep = HashPageGetBitmap(mappage);
794
795 data = XLogRecGetBlockData(record, 5, &datalen);
796 bitmap_page_bit = (uint32 *) data;
797
798 CLRBIT(freep, *bitmap_page_bit);
799
800 PageSetLSN(mappage, lsn);
801 MarkBufferDirty(mapbuf);
802 }
803 if (BufferIsValid(mapbuf))
804 UnlockReleaseBuffer(mapbuf);
805
806 /* replay the record for meta page */
807 if (XLogRecHasBlockRef(record, 6))
808 {
809 Buffer metabuf;
810
811 if (XLogReadBufferForRedo(record, 6, &metabuf) == BLK_NEEDS_REDO)
812 {
813 HashMetaPage metap;
814 Page page;
815 char *data;
816 uint32 *firstfree_ovflpage;
817 Size datalen;
818
819 data = XLogRecGetBlockData(record, 6, &datalen);
820 firstfree_ovflpage = (uint32 *) data;
821
822 page = BufferGetPage(metabuf);
823 metap = HashPageGetMeta(page);
824 metap->hashm_firstfree = *firstfree_ovflpage;
825
826 PageSetLSN(page, lsn);
827 MarkBufferDirty(metabuf);
828 }
829 if (BufferIsValid(metabuf))
830 UnlockReleaseBuffer(metabuf);
831 }
832 }
833
834 /*
835 * replay delete operation of hash index
836 */
837 static void
838 hash_xlog_delete(XLogReaderState *record)
839 {
840 XLogRecPtr lsn = record->EndRecPtr;
841 xl_hash_delete *xldata = (xl_hash_delete *) XLogRecGetData(record);
842 Buffer bucketbuf = InvalidBuffer;
843 Buffer deletebuf;
844 Page page;
845 XLogRedoAction action;
846
847 /*
848 * Ensure we have a cleanup lock on primary bucket page before we start
849 * with the actual replay operation. This is to ensure that neither a
850 * scan can start nor a scan can be already-in-progress during the replay
851 * of this operation. If we allow scans during this operation, then they
852 * can miss some records or show the same record multiple times.
853 */
854 if (xldata->is_primary_bucket_page)
855 action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &deletebuf);
856 else
857 {
858 /*
859 * we don't care for return value as the purpose of reading bucketbuf
860 * is to ensure a cleanup lock on primary bucket page.
861 */
862 (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
863
864 action = XLogReadBufferForRedo(record, 1, &deletebuf);
865 }
866
867 /* replay the record for deleting entries in bucket page */
868 if (action == BLK_NEEDS_REDO)
869 {
870 char *ptr;
871 Size len;
872
873 ptr = XLogRecGetBlockData(record, 1, &len);
874
875 page = (Page) BufferGetPage(deletebuf);
876
877 if (len > 0)
878 {
879 OffsetNumber *unused;
880 OffsetNumber *unend;
881
882 unused = (OffsetNumber *) ptr;
883 unend = (OffsetNumber *) ((char *) ptr + len);
884
885 if ((unend - unused) > 0)
886 PageIndexMultiDelete(page, unused, unend - unused);
887 }
888
889 /*
890 * Mark the page as not containing any LP_DEAD items only if
891 * clear_dead_marking flag is set to true. See comments in
892 * hashbucketcleanup() for details.
893 */
894 if (xldata->clear_dead_marking)
895 {
896 HashPageOpaque pageopaque;
897
898 pageopaque = HashPageGetOpaque(page);
899 pageopaque->hasho_flag &= ~LH_PAGE_HAS_DEAD_TUPLES;
900 }
901
902 PageSetLSN(page, lsn);
903 MarkBufferDirty(deletebuf);
904 }
905 if (BufferIsValid(deletebuf))
906 UnlockReleaseBuffer(deletebuf);
907
908 if (BufferIsValid(bucketbuf))
909 UnlockReleaseBuffer(bucketbuf);
910 }
911
912 /*
913 * replay split cleanup flag operation for primary bucket page.
914 */
915 static void
916 hash_xlog_split_cleanup(XLogReaderState *record)
917 {
918 XLogRecPtr lsn = record->EndRecPtr;
919 Buffer buffer;
920 Page page;
921
922 if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
923 {
924 HashPageOpaque bucket_opaque;
925
926 page = (Page) BufferGetPage(buffer);
927
928 bucket_opaque = HashPageGetOpaque(page);
929 bucket_opaque->hasho_flag &= ~LH_BUCKET_NEEDS_SPLIT_CLEANUP;
930 PageSetLSN(page, lsn);
931 MarkBufferDirty(buffer);
932 }
933 if (BufferIsValid(buffer))
934 UnlockReleaseBuffer(buffer);
935 }
936
937 /*
938 * replay for update meta page
939 */
940 static void
941 hash_xlog_update_meta_page(XLogReaderState *record)
942 {
943 HashMetaPage metap;
944 XLogRecPtr lsn = record->EndRecPtr;
945 xl_hash_update_meta_page *xldata = (xl_hash_update_meta_page *) XLogRecGetData(record);
946 Buffer metabuf;
947 Page page;
948
949 if (XLogReadBufferForRedo(record, 0, &metabuf) == BLK_NEEDS_REDO)
950 {
951 page = BufferGetPage(metabuf);
952 metap = HashPageGetMeta(page);
953
954 metap->hashm_ntuples = xldata->ntuples;
955
956 PageSetLSN(page, lsn);
957 MarkBufferDirty(metabuf);
958 }
959 if (BufferIsValid(metabuf))
960 UnlockReleaseBuffer(metabuf);
961 }
962
963 /*
964 * replay delete operation in hash index to remove
965 * tuples marked as DEAD during index tuple insertion.
966 */
967 static void
968 hash_xlog_vacuum_one_page(XLogReaderState *record)
969 {
970 XLogRecPtr lsn = record->EndRecPtr;
971 xl_hash_vacuum_one_page *xldata;
972 Buffer buffer;
973 Buffer metabuf;
974 Page page;
975 XLogRedoAction action;
976 HashPageOpaque pageopaque;
977 OffsetNumber *toDelete;
978
979 xldata = (xl_hash_vacuum_one_page *) XLogRecGetData(record);
980 toDelete = xldata->offsets;
981
982 /*
983 * If we have any conflict processing to do, it must happen before we
984 * update the page.
985 *
986 * Hash index records that are marked as LP_DEAD and being removed during
987 * hash index tuple insertion can conflict with standby queries. You might
988 * think that vacuum records would conflict as well, but we've handled
989 * that already. XLOG_HEAP2_PRUNE_VACUUM_SCAN records provide the highest
990 * xid cleaned by the vacuum of the heap and so we can resolve any
991 * conflicts just once when that arrives. After that we know that no
992 * conflicts exist from individual hash index vacuum records on that
993 * index.
994 */
995 if (InHotStandby)
996 {
997 RelFileLocator rlocator;
998
999 XLogRecGetBlockTag(record, 0, &rlocator, NULL, NULL);
1000 ResolveRecoveryConflictWithSnapshot(xldata->snapshotConflictHorizon,
1001 xldata->isCatalogRel,
1002 rlocator);
1003 }
1004
1005 action = XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &buffer);
1006
1007 if (action == BLK_NEEDS_REDO)
1008 {
1009 page = (Page) BufferGetPage(buffer);
1010
1011 PageIndexMultiDelete(page, toDelete, xldata->ntuples);
1012
1013 /*
1014 * Mark the page as not containing any LP_DEAD items. See comments in
1015 * _hash_vacuum_one_page() for details.
1016 */
1017 pageopaque = HashPageGetOpaque(page);
1018 pageopaque->hasho_flag &= ~LH_PAGE_HAS_DEAD_TUPLES;
1019
1020 PageSetLSN(page, lsn);
1021 MarkBufferDirty(buffer);
1022 }
1023 if (BufferIsValid(buffer))
1024 UnlockReleaseBuffer(buffer);
1025
1026 if (XLogReadBufferForRedo(record, 1, &metabuf) == BLK_NEEDS_REDO)
1027 {
1028 Page metapage;
1029 HashMetaPage metap;
1030
1031 metapage = BufferGetPage(metabuf);
1032 metap = HashPageGetMeta(metapage);
1033
1034 metap->hashm_ntuples -= xldata->ntuples;
1035
1036 PageSetLSN(metapage, lsn);
1037 MarkBufferDirty(metabuf);
1038 }
1039 if (BufferIsValid(metabuf))
1040 UnlockReleaseBuffer(metabuf);
1041 }
1042
1043 void
1044 hash_redo(XLogReaderState *record)
1045 {
1046 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1047
1048 switch (info)
1049 {
1050 case XLOG_HASH_INIT_META_PAGE:
1051 hash_xlog_init_meta_page(record);
1052 break;
1053 case XLOG_HASH_INIT_BITMAP_PAGE:
1054 hash_xlog_init_bitmap_page(record);
1055 break;
1056 case XLOG_HASH_INSERT:
1057 hash_xlog_insert(record);
1058 break;
1059 case XLOG_HASH_ADD_OVFL_PAGE:
1060 hash_xlog_add_ovfl_page(record);
1061 break;
1062 case XLOG_HASH_SPLIT_ALLOCATE_PAGE:
1063 hash_xlog_split_allocate_page(record);
1064 break;
1065 case XLOG_HASH_SPLIT_PAGE:
1066 hash_xlog_split_page(record);
1067 break;
1068 case XLOG_HASH_SPLIT_COMPLETE:
1069 hash_xlog_split_complete(record);
1070 break;
1071 case XLOG_HASH_MOVE_PAGE_CONTENTS:
1072 hash_xlog_move_page_contents(record);
1073 break;
1074 case XLOG_HASH_SQUEEZE_PAGE:
1075 hash_xlog_squeeze_page(record);
1076 break;
1077 case XLOG_HASH_DELETE:
1078 hash_xlog_delete(record);
1079 break;
1080 case XLOG_HASH_SPLIT_CLEANUP:
1081 hash_xlog_split_cleanup(record);
1082 break;
1083 case XLOG_HASH_UPDATE_META_PAGE:
1084 hash_xlog_update_meta_page(record);
1085 break;
1086 case XLOG_HASH_VACUUM_ONE_PAGE:
1087 hash_xlog_vacuum_one_page(record);
1088 break;
1089 default:
1090 elog(PANIC, "hash_redo: unknown op code %u", info);
1091 }
1092 }
1093
1094 /*
1095 * Mask a hash page before performing consistency checks on it.
1096 */
1097 void
1098 hash_mask(char *pagedata, BlockNumber blkno)
1099 {
1100 Page page = (Page) pagedata;
1101 HashPageOpaque opaque;
1102 int pagetype;
1103
1104 mask_page_lsn_and_checksum(page);
1105
1106 mask_page_hint_bits(page);
1107 mask_unused_space(page);
1108
1109 opaque = HashPageGetOpaque(page);
1110
1111 pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
1112 if (pagetype == LH_UNUSED_PAGE)
1113 {
1114 /*
1115 * Mask everything on a UNUSED page.
1116 */
1117 mask_page_content(page);
1118 }
1119 else if (pagetype == LH_BUCKET_PAGE ||
1120 pagetype == LH_OVERFLOW_PAGE)
1121 {
1122 /*
1123 * In hash bucket and overflow pages, it is possible to modify the
1124 * LP_FLAGS without emitting any WAL record. Hence, mask the line
1125 * pointer flags. See hashgettuple(), _hash_kill_items() for details.
1126 */
1127 mask_lp_flags(page);
1128 }
1129
1130 /*
1131 * It is possible that the hint bit LH_PAGE_HAS_DEAD_TUPLES may remain
1132 * unlogged. So, mask it. See _hash_kill_items() for details.
1133 */
1134 opaque->hasho_flag &= ~LH_PAGE_HAS_DEAD_TUPLES;
1135 }