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:1469
#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:3541
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:293
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#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:1469
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:3541
float8 last_cleanup_num_heap_tuples
Definition: nbtxlog.h:56
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:305
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:1521
#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:745
size_t Size
Definition: c.h:473
#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:2668
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:1066
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:410
#define PANIC
Definition: elog.h:53
unsigned short uint16
Definition: c.h:373
#define InvalidOffsetNumber
Definition: off.h:26
size_t Size
Definition: c.h:473
#define MAXALIGN(LEN)
Definition: c.h:698
#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 1042 of file nbtxlog.c.

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

1043 {
1044  Page page = (Page) pagedata;
1045  BTPageOpaque maskopaq;
1046 
1048 
1049  mask_page_hint_bits(page);
1050  mask_unused_space(page);
1051 
1052  maskopaq = (BTPageOpaque) PageGetSpecialPointer(page);
1053 
1054  if (P_ISLEAF(maskopaq))
1055  {
1056  /*
1057  * In btree leaf pages, it is possible to modify the LP_FLAGS without
1058  * emitting any WAL record. Hence, mask the line pointer flags. See
1059  * _bt_killitems(), _bt_check_unique() for details.
1060  */
1061  mask_lp_flags(page);
1062  }
1063 
1064  /*
1065  * BTP_HAS_GARBAGE is just an un-logged hint bit. So, mask it. See
1066  * _bt_killitems(), _bt_check_unique() for details.
1067  */
1068  maskopaq->btpo_flags &= ~BTP_HAS_GARBAGE;
1069 
1070  /*
1071  * During replay of a btree page split, we don't set the BTP_SPLIT_END
1072  * flag of the right sibling and initialize the cycle_id to 0 for the same
1073  * page. See btree_xlog_split() for details.
1074  */
1075  maskopaq->btpo_flags &= ~BTP_SPLIT_END;
1076  maskopaq->btpo_cycleid = 0;
1077 }
#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
BTCycleId btpo_cycleid
Definition: nbtree.h:66
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 965 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.

966 {
967  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
968  MemoryContext oldCtx;
969 
970  oldCtx = MemoryContextSwitchTo(opCtx);
971  switch (info)
972  {
974  btree_xlog_insert(true, false, false, record);
975  break;
977  btree_xlog_insert(false, false, false, record);
978  break;
980  btree_xlog_insert(false, true, false, record);
981  break;
982  case XLOG_BTREE_SPLIT_L:
983  btree_xlog_split(true, record);
984  break;
985  case XLOG_BTREE_SPLIT_R:
986  btree_xlog_split(false, record);
987  break;
989  btree_xlog_insert(true, false, true, record);
990  break;
991  case XLOG_BTREE_DEDUP:
992  btree_xlog_dedup(record);
993  break;
994  case XLOG_BTREE_VACUUM:
995  btree_xlog_vacuum(record);
996  break;
997  case XLOG_BTREE_DELETE:
998  btree_xlog_delete(record);
999  break;
1001  btree_xlog_mark_page_halfdead(info, record);
1002  break;
1005  btree_xlog_unlink_page(info, record);
1006  break;
1007  case XLOG_BTREE_NEWROOT:
1008  btree_xlog_newroot(record);
1009  break;
1010  case XLOG_BTREE_REUSE_PAGE:
1011  btree_xlog_reuse_page(record);
1012  break;
1014  _bt_restore_meta(record, 0);
1015  break;
1016  default:
1017  elog(PANIC, "btree_redo: unknown op code %u", info);
1018  }
1019  MemoryContextSwitchTo(oldCtx);
1021 }
static void btree_xlog_vacuum(XLogReaderState *record)
Definition: nbtxlog.c:560
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned char uint8
Definition: c.h:372
#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:647
#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:903
static void btree_xlog_reuse_page(XLogReaderState *record)
Definition: nbtxlog.c:943
#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:467
static void btree_xlog_insert(bool isleaf, bool ismeta, bool posting, XLogReaderState *record)
Definition: nbtxlog.c:163
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#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:777
#define XLOG_BTREE_INSERT_UPPER
Definition: nbtxlog.h:27
static void btree_xlog_mark_page_halfdead(uint8 info, XLogReaderState *record)
Definition: nbtxlog.c:692
#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 1032 of file nbtxlog.c.

References MemoryContextDelete().

1033 {
1035  opCtx = NULL;
1036 }
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 467 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, BTDedupStateData::nmaxitems, 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().

