PostgreSQL Source Code  git master
nbtxlog.c File Reference
#include "postgres.h"
#include "access/bufmask.h"
#include "access/nbtree.h"
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xlogutils.h"
#include "miscadmin.h"
#include "storage/procarray.h"
#include "utils/memutils.h"
Include dependency graph for nbtxlog.c:

Go to the source code of this file.

Functions

static void _bt_restore_page (Page page, char *from, int len)
 
static void _bt_restore_meta (XLogReaderState *record, uint8 block_id)
 
static void _bt_clear_incomplete_split (XLogReaderState *record, uint8 block_id)
 
static void btree_xlog_insert (bool isleaf, bool ismeta, bool posting, XLogReaderState *record)
 
static void btree_xlog_split (bool newitemonleft, XLogReaderState *record)
 
static void btree_xlog_dedup (XLogReaderState *record)
 
static void btree_xlog_vacuum (XLogReaderState *record)
 
static void btree_xlog_delete (XLogReaderState *record)
 
static void btree_xlog_mark_page_halfdead (uint8 info, XLogReaderState *record)
 
static void btree_xlog_unlink_page (uint8 info, XLogReaderState *record)
 
static void btree_xlog_newroot (XLogReaderState *record)
 
static void btree_xlog_reuse_page (XLogReaderState *record)
 
void btree_redo (XLogReaderState *record)
 
void btree_xlog_startup (void)
 
void btree_xlog_cleanup (void)
 
void btree_mask (char *pagedata, BlockNumber blkno)
 

Variables

static MemoryContext opCtx
 

Function Documentation

◆ _bt_clear_incomplete_split()

static void _bt_clear_incomplete_split ( XLogReaderState record,
uint8  block_id 
)
static

Definition at line 142 of file nbtxlog.c.

References Assert, BLK_NEEDS_REDO, BTP_INCOMPLETE_SPLIT, BTPageOpaqueData::btpo_flags, buf, BufferGetPage, BufferIsValid, XLogReaderState::EndRecPtr, MarkBufferDirty(), P_INCOMPLETE_SPLIT, PageGetSpecialPointer, PageSetLSN, UnlockReleaseBuffer(), and XLogReadBufferForRedo().

Referenced by btree_xlog_insert(), btree_xlog_newroot(), and btree_xlog_split().

143 {
144  XLogRecPtr lsn = record->EndRecPtr;
145  Buffer buf;
146 
147  if (XLogReadBufferForRedo(record, block_id, &buf) == BLK_NEEDS_REDO)
148  {
149  Page page = (Page) BufferGetPage(buf);
151 
152  Assert(P_INCOMPLETE_SPLIT(pageop));
153  pageop->btpo_flags &= ~BTP_INCOMPLETE_SPLIT;
154 
155  PageSetLSN(page, lsn);
156  MarkBufferDirty(buf);
157  }
158  if (BufferIsValid(buf))
159  UnlockReleaseBuffer(buf);
160 }
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
#define BTP_INCOMPLETE_SPLIT
Definition: nbtree.h:79
#define P_INCOMPLETE_SPLIT(opaque)
Definition: nbtree.h:221
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
static char * buf
Definition: pg_test_fsync.c:67
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:294
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78

◆ _bt_restore_meta()

static void _bt_restore_meta ( XLogReaderState record,
uint8  block_id 
)
static

Definition at line 85 of file nbtxlog.c.

References _bt_pageinit(), xl_btree_metadata::allequalimage, Assert, BTMetaPageData::btm_allequalimage, BTMetaPageData::btm_fastlevel, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_last_cleanup_num_heap_tuples, BTMetaPageData::btm_level, BTMetaPageData::btm_magic, BTMetaPageData::btm_oldest_btpo_xact, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTP_META, BTPageGetMeta, BTPageOpaqueData::btpo_flags, BTREE_MAGIC, BTREE_METAPAGE, BTREE_NOVAC_VERSION, BufferGetBlockNumber(), BufferGetPage, BufferGetPageSize, XLogReaderState::EndRecPtr, xl_btree_metadata::fastlevel, xl_btree_metadata::fastroot, xl_btree_metadata::last_cleanup_num_heap_tuples, xl_btree_metadata::level, MarkBufferDirty(), xl_btree_metadata::oldest_btpo_xact, PageGetSpecialPointer, PageSetLSN, xl_btree_metadata::root, UnlockReleaseBuffer(), xl_btree_metadata::version, XLogInitBufferForRedo(), and XLogRecGetBlockData().

Referenced by btree_redo(), btree_xlog_insert(), btree_xlog_newroot(), and btree_xlog_unlink_page().

86 {
87  XLogRecPtr lsn = record->EndRecPtr;
88  Buffer metabuf;
89  Page metapg;
90  BTMetaPageData *md;
91  BTPageOpaque pageop;
92  xl_btree_metadata *xlrec;
93  char *ptr;
94  Size len;
95 
96  metabuf = XLogInitBufferForRedo(record, block_id);
97  ptr = XLogRecGetBlockData(record, block_id, &len);
98 
99  Assert(len == sizeof(xl_btree_metadata));
101  xlrec = (xl_btree_metadata *) ptr;
102  metapg = BufferGetPage(metabuf);
103 
104  _bt_pageinit(metapg, BufferGetPageSize(metabuf));
105 
106  md = BTPageGetMeta(metapg);
107  md->btm_magic = BTREE_MAGIC;
108  md->btm_version = xlrec->version;
109  md->btm_root = xlrec->root;
110  md->btm_level = xlrec->level;
111  md->btm_fastroot = xlrec->fastroot;
112  md->btm_fastlevel = xlrec->fastlevel;
113  /* Cannot log BTREE_MIN_VERSION index metapage without upgrade */
117  md->btm_allequalimage = xlrec->allequalimage;
118 
119  pageop = (BTPageOpaque) PageGetSpecialPointer(metapg);
120  pageop->btpo_flags = BTP_META;
121 
122  /*
123  * Set pd_lower just past the end of the metadata. This is essential,
124  * because without doing so, metadata will be lost if xlog.c compresses
125  * the page.
126  */
127  ((PageHeader) metapg)->pd_lower =
128  ((char *) md + sizeof(BTMetaPageData)) - (char *) metapg;
129 
130  PageSetLSN(metapg, lsn);
131  MarkBufferDirty(metabuf);
132  UnlockReleaseBuffer(metabuf);
133 }
bool allequalimage
Definition: nbtxlog.h:57
uint32 btm_version
Definition: nbtree.h:101
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
uint32 btm_magic
Definition: nbtree.h:100
BlockNumber root
Definition: nbtxlog.h:51
#define BTP_META
Definition: nbtree.h:75
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
BlockNumber btm_fastroot
Definition: nbtree.h:104
#define BTREE_MAGIC
Definition: nbtree.h:142
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
float8 last_cleanup_num_heap_tuples
Definition: nbtxlog.h:56
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:306
TransactionId oldest_btpo_xact
Definition: nbtxlog.h:55
#define BTPageGetMeta(p)
Definition: nbtree.h:114
bool btm_allequalimage
Definition: nbtree.h:111
#define BTREE_NOVAC_VERSION
Definition: nbtree.h:145
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define BTREE_METAPAGE
Definition: nbtree.h:141
uint32 version
Definition: nbtxlog.h:50
uint32 btm_fastlevel
Definition: nbtree.h:105
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1514
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156
BlockNumber btm_root
Definition: nbtree.h:102
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
float8 btm_last_cleanup_num_heap_tuples
Definition: nbtree.h:109
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2633
uint32 fastlevel
Definition: nbtxlog.h:54
uint32 btm_level
Definition: nbtree.h:103
uint32 level
Definition: nbtxlog.h:52
void _bt_pageinit(Page page, Size size)
Definition: nbtpage.c:959
BlockNumber fastroot
Definition: nbtxlog.h:53
TransactionId btm_oldest_btpo_xact
Definition: nbtree.h:107
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78

◆ _bt_restore_page()

static void _bt_restore_page ( Page  page,
char *  from,
int  len 
)
static

Definition at line 38 of file nbtxlog.c.

References elog, i, IndexTupleSize, InvalidOffsetNumber, MAXALIGN, MaxIndexTuplesPerPage, PageAddItem, and PANIC.

Referenced by btree_xlog_newroot(), and btree_xlog_split().

