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.

blinsert.c 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*-------------------------------------------------------------------------
  2. *
  3. * blinsert.c
  4. * Bloom index build and insert functions.
  5. *
  6. * Copyright (c) 2016-2019, PostgreSQL Global Development Group
  7. *
  8. * IDENTIFICATION
  9. * contrib/bloom/blinsert.c
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #include "postgres.h"
  14. #include "access/genam.h"
  15. #include "access/generic_xlog.h"
  16. #include "catalog/index.h"
  17. #include "miscadmin.h"
  18. #include "storage/bufmgr.h"
  19. #include "storage/indexfsm.h"
  20. #include "storage/smgr.h"
  21. #include "utils/memutils.h"
  22. #include "utils/rel.h"
  23. #include "bloom.h"
  24. PG_MODULE_MAGIC;
  25. /*
  26. * State of bloom index build. We accumulate one page data here before
  27. * flushing it to buffer manager.
  28. */
  29. typedef struct
  30. {
  31. BloomState blstate; /* bloom index state */
  32. int64 indtuples; /* total number of tuples indexed */
  33. MemoryContext tmpCtx; /* temporary memory context reset after each
  34. * tuple */
  35. PGAlignedBlock data; /* cached page */
  36. int count; /* number of tuples in cached page */
  37. } BloomBuildState;
  38. /*
  39. * Flush page cached in BloomBuildState.
  40. */
  41. static void
  42. flushCachedPage(Relation index, BloomBuildState *buildstate)
  43. {
  44. Page page;
  45. Buffer buffer = BloomNewBuffer(index);
  46. GenericXLogState *state;
  47. state = GenericXLogStart(index);
  48. page = GenericXLogRegisterBuffer(state, buffer, GENERIC_XLOG_FULL_IMAGE);
  49. memcpy(page, buildstate->data.data, BLCKSZ);
  50. GenericXLogFinish(state);
  51. UnlockReleaseBuffer(buffer);
  52. }
  53. /*
  54. * (Re)initialize cached page in BloomBuildState.
  55. */
  56. static void
  57. initCachedPage(BloomBuildState *buildstate)
  58. {
  59. memset(buildstate->data.data, 0, BLCKSZ);
  60. BloomInitPage(buildstate->data.data, 0);
  61. buildstate->count = 0;
  62. }
  63. /*
  64. * Per-tuple callback from IndexBuildHeapScan.
  65. */
  66. static void
  67. bloomBuildCallback(Relation index, HeapTuple htup, Datum *values,
  68. bool *isnull, bool tupleIsAlive, void *state)
  69. {
  70. BloomBuildState *buildstate = (BloomBuildState *) state;
  71. MemoryContext oldCtx;
  72. BloomTuple *itup;
  73. oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
  74. itup = BloomFormTuple(&buildstate->blstate, &htup->t_self, values, isnull);
  75. /* Try to add next item to cached page */
  76. if (BloomPageAddItem(&buildstate->blstate, buildstate->data.data, itup))
  77. {
  78. /* Next item was added successfully */
  79. buildstate->count++;
  80. }
  81. else
  82. {
  83. /* Cached page is full, flush it out and make a new one */
  84. flushCachedPage(index, buildstate);
  85. CHECK_FOR_INTERRUPTS();
  86. initCachedPage(buildstate);
  87. if (!BloomPageAddItem(&buildstate->blstate, buildstate->data.data, itup))
  88. {
  89. /* We shouldn't be here since we're inserting to the empty page */
  90. elog(ERROR, "could not add new bloom tuple to empty page");
  91. }
  92. /* Next item was added successfully */
  93. buildstate->count++;
  94. }
  95. /* Update total tuple count */
  96. buildstate->indtuples += 1;
  97. MemoryContextSwitchTo(oldCtx);
  98. MemoryContextReset(buildstate->tmpCtx);
  99. }
  100. /*
  101. * Build a new bloom index.
  102. */
  103. IndexBuildResult *
  104. blbuild(Relation heap, Relation index, IndexInfo *indexInfo)
  105. {
  106. IndexBuildResult *result;
  107. double reltuples;
  108. BloomBuildState buildstate;
  109. if (RelationGetNumberOfBlocks(index) != 0)
  110. elog(ERROR, "index \"%s\" already contains data",
  111. RelationGetRelationName(index));
  112. /* Initialize the meta page */
  113. BloomInitMetapage(index);
  114. /* Initialize the bloom build state */
  115. memset(&buildstate, 0, sizeof(buildstate));
  116. initBloomState(&buildstate.blstate, index);
  117. buildstate.tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
  118. "Bloom build temporary context",
  119. ALLOCSET_DEFAULT_SIZES);
  120. initCachedPage(&buildstate);
  121. /* Do the heap scan */
  122. reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
  123. bloomBuildCallback, (void *) &buildstate,
  124. NULL);
  125. /* Flush last page if needed (it will be, unless heap was empty) */
  126. if (buildstate.count > 0)
  127. flushCachedPage(index, &buildstate);
  128. MemoryContextDelete(buildstate.tmpCtx);
  129. result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
  130. result->heap_tuples = reltuples;
  131. result->index_tuples = buildstate.indtuples;
  132. return result;
  133. }
  134. /*
  135. * Build an empty bloom index in the initialization fork.
  136. */
  137. void
  138. blbuildempty(Relation index)
  139. {
  140. Page metapage;
  141. /* Construct metapage. */
  142. metapage = (Page) palloc(BLCKSZ);
  143. BloomFillMetapage(index, metapage);
  144. /*
  145. * Write the page and log it. It might seem that an immediate sync would
  146. * be sufficient to guarantee that the file exists on disk, but recovery
  147. * itself might remove it while replaying, for example, an
  148. * XLOG_DBASE_CREATE or XLOG_TBLSPC_CREATE record. Therefore, we need
  149. * this even when wal_level=minimal.
  150. */
  151. PageSetChecksumInplace(metapage, BLOOM_METAPAGE_BLKNO);
  152. smgrwrite(index->rd_smgr, INIT_FORKNUM, BLOOM_METAPAGE_BLKNO,
  153. (char *) metapage, true);
  154. log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
  155. BLOOM_METAPAGE_BLKNO, metapage, true);
  156. /*
  157. * An immediate sync is required even if we xlog'd the page, because the
  158. * write did not go through shared_buffers and therefore a concurrent
  159. * checkpoint may have moved the redo pointer past our xlog record.
  160. */
  161. smgrimmedsync(index->rd_smgr, INIT_FORKNUM);
  162. }
  163. /*
  164. * Insert new tuple to the bloom index.
  165. */
  166. bool
  167. blinsert(Relation index, Datum *values, bool *isnull,
  168. ItemPointer ht_ctid, Relation heapRel,
  169. IndexUniqueCheck checkUnique,
  170. IndexInfo *indexInfo)
  171. {
  172. BloomState blstate;
  173. BloomTuple *itup;
  174. MemoryContext oldCtx;
  175. MemoryContext insertCtx;
  176. BloomMetaPageData *metaData;
  177. Buffer buffer,
  178. metaBuffer;
  179. Page page,
  180. metaPage;
  181. BlockNumber blkno = InvalidBlockNumber;
  182. OffsetNumber nStart;
  183. GenericXLogState *state;
  184. insertCtx = AllocSetContextCreate(CurrentMemoryContext,
  185. "Bloom insert temporary context",
  186. ALLOCSET_DEFAULT_SIZES);
  187. oldCtx = MemoryContextSwitchTo(insertCtx);
  188. initBloomState(&blstate, index);
  189. itup = BloomFormTuple(&blstate, ht_ctid, values, isnull);
  190. /*
  191. * At first, try to insert new tuple to the first page in notFullPage
  192. * array. If successful, we don't need to modify the meta page.
  193. */
  194. metaBuffer = ReadBuffer(index, BLOOM_METAPAGE_BLKNO);
  195. LockBuffer(metaBuffer, BUFFER_LOCK_SHARE);
  196. metaData = BloomPageGetMeta(BufferGetPage(metaBuffer));
  197. if (metaData->nEnd > metaData->nStart)
  198. {
  199. Page page;
  200. blkno = metaData->notFullPage[metaData->nStart];
  201. Assert(blkno != InvalidBlockNumber);
  202. /* Don't hold metabuffer lock while doing insert */
  203. LockBuffer(metaBuffer, BUFFER_LOCK_UNLOCK);
  204. buffer = ReadBuffer(index, blkno);
  205. LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
  206. state = GenericXLogStart(index);
  207. page = GenericXLogRegisterBuffer(state, buffer, 0);
  208. /*
  209. * We might have found a page that was recently deleted by VACUUM. If
  210. * so, we can reuse it, but we must reinitialize it.
  211. */
  212. if (PageIsNew(page) || BloomPageIsDeleted(page))
  213. BloomInitPage(page, 0);
  214. if (BloomPageAddItem(&blstate, page, itup))
  215. {
  216. /* Success! Apply the change, clean up, and exit */
  217. GenericXLogFinish(state);
  218. UnlockReleaseBuffer(buffer);
  219. ReleaseBuffer(metaBuffer);
  220. MemoryContextSwitchTo(oldCtx);
  221. MemoryContextDelete(insertCtx);
  222. return false;
  223. }
  224. /* Didn't fit, must try other pages */
  225. GenericXLogAbort(state);
  226. UnlockReleaseBuffer(buffer);
  227. }
  228. else
  229. {
  230. /* No entries in notFullPage */
  231. LockBuffer(metaBuffer, BUFFER_LOCK_UNLOCK);
  232. }
  233. /*
  234. * Try other pages in notFullPage array. We will have to change nStart in
  235. * metapage. Thus, grab exclusive lock on metapage.
  236. */
  237. LockBuffer(metaBuffer, BUFFER_LOCK_EXCLUSIVE);
  238. /* nStart might have changed while we didn't have lock */
  239. nStart = metaData->nStart;
  240. /* Skip first page if we already tried it above */
  241. if (nStart < metaData->nEnd &&
  242. blkno == metaData->notFullPage[nStart])
  243. nStart++;
  244. /*
  245. * This loop iterates for each page we try from the notFullPage array, and
  246. * will also initialize a GenericXLogState for the fallback case of having
  247. * to allocate a new page.
  248. */
  249. for (;;)
  250. {
  251. state = GenericXLogStart(index);
  252. /* get modifiable copy of metapage */
  253. metaPage = GenericXLogRegisterBuffer(state, metaBuffer, 0);
  254. metaData = BloomPageGetMeta(metaPage);
  255. if (nStart >= metaData->nEnd)
  256. break; /* no more entries in notFullPage array */
  257. blkno = metaData->notFullPage[nStart];
  258. Assert(blkno != InvalidBlockNumber);
  259. buffer = ReadBuffer(index, blkno);
  260. LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
  261. page = GenericXLogRegisterBuffer(state, buffer, 0);
  262. /* Basically same logic as above */
  263. if (PageIsNew(page) || BloomPageIsDeleted(page))
  264. BloomInitPage(page, 0);
  265. if (BloomPageAddItem(&blstate, page, itup))
  266. {
  267. /* Success! Apply the changes, clean up, and exit */
  268. metaData->nStart = nStart;
  269. GenericXLogFinish(state);
  270. UnlockReleaseBuffer(buffer);
  271. UnlockReleaseBuffer(metaBuffer);
  272. MemoryContextSwitchTo(oldCtx);
  273. MemoryContextDelete(insertCtx);
  274. return false;
  275. }
  276. /* Didn't fit, must try other pages */
  277. GenericXLogAbort(state);
  278. UnlockReleaseBuffer(buffer);
  279. nStart++;
  280. }
  281. /*
  282. * Didn't find place to insert in notFullPage array. Allocate new page.
  283. * (XXX is it good to do this while holding ex-lock on the metapage??)
  284. */
  285. buffer = BloomNewBuffer(index);
  286. page = GenericXLogRegisterBuffer(state, buffer, GENERIC_XLOG_FULL_IMAGE);
  287. BloomInitPage(page, 0);
  288. if (!BloomPageAddItem(&blstate, page, itup))
  289. {
  290. /* We shouldn't be here since we're inserting to an empty page */
  291. elog(ERROR, "could not add new bloom tuple to empty page");
  292. }
  293. /* Reset notFullPage array to contain just this new page */
  294. metaData->nStart = 0;
  295. metaData->nEnd = 1;
  296. metaData->notFullPage[0] = BufferGetBlockNumber(buffer);
  297. /* Apply the changes, clean up, and exit */
  298. GenericXLogFinish(state);
  299. UnlockReleaseBuffer(buffer);
  300. UnlockReleaseBuffer(metaBuffer);
  301. MemoryContextSwitchTo(oldCtx);
  302. MemoryContextDelete(insertCtx);
  303. return false;
  304. }