468 {
469  XLogRecPtr lsn = record->EndRecPtr;
470  xl_btree_dedup *xlrec = (xl_btree_dedup *) XLogRecGetData(record);
471  Buffer buf;
472 
473  if (XLogReadBufferForRedo(record, 0, &buf) == BLK_NEEDS_REDO)
474  {
475  char *ptr = XLogRecGetBlockData(record, 0, NULL);
476  Page page = (Page) BufferGetPage(buf);
478  OffsetNumber offnum,
479  minoff,
480  maxoff;
483  Page newpage;
484 
485  state = (BTDedupState) palloc(sizeof(BTDedupStateData));
486  state->deduplicate = true; /* unused */
487  state->nmaxitems = 0; /* unused */
488  /* Conservatively use larger maxpostingsize than primary */
489  state->maxpostingsize = BTMaxItemSize(page);
490  state->base = NULL;
491  state->baseoff = InvalidOffsetNumber;
492  state->basetupsize = 0;
493  state->htids = palloc(state->maxpostingsize);
494  state->nhtids = 0;
495  state->nitems = 0;
496  state->phystupsize = 0;
497  state->nintervals = 0;
498 
499  minoff = P_FIRSTDATAKEY(opaque);
500  maxoff = PageGetMaxOffsetNumber(page);
501  newpage = PageGetTempPageCopySpecial(page);
502 
503  if (!P_RIGHTMOST(opaque))
504  {
505  ItemId itemid = PageGetItemId(page, P_HIKEY);
506  Size itemsz = ItemIdGetLength(itemid);
507  IndexTuple item = (IndexTuple) PageGetItem(page, itemid);
508 
509  if (PageAddItem(newpage, (Item) item, itemsz, P_HIKEY,
510  false, false) == InvalidOffsetNumber)
511  elog(ERROR, "deduplication failed to add highkey");
512  }
513 
514  intervals = (BTDedupInterval *) ptr;
515  for (offnum = minoff;
516  offnum <= maxoff;
517  offnum = OffsetNumberNext(offnum))
518  {
519  ItemId itemid = PageGetItemId(page, offnum);
520  IndexTuple itup = (IndexTuple) PageGetItem(page, itemid);
521 
522  if (offnum == minoff)
523  _bt_dedup_start_pending(state, itup, offnum);
524  else if (state->nintervals < xlrec->nintervals &&
525  state->baseoff == intervals[state->nintervals].baseoff &&
526  state->nitems < intervals[state->nintervals].nitems)
527  {
528  if (!_bt_dedup_save_htid(state, itup))
529  elog(ERROR, "deduplication failed to add heap tid to pending posting list");
530  }
531  else
532  {
533  _bt_dedup_finish_pending(newpage, state);
534  _bt_dedup_start_pending(state, itup, offnum);
535  }
536  }
537 
538  _bt_dedup_finish_pending(newpage, state);
539  Assert(state->nintervals == xlrec->nintervals);
540  Assert(memcmp(state->intervals, intervals,
541  state->nintervals * sizeof(BTDedupInterval)) == 0);
542 
543  if (P_HAS_GARBAGE(opaque))
544  {
545  BTPageOpaque nopaque = (BTPageOpaque) PageGetSpecialPointer(newpage);
546 
547  nopaque->btpo_flags &= ~BTP_HAS_GARBAGE;
548  }
549 
550  PageRestoreTempPage(newpage, page);
551  PageSetLSN(page, lsn);
552  MarkBufferDirty(buf);
553  }
554 
555  if (BufferIsValid(buf))
556  UnlockReleaseBuffer(buf);
557 }
char * intervals[]
IndexTuple base
Definition: nbtree.h:746
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:403
uint16 nintervals
Definition: nbtxlog.h:172
OffsetNumber baseoff
Definition: nbtree.h:747
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
#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:410
void _bt_dedup_start_pending(BTDedupState state, IndexTuple base, OffsetNumber baseoff)
Definition: nbtdedup.c:324
Size _bt_dedup_finish_pending(Page newpage, BTDedupState state)
Definition: nbtdedup.c:446
#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:751
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
Size phystupsize
Definition: nbtree.h:754
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define ERROR
Definition: elog.h:43
BTDedupInterval intervals[MaxIndexTuplesPerPage]
Definition: nbtree.h:763
static char * buf
Definition: pg_test_fsync.c:67
bool _bt_dedup_save_htid(BTDedupState state, IndexTuple itup)
Definition: nbtdedup.c:375
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:1521
#define InvalidOffsetNumber
Definition: off.h:26
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:293
uint16 nitems
Definition: nbtree.h:717
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
Definition: regguts.h:298
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:473
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
Size basetupsize
Definition: nbtree.h:748
Size maxpostingsize
Definition: nbtree.h:743
#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:766
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 647 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().