39 {
40  IndexTupleData itupdata;
41  Size itemsz;
42  char *end = from + len;
44  uint16 itemsizes[MaxIndexTuplesPerPage];
45  int i;
46  int nitems;
47 
48  /*
49  * To get the items back in the original order, we add them to the page in
50  * reverse. To figure out where one tuple ends and another begins, we
51  * have to scan them in forward order first.
52  */
53  i = 0;
54  while (from < end)
55  {
56  /*
57  * As we step through the items, 'from' won't always be properly
58  * aligned, so we need to use memcpy(). Further, we use Item (which
59  * is just a char*) here for our items array for the same reason;
60  * wouldn't want the compiler or anyone thinking that an item is
61  * aligned when it isn't.
62  */
63  memcpy(&itupdata, from, sizeof(IndexTupleData));
64  itemsz = IndexTupleSize(&itupdata);
65  itemsz = MAXALIGN(itemsz);
66 
67  items[i] = (Item) from;
68  itemsizes[i] = itemsz;
69  i++;
70 
71  from += itemsz;
72  }
73  nitems = i;
74 
75  for (i = nitems - 1; i >= 0; i--)
76  {
77  if (PageAddItem(page, items[i], itemsizes[i], nitems - i,
78  false, false) == InvalidOffsetNumber)
79  elog(PANIC, "_bt_restore_page: cannot add item to page");
80  from += itemsz;
81  }
82 }
Pointer Item
Definition: item.h:17
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
#define PANIC
Definition: elog.h:53
unsigned short uint16
Definition: c.h:366
#define InvalidOffsetNumber
Definition: off.h:26
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:691
#define MaxIndexTuplesPerPage
Definition: itup.h:145
#define elog(elevel,...)
Definition: elog.h:214
int i
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ btree_mask()

void btree_mask ( char *  pagedata,
BlockNumber  blkno 
)

Definition at line 1021 of file nbtxlog.c.

References BTP_HAS_GARBAGE, BTP_SPLIT_END, BTPageOpaqueData::btpo_cycleid, BTPageOpaqueData::btpo_flags, mask_lp_flags(), mask_page_content(), mask_page_hint_bits(), mask_page_lsn_and_checksum(), mask_unused_space(), P_ISDELETED, P_ISLEAF, and PageGetSpecialPointer.

1022 {
1023  Page page = (Page) pagedata;
1024  BTPageOpaque maskopaq;
1025 
1027 
1028  mask_page_hint_bits(page);
1029  mask_unused_space(page);
1030 
1031  maskopaq = (BTPageOpaque) PageGetSpecialPointer(page);
1032 
1033  if (P_ISDELETED(maskopaq))
1034  {
1035  /*
1036  * Mask page content on a DELETED page since it will be re-initialized
1037  * during replay. See btree_xlog_unlink_page() for details.
1038  */
1039  mask_page_content(page);
1040  }
1041  else if (P_ISLEAF(maskopaq))
1042  {
1043  /*
1044  * In btree leaf pages, it is possible to modify the LP_FLAGS without
1045  * emitting any WAL record. Hence, mask the line pointer flags. See
1046  * _bt_killitems(), _bt_check_unique() for details.
1047  */
1048  mask_lp_flags(page);
1049  }
1050 
1051  /*
1052  * BTP_HAS_GARBAGE is just an un-logged hint bit. So, mask it. See
1053  * _bt_killitems(), _bt_check_unique() for details.
1054  */
1055  maskopaq->btpo_flags &= ~BTP_HAS_GARBAGE;
1056 
1057  /*
1058  * During replay of a btree page split, we don't set the BTP_SPLIT_END
1059  * flag of the right sibling and initialize the cycle_id to 0 for the same
1060  * page. See btree_xlog_split() for details.
1061  */
1062  maskopaq->btpo_flags &= ~BTP_SPLIT_END;
1063  maskopaq->btpo_cycleid = 0;
1064 }
#define BTP_SPLIT_END
Definition: nbtree.h:77
void mask_page_hint_bits(Page page)
Definition: bufmask.c:46
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
void mask_unused_space(Page page)
Definition: bufmask.c:71
void mask_page_content(Page page)
Definition: bufmask.c:119
BTCycleId btpo_cycleid
Definition: nbtree.h:66
#define P_ISDELETED(opaque)
Definition: nbtree.h:216
void mask_page_lsn_and_checksum(Page page)
Definition: bufmask.c:31
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
uint16 btpo_flags
Definition: nbtree.h:65
void mask_lp_flags(Page page)
Definition: bufmask.c:95
#define BTP_HAS_GARBAGE
Definition: nbtree.h:78
Pointer Page
Definition: bufpage.h:78
#define P_ISLEAF(opaque)
Definition: nbtree.h:214

◆ btree_redo()

void btree_redo ( XLogReaderState record)

Definition at line 944 of file nbtxlog.c.

