You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

brin_xlog.c 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * brin_xlog.c
  3. * XLog replay routines for BRIN indexes
  4. *
  5. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  6. * Portions Copyright (c) 1994, Regents of the University of California
  7. *
  8. * IDENTIFICATION
  9. * src/backend/access/brin/brin_xlog.c
  10. */
  11. #include "postgres.h"
  12. #include "access/brin_page.h"
  13. #include "access/brin_pageops.h"
  14. #include "access/brin_xlog.h"
  15. #include "access/bufmask.h"
  16. #include "access/xlogutils.h"
  17. /*
  18. * xlog replay routines
  19. */
  20. static void
  21. brin_xlog_createidx(XLogReaderState *record)
  22. {
  23. XLogRecPtr lsn = record->EndRecPtr;
  24. xl_brin_createidx *xlrec = (xl_brin_createidx *) XLogRecGetData(record);
  25. Buffer buf;
  26. Page page;
  27. /* create the index' metapage */
  28. buf = XLogInitBufferForRedo(record, 0);
  29. Assert(BufferIsValid(buf));
  30. page = (Page) BufferGetPage(buf);
  31. brin_metapage_init(page, xlrec->pagesPerRange, xlrec->version);
  32. PageSetLSN(page, lsn);
  33. MarkBufferDirty(buf);
  34. UnlockReleaseBuffer(buf);
  35. }
  36. /*
  37. * Common part of an insert or update. Inserts the new tuple and updates the
  38. * revmap.
  39. */
  40. static void
  41. brin_xlog_insert_update(XLogReaderState *record,
  42. xl_brin_insert *xlrec)
  43. {
  44. XLogRecPtr lsn = record->EndRecPtr;
  45. Buffer buffer;
  46. BlockNumber regpgno;
  47. Page page;
  48. XLogRedoAction action;
  49. /*
  50. * If we inserted the first and only tuple on the page, re-initialize the
  51. * page from scratch.
  52. */
  53. if (XLogRecGetInfo(record) & XLOG_BRIN_INIT_PAGE)
  54. {
  55. buffer = XLogInitBufferForRedo(record, 0);
  56. page = BufferGetPage(buffer);
  57. brin_page_init(page, BRIN_PAGETYPE_REGULAR);
  58. action = BLK_NEEDS_REDO;
  59. }
  60. else
  61. {
  62. action = XLogReadBufferForRedo(record, 0, &buffer);
  63. }
  64. /* need this page's blkno to store in revmap */
  65. regpgno = BufferGetBlockNumber(buffer);
  66. /* insert the index item into the page */
  67. if (action == BLK_NEEDS_REDO)
  68. {
  69. OffsetNumber offnum;
  70. BrinTuple *tuple;
  71. Size tuplen;
  72. tuple = (BrinTuple *) XLogRecGetBlockData(record, 0, &tuplen);
  73. Assert(tuple->bt_blkno == xlrec->heapBlk);
  74. page = (Page) BufferGetPage(buffer);
  75. offnum = xlrec->offnum;
  76. if (PageGetMaxOffsetNumber(page) + 1 < offnum)
  77. elog(PANIC, "brin_xlog_insert_update: invalid max offset number");
  78. offnum = PageAddItem(page, (Item) tuple, tuplen, offnum, true, false);
  79. if (offnum == InvalidOffsetNumber)
  80. elog(PANIC, "brin_xlog_insert_update: failed to add tuple");
  81. PageSetLSN(page, lsn);
  82. MarkBufferDirty(buffer);
  83. }
  84. if (BufferIsValid(buffer))
  85. UnlockReleaseBuffer(buffer);
  86. /* update the revmap */
  87. action = XLogReadBufferForRedo(record, 1, &buffer);
  88. if (action == BLK_NEEDS_REDO)
  89. {
  90. ItemPointerData tid;
  91. ItemPointerSet(&tid, regpgno, xlrec->offnum);
  92. page = (Page) BufferGetPage(buffer);
  93. brinSetHeapBlockItemptr(buffer, xlrec->pagesPerRange, xlrec->heapBlk,
  94. tid);
  95. PageSetLSN(page, lsn);
  96. MarkBufferDirty(buffer);
  97. }
  98. if (BufferIsValid(buffer))
  99. UnlockReleaseBuffer(buffer);
  100. /* XXX no FSM updates here ... */
  101. }
  102. /*
  103. * replay a BRIN index insertion
  104. */
  105. static void
  106. brin_xlog_insert(XLogReaderState *record)
  107. {
  108. xl_brin_insert *xlrec = (xl_brin_insert *) XLogRecGetData(record);
  109. brin_xlog_insert_update(record, xlrec);
  110. }
  111. /*
  112. * replay a BRIN index update
  113. */
  114. static void
  115. brin_xlog_update(XLogReaderState *record)
  116. {
  117. XLogRecPtr lsn = record->EndRecPtr;
  118. xl_brin_update *xlrec = (xl_brin_update *) XLogRecGetData(record);
  119. Buffer buffer;
  120. XLogRedoAction action;
  121. /* First remove the old tuple */
  122. action = XLogReadBufferForRedo(record, 2, &buffer);
  123. if (action == BLK_NEEDS_REDO)
  124. {
  125. Page page;
  126. OffsetNumber offnum;
  127. page = (Page) BufferGetPage(buffer);
  128. offnum = xlrec->oldOffnum;
  129. PageIndexTupleDeleteNoCompact(page, offnum);
  130. PageSetLSN(page, lsn);
  131. MarkBufferDirty(buffer);
  132. }
  133. /* Then insert the new tuple and update revmap, like in an insertion. */
  134. brin_xlog_insert_update(record, &xlrec->insert);
  135. if (BufferIsValid(buffer))
  136. UnlockReleaseBuffer(buffer);
  137. }
  138. /*
  139. * Update a tuple on a single page.
  140. */
  141. static void
  142. brin_xlog_samepage_update(XLogReaderState *record)
  143. {
  144. XLogRecPtr lsn = record->EndRecPtr;
  145. xl_brin_samepage_update *xlrec;
  146. Buffer buffer;
  147. XLogRedoAction action;
  148. xlrec = (xl_brin_samepage_update *) XLogRecGetData(record);
  149. action = XLogReadBufferForRedo(record, 0, &buffer);
  150. if (action == BLK_NEEDS_REDO)
  151. {
  152. Size tuplen;
  153. BrinTuple *brintuple;
  154. Page page;
  155. OffsetNumber offnum;
  156. brintuple = (BrinTuple *) XLogRecGetBlockData(record, 0, &tuplen);
  157. page = (Page) BufferGetPage(buffer);
  158. offnum = xlrec->offnum;
  159. if (!PageIndexTupleOverwrite(page, offnum, (Item) brintuple, tuplen))
  160. elog(PANIC, "brin_xlog_samepage_update: failed to replace tuple");
  161. PageSetLSN(page, lsn);
  162. MarkBufferDirty(buffer);
  163. }
  164. if (BufferIsValid(buffer))
  165. UnlockReleaseBuffer(buffer);
  166. /* XXX no FSM updates here ... */
  167. }
  168. /*
  169. * Replay a revmap page extension
  170. */
  171. static void
  172. brin_xlog_revmap_extend(XLogReaderState *record)
  173. {
  174. XLogRecPtr lsn = record->EndRecPtr;
  175. xl_brin_revmap_extend *xlrec;
  176. Buffer metabuf;
  177. Buffer buf;
  178. Page page;
  179. BlockNumber targetBlk;
  180. XLogRedoAction action;
  181. xlrec = (xl_brin_revmap_extend *) XLogRecGetData(record);
  182. XLogRecGetBlockTag(record, 1, NULL, NULL, &targetBlk);
  183. Assert(xlrec->targetBlk == targetBlk);
  184. /* Update the metapage */
  185. action = XLogReadBufferForRedo(record, 0, &metabuf);
  186. if (action == BLK_NEEDS_REDO)
  187. {
  188. Page metapg;
  189. BrinMetaPageData *metadata;
  190. metapg = BufferGetPage(metabuf);
  191. metadata = (BrinMetaPageData *) PageGetContents(metapg);
  192. Assert(metadata->lastRevmapPage == xlrec->targetBlk - 1);
  193. metadata->lastRevmapPage = xlrec->targetBlk;
  194. PageSetLSN(metapg, lsn);
  195. /*
  196. * Set pd_lower just past the end of the metadata. This is essential,
  197. * because without doing so, metadata will be lost if xlog.c
  198. * compresses the page. (We must do this here because pre-v11
  199. * versions of PG did not set the metapage's pd_lower correctly, so a
  200. * pg_upgraded index might contain the wrong value.)
  201. */
  202. ((PageHeader) metapg)->pd_lower =
  203. ((char *) metadata + sizeof(BrinMetaPageData)) - (char *) metapg;
  204. MarkBufferDirty(metabuf);
  205. }
  206. /*
  207. * Re-init the target block as a revmap page. There's never a full- page
  208. * image here.
  209. */
  210. buf = XLogInitBufferForRedo(record, 1);
  211. page = (Page) BufferGetPage(buf);
  212. brin_page_init(page, BRIN_PAGETYPE_REVMAP);
  213. PageSetLSN(page, lsn);
  214. MarkBufferDirty(buf);
  215. UnlockReleaseBuffer(buf);
  216. if (BufferIsValid(metabuf))
  217. UnlockReleaseBuffer(metabuf);
  218. }
  219. static void
  220. brin_xlog_desummarize_page(XLogReaderState *record)
  221. {
  222. XLogRecPtr lsn = record->EndRecPtr;
  223. xl_brin_desummarize *xlrec;
  224. Buffer buffer;
  225. XLogRedoAction action;
  226. xlrec = (xl_brin_desummarize *) XLogRecGetData(record);
  227. /* Update the revmap */
  228. action = XLogReadBufferForRedo(record, 0, &buffer);
  229. if (action == BLK_NEEDS_REDO)
  230. {
  231. ItemPointerData iptr;
  232. ItemPointerSetInvalid(&iptr);
  233. brinSetHeapBlockItemptr(buffer, xlrec->pagesPerRange, xlrec->heapBlk, iptr);
  234. PageSetLSN(BufferGetPage(buffer), lsn);
  235. MarkBufferDirty(buffer);
  236. }
  237. if (BufferIsValid(buffer))
  238. UnlockReleaseBuffer(buffer);
  239. /* remove the leftover entry from the regular page */
  240. action = XLogReadBufferForRedo(record, 1, &buffer);
  241. if (action == BLK_NEEDS_REDO)
  242. {
  243. Page regPg = BufferGetPage(buffer);
  244. PageIndexTupleDeleteNoCompact(regPg, xlrec->regOffset);
  245. PageSetLSN(regPg, lsn);
  246. MarkBufferDirty(buffer);
  247. }
  248. if (BufferIsValid(buffer))
  249. UnlockReleaseBuffer(buffer);
  250. }
  251. void
  252. brin_redo(XLogReaderState *record)
  253. {
  254. uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
  255. switch (info & XLOG_BRIN_OPMASK)
  256. {
  257. case XLOG_BRIN_CREATE_INDEX:
  258. brin_xlog_createidx(record);
  259. break;
  260. case XLOG_BRIN_INSERT:
  261. brin_xlog_insert(record);
  262. break;
  263. case XLOG_BRIN_UPDATE:
  264. brin_xlog_update(record);
  265. break;
  266. case XLOG_BRIN_SAMEPAGE_UPDATE:
  267. brin_xlog_samepage_update(record);
  268. break;
  269. case XLOG_BRIN_REVMAP_EXTEND:
  270. brin_xlog_revmap_extend(record);
  271. break;
  272. case XLOG_BRIN_DESUMMARIZE:
  273. brin_xlog_desummarize_page(record);
  274. break;
  275. default:
  276. elog(PANIC, "brin_redo: unknown op code %u", info);
  277. }
  278. }
  279. /*
  280. * Mask a BRIN page before doing consistency checks.
  281. */
  282. void
  283. brin_mask(char *pagedata, BlockNumber blkno)
  284. {
  285. Page page = (Page) pagedata;
  286. PageHeader pagehdr = (PageHeader) page;
  287. mask_page_lsn_and_checksum(page);
  288. mask_page_hint_bits(page);
  289. /*
  290. * Regular brin pages contain unused space which needs to be masked.
  291. * Similarly for meta pages, but mask it only if pd_lower appears to have
  292. * been set correctly.
  293. */
  294. if (BRIN_IS_REGULAR_PAGE(page) ||
  295. (BRIN_IS_META_PAGE(page) && pagehdr->pd_lower > SizeOfPageHeaderData))
  296. {
  297. mask_unused_space(page);
  298. }
  299. }