648 {
649  XLogRecPtr lsn = record->EndRecPtr;
650  xl_btree_delete *xlrec = (xl_btree_delete *) XLogRecGetData(record);
651  Buffer buffer;
652  Page page;
653  BTPageOpaque opaque;
654 
655  /*
656  * If we have any conflict processing to do, it must happen before we
657  * update the page
658  */
659  if (InHotStandby)
660  {
661  RelFileNode rnode;
662 
663  XLogRecGetBlockTag(record, 0, &rnode, NULL, NULL);
664 
666  }
667 
668  /*
669  * We don't need to take a cleanup lock to apply these changes. See
670  * nbtree/README for details.
671  */
672  if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
673  {
674  char *ptr = XLogRecGetBlockData(record, 0, NULL);
675 
676  page = (Page) BufferGetPage(buffer);
677 
678  PageIndexMultiDelete(page, (OffsetNumber *) ptr, xlrec->ndeleted);
679 
680  /* Mark the page as not containing any LP_DEAD items */
681  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
682  opaque->btpo_flags &= ~BTP_HAS_GARBAGE;
683 
684  PageSetLSN(page, lsn);
685  MarkBufferDirty(buffer);
686  }
687  if (BufferIsValid(buffer))
688  UnlockReleaseBuffer(buffer);
689 }
TransactionId latestRemovedXid
Definition: nbtxlog.h:189
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
#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:310
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1497
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1521
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:293
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 (the locks are coupled), so that clearing the flag and inserting
176  * the downlink appear atomic to other backends. We don't bother with
177  * that during replay, because readers don't care about the
178  * incomplete-split flag and there 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:1469
Pointer Item
Definition: item.h:17
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:410
#define PANIC
Definition: elog.h:53
IndexTuple _bt_swap_posting(IndexTuple newitem, IndexTuple oposting, int postingoff)
Definition: nbtdedup.c:774
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
unsigned short uint16
Definition: c.h:373
static void _bt_restore_meta(XLogReaderState *record, uint8 block_id)
Definition: nbtxlog.c:85
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
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:1521
OffsetNumber offnum
Definition: nbtxlog.h:81
#define InvalidOffsetNumber
Definition: off.h:26
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:293
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
size_t Size
Definition: c.h:473
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
#define MAXALIGN(LEN)
Definition: c.h:698
#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 692 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().

693 {
694  XLogRecPtr lsn = record->EndRecPtr;
696  Buffer buffer;
697  Page page;
698  BTPageOpaque pageop;
699  IndexTupleData trunctuple;
700 
701  /*
702  * In normal operation, we would lock all the pages this WAL record
703  * touches before changing any of them. In WAL replay, it should be okay
704  * to lock just one page at a time, since no concurrent index updates can
705  * be happening, and readers should not care whether they arrive at the
706  * target page or not (since it's surely empty).
707  */
708 
709  /* to-be-deleted subtree's parent page */
710  if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
711  {
712  OffsetNumber poffset;
713  ItemId itemid;
714  IndexTuple itup;
715  OffsetNumber nextoffset;
716  BlockNumber rightsib;
717 
718  page = (Page) BufferGetPage(buffer);
719  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
720 
721  poffset = xlrec->poffset;
722 
723  nextoffset = OffsetNumberNext(poffset);
724  itemid = PageGetItemId(page, nextoffset);
725  itup = (IndexTuple) PageGetItem(page, itemid);
726  rightsib = BTreeTupleGetDownLink(itup);
727 
728  itemid = PageGetItemId(page, poffset);
729  itup = (IndexTuple) PageGetItem(page, itemid);
730  BTreeTupleSetDownLink(itup, rightsib);
731  nextoffset = OffsetNumberNext(poffset);
732  PageIndexTupleDelete(page, nextoffset);
733 
734  PageSetLSN(page, lsn);
735  MarkBufferDirty(buffer);
736  }
737 
738  /*
739  * Don't need to couple cross-level locks in REDO routines, so release
740  * lock on internal page immediately
741  */
742  if (BufferIsValid(buffer))
743  UnlockReleaseBuffer(buffer);
744 
745  /* Rewrite the leaf page as a halfdead page */
746  buffer = XLogInitBufferForRedo(record, 0);
747  page = (Page) BufferGetPage(buffer);
748 
749  _bt_pageinit(page, BufferGetPageSize(buffer));
750  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
751 
752  pageop->btpo_prev = xlrec->leftblk;
753  pageop->btpo_next = xlrec->rightblk;
754  pageop->btpo.level = 0;
755  pageop->btpo_flags = BTP_HALF_DEAD | BTP_LEAF;
756  pageop->btpo_cycleid = 0;
757 
758  /*
759  * Construct a dummy high key item that points to top parent page (value
760  * is InvalidBlockNumber when the top parent page is the leaf page itself)
761  */
762  MemSet(&trunctuple, 0, sizeof(IndexTupleData));
763  trunctuple.t_info = sizeof(IndexTupleData);
764  BTreeTupleSetTopParent(&trunctuple, xlrec->topparent);
765 
766  if (PageAddItem(page, (Item) &trunctuple, sizeof(IndexTupleData), P_HIKEY,
767  false, false) == InvalidOffsetNumber)
768  elog(ERROR, "could not add dummy high key to half-dead page");
769 
770  PageSetLSN(page, lsn);
771  MarkBufferDirty(buffer);
772  UnlockReleaseBuffer(buffer);
773 }
BlockNumber btpo_next
Definition: nbtree.h:59
void PageIndexTupleDelete(Page page, OffsetNumber offnum)
Definition: bufpage.c:719
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
#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:949
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:410
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:310
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define ERROR
Definition: elog.h:43
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:305
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:293
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:1066
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 903 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().