References _bt_restore_meta(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_mark_page_halfdead(), btree_xlog_newroot(), btree_xlog_reuse_page(), btree_xlog_split(), btree_xlog_unlink_page(), btree_xlog_vacuum(), elog, MemoryContextReset(), MemoryContextSwitchTo(), PANIC, XLOG_BTREE_DEDUP, XLOG_BTREE_DELETE, XLOG_BTREE_INSERT_LEAF, XLOG_BTREE_INSERT_META, XLOG_BTREE_INSERT_POST, XLOG_BTREE_INSERT_UPPER, XLOG_BTREE_MARK_PAGE_HALFDEAD, XLOG_BTREE_META_CLEANUP, XLOG_BTREE_NEWROOT, XLOG_BTREE_REUSE_PAGE, XLOG_BTREE_SPLIT_L, XLOG_BTREE_SPLIT_R, XLOG_BTREE_UNLINK_PAGE, XLOG_BTREE_UNLINK_PAGE_META, XLOG_BTREE_VACUUM, XLogRecGetInfo, and XLR_INFO_MASK.

945 {
946  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
947  MemoryContext oldCtx;
948 
949  oldCtx = MemoryContextSwitchTo(opCtx);
950  switch (info)
951  {
953  btree_xlog_insert(true, false, false, record);
954  break;
956  btree_xlog_insert(false, false, false, record);
957  break;
959  btree_xlog_insert(false, true, false, record);
960  break;
961  case XLOG_BTREE_SPLIT_L:
962  btree_xlog_split(true, record);
963  break;
964  case XLOG_BTREE_SPLIT_R:
965  btree_xlog_split(false, record);
966  break;
968  btree_xlog_insert(true, false, true, record);
969  break;
970  case XLOG_BTREE_DEDUP:
971  btree_xlog_dedup(record);
972  break;
973  case XLOG_BTREE_VACUUM:
974  btree_xlog_vacuum(record);
975  break;
976  case XLOG_BTREE_DELETE:
977  btree_xlog_delete(record);
978  break;
980  btree_xlog_mark_page_halfdead(info, record);
981  break;
984  btree_xlog_unlink_page(info, record);
985  break;
986  case XLOG_BTREE_NEWROOT:
987  btree_xlog_newroot(record);
988  break;
990  btree_xlog_reuse_page(record);
991  break;
993  _bt_restore_meta(record, 0);
994  break;
995  default:
996  elog(PANIC, "btree_redo: unknown op code %u", info);
997  }
998  MemoryContextSwitchTo(oldCtx);
1000 }
static void btree_xlog_vacuum(XLogReaderState *record)
Definition: nbtxlog.c:558
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned char uint8
Definition: c.h:365
#define XLOG_BTREE_INSERT_META
Definition: nbtxlog.h:28
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
static void btree_xlog_delete(XLogReaderState *record)
Definition: nbtxlog.c:645
#define PANIC
Definition: elog.h:53
#define XLOG_BTREE_NEWROOT
Definition: nbtxlog.h:36
static void _bt_restore_meta(XLogReaderState *record, uint8 block_id)
Definition: nbtxlog.c:85
static void btree_xlog_newroot(XLogReaderState *record)
Definition: nbtxlog.c:882
static void btree_xlog_reuse_page(XLogReaderState *record)
Definition: nbtxlog.c:922
#define XLOG_BTREE_INSERT_LEAF
Definition: nbtxlog.h:26
#define XLOG_BTREE_VACUUM
Definition: nbtxlog.h:38
#define XLOG_BTREE_UNLINK_PAGE
Definition: nbtxlog.h:34
static void btree_xlog_dedup(XLogReaderState *record)
Definition: nbtxlog.c:466
static void btree_xlog_insert(bool isleaf, bool ismeta, bool posting, XLogReaderState *record)
Definition: nbtxlog.c:163
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:307
#define XLOG_BTREE_DELETE
Definition: nbtxlog.h:33
#define XLOG_BTREE_DEDUP
Definition: nbtxlog.h:32
#define XLOG_BTREE_REUSE_PAGE
Definition: nbtxlog.h:40
#define XLOG_BTREE_MARK_PAGE_HALFDEAD
Definition: nbtxlog.h:37
#define XLOG_BTREE_SPLIT_R
Definition: nbtxlog.h:30
static MemoryContext opCtx
Definition: nbtxlog.c:27
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_BTREE_INSERT_POST
Definition: nbtxlog.h:31
static void btree_xlog_unlink_page(uint8 info, XLogReaderState *record)
Definition: nbtxlog.c:770
#define XLOG_BTREE_INSERT_UPPER
Definition: nbtxlog.h:27
static void btree_xlog_mark_page_halfdead(uint8 info, XLogReaderState *record)
Definition: nbtxlog.c:690
#define elog(elevel,...)
Definition: elog.h:214
#define XLOG_BTREE_SPLIT_L
Definition: nbtxlog.h:29
#define XLOG_BTREE_UNLINK_PAGE_META
Definition: nbtxlog.h:35
static void btree_xlog_split(bool newitemonleft, XLogReaderState *record)
Definition: nbtxlog.c:254
#define XLOG_BTREE_META_CLEANUP
Definition: nbtxlog.h:42

◆ btree_xlog_cleanup()

void btree_xlog_cleanup ( void  )

Definition at line 1011 of file nbtxlog.c.

References MemoryContextDelete().

1012 {
1014  opCtx = NULL;
1015 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
static MemoryContext opCtx
Definition: nbtxlog.c:27

◆ btree_xlog_dedup()

static void btree_xlog_dedup ( XLogReaderState record)
static

Definition at line 466 of file nbtxlog.c.

References _bt_dedup_finish_pending(), _bt_dedup_save_htid(), _bt_dedup_start_pending(), Assert, BTDedupStateData::base, BTDedupInterval::baseoff, BTDedupStateData::baseoff, BTDedupStateData::basetupsize, BLK_NEEDS_REDO, BTMaxItemSize, BTP_HAS_GARBAGE, BTPageOpaqueData::btpo_flags, buf, BufferGetPage, BufferIsValid, BTDedupStateData::deduplicate, elog, XLogReaderState::EndRecPtr, ERROR, BTDedupStateData::htids, intervals, BTDedupStateData::intervals, InvalidOffsetNumber, ItemIdGetLength, MarkBufferDirty(), BTDedupStateData::maxpostingsize, BTDedupStateData::nhtids, xl_btree_dedup::nintervals, BTDedupStateData::nintervals, BTDedupInterval::nitems, BTDedupStateData::nitems, OffsetNumberNext, P_FIRSTDATAKEY, P_HAS_GARBAGE, P_HIKEY, P_RIGHTMOST, PageAddItem, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageGetSpecialPointer, PageGetTempPageCopySpecial(), PageRestoreTempPage(), PageSetLSN, palloc(), BTDedupStateData::phystupsize, UnlockReleaseBuffer(), XLogReadBufferForRedo(), XLogRecGetBlockData(), and XLogRecGetData.

Referenced by btree_redo().

467 {
468  XLogRecPtr lsn = record->EndRecPtr;
469  xl_btree_dedup *xlrec = (xl_btree_dedup *) XLogRecGetData(record);
470  Buffer buf;
471 
472  if (XLogReadBufferForRedo(record, 0, &buf) == BLK_NEEDS_REDO)
473  {
474  char *ptr = XLogRecGetBlockData(record, 0, NULL);
475  Page page = (Page) BufferGetPage(buf);
477  OffsetNumber offnum,
478  minoff,
479  maxoff;
482  Page newpage;
483 
484  state = (BTDedupState) palloc(sizeof(BTDedupStateData));
485  state->deduplicate = true; /* unused */
486  /* Conservatively use larger maxpostingsize than primary */
487  state->maxpostingsize = BTMaxItemSize(page);
488  state->base = NULL;
489  state->baseoff = InvalidOffsetNumber;
490  state->basetupsize = 0;
491  state->htids = palloc(state->maxpostingsize);
492  state->nhtids = 0;
493  state->nitems = 0;
494  state->phystupsize = 0;
495  state->nintervals = 0;
496 
497  minoff = P_FIRSTDATAKEY(opaque);
498  maxoff = PageGetMaxOffsetNumber(page);
499  newpage = PageGetTempPageCopySpecial(page);
500 
501  if (!P_RIGHTMOST(opaque))
502  {
503  ItemId itemid = PageGetItemId(page, P_HIKEY);
504  Size itemsz = ItemIdGetLength(itemid);
505  IndexTuple item = (IndexTuple) PageGetItem(page, itemid);
506 
507  if (PageAddItem(newpage, (Item) item, itemsz, P_HIKEY,
508  false, false) == InvalidOffsetNumber)
509  elog(ERROR, "deduplication failed to add highkey");
510  }
511 
512  intervals = (BTDedupInterval *) ptr;
513  for (offnum = minoff;
514  offnum <= maxoff;
515  offnum = OffsetNumberNext(offnum))
516  {
517  ItemId itemid = PageGetItemId(page, offnum);
518  IndexTuple itup = (IndexTuple) PageGetItem(page, itemid);
519 
520  if (offnum == minoff)
521  _bt_dedup_start_pending(state, itup, offnum);
522  else if (state->nintervals < xlrec->nintervals &&
523  state->baseoff == intervals[state->nintervals].baseoff &&
524  state->nitems < intervals[state->nintervals].nitems)
525  {
526  if (!_bt_dedup_save_htid(state, itup))
527  elog(ERROR, "deduplication failed to add heap tid to pending posting list");
528  }
529  else
530  {
531  _bt_dedup_finish_pending(newpage, state);
532  _bt_dedup_start_pending(state, itup, offnum);
533  }
534  }
535 
536  _bt_dedup_finish_pending(newpage, state);
537  Assert(state->nintervals == xlrec->nintervals);
538  Assert(memcmp(state->intervals, intervals,
539  state->nintervals * sizeof(BTDedupInterval)) == 0);
540 
541  if (P_HAS_GARBAGE(opaque))
542  {
543  BTPageOpaque nopaque = (BTPageOpaque) PageGetSpecialPointer(newpage);
544 
545  nopaque->btpo_flags &= ~BTP_HAS_GARBAGE;
546  }
547 
548  PageRestoreTempPage(newpage, page);
549  PageSetLSN(page, lsn);
550  MarkBufferDirty(buf);
551  }
552 
553  if (BufferIsValid(buf))
554  UnlockReleaseBuffer(buf);
555 }
char * intervals[]
IndexTuple base
Definition: nbtree.h:745
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:403
uint16 nintervals
Definition: nbtxlog.h:172
OffsetNumber baseoff
Definition: nbtree.h:746
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:244
Pointer Item
Definition: item.h:17
#define P_HAS_GARBAGE(opaque)
Definition: nbtree.h:220
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
void _bt_dedup_start_pending(BTDedupState state, IndexTuple base, OffsetNumber baseoff)
Definition: nbtdedup.c:326
Size _bt_dedup_finish_pending(Page newpage, BTDedupState state)
Definition: nbtdedup.c:431
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
uint16 OffsetNumber
Definition: off.h:24
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:381
ItemPointer htids
Definition: nbtree.h:750
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
Size phystupsize
Definition: nbtree.h:753
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
#define ERROR
Definition: elog.h:43
BTDedupInterval intervals[MaxIndexTuplesPerPage]
Definition: nbtree.h:762
static char * buf
Definition: pg_test_fsync.c:67
bool _bt_dedup_save_htid(BTDedupState state, IndexTuple itup)
Definition: nbtdedup.c:377
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1514
#define InvalidOffsetNumber
Definition: off.h:26
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:294
uint16 nitems
Definition: nbtree.h:717
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
Definition: regguts.h:298
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:466
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
Size basetupsize
Definition: nbtree.h:747
Size maxpostingsize
Definition: nbtree.h:742
#define BTMaxItemSize(page)
Definition: nbtree.h:157
#define P_HIKEY
Definition: nbtree.h:242
void * palloc(Size size)
Definition: mcxt.c:949
bool deduplicate
Definition: nbtree.h:741
#define elog(elevel,...)
Definition: elog.h:214
BTDedupStateData * BTDedupState
Definition: nbtree.h:765
OffsetNumber baseoff
Definition: nbtree.h:716
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:213
#define BTP_HAS_GARBAGE
Definition: nbtree.h:78
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78

◆ btree_xlog_delete()

static void btree_xlog_delete ( XLogReaderState record)
static

Definition at line 645 of file nbtxlog.c.

References BLK_NEEDS_REDO, BTP_HAS_GARBAGE, BTPageOpaqueData::btpo_flags, BufferGetPage, BufferIsValid, XLogReaderState::EndRecPtr, InHotStandby, xl_btree_delete::latestRemovedXid, MarkBufferDirty(), xl_btree_delete::ndeleted, PageGetSpecialPointer, PageIndexMultiDelete(), PageSetLSN, ResolveRecoveryConflictWithSnapshot(), UnlockReleaseBuffer(), XLogReadBufferForRedo(), XLogRecGetBlockData(), XLogRecGetBlockTag(), and XLogRecGetData.

Referenced by btree_redo().

646 {
647  XLogRecPtr lsn = record->EndRecPtr;
648  xl_btree_delete *xlrec = (xl_btree_delete *) XLogRecGetData(record);
649  Buffer buffer;
650  Page page;
651  BTPageOpaque opaque;
652 
653  /*
654  * If we have any conflict processing to do, it must happen before we
655  * update the page
656  */
657  if (InHotStandby)
658  {
659  RelFileNode rnode;
660 
661  XLogRecGetBlockTag(record, 0, &rnode, NULL, NULL);
662 
664  }
665 
666  /*
667  * We don't need to take a cleanup lock to apply these changes. See
668  * nbtree/README for details.
669  */
670  if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
671  {
672  char *ptr = XLogRecGetBlockData(record, 0, NULL);
673 
674  page = (Page) BufferGetPage(buffer);
675 
676  PageIndexMultiDelete(page, (OffsetNumber *) ptr, xlrec->ndeleted);
677 
678  /* Mark the page as not containing any LP_DEAD items */
679  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
680  opaque->btpo_flags &= ~BTP_HAS_GARBAGE;
681 
682  PageSetLSN(page, lsn);
683  MarkBufferDirty(buffer);
684  }
685  if (BufferIsValid(buffer))
686  UnlockReleaseBuffer(buffer);
687 }
TransactionId latestRemovedXid
Definition: nbtxlog.h:189
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
#define InHotStandby
Definition: xlog.h:74
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
uint32 ndeleted
Definition: nbtxlog.h:190
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
uint16 OffsetNumber
Definition: off.h:24
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1490
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1514
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:294
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:828
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
void ResolveRecoveryConflictWithSnapshot(TransactionId latestRemovedXid, RelFileNode node)
Definition: standby.c:304
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
#define BTP_HAS_GARBAGE
Definition: nbtree.h:78
Pointer Page
Definition: bufpage.h:78

◆ btree_xlog_insert()

static void btree_xlog_insert ( bool  isleaf,
bool  ismeta,
bool  posting,
XLogReaderState record 
)
static

Definition at line 163 of file nbtxlog.c.

References _bt_clear_incomplete_split(), _bt_restore_meta(), _bt_swap_posting(), Assert, BLK_NEEDS_REDO, BufferGetPage, BufferIsValid, CopyIndexTuple(), elog, XLogReaderState::EndRecPtr, IndexTupleSize, InvalidOffsetNumber, MarkBufferDirty(), MAXALIGN, xl_btree_insert::offnum, OffsetNumberPrev, PageAddItem, PageGetItem, PageGetItemId, PageSetLSN, PANIC, UnlockReleaseBuffer(), XLogReadBufferForRedo(), XLogRecGetBlockData(), and XLogRecGetData.

Referenced by btree_redo().

165 {
166  XLogRecPtr lsn = record->EndRecPtr;
167  xl_btree_insert *xlrec = (xl_btree_insert *) XLogRecGetData(record);
168  Buffer buffer;
169  Page page;
170 
171  /*
172  * Insertion to an internal page finishes an incomplete split at the child
173  * level. Clear the incomplete-split flag in the child. Note: during
174  * normal operation, the child and parent pages are locked at the same
175  * time, so that clearing the flag and inserting the downlink appear
176  * atomic to other backends. We don't bother with that during replay,
177  * because readers don't care about the incomplete-split flag and there
178  * cannot be updates happening.
179  */
180  if (!isleaf)
181  _bt_clear_incomplete_split(record, 1);
182  if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
183  {
184  Size datalen;
185  char *datapos = XLogRecGetBlockData(record, 0, &datalen);
186 
187  page = BufferGetPage(buffer);
188 
189  if (!posting)
190  {
191  /* Simple retail insertion */
192  if (PageAddItem(page, (Item) datapos, datalen, xlrec->offnum,
193  false, false) == InvalidOffsetNumber)
194  elog(PANIC, "failed to add new item");
195  }
196  else
197  {
198  ItemId itemid;
199  IndexTuple oposting,
200  newitem,
201  nposting;
202  uint16 postingoff;
203 
204  /*
205  * A posting list split occurred during leaf page insertion. WAL
206  * record data will start with an offset number representing the
207  * point in an existing posting list that a split occurs at.
208  *
209  * Use _bt_swap_posting() to repeat posting list split steps from
210  * primary. Note that newitem from WAL record is 'orignewitem',
211  * not the final version of newitem that is actually inserted on
212  * page.
213  */
214  postingoff = *((uint16 *) datapos);
215  datapos += sizeof(uint16);
216  datalen -= sizeof(uint16);
217 
218  itemid = PageGetItemId(page, OffsetNumberPrev(xlrec->offnum));
219  oposting = (IndexTuple) PageGetItem(page, itemid);
220 
221  /* Use mutable, aligned newitem copy in _bt_swap_posting() */
222  Assert(isleaf && postingoff > 0);
223  newitem = CopyIndexTuple((IndexTuple) datapos);
224  nposting = _bt_swap_posting(newitem, oposting, postingoff);
225 
226  /* Replace existing posting list with post-split version */
227  memcpy(oposting, nposting, MAXALIGN(IndexTupleSize(nposting)));
228 
229  /* Insert "final" new item (not orignewitem from WAL stream) */
230  Assert(IndexTupleSize(newitem) == datalen);
231  if (PageAddItem(page, (Item) newitem, datalen, xlrec->offnum,
232  false, false) == InvalidOffsetNumber)
233  elog(PANIC, "failed to add posting split new item");
234  }
235 
236  PageSetLSN(page, lsn);
237  MarkBufferDirty(buffer);
238  }
239  if (BufferIsValid(buffer))
240  UnlockReleaseBuffer(buffer);
241 
242  /*
243  * Note: in normal operation, we'd update the metapage while still holding
244  * lock on the page we inserted into. But during replay it's not
245  * necessary to hold that lock, since no other index updates can be
246  * happening concurrently, and readers will cope fine with following an
247  * obsolete link from the metapage.
248  */
249  if (ismeta)
250  _bt_restore_meta(record, 2);
251 }
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
Pointer Item
Definition: item.h:17
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
#define PANIC
Definition: elog.h:53
IndexTuple _bt_swap_posting(IndexTuple newitem, IndexTuple oposting, int postingoff)
Definition: nbtdedup.c:759
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
unsigned short uint16
Definition: c.h:366
static void _bt_restore_meta(XLogReaderState *record, uint8 block_id)
Definition: nbtxlog.c:85
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:510
static void _bt_clear_incomplete_split(XLogReaderState *record, uint8 block_id)
Definition: nbtxlog.c:142
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1514
OffsetNumber offnum
Definition: nbtxlog.h:81
#define InvalidOffsetNumber
Definition: off.h:26
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:294
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
#define MAXALIGN(LEN)
Definition: c.h:691
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define elog(elevel,...)
Definition: elog.h:214
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ btree_xlog_mark_page_halfdead()

static void btree_xlog_mark_page_halfdead ( uint8  info,
XLogReaderState record 
)
static

Definition at line 690 of file nbtxlog.c.

References _bt_pageinit(), BLK_NEEDS_REDO, BTP_HALF_DEAD, BTP_LEAF, BTPageOpaqueData::btpo, BTPageOpaqueData::btpo_cycleid, BTPageOpaqueData::btpo_flags, BTPageOpaqueData::btpo_next, BTPageOpaqueData::btpo_prev, BTreeTupleGetDownLink(), BTreeTupleSetDownLink(), BTreeTupleSetTopParent(), BufferGetPage, BufferGetPageSize, BufferIsValid, elog, XLogReaderState::EndRecPtr, ERROR, InvalidOffsetNumber, xl_btree_mark_page_halfdead::leftblk, BTPageOpaqueData::level, MarkBufferDirty(), MemSet, OffsetNumberNext, P_HIKEY, PageAddItem, PageGetItem, PageGetItemId, PageGetSpecialPointer, PageIndexTupleDelete(), PageSetLSN, xl_btree_mark_page_halfdead::poffset, xl_btree_mark_page_halfdead::rightblk, IndexTupleData::t_info, xl_btree_mark_page_halfdead::topparent, UnlockReleaseBuffer(), XLogInitBufferForRedo(), XLogReadBufferForRedo(), and XLogRecGetData.

Referenced by btree_redo().

691 {
692  XLogRecPtr lsn = record->EndRecPtr;
694  Buffer buffer;
695  Page page;
696  BTPageOpaque pageop;
697  IndexTupleData trunctuple;
698 
699  /*
700  * In normal operation, we would lock all the pages this WAL record
701  * touches before changing any of them. In WAL replay, it should be okay
702  * to lock just one page at a time, since no concurrent index updates can
703  * be happening, and readers should not care whether they arrive at the
704  * target page or not (since it's surely empty).
705  */
706 
707  /* to-be-deleted subtree's parent page */
708  if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
709  {
710  OffsetNumber poffset;
711  ItemId itemid;
712  IndexTuple itup;
713  OffsetNumber nextoffset;
714  BlockNumber rightsib;
715 
716  page = (Page) BufferGetPage(buffer);
717  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
718 
719  poffset = xlrec->poffset;
720 
721  nextoffset = OffsetNumberNext(poffset);
722  itemid = PageGetItemId(page, nextoffset);
723  itup = (IndexTuple) PageGetItem(page, itemid);
724  rightsib = BTreeTupleGetDownLink(itup);
725 
726  itemid = PageGetItemId(page, poffset);
727  itup = (IndexTuple) PageGetItem(page, itemid);
728  BTreeTupleSetDownLink(itup, rightsib);
729  nextoffset = OffsetNumberNext(poffset);
730  PageIndexTupleDelete(page, nextoffset);
731 
732  PageSetLSN(page, lsn);
733  MarkBufferDirty(buffer);
734  }
735  if (BufferIsValid(buffer))
736  UnlockReleaseBuffer(buffer);
737 
738  /* Rewrite the leaf page as a halfdead page */
739  buffer = XLogInitBufferForRedo(record, 0);
740  page = (Page) BufferGetPage(buffer);
741 
742  _bt_pageinit(page, BufferGetPageSize(buffer));
743  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
744 
745  pageop->btpo_prev = xlrec->leftblk;
746  pageop->btpo_next = xlrec->rightblk;
747  pageop->btpo.level = 0;
748  pageop->btpo_flags = BTP_HALF_DEAD | BTP_LEAF;
749  pageop->btpo_cycleid = 0;
750 
751  /*
752  * Construct a dummy high key item that points to top parent page (value
753  * is InvalidBlockNumber when the top parent page is the leaf page itself)
754  */
755  MemSet(&trunctuple, 0, sizeof(IndexTupleData));
756  trunctuple.t_info = sizeof(IndexTupleData);
757  BTreeTupleSetTopParent(&trunctuple, xlrec->topparent);
758 
759  if (PageAddItem(page, (Item) &trunctuple, sizeof(IndexTupleData), P_HIKEY,
760  false, false) == InvalidOffsetNumber)
761  elog(ERROR, "could not add dummy high key to half-dead page");
762 
763  PageSetLSN(page, lsn);
764  MarkBufferDirty(buffer);
765  UnlockReleaseBuffer(buffer);
766 }
BlockNumber btpo_next
Definition: nbtree.h:59
void PageIndexTupleDelete(Page page, OffsetNumber offnum)
Definition: bufpage.c:719
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
#define BTP_LEAF
Definition: nbtree.h:72
#define BTP_HALF_DEAD
Definition: nbtree.h:76
union BTPageOpaqueData::@45 btpo
Pointer Item
Definition: item.h:17
static BlockNumber BTreeTupleGetDownLink(IndexTuple pivot)
Definition: nbtree.h:424
#define MemSet(start, val, len)
Definition: c.h:971
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
uint32 BlockNumber
Definition: block.h:31
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
uint16 OffsetNumber
Definition: off.h:24
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
#define ERROR
Definition: elog.h:43
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:306
BTCycleId btpo_cycleid
Definition: nbtree.h:66
BlockNumber btpo_prev
Definition: nbtree.h:58
IndexTupleData * IndexTuple
Definition: itup.h:53
static void BTreeTupleSetDownLink(IndexTuple pivot, BlockNumber blkno)
Definition: nbtree.h:430
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
uint32 level
Definition: nbtree.h:62
struct IndexTupleData IndexTupleData
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156
#define InvalidOffsetNumber
Definition: off.h:26
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:294
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define P_HIKEY
Definition: nbtree.h:242
#define elog(elevel,...)
Definition: elog.h:214
void _bt_pageinit(Page page, Size size)
Definition: nbtpage.c:959
unsigned short t_info
Definition: itup.h:49
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
static void BTreeTupleSetTopParent(IndexTuple leafhikey, BlockNumber blkno)
Definition: nbtree.h:494
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78

◆ btree_xlog_newroot()

static void btree_xlog_newroot ( XLogReaderState record)
static

Definition at line 882 of file nbtxlog.c.

References _bt_clear_incomplete_split(), _bt_pageinit(), _bt_restore_meta(), _bt_restore_page(), BTP_LEAF, BTP_ROOT, BTPageOpaqueData::btpo, BTPageOpaqueData::btpo_cycleid, BTPageOpaqueData::btpo_flags, BTPageOpaqueData::btpo_next, BTPageOpaqueData::btpo_prev, BufferGetPage, BufferGetPageSize, XLogReaderState::EndRecPtr, BTPageOpaqueData::level, xl_btree_newroot::level, MarkBufferDirty(), P_NONE, PageGetSpecialPointer, PageSetLSN, UnlockReleaseBuffer(), XLogInitBufferForRedo(), XLogRecGetBlockData(), and XLogRecGetData.

Referenced by btree_redo().

883 {
884  XLogRecPtr lsn = record->EndRecPtr;
885  xl_btree_newroot *xlrec = (xl_btree_newroot *) XLogRecGetData(record);
886  Buffer buffer;
887  Page page;
888  BTPageOpaque pageop;
889  char *ptr;
890  Size len;
891 
892  buffer = XLogInitBufferForRedo(record, 0);
893  page = (Page) BufferGetPage(buffer);
894 
895  _bt_pageinit(page, BufferGetPageSize(buffer));
896  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
897 
898  pageop->btpo_flags = BTP_ROOT;
899  pageop->btpo_prev = pageop->btpo_next = P_NONE;
900  pageop->btpo.level = xlrec->level;
901  if (xlrec->level == 0)
902  pageop->btpo_flags |= BTP_LEAF;
903  pageop->btpo_cycleid = 0;
904 
905  if (xlrec->level > 0)
906  {
907  ptr = XLogRecGetBlockData(record, 0, &len);
908  _bt_restore_page(page, ptr, len);
909 
910  /* Clear the incomplete-split flag in left child */
911  _bt_clear_incomplete_split(record, 1);
912  }
913 
914  PageSetLSN(page, lsn);
915  MarkBufferDirty(buffer);
916  UnlockReleaseBuffer(buffer);
917 
918  _bt_restore_meta(record, 2);
919 }
#define BTP_ROOT
Definition: nbtree.h:73
BlockNumber btpo_next
Definition: nbtree.h:59
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
#define BTP_LEAF
Definition: nbtree.h:72
union BTPageOpaqueData::@45 btpo
#define P_NONE
Definition: nbtree.h:206
uint32 level
Definition: nbtxlog.h:319
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
static void _bt_restore_meta(XLogReaderState *record, uint8 block_id)
Definition: nbtxlog.c:85
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:306
BTCycleId btpo_cycleid
Definition: nbtree.h:66
BlockNumber btpo_prev
Definition: nbtree.h:58
static void _bt_clear_incomplete_split(XLogReaderState *record, uint8 block_id)
Definition: nbtxlog.c:142
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
uint32 level
Definition: nbtree.h:62
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1514
static void _bt_restore_page(Page page, char *from, int len)
Definition: nbtxlog.c:38
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:466
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
void _bt_pageinit(Page page, Size size)
Definition: nbtpage.c:959
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78

◆ btree_xlog_reuse_page()

static void btree_xlog_reuse_page ( XLogReaderState record)
static

Definition at line 922 of file nbtxlog.c.

References InHotStandby, xl_btree_reuse_page::latestRemovedXid, xl_btree_reuse_page::node, ResolveRecoveryConflictWithSnapshot(), and XLogRecGetData.

Referenced by btree_redo().

923 {
925 
926  /*
927  * Btree reuse_page records exist to provide a conflict point when we
928  * reuse pages in the index via the FSM. That's all they do though.
929  *
930  * latestRemovedXid was the page's btpo.xact. The btpo.xact <
931  * RecentGlobalXmin test in _bt_page_recyclable() conceptually mirrors the
932  * pgxact->xmin > limitXmin test in GetConflictingVirtualXIDs().
933  * Consequently, one XID value achieves the same exclusion effect on
934  * master and standby.
935  */
936  if (InHotStandby)
937  {
939  xlrec->node);
940  }
941 }
RelFileNode node
Definition: nbtxlog.h:206
#define InHotStandby
Definition: xlog.h:74
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
void ResolveRecoveryConflictWithSnapshot(TransactionId latestRemovedXid, RelFileNode node)
Definition: standby.c:304
TransactionId latestRemovedXid
Definition: nbtxlog.h:208

◆ btree_xlog_split()

static void btree_xlog_split ( bool  newitemonleft,
XLogReaderState record 
)
static

Definition at line 254 of file nbtxlog.c.

References _bt_clear_incomplete_split(), _bt_pageinit(), _bt_restore_page(), _bt_swap_posting(), Assert, BLK_NEEDS_REDO, BTP_INCOMPLETE_SPLIT, BTP_LEAF, BTPageOpaqueData::btpo, BTPageOpaqueData::btpo_cycleid, BTPageOpaqueData::btpo_flags, BTPageOpaqueData::btpo_next, BTPageOpaqueData::btpo_prev, BufferGetPage, BufferGetPageSize, BufferIsValid, CopyIndexTuple(), elog, XLogReaderState::EndRecPtr, ERROR, xl_btree_split::firstrightoff, IndexTupleSize, InvalidOffsetNumber, ItemIdGetLength, BTPageOpaqueData::level, xl_btree_split::level, MarkBufferDirty(), MAXALIGN, xl_btree_split::newitemoff, OffsetNumberNext, OffsetNumberPrev, P_FIRSTDATAKEY, P_HIKEY, P_NONE, PageAddItem, PageGetItem, PageGetItemId, PageGetSpecialPointer, PageGetTempPageCopySpecial(), PageRestoreTempPage(), PageSetLSN, PANIC, xl_btree_split::postingoff, UnlockReleaseBuffer(), XLogInitBufferForRedo(), XLogReadBufferForRedo(), XLogRecGetBlockData(), XLogRecGetBlockTag(), and XLogRecGetData.

Referenced by btree_redo().

255 {
256  XLogRecPtr lsn = record->EndRecPtr;
257  xl_btree_split *xlrec = (xl_btree_split *) XLogRecGetData(record);
258  bool isleaf = (xlrec->level == 0);
259  Buffer lbuf;
260  Buffer rbuf;
261  Page rpage;
262  BTPageOpaque ropaque;
263  char *datapos;
264  Size datalen;
265  BlockNumber leftsib;
266  BlockNumber rightsib;
267  BlockNumber rnext;
268 
269  XLogRecGetBlockTag(record, 0, NULL, NULL, &leftsib);
270  XLogRecGetBlockTag(record, 1, NULL, NULL, &rightsib);
271  if (!XLogRecGetBlockTag(record, 2, NULL, NULL, &rnext))
272  rnext = P_NONE;
273 
274  /*
275  * Clear the incomplete split flag on the left sibling of the child page
276  * this is a downlink for. (Like in btree_xlog_insert, this can be done
277  * before locking the other pages)
278  */
279  if (!isleaf)
280  _bt_clear_incomplete_split(record, 3);
281 
282  /* Reconstruct right (new) sibling page from scratch */
283  rbuf = XLogInitBufferForRedo(record, 1);
284  datapos = XLogRecGetBlockData(record, 1, &datalen);
285  rpage = (Page) BufferGetPage(rbuf);
286 
287  _bt_pageinit(rpage, BufferGetPageSize(rbuf));
288  ropaque = (BTPageOpaque) PageGetSpecialPointer(rpage);
289 
290  ropaque->btpo_prev = leftsib;
291  ropaque->btpo_next = rnext;
292  ropaque->btpo.level = xlrec->level;
293  ropaque->btpo_flags = isleaf ? BTP_LEAF : 0;
294  ropaque->btpo_cycleid = 0;
295 
296  _bt_restore_page(rpage, datapos, datalen);
297 
298  PageSetLSN(rpage, lsn);
299  MarkBufferDirty(rbuf);
300 
301  /* Now reconstruct left (original) sibling page */
302  if (XLogReadBufferForRedo(record, 0, &lbuf) == BLK_NEEDS_REDO)
303  {
304  /*
305  * To retain the same physical order of the tuples that they had, we
306  * initialize a temporary empty page for the left page and add all the
307  * items to that in item number order. This mirrors how _bt_split()
308  * works. Retaining the same physical order makes WAL consistency
309  * checking possible. See also _bt_restore_page(), which does the
310  * same for the right page.
311  */
312  Page lpage = (Page) BufferGetPage(lbuf);
314  OffsetNumber off;
315  IndexTuple newitem = NULL,
316  left_hikey = NULL,
317  nposting = NULL;
318  Size newitemsz = 0,
319  left_hikeysz = 0;
320  Page newlpage;
321  OffsetNumber leftoff,
322  replacepostingoff = InvalidOffsetNumber;
323 
324  datapos = XLogRecGetBlockData(record, 0, &datalen);
325 
326  if (newitemonleft || xlrec->postingoff != 0)
327  {
328  newitem = (IndexTuple) datapos;
329  newitemsz = MAXALIGN(IndexTupleSize(newitem));
330  datapos += newitemsz;
331  datalen -= newitemsz;
332 
333  if (xlrec->postingoff != 0)
334  {
335  ItemId itemid;
336  IndexTuple oposting;
337 
338  /* Posting list must be at offset number before new item's */
339  replacepostingoff = OffsetNumberPrev(xlrec->newitemoff);
340 
341  /* Use mutable, aligned newitem copy in _bt_swap_posting() */
342  newitem = CopyIndexTuple(newitem);
343  itemid = PageGetItemId(lpage, replacepostingoff);
344  oposting = (IndexTuple) PageGetItem(lpage, itemid);
345  nposting = _bt_swap_posting(newitem, oposting,
346  xlrec->postingoff);
347  }
348  }
349 
350  /*
351  * Extract left hikey and its size. We assume that 16-bit alignment
352  * is enough to apply IndexTupleSize (since it's fetching from a
353  * uint16 field).
354  */
355  left_hikey = (IndexTuple) datapos;
356  left_hikeysz = MAXALIGN(IndexTupleSize(left_hikey));
357  datapos += left_hikeysz;
358  datalen -= left_hikeysz;
359 
360  Assert(datalen == 0);
361 
362  newlpage = PageGetTempPageCopySpecial(lpage);
363 
364  /* Set high key */
365  leftoff = P_HIKEY;
366  if (PageAddItem(newlpage, (Item) left_hikey, left_hikeysz,
367  P_HIKEY, false, false) == InvalidOffsetNumber)
368  elog(PANIC, "failed to add high key to left page after split");
369  leftoff = OffsetNumberNext(leftoff);
370 
371  for (off = P_FIRSTDATAKEY(lopaque); off < xlrec->firstrightoff; off++)
372  {
373  ItemId itemid;
374  Size itemsz;
375  IndexTuple item;
376 
377  /* Add replacement posting list when required */
378  if (off == replacepostingoff)
379  {
380  Assert(newitemonleft ||
381  xlrec->firstrightoff == xlrec->newitemoff);
382  if (PageAddItem(newlpage, (Item) nposting,
383  MAXALIGN(IndexTupleSize(nposting)), leftoff,
384  false, false) == InvalidOffsetNumber)
385  elog(ERROR, "failed to add new posting list item to left page after split");
386  leftoff = OffsetNumberNext(leftoff);
387  continue; /* don't insert oposting */
388  }
389 
390  /* add the new item if it was inserted on left page */
391  else if (newitemonleft && off == xlrec->newitemoff)
392  {
393  if (PageAddItem(newlpage, (Item) newitem, newitemsz, leftoff,
394  false, false) == InvalidOffsetNumber)
395  elog(ERROR, "failed to add new item to left page after split");
396  leftoff = OffsetNumberNext(leftoff);
397  }
398 
399  itemid = PageGetItemId(lpage, off);
400  itemsz = ItemIdGetLength(itemid);
401  item = (IndexTuple) PageGetItem(lpage, itemid);
402  if (PageAddItem(newlpage, (Item) item, itemsz, leftoff,
403  false, false) == InvalidOffsetNumber)
404  elog(ERROR, "failed to add old item to left page after split");
405  leftoff = OffsetNumberNext(leftoff);
406  }
407 
408  /* cope with possibility that newitem goes at the end */
409  if (newitemonleft && off == xlrec->newitemoff)
410  {
411  if (PageAddItem(newlpage, (Item) newitem, newitemsz, leftoff,
412  false, false) == InvalidOffsetNumber)
413  elog(ERROR, "failed to add new item to left page after split");
414  leftoff = OffsetNumberNext(leftoff);
415  }
416 
417  PageRestoreTempPage(newlpage, lpage);
418 
419  /* Fix opaque fields */
420  lopaque->btpo_flags = BTP_INCOMPLETE_SPLIT;
421  if (isleaf)
422  lopaque->btpo_flags |= BTP_LEAF;
423  lopaque->btpo_next = rightsib;
424  lopaque->btpo_cycleid = 0;
425 
426  PageSetLSN(lpage, lsn);
427  MarkBufferDirty(lbuf);
428  }
429 
430  /*
431  * We no longer need the buffers. They must be released together, so that
432  * readers cannot observe two inconsistent halves.
433  */
434  if (BufferIsValid(lbuf))
435  UnlockReleaseBuffer(lbuf);
436  UnlockReleaseBuffer(rbuf);
437 
438  /*
439  * Fix left-link of the page to the right of the new right sibling.
440  *
441  * Note: in normal operation, we do this while still holding lock on the
442  * two split pages. However, that's not necessary for correctness in WAL
443  * replay, because no other index update can be in progress, and readers
444  * will cope properly when following an obsolete left-link.
445  */
446  if (rnext != P_NONE)
447  {
448  Buffer buffer;
449 
450  if (XLogReadBufferForRedo(record, 2, &buffer) == BLK_NEEDS_REDO)
451  {
452  Page page = (Page) BufferGetPage(buffer);
454 
455  pageop->btpo_prev = rightsib;
456 
457  PageSetLSN(page, lsn);
458  MarkBufferDirty(buffer);
459  }
460  if (BufferIsValid(buffer))
461  UnlockReleaseBuffer(buffer);
462  }
463 }
BlockNumber btpo_next
Definition: nbtree.h:59
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:403
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:244
#define BTP_LEAF
Definition: nbtree.h:72
union BTPageOpaqueData::@45 btpo
Pointer Item
Definition: item.h:17
#define P_NONE
Definition: nbtree.h:206
#define BTP_INCOMPLETE_SPLIT
Definition: nbtree.h:79
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
uint32 BlockNumber
Definition: block.h:31
#define PANIC
Definition: elog.h:53
IndexTuple _bt_swap_posting(IndexTuple newitem, IndexTuple oposting, int postingoff)
Definition: nbtdedup.c:759
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
uint16 OffsetNumber
Definition: off.h:24
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:381
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
#define ERROR
Definition: elog.h:43
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:306
OffsetNumber newitemoff
Definition: nbtxlog.h:157
BTCycleId btpo_cycleid
Definition: nbtree.h:66
BlockNumber btpo_prev
Definition: nbtree.h:58
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:510
static void _bt_clear_incomplete_split(XLogReaderState *record, uint8 block_id)
Definition: nbtxlog.c:142
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
uint32 level
Definition: nbtree.h:62
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1490
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1514
uint32 level
Definition: nbtxlog.h:155
static void _bt_restore_page(Page page, char *from, int len)
Definition: nbtxlog.c:38
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156
#define InvalidOffsetNumber
Definition: off.h:26
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:294
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:466
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
#define MAXALIGN(LEN)
Definition: c.h:691
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define P_HIKEY
Definition: nbtree.h:242
#define elog(elevel,...)
Definition: elog.h:214
void _bt_pageinit(Page page, Size size)
Definition: nbtpage.c:959
uint16 postingoff
Definition: nbtxlog.h:158
uint16 btpo_flags
Definition: nbtree.h:65
OffsetNumber firstrightoff
Definition: nbtxlog.h:156
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ btree_xlog_startup()

void btree_xlog_startup ( void  )

Definition at line 1003 of file nbtxlog.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, and CurrentMemoryContext.

1004 {
1006  "Btree recovery temporary context",
1008 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static MemoryContext opCtx
Definition: nbtxlog.c:27

◆ btree_xlog_unlink_page()

static void btree_xlog_unlink_page ( uint8  info,
XLogReaderState record 
)
static

Definition at line 770 of file nbtxlog.c.

References _bt_pageinit(), _bt_restore_meta(), BLK_NEEDS_REDO, BTP_DELETED, BTP_HALF_DEAD, BTP_LEAF, BTPageOpaqueData::btpo, BTPageOpaqueData::btpo_cycleid, BTPageOpaqueData::btpo_flags, BTPageOpaqueData::btpo_next, BTPageOpaqueData::btpo_prev, xl_btree_unlink_page::btpo_xact, BTreeTupleSetTopParent(), BufferGetPage, BufferGetPageSize, BufferIsValid, elog, XLogReaderState::EndRecPtr, ERROR, InvalidOffsetNumber, xl_btree_unlink_page::leafleftsib, xl_btree_unlink_page::leafrightsib, xl_btree_unlink_page::leftsib, BTPageOpaqueData::level, MarkBufferDirty(), MemSet, P_HIKEY, P_NONE, PageAddItem, PageGetSpecialPointer, PageSetLSN, xl_btree_unlink_page::rightsib, IndexTupleData::t_info, xl_btree_unlink_page::topparent, UnlockReleaseBuffer(), BTPageOpaqueData::xact, XLOG_BTREE_UNLINK_PAGE_META, XLogInitBufferForRedo(), XLogReadBufferForRedo(), XLogRecGetData, and XLogRecHasBlockRef.

Referenced by btree_redo().

771 {
772  XLogRecPtr lsn = record->EndRecPtr;
774  BlockNumber leftsib;
775  BlockNumber rightsib;
776  Buffer buffer;
777  Page page;
778  BTPageOpaque pageop;
779 
780  leftsib = xlrec->leftsib;
781  rightsib = xlrec->rightsib;
782 
783  /*
784  * In normal operation, we would lock all the pages this WAL record
785  * touches before changing any of them. In WAL replay, it should be okay
786  * to lock just one page at a time, since no concurrent index updates can
787  * be happening, and readers should not care whether they arrive at the
788  * target page or not (since it's surely empty).
789  */
790 
791  /* Fix left-link of right sibling */
792  if (XLogReadBufferForRedo(record, 2, &buffer) == BLK_NEEDS_REDO)
793  {
794  page = (Page) BufferGetPage(buffer);
795  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
796  pageop->btpo_prev = leftsib;
797 
798  PageSetLSN(page, lsn);
799  MarkBufferDirty(buffer);
800  }
801  if (BufferIsValid(buffer))
802  UnlockReleaseBuffer(buffer);
803 
804  /* Fix right-link of left sibling, if any */
805  if (leftsib != P_NONE)
806  {
807  if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
808  {
809  page = (Page) BufferGetPage(buffer);
810  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
811  pageop->btpo_next = rightsib;
812 
813  PageSetLSN(page, lsn);
814  MarkBufferDirty(buffer);
815  }
816  if (BufferIsValid(buffer))
817  UnlockReleaseBuffer(buffer);
818  }
819 
820  /* Rewrite target page as empty deleted page */
821  buffer = XLogInitBufferForRedo(record, 0);
822  page = (Page) BufferGetPage(buffer);
823 
824  _bt_pageinit(page, BufferGetPageSize(buffer));
825  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
826 
827  pageop->btpo_prev = leftsib;
828  pageop->btpo_next = rightsib;
829  pageop->btpo.xact = xlrec->btpo_xact;
830  pageop->btpo_flags = BTP_DELETED;
831  pageop->btpo_cycleid = 0;
832 
833  PageSetLSN(page, lsn);
834  MarkBufferDirty(buffer);
835  UnlockReleaseBuffer(buffer);
836 
837  /*
838  * If we deleted a parent of the targeted leaf page, instead of the leaf
839  * itself, update the leaf to point to the next remaining child in the
840  * to-be-deleted subtree
841  */
842  if (XLogRecHasBlockRef(record, 3))
843  {
844  /*
845  * There is no real data on the page, so we just re-create it from
846  * scratch using the information from the WAL record.
847  */
848  IndexTupleData trunctuple;
849 
850  buffer = XLogInitBufferForRedo(record, 3);
851  page = (Page) BufferGetPage(buffer);
852 
853  _bt_pageinit(page, BufferGetPageSize(buffer));
854  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
855 
856  pageop->btpo_flags = BTP_HALF_DEAD | BTP_LEAF;
857  pageop->btpo_prev = xlrec->leafleftsib;
858  pageop->btpo_next = xlrec->leafrightsib;
859  pageop->btpo.level = 0;
860  pageop->btpo_cycleid = 0;
861 
862  /* Add a dummy hikey item */
863  MemSet(&trunctuple, 0, sizeof(IndexTupleData));
864  trunctuple.t_info = sizeof(IndexTupleData);
865  BTreeTupleSetTopParent(&trunctuple, xlrec->topparent);
866 
867  if (PageAddItem(page, (Item) &trunctuple, sizeof(IndexTupleData), P_HIKEY,
868  false, false) == InvalidOffsetNumber)
869  elog(ERROR, "could not add dummy high key to half-dead page");
870 
871  PageSetLSN(page, lsn);
872  MarkBufferDirty(buffer);
873  UnlockReleaseBuffer(buffer);
874  }
875 
876  /* Update metapage if needed */
877  if (info == XLOG_BTREE_UNLINK_PAGE_META)
878  _bt_restore_meta(record, 4);
879 }
BlockNumber btpo_next
Definition: nbtree.h:59
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
#define BTP_LEAF
Definition: nbtree.h:72
#define BTP_HALF_DEAD
Definition: nbtree.h:76
union BTPageOpaqueData::@45 btpo
Pointer Item
Definition: item.h:17
#define P_NONE
Definition: nbtree.h:206
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:314
#define MemSet(start, val, len)
Definition: c.h:971
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
uint32 BlockNumber
Definition: block.h:31
#define BTP_DELETED
Definition: nbtree.h:74
TransactionId xact
Definition: nbtree.h:63
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
static void _bt_restore_meta(XLogReaderState *record, uint8 block_id)
Definition: nbtxlog.c:85
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
#define ERROR
Definition: elog.h:43
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:306
BTCycleId btpo_cycleid
Definition: nbtree.h:66
BlockNumber btpo_prev
Definition: nbtree.h:58
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
uint32 level
Definition: nbtree.h:62
struct IndexTupleData IndexTupleData
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156
#define InvalidOffsetNumber
Definition: off.h:26
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:294
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define P_HIKEY
Definition: nbtree.h:242
#define elog(elevel,...)
Definition: elog.h:214
void _bt_pageinit(Page page, Size size)
Definition: nbtpage.c:959
#define XLOG_BTREE_UNLINK_PAGE_META
Definition: nbtxlog.h:35
unsigned short t_info
Definition: itup.h:49
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
static void BTreeTupleSetTopParent(IndexTuple leafhikey, BlockNumber blkno)
Definition: nbtree.h:494
Pointer Page
Definition: bufpage.h:78

◆ btree_xlog_vacuum()

static void btree_xlog_vacuum ( XLogReaderState record)
static

Definition at line 558 of file nbtxlog.c.

References _bt_update_posting(), BLK_NEEDS_REDO, BTP_HAS_GARBAGE, BTPageOpaqueData::btpo_flags, BufferGetPage, BufferIsValid, BTVacuumPostingData::deletetids, elog, XLogReaderState::EndRecPtr, i, IndexTupleSize, BTVacuumPostingData::itup, MarkBufferDirty(), MAXALIGN, xl_btree_vacuum::ndeleted, xl_btree_update::ndeletedtids, BTVacuumPostingData::ndeletedtids, xl_btree_vacuum::nupdated, offsetof, PageGetItem, PageGetItemId, PageGetSpecialPointer, PageIndexMultiDelete(), PageIndexTupleOverwrite(), PageSetLSN, palloc(), PANIC, pfree(), RBM_NORMAL, SizeOfBtreeUpdate, UnlockReleaseBuffer(), BTVacuumPostingData::updatedoffset, XLogReadBufferForRedoExtended(), XLogRecGetBlockData(), and XLogRecGetData.

Referenced by btree_redo().

559 {
560  XLogRecPtr lsn = record->EndRecPtr;
561  xl_btree_vacuum *xlrec = (xl_btree_vacuum *) XLogRecGetData(record);
562  Buffer buffer;
563  Page page;
564  BTPageOpaque opaque;
565 
566  /*
567  * We need to take a cleanup lock here, just like btvacuumpage(). However,
568  * it isn't necessary to exhaustively get a cleanup lock on every block in
569  * the index during recovery (just getting a cleanup lock on pages with
570  * items to kill suffices). See nbtree/README for details.
571  */
572  if (XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &buffer)
573  == BLK_NEEDS_REDO)
574  {
575  char *ptr = XLogRecGetBlockData(record, 0, NULL);
576 
577  page = (Page) BufferGetPage(buffer);
578 
579  if (xlrec->nupdated > 0)
580  {
581  OffsetNumber *updatedoffsets;
582  xl_btree_update *updates;
583 
584  updatedoffsets = (OffsetNumber *)
585  (ptr + xlrec->ndeleted * sizeof(OffsetNumber));
586  updates = (xl_btree_update *) ((char *) updatedoffsets +
587  xlrec->nupdated *
588  sizeof(OffsetNumber));
589 
590  for (int i = 0; i < xlrec->nupdated; i++)
591  {
592  BTVacuumPosting vacposting;
593  IndexTuple origtuple;
594  ItemId itemid;
595  Size itemsz;
596 
597  itemid = PageGetItemId(page, updatedoffsets[i]);
598  origtuple = (IndexTuple) PageGetItem(page, itemid);
599 
600  vacposting = palloc(offsetof(BTVacuumPostingData, deletetids) +
601  updates->ndeletedtids * sizeof(uint16));
602  vacposting->updatedoffset = updatedoffsets[i];
603  vacposting->itup = origtuple;
604  vacposting->ndeletedtids = updates->ndeletedtids;
605  memcpy(vacposting->deletetids,
606  (char *) updates + SizeOfBtreeUpdate,
607  updates->ndeletedtids * sizeof(uint16));
608 
609  _bt_update_posting(vacposting);
610 
611  /* Overwrite updated version of tuple */
612  itemsz = MAXALIGN(IndexTupleSize(vacposting->itup));
613  if (!PageIndexTupleOverwrite(page, updatedoffsets[i],
614  (Item) vacposting->itup, itemsz))
615  elog(PANIC, "failed to update partially dead item");
616 
617  pfree(vacposting->itup);
618  pfree(vacposting);
619 
620  /* advance to next xl_btree_update from array */
621  updates = (xl_btree_update *)
622  ((char *) updates + SizeOfBtreeUpdate +
623  updates->ndeletedtids * sizeof(uint16));
624  }
625  }
626 
627  if (xlrec->ndeleted > 0)
628  PageIndexMultiDelete(page, (OffsetNumber *) ptr, xlrec->ndeleted);
629 
630  /*
631  * Mark the page as not containing any LP_DEAD items --- see comments
632  * in _bt_delitems_vacuum().
633  */
634  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
635  opaque->btpo_flags &= ~BTP_HAS_GARBAGE;
636 
637  PageSetLSN(page, lsn);
638  MarkBufferDirty(buffer);
639  }
640  if (BufferIsValid(buffer))
641  UnlockReleaseBuffer(buffer);
642 }
uint16 ndeletedtids
Definition: nbtree.h:781
void _bt_update_posting(BTVacuumPosting vacposting)
Definition: nbtdedup.c:661
#define SizeOfBtreeUpdate
Definition: nbtxlog.h:225
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
OffsetNumber updatedoffset
Definition: nbtree.h:778
IndexTuple itup
Definition: nbtree.h:777
Pointer Item
Definition: item.h:17
uint16 nupdated
Definition: nbtxlog.h:243
#define PANIC
Definition: elog.h:53
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
uint16 OffsetNumber
Definition: off.h:24
unsigned short uint16
Definition: c.h:366
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLogRecGetData(decoder)
Definition: xlogreader.h:311
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
Definition: bufpage.c:1060
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
uint16 ndeleted
Definition: nbtxlog.h:242
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1514
uint16 deletetids[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtree.h:782
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:828
size_t Size
Definition: c.h:466
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define MAXALIGN(LEN)
Definition: c.h:691
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
void * palloc(Size size)
Definition: mcxt.c:949
XLogRedoAction XLogReadBufferForRedoExtended(XLogReaderState *record, uint8 block_id, ReadBufferMode mode, bool get_cleanup_lock, Buffer *buf)
Definition: xlogutils.c:331
#define elog(elevel,...)
Definition: elog.h:214
int i
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
#define offsetof(type, field)
Definition: c.h:661
#define BTP_HAS_GARBAGE
Definition: nbtree.h:78
uint16 ndeletedtids
Definition: nbtxlog.h:220
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define IndexTupleSize(itup)
Definition: itup.h:71

Variable Documentation

◆ opCtx

MemoryContext opCtx
static

Definition at line 27 of file nbtxlog.c.