PostgreSQL Source Code  git master
blinsert.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/generic_xlog.h"
#include "access/tableam.h"
#include "bloom.h"
#include "catalog/index.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/indexfsm.h"
#include "storage/smgr.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for blinsert.c:

Go to the source code of this file.

Data Structures

struct  BloomBuildState
 

Functions

static void flushCachedPage (Relation index, BloomBuildState *buildstate)
 
static void initCachedPage (BloomBuildState *buildstate)
 
static void bloomBuildCallback (Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
 
IndexBuildResultblbuild (Relation heap, Relation index, IndexInfo *indexInfo)
 
void blbuildempty (Relation index)
 
bool blinsert (Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ blbuild()

IndexBuildResult* blbuild ( Relation  heap,
Relation  index,
IndexInfo indexInfo 
)

Definition at line 121 of file blinsert.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, bloomBuildCallback(), BloomInitMetapage(), BloomBuildState::blstate, CurrentMemoryContext, elog, ERROR, flushCachedPage(), IndexBuildResult::heap_tuples, IndexBuildResult::index_tuples, initBloomState(), initCachedPage(), MemoryContextDelete(), palloc(), RelationGetNumberOfBlocks, RelationGetRelationName, table_index_build_scan(), and BloomBuildState::tmpCtx.

Referenced by blhandler().

122 {
123  IndexBuildResult *result;
124  double reltuples;
125  BloomBuildState buildstate;
126 
127  if (RelationGetNumberOfBlocks(index) != 0)
128  elog(ERROR, "index \"%s\" already contains data",
129  RelationGetRelationName(index));
130 
131  /* Initialize the meta page */
132  BloomInitMetapage(index);
133 
134  /* Initialize the bloom build state */
135  memset(&buildstate, 0, sizeof(buildstate));
136  initBloomState(&buildstate.blstate, index);
138  "Bloom build temporary context",
140  initCachedPage(&buildstate);
141 
142  /* Do the heap scan */
143  reltuples = table_index_build_scan(heap, index, indexInfo, true, true,
144  bloomBuildCallback, (void *) &buildstate,
145  NULL);
146 
147  /* Flush last page if needed (it will be, unless heap was empty) */
148  if (buildstate.count > 0)
149  flushCachedPage(index, &buildstate);
150 
151  MemoryContextDelete(buildstate.tmpCtx);
152 
153  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
154  result->heap_tuples = reltuples;
155  result->index_tuples = buildstate.indtuples;
156 
157  return result;
158 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
void initBloomState(BloomState *state, Relation index)
Definition: blutils.c:161
#define ERROR
Definition: elog.h:46
static void flushCachedPage(Relation index, BloomBuildState *buildstate)
Definition: blinsert.c:47
static double table_index_build_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1745
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
MemoryContext tmpCtx
Definition: blinsert.c:37
#define RelationGetRelationName(relation)
Definition: rel.h:511
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:213
static void initCachedPage(BloomBuildState *buildstate)
Definition: blinsert.c:64
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
BloomState blstate
Definition: blinsert.c:35
int64 indtuples
Definition: blinsert.c:36
static void bloomBuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: blinsert.c:74
double index_tuples
Definition: genam.h:33
double heap_tuples
Definition: genam.h:32
void BloomInitMetapage(Relation index)
Definition: blutils.c:454

◆ blbuildempty()

void blbuildempty ( Relation  index)

Definition at line 164 of file blinsert.c.

References BLOOM_METAPAGE_BLKNO, BloomFillMetapage(), INIT_FORKNUM, log_newpage(), PageSetChecksumInplace(), palloc(), RelationGetSmgr(), smgrimmedsync(), and smgrwrite().

Referenced by blhandler().

165 {
166  Page metapage;
167 
168  /* Construct metapage. */
169  metapage = (Page) palloc(BLCKSZ);
170  BloomFillMetapage(index, metapage);
171 
172  /*
173  * Write the page and log it. It might seem that an immediate sync would
174  * be sufficient to guarantee that the file exists on disk, but recovery
175  * itself might remove it while replaying, for example, an
176  * XLOG_DBASE_CREATE or XLOG_TBLSPC_CREATE record. Therefore, we need
177  * this even when wal_level=minimal.
178  */
181  (char *) metapage, true);
182  log_newpage(&(RelationGetSmgr(index))->smgr_rnode.node, INIT_FORKNUM,
183  BLOOM_METAPAGE_BLKNO, metapage, true);
184 
185  /*
186  * An immediate sync is required even if we xlog'd the page, because the
187  * write did not go through shared_buffers and therefore a concurrent
188  * checkpoint may have moved the redo pointer past our xlog record.
189  */
191 }
#define BLOOM_METAPAGE_BLKNO
Definition: bloom.h:78
void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:523
void BloomFillMetapage(Relation index, Page metaPage)
Definition: blutils.c:422
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:544
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1532
void * palloc(Size size)
Definition: mcxt.c:1062
XLogRecPtr log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:1048
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:660
Pointer Page
Definition: bufpage.h:78

◆ blinsert()

bool blinsert ( Relation  index,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
bool  indexUnchanged,
IndexInfo indexInfo 
)

Definition at line 197 of file blinsert.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, BLOOM_METAPAGE_BLKNO, BloomFormTuple(), BloomInitPage(), BloomNewBuffer(), BloomPageAddItem(), BloomPageGetMeta, BloomPageIsDeleted, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, CurrentMemoryContext, elog, ERROR, GENERIC_XLOG_FULL_IMAGE, GenericXLogAbort(), GenericXLogFinish(), GenericXLogRegisterBuffer(), GenericXLogStart(), initBloomState(), InvalidBlockNumber, LockBuffer(), MemoryContextDelete(), MemoryContextSwitchTo(), BloomMetaPageData::nEnd, BloomMetaPageData::notFullPage, BloomMetaPageData::nStart, PageIsNew, ReadBuffer(), ReleaseBuffer(), and UnlockReleaseBuffer().

Referenced by blhandler().

202 {
203  BloomState blstate;
204  BloomTuple *itup;
205  MemoryContext oldCtx;
206  MemoryContext insertCtx;
207  BloomMetaPageData *metaData;
208  Buffer buffer,
209  metaBuffer;
210  Page page,
211  metaPage;
213  OffsetNumber nStart;
215 
217  "Bloom insert temporary context",
219 
220  oldCtx = MemoryContextSwitchTo(insertCtx);
221 
222  initBloomState(&blstate, index);
223  itup = BloomFormTuple(&blstate, ht_ctid, values, isnull);
224 
225  /*
226  * At first, try to insert new tuple to the first page in notFullPage
227  * array. If successful, we don't need to modify the meta page.
228  */
229  metaBuffer = ReadBuffer(index, BLOOM_METAPAGE_BLKNO);
230  LockBuffer(metaBuffer, BUFFER_LOCK_SHARE);
231  metaData = BloomPageGetMeta(BufferGetPage(metaBuffer));
232 
233  if (metaData->nEnd > metaData->nStart)
234  {
235  Page page;
236 
237  blkno = metaData->notFullPage[metaData->nStart];
238  Assert(blkno != InvalidBlockNumber);
239 
240  /* Don't hold metabuffer lock while doing insert */
241  LockBuffer(metaBuffer, BUFFER_LOCK_UNLOCK);
242 
243  buffer = ReadBuffer(index, blkno);
245 
246  state = GenericXLogStart(index);
247  page = GenericXLogRegisterBuffer(state, buffer, 0);
248 
249  /*
250  * We might have found a page that was recently deleted by VACUUM. If
251  * so, we can reuse it, but we must reinitialize it.
252  */
253  if (PageIsNew(page) || BloomPageIsDeleted(page))
254  BloomInitPage(page, 0);
255 
256  if (BloomPageAddItem(&blstate, page, itup))
257  {
258  /* Success! Apply the change, clean up, and exit */
259  GenericXLogFinish(state);
260  UnlockReleaseBuffer(buffer);
261  ReleaseBuffer(metaBuffer);
262  MemoryContextSwitchTo(oldCtx);
263  MemoryContextDelete(insertCtx);
264  return false;
265  }
266 
267  /* Didn't fit, must try other pages */
268  GenericXLogAbort(state);
269  UnlockReleaseBuffer(buffer);
270  }
271  else
272  {
273  /* No entries in notFullPage */
274  LockBuffer(metaBuffer, BUFFER_LOCK_UNLOCK);
275  }
276 
277  /*
278  * Try other pages in notFullPage array. We will have to change nStart in
279  * metapage. Thus, grab exclusive lock on metapage.
280  */
281  LockBuffer(metaBuffer, BUFFER_LOCK_EXCLUSIVE);
282 
283  /* nStart might have changed while we didn't have lock */
284  nStart = metaData->nStart;
285 
286  /* Skip first page if we already tried it above */
287  if (nStart < metaData->nEnd &&
288  blkno == metaData->notFullPage[nStart])
289  nStart++;
290 
291  /*
292  * This loop iterates for each page we try from the notFullPage array, and
293  * will also initialize a GenericXLogState for the fallback case of having
294  * to allocate a new page.
295  */
296  for (;;)
297  {
298  state = GenericXLogStart(index);
299 
300  /* get modifiable copy of metapage */
301  metaPage = GenericXLogRegisterBuffer(state, metaBuffer, 0);
302  metaData = BloomPageGetMeta(metaPage);
303 
304  if (nStart >= metaData->nEnd)
305  break; /* no more entries in notFullPage array */
306 
307  blkno = metaData->notFullPage[nStart];
308  Assert(blkno != InvalidBlockNumber);
309 
310  buffer = ReadBuffer(index, blkno);
312  page = GenericXLogRegisterBuffer(state, buffer, 0);
313 
314  /* Basically same logic as above */
315  if (PageIsNew(page) || BloomPageIsDeleted(page))
316  BloomInitPage(page, 0);
317 
318  if (BloomPageAddItem(&blstate, page, itup))
319  {
320  /* Success! Apply the changes, clean up, and exit */
321  metaData->nStart = nStart;
322  GenericXLogFinish(state);
323  UnlockReleaseBuffer(buffer);
324  UnlockReleaseBuffer(metaBuffer);
325  MemoryContextSwitchTo(oldCtx);
326  MemoryContextDelete(insertCtx);
327  return false;
328  }
329 
330  /* Didn't fit, must try other pages */
331  GenericXLogAbort(state);
332  UnlockReleaseBuffer(buffer);
333  nStart++;
334  }
335 
336  /*
337  * Didn't find place to insert in notFullPage array. Allocate new page.
338  * (XXX is it good to do this while holding ex-lock on the metapage??)
339  */
340  buffer = BloomNewBuffer(index);
341 
342  page = GenericXLogRegisterBuffer(state, buffer, GENERIC_XLOG_FULL_IMAGE);
343  BloomInitPage(page, 0);
344 
345  if (!BloomPageAddItem(&blstate, page, itup))
346  {
347  /* We shouldn't be here since we're inserting to an empty page */
348  elog(ERROR, "could not add new bloom tuple to empty page");
349  }
350 
351  /* Reset notFullPage array to contain just this new page */
352  metaData->nStart = 0;
353  metaData->nEnd = 1;
354  metaData->notFullPage[0] = BufferGetBlockNumber(buffer);
355 
356  /* Apply the changes, clean up, and exit */
357  GenericXLogFinish(state);
358 
359  UnlockReleaseBuffer(buffer);
360  UnlockReleaseBuffer(metaBuffer);
361 
362  MemoryContextSwitchTo(oldCtx);
363  MemoryContextDelete(insertCtx);
364 
365  return false;
366 }
#define BLOOM_METAPAGE_BLKNO
Definition: bloom.h:78
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define BloomPageIsDeleted(page)
Definition: bloom.h:64
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
void initBloomState(BloomState *state, Relation index)
Definition: blutils.c:161
uint16 nEnd
Definition: bloom.h:125
Buffer BloomNewBuffer(Relation index)
Definition: blutils.c:353
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3772
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
Page GenericXLogRegisterBuffer(GenericXLogState *state, Buffer buffer, int flags)
Definition: generic_xlog.c:295
uint16 OffsetNumber
Definition: off.h:24
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3795
#define ERROR
Definition: elog.h:46
void BloomInitPage(Page page, uint16 flags)
Definition: blutils.c:407
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
bool BloomPageAddItem(BloomState *state, Page page, BloomTuple *tuple)
Definition: blutils.c:318
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
FreeBlockNumberArray notFullPage
Definition: bloom.h:127
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define BloomPageGetMeta(page)
Definition: bloom.h:136
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4011
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
BloomTuple * BloomFormTuple(BloomState *state, ItemPointer iptr, Datum *values, bool *isnull)
Definition: blutils.c:293
XLogRecPtr GenericXLogFinish(GenericXLogState *state)
Definition: generic_xlog.c:333
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:694
#define InvalidBlockNumber
Definition: block.h:33
uint16 nStart
Definition: bloom.h:124
static Datum values[MAXATTR]
Definition: bootstrap.c:166
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2752
#define PageIsNew(page)
Definition: bufpage.h:229
#define elog(elevel,...)
Definition: elog.h:232
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
void GenericXLogAbort(GenericXLogState *state)
Definition: generic_xlog.c:444
int Buffer
Definition: buf.h:23
#define GENERIC_XLOG_FULL_IMAGE
Definition: generic_xlog.h:26
GenericXLogState * GenericXLogStart(Relation relation)
Definition: generic_xlog.c:267
Pointer Page
Definition: bufpage.h:78

◆ bloomBuildCallback()

static void bloomBuildCallback ( Relation  index,
ItemPointer  tid,
Datum values,
bool isnull,
bool  tupleIsAlive,
void *  state 
)
static

Definition at line 74 of file blinsert.c.

References BloomFormTuple(), BloomPageAddItem(), BloomBuildState::blstate, CHECK_FOR_INTERRUPTS, BloomBuildState::count, BloomBuildState::data, PGAlignedBlock::data, elog, ERROR, flushCachedPage(), BloomBuildState::indtuples, initCachedPage(), MemoryContextReset(), MemoryContextSwitchTo(), and BloomBuildState::tmpCtx.

Referenced by blbuild().

76 {
77  BloomBuildState *buildstate = (BloomBuildState *) state;
78  MemoryContext oldCtx;
79  BloomTuple *itup;
80 
81  oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
82 
83  itup = BloomFormTuple(&buildstate->blstate, tid, values, isnull);
84 
85  /* Try to add next item to cached page */
86  if (BloomPageAddItem(&buildstate->blstate, buildstate->data.data, itup))
87  {
88  /* Next item was added successfully */
89  buildstate->count++;
90  }
91  else
92  {
93  /* Cached page is full, flush it out and make a new one */
94  flushCachedPage(index, buildstate);
95 
97 
98  initCachedPage(buildstate);
99 
100  if (!BloomPageAddItem(&buildstate->blstate, buildstate->data.data, itup))
101  {
102  /* We shouldn't be here since we're inserting to the empty page */
103  elog(ERROR, "could not add new bloom tuple to empty page");
104  }
105 
106  /* Next item was added successfully */
107  buildstate->count++;
108  }
109 
110  /* Update total tuple count */
111  buildstate->indtuples += 1;
112 
113  MemoryContextSwitchTo(oldCtx);
114  MemoryContextReset(buildstate->tmpCtx);
115 }
PGAlignedBlock data
Definition: blinsert.c:39
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:143
char data[BLCKSZ]
Definition: c.h:1141
#define ERROR
Definition: elog.h:46
static void flushCachedPage(Relation index, BloomBuildState *buildstate)
Definition: blinsert.c:47
MemoryContext tmpCtx
Definition: blinsert.c:37
bool BloomPageAddItem(BloomState *state, Page page, BloomTuple *tuple)
Definition: blutils.c:318
static void initCachedPage(BloomBuildState *buildstate)
Definition: blinsert.c:64
Definition: regguts.h:317
BloomTuple * BloomFormTuple(BloomState *state, ItemPointer iptr, Datum *values, bool *isnull)
Definition: blutils.c:293
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
BloomState blstate
Definition: blinsert.c:35
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
int64 indtuples
Definition: blinsert.c:36

◆ flushCachedPage()

static void flushCachedPage ( Relation  index,
BloomBuildState buildstate 
)
static

Definition at line 47 of file blinsert.c.

References BloomNewBuffer(), BloomBuildState::data, PGAlignedBlock::data, GENERIC_XLOG_FULL_IMAGE, GenericXLogFinish(), GenericXLogRegisterBuffer(), GenericXLogStart(), and UnlockReleaseBuffer().

Referenced by blbuild(), and bloomBuildCallback().

48 {
49  Page page;
50  Buffer buffer = BloomNewBuffer(index);
52 
53  state = GenericXLogStart(index);
55  memcpy(page, buildstate->data.data, BLCKSZ);
56  GenericXLogFinish(state);
57  UnlockReleaseBuffer(buffer);
58 }
PGAlignedBlock data
Definition: blinsert.c:39
Buffer BloomNewBuffer(Relation index)
Definition: blutils.c:353
Page GenericXLogRegisterBuffer(GenericXLogState *state, Buffer buffer, int flags)
Definition: generic_xlog.c:295
char data[BLCKSZ]
Definition: c.h:1141
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3795
Definition: regguts.h:317
XLogRecPtr GenericXLogFinish(GenericXLogState *state)
Definition: generic_xlog.c:333
int Buffer
Definition: buf.h:23
#define GENERIC_XLOG_FULL_IMAGE
Definition: generic_xlog.h:26
GenericXLogState * GenericXLogStart(Relation relation)
Definition: generic_xlog.c:267
Pointer Page
Definition: bufpage.h:78

◆ initCachedPage()

static void initCachedPage ( BloomBuildState buildstate)
static

Definition at line 64 of file blinsert.c.

References BloomInitPage(), BloomBuildState::count, BloomBuildState::data, and PGAlignedBlock::data.

Referenced by blbuild(), and bloomBuildCallback().

65 {
66  BloomInitPage(buildstate->data.data, 0);
67  buildstate->count = 0;
68 }
PGAlignedBlock data
Definition: blinsert.c:39
char data[BLCKSZ]
Definition: c.h:1141
void BloomInitPage(Page page, uint16 flags)
Definition: blutils.c:407

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 27 of file blinsert.c.