904 {
905  XLogRecPtr lsn = record->EndRecPtr;
906  xl_btree_newroot *xlrec = (xl_btree_newroot *) XLogRecGetData(record);
907  Buffer buffer;
908  Page page;
909  BTPageOpaque pageop;
910  char *ptr;
911  Size len;
912 
913  buffer = XLogInitBufferForRedo(record, 0);
914  page = (Page) BufferGetPage(buffer);
915 
916  _bt_pageinit(page, BufferGetPageSize(buffer));
917  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
918 
919  pageop->btpo_flags = BTP_ROOT;
920  pageop->btpo_prev = pageop->btpo_next = P_NONE;
921  pageop->btpo.level = xlrec->level;
922  if (xlrec->level == 0)
923  pageop->btpo_flags |= BTP_LEAF;
924  pageop->btpo_cycleid = 0;
925 
926  if (xlrec->level > 0)
927  {
928  ptr = XLogRecGetBlockData(record, 0, &len);
929  _bt_restore_page(page, ptr, len);
930 
931  /* Clear the incomplete-split flag in left child */
932  _bt_clear_incomplete_split(record, 1);
933  }
934 
935  PageSetLSN(page, lsn);
936  MarkBufferDirty(buffer);
937  UnlockReleaseBuffer(buffer);
938 
939  _bt_restore_meta(record, 2);
940 }
#define BTP_ROOT
Definition: nbtree.h:73
BlockNumber btpo_next
Definition: nbtree.h:59
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
#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:310
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:305
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:1521
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:473
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
void _bt_pageinit(Page page, Size size)
Definition: nbtpage.c:1066
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 943 of file nbtxlog.c.

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

Referenced by btree_redo().

944 {
946 
947  /*
948  * Btree reuse_page records exist to provide a conflict point when we
949  * reuse pages in the index via the FSM. That's all they do though.
950  *
951  * latestRemovedXid was the page's btpo.xact. The
952  * GlobalVisCheckRemovableXid test in _bt_page_recyclable() conceptually
953  * mirrors the pgxact->xmin > limitXmin test in
954  * GetConflictingVirtualXIDs(). Consequently, one XID value achieves the
955  * same exclusion effect on primary and standby.
956  */
957  if (InHotStandby)
958  {
960  xlrec->node);
961  }
962 }
RelFileNode node
Definition: nbtxlog.h:206
#define InHotStandby
Definition: xlog.h:74
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
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, buf, 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, 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 buf;
260  Buffer rbuf;
261  Page rpage;
262  BTPageOpaque ropaque;
263  char *datapos;
264  Size datalen;
265  BlockNumber origpagenumber;
266  BlockNumber rightpagenumber;
267  BlockNumber spagenumber;
268 
269  XLogRecGetBlockTag(record, 0, NULL, NULL, &origpagenumber);
270  XLogRecGetBlockTag(record, 1, NULL, NULL, &rightpagenumber);
271  if (!XLogRecGetBlockTag(record, 2, NULL, NULL, &spagenumber))
272  spagenumber = P_NONE;
273 
274  /*
275  * Clear the incomplete split flag on the appropriate child page one level
276  * down when origpage/buf is an internal page (there must have been
277  * cascading page splits during original execution in the event of an
278  * internal page split). This is like the corresponding btree_xlog_insert
279  * call for internal pages. We're not clearing the incomplete split flag
280  * for the current page split here (you can think of this as part of the
281  * insert of newitem that the page split action needs to perform in
282  * passing).
283  *
284  * Like in btree_xlog_insert, this can be done before locking other pages.
285  * We never need to couple cross-level locks in REDO routines.
286  */
287  if (!isleaf)
288  _bt_clear_incomplete_split(record, 3);
289 
290  /* Reconstruct right (new) sibling page from scratch */
291  rbuf = XLogInitBufferForRedo(record, 1);
292  datapos = XLogRecGetBlockData(record, 1, &datalen);
293  rpage = (Page) BufferGetPage(rbuf);
294 
295  _bt_pageinit(rpage, BufferGetPageSize(rbuf));
296  ropaque = (BTPageOpaque) PageGetSpecialPointer(rpage);
297 
298  ropaque->btpo_prev = origpagenumber;
299  ropaque->btpo_next = spagenumber;
300  ropaque->btpo.level = xlrec->level;
301  ropaque->btpo_flags = isleaf ? BTP_LEAF : 0;
302  ropaque->btpo_cycleid = 0;
303 
304  _bt_restore_page(rpage, datapos, datalen);
305 
306  PageSetLSN(rpage, lsn);
307  MarkBufferDirty(rbuf);
308 
309  /* Now reconstruct original page (left half of split) */
310  if (XLogReadBufferForRedo(record, 0, &buf) == BLK_NEEDS_REDO)
311  {
312  /*
313  * To retain the same physical order of the tuples that they had, we
314  * initialize a temporary empty page for the left page and add all the
315  * items to that in item number order. This mirrors how _bt_split()
316  * works. Retaining the same physical order makes WAL consistency
317  * checking possible. See also _bt_restore_page(), which does the
318  * same for the right page.
319  */
320  Page origpage = (Page) BufferGetPage(buf);
321  BTPageOpaque oopaque = (BTPageOpaque) PageGetSpecialPointer(origpage);
322  OffsetNumber off;
323  IndexTuple newitem = NULL,
324  left_hikey = NULL,
325  nposting = NULL;
326  Size newitemsz = 0,
327  left_hikeysz = 0;
328  Page leftpage;
329  OffsetNumber leftoff,
330  replacepostingoff = InvalidOffsetNumber;
331 
332  datapos = XLogRecGetBlockData(record, 0, &datalen);
333 
334  if (newitemonleft || xlrec->postingoff != 0)
335  {
336  newitem = (IndexTuple) datapos;
337  newitemsz = MAXALIGN(IndexTupleSize(newitem));
338  datapos += newitemsz;
339  datalen -= newitemsz;
340 
341  if (xlrec->postingoff != 0)
342  {
343  ItemId itemid;
344  IndexTuple oposting;
345 
346  /* Posting list must be at offset number before new item's */
347  replacepostingoff = OffsetNumberPrev(xlrec->newitemoff);
348 
349  /* Use mutable, aligned newitem copy in _bt_swap_posting() */
350  newitem = CopyIndexTuple(newitem);
351  itemid = PageGetItemId(origpage, replacepostingoff);
352  oposting = (IndexTuple) PageGetItem(origpage, itemid);
353  nposting = _bt_swap_posting(newitem, oposting,
354  xlrec->postingoff);
355  }
356  }
357 
358  /*
359  * Extract left hikey and its size. We assume that 16-bit alignment
360  * is enough to apply IndexTupleSize (since it's fetching from a
361  * uint16 field).
362  */
363  left_hikey = (IndexTuple) datapos;
364  left_hikeysz = MAXALIGN(IndexTupleSize(left_hikey));
365  datapos += left_hikeysz;
366  datalen -= left_hikeysz;
367 
368  Assert(datalen == 0);
369 
370  leftpage = PageGetTempPageCopySpecial(origpage);
371 
372  /* Add high key tuple from WAL record to temp page */
373  leftoff = P_HIKEY;
374  if (PageAddItem(leftpage, (Item) left_hikey, left_hikeysz, P_HIKEY,
375  false, false) == InvalidOffsetNumber)
376  elog(ERROR, "failed to add high key to left page after split");
377  leftoff = OffsetNumberNext(leftoff);
378 
379  for (off = P_FIRSTDATAKEY(oopaque); off < xlrec->firstrightoff; off++)
380  {
381  ItemId itemid;
382  Size itemsz;
383  IndexTuple item;
384 
385  /* Add replacement posting list when required */
386  if (off == replacepostingoff)
387  {
388  Assert(newitemonleft ||
389  xlrec->firstrightoff == xlrec->newitemoff);
390  if (PageAddItem(leftpage, (Item) nposting,
391  MAXALIGN(IndexTupleSize(nposting)), leftoff,
392  false, false) == InvalidOffsetNumber)
393  elog(ERROR, "failed to add new posting list item to left page after split");
394  leftoff = OffsetNumberNext(leftoff);
395  continue; /* don't insert oposting */
396  }
397 
398  /* add the new item if it was inserted on left page */
399  else if (newitemonleft && off == xlrec->newitemoff)
400  {
401  if (PageAddItem(leftpage, (Item) newitem, newitemsz, leftoff,
402  false, false) == InvalidOffsetNumber)
403  elog(ERROR, "failed to add new item to left page after split");
404  leftoff = OffsetNumberNext(leftoff);
405  }
406 
407  itemid = PageGetItemId(origpage, off);
408  itemsz = ItemIdGetLength(itemid);
409  item = (IndexTuple) PageGetItem(origpage, itemid);
410  if (PageAddItem(leftpage, (Item) item, itemsz, leftoff,
411  false, false) == InvalidOffsetNumber)
412  elog(ERROR, "failed to add old item to left page after split");
413  leftoff = OffsetNumberNext(leftoff);
414  }
415 
416  /* cope with possibility that newitem goes at the end */
417  if (newitemonleft && off == xlrec->newitemoff)
418  {
419  if (PageAddItem(leftpage, (Item) newitem, newitemsz, leftoff,
420  false, false) == InvalidOffsetNumber)
421  elog(ERROR, "failed to add new item to left page after split");
422  leftoff = OffsetNumberNext(leftoff);
423  }
424 
425  PageRestoreTempPage(leftpage, origpage);
426 
427  /* Fix opaque fields */
428  oopaque->btpo_flags = BTP_INCOMPLETE_SPLIT;
429  if (isleaf)
430  oopaque->btpo_flags |= BTP_LEAF;
431  oopaque->btpo_next = rightpagenumber;
432  oopaque->btpo_cycleid = 0;
433 
434  PageSetLSN(origpage, lsn);
435  MarkBufferDirty(buf);
436  }
437 
438  /* Fix left-link of the page to the right of the new right sibling */
439  if (spagenumber != P_NONE)
440  {
441  Buffer sbuf;
442 
443  if (XLogReadBufferForRedo(record, 2, &sbuf) == BLK_NEEDS_REDO)
444  {
445  Page spage = (Page) BufferGetPage(sbuf);
447 
448  spageop->btpo_prev = rightpagenumber;
449 
450  PageSetLSN(spage, lsn);
451  MarkBufferDirty(sbuf);
452  }
453  if (BufferIsValid(sbuf))
454  UnlockReleaseBuffer(sbuf);
455  }
456 
457  /*
458  * Finally, release the remaining buffers. sbuf, rbuf, and buf must be
459  * released together, so that readers cannot observe inconsistencies.
460  */
461  UnlockReleaseBuffer(rbuf);
462  if (BufferIsValid(buf))
463  UnlockReleaseBuffer(buf);
464 }
BlockNumber btpo_next
Definition: nbtree.h:59
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:403
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
#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:410
uint32 BlockNumber
Definition: block.h:31
IndexTuple _bt_swap_posting(IndexTuple newitem, IndexTuple oposting, int postingoff)
Definition: nbtdedup.c:774
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:310
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define ERROR
Definition: elog.h:43
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:305
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
static char * buf
Definition: pg_test_fsync.c:67
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:1497
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1521
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:293
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:473
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
#define MAXALIGN(LEN)
Definition: c.h:698
#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:1066
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 1024 of file nbtxlog.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, and CurrentMemoryContext.

1025 {
1027  "Btree recovery temporary context",
1029 }
#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 777 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, InvalidBuffer, 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().

778 {
779  XLogRecPtr lsn = record->EndRecPtr;
781  BlockNumber leftsib;
782  BlockNumber rightsib;
783  Buffer leftbuf;
784  Buffer target;
785  Buffer rightbuf;
786  Page page;
787  BTPageOpaque pageop;
788 
789  leftsib = xlrec->leftsib;
790  rightsib = xlrec->rightsib;
791 
792  /*
793  * In normal operation, we would lock all the pages this WAL record
794  * touches before changing any of them. In WAL replay, we at least lock
795  * the pages in the same standard left-to-right order (leftsib, target,
796  * rightsib), and don't release the sibling locks until the target is
797  * marked deleted.
798  */
799 
800  /* Fix right-link of left sibling, if any */
801  if (leftsib != P_NONE)
802  {
803  if (XLogReadBufferForRedo(record, 1, &leftbuf) == BLK_NEEDS_REDO)
804  {
805  page = (Page) BufferGetPage(leftbuf);
806  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
807  pageop->btpo_next = rightsib;
808 
809  PageSetLSN(page, lsn);
810  MarkBufferDirty(leftbuf);
811  }
812  }
813  else
814  leftbuf = InvalidBuffer;
815 
816  /* Rewrite target page as empty deleted page */
817  target = XLogInitBufferForRedo(record, 0);
818  page = (Page) BufferGetPage(target);
819 
820  _bt_pageinit(page, BufferGetPageSize(target));
821  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
822 
823  pageop->btpo_prev = leftsib;
824  pageop->btpo_next = rightsib;
825  pageop->btpo.xact = xlrec->btpo_xact;
826  pageop->btpo_flags = BTP_DELETED;
827  pageop->btpo_cycleid = 0;
828 
829  PageSetLSN(page, lsn);
830  MarkBufferDirty(target);
831 
832  /* Fix left-link of right sibling */
833  if (XLogReadBufferForRedo(record, 2, &rightbuf) == BLK_NEEDS_REDO)
834  {
835  page = (Page) BufferGetPage(rightbuf);
836  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
837  pageop->btpo_prev = leftsib;
838 
839  PageSetLSN(page, lsn);
840  MarkBufferDirty(rightbuf);
841  }
842 
843  /* Release siblings */
844  if (BufferIsValid(leftbuf))
845  UnlockReleaseBuffer(leftbuf);
846  if (BufferIsValid(rightbuf))
847  UnlockReleaseBuffer(rightbuf);
848 
849  /* Release target */
850  UnlockReleaseBuffer(target);
851 
852  /*
853  * If we deleted a parent of the targeted leaf page, instead of the leaf
854  * itself, update the leaf to point to the next remaining child in the
855  * to-be-deleted subtree
856  */
857  if (XLogRecHasBlockRef(record, 3))
858  {
859  /*
860  * There is no real data on the page, so we just re-create it from
861  * scratch using the information from the WAL record.
862  *
863  * Note that we don't end up here when the target page is also the
864  * leafbuf page. There is no need to add a dummy hikey item with a
865  * top parent link when deleting leafbuf because it's the last page
866  * we'll delete in the subtree undergoing deletion.
867  */
868  Buffer leafbuf;
869  IndexTupleData trunctuple;
870 
871  leafbuf = XLogInitBufferForRedo(record, 3);
872  page = (Page) BufferGetPage(leafbuf);
873 
874  _bt_pageinit(page, BufferGetPageSize(leafbuf));
875  pageop = (BTPageOpaque) PageGetSpecialPointer(page);
876 
877  pageop->btpo_flags = BTP_HALF_DEAD | BTP_LEAF;
878  pageop->btpo_prev = xlrec->leafleftsib;
879  pageop->btpo_next = xlrec->leafrightsib;
880  pageop->btpo.level = 0;
881  pageop->btpo_cycleid = 0;
882 
883  /* Add a dummy hikey item */
884  MemSet(&trunctuple, 0, sizeof(IndexTupleData));
885  trunctuple.t_info = sizeof(IndexTupleData);
886  BTreeTupleSetTopParent(&trunctuple, xlrec->topparent);
887 
888  if (PageAddItem(page, (Item) &trunctuple, sizeof(IndexTupleData), P_HIKEY,
889  false, false) == InvalidOffsetNumber)
890  elog(ERROR, "could not add dummy high key to half-dead page");
891 
892  PageSetLSN(page, lsn);
893  MarkBufferDirty(leafbuf);
894  UnlockReleaseBuffer(leafbuf);
895  }
896 
897  /* Update metapage if needed */
898  if (info == XLOG_BTREE_UNLINK_PAGE_META)
899  _bt_restore_meta(record, 4);
900 }
BlockNumber btpo_next
Definition: nbtree.h:59
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
#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 InvalidBuffer
Definition: buf.h:25
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:313
#define MemSet(start, val, len)
Definition: c.h:949
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:410
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:310
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define ERROR
Definition: elog.h:43
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:305
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:293
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:1066
#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 560 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().

561 {
562  XLogRecPtr lsn = record->EndRecPtr;
563  xl_btree_vacuum *xlrec = (xl_btree_vacuum *) XLogRecGetData(record);
564  Buffer buffer;
565  Page page;
566  BTPageOpaque opaque;
567 
568  /*
569  * We need to take a cleanup lock here, just like btvacuumpage(). However,
570  * it isn't necessary to exhaustively get a cleanup lock on every block in
571  * the index during recovery (just getting a cleanup lock on pages with
572  * items to kill suffices). See nbtree/README for details.
573  */
574  if (XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &buffer)
575  == BLK_NEEDS_REDO)
576  {
577  char *ptr = XLogRecGetBlockData(record, 0, NULL);
578 
579  page = (Page) BufferGetPage(buffer);
580 
581  if (xlrec->nupdated > 0)
582  {
583  OffsetNumber *updatedoffsets;
584  xl_btree_update *updates;
585 
586  updatedoffsets = (OffsetNumber *)
587  (ptr + xlrec->ndeleted * sizeof(OffsetNumber));
588  updates = (xl_btree_update *) ((char *) updatedoffsets +
589  xlrec->nupdated *
590  sizeof(OffsetNumber));
591 
592  for (int i = 0; i < xlrec->nupdated; i++)
593  {
594  BTVacuumPosting vacposting;
595  IndexTuple origtuple;
596  ItemId itemid;
597  Size itemsz;
598 
599  itemid = PageGetItemId(page, updatedoffsets[i]);
600  origtuple = (IndexTuple) PageGetItem(page, itemid);
601 
602  vacposting = palloc(offsetof(BTVacuumPostingData, deletetids) +
603  updates->ndeletedtids * sizeof(uint16));
604  vacposting->updatedoffset = updatedoffsets[i];
605  vacposting->itup = origtuple;
606  vacposting->ndeletedtids = updates->ndeletedtids;
607  memcpy(vacposting->deletetids,
608  (char *) updates + SizeOfBtreeUpdate,
609  updates->ndeletedtids * sizeof(uint16));
610 
611  _bt_update_posting(vacposting);
612 
613  /* Overwrite updated version of tuple */
614  itemsz = MAXALIGN(IndexTupleSize(vacposting->itup));
615  if (!PageIndexTupleOverwrite(page, updatedoffsets[i],
616  (Item) vacposting->itup, itemsz))
617  elog(PANIC, "failed to update partially dead item");
618 
619  pfree(vacposting->itup);
620  pfree(vacposting);
621 
622  /* advance to next xl_btree_update from array */
623  updates = (xl_btree_update *)
624  ((char *) updates + SizeOfBtreeUpdate +
625  updates->ndeletedtids * sizeof(uint16));
626  }
627  }
628 
629  if (xlrec->ndeleted > 0)
630  PageIndexMultiDelete(page, (OffsetNumber *) ptr, xlrec->ndeleted);
631 
632  /*
633  * Mark the page as not containing any LP_DEAD items --- see comments
634  * in _bt_delitems_vacuum().
635  */
636  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
637  opaque->btpo_flags &= ~BTP_HAS_GARBAGE;
638 
639  PageSetLSN(page, lsn);
640  MarkBufferDirty(buffer);
641  }
642  if (BufferIsValid(buffer))
643  UnlockReleaseBuffer(buffer);
644 }
uint16 ndeletedtids
Definition: nbtree.h:782
void _bt_update_posting(BTVacuumPosting vacposting)
Definition: nbtdedup.c:676
#define SizeOfBtreeUpdate
Definition: nbtxlog.h:225
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
OffsetNumber updatedoffset
Definition: nbtree.h:779
IndexTuple itup
Definition: nbtree.h:778
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:373
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
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:1521
uint16 deletetids[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtree.h:783
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:828
size_t Size
Definition: c.h:473
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define MAXALIGN(LEN)
Definition: c.h:698
#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:330
#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:668
#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.