PostgreSQL Source Code  git master
gistbuildbuffers.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/gist_private.h"
#include "catalog/index.h"
#include "miscadmin.h"
#include "storage/buffile.h"
#include "storage/bufmgr.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for gistbuildbuffers.c:

Go to the source code of this file.

Data Structures

struct  RelocationBufferInfo
 

Functions

static GISTNodeBufferPagegistAllocateNewPageBuffer (GISTBuildBuffers *gfbb)
 
static void gistAddLoadedBuffer (GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
 
static void gistLoadNodeBuffer (GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
 
static void gistUnloadNodeBuffer (GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
 
static void gistPlaceItupToPage (GISTNodeBufferPage *pageBuffer, IndexTuple item)
 
static void gistGetItupFromPage (GISTNodeBufferPage *pageBuffer, IndexTuple *item)
 
static long gistBuffersGetFreeBlock (GISTBuildBuffers *gfbb)
 
static void gistBuffersReleaseBlock (GISTBuildBuffers *gfbb, long blocknum)
 
static void ReadTempFileBlock (BufFile *file, long blknum, void *ptr)
 
static void WriteTempFileBlock (BufFile *file, long blknum, void *ptr)
 
GISTBuildBuffersgistInitBuildBuffers (int pagesPerBuffer, int levelStep, int maxLevel)
 
GISTNodeBuffergistGetNodeBuffer (GISTBuildBuffers *gfbb, GISTSTATE *giststate, BlockNumber nodeBlocknum, int level)
 
void gistUnloadNodeBuffers (GISTBuildBuffers *gfbb)
 
void gistPushItupToNodeBuffer (GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer, IndexTuple itup)
 
bool gistPopItupFromNodeBuffer (GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer, IndexTuple *itup)
 
void gistFreeBuildBuffers (GISTBuildBuffers *gfbb)
 
void gistRelocateBuildBuffersOnSplit (GISTBuildBuffers *gfbb, GISTSTATE *giststate, Relation r, int level, Buffer buffer, List *splitinfo)
 

Function Documentation

◆ gistAddLoadedBuffer()

static void gistAddLoadedBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer 
)
static

Definition at line 203 of file gistbuildbuffers.c.

References GISTNodeBuffer::isTemp, GISTBuildBuffers::loadedBuffers, GISTBuildBuffers::loadedBuffersCount, GISTBuildBuffers::loadedBuffersLen, and repalloc().

Referenced by gistLoadNodeBuffer(), and gistPushItupToNodeBuffer().

204 {
205  /* Never add a temporary buffer to the array */
206  if (nodeBuffer->isTemp)
207  return;
208 
209  /* Enlarge the array if needed */
210  if (gfbb->loadedBuffersCount >= gfbb->loadedBuffersLen)
211  {
212  gfbb->loadedBuffersLen *= 2;
213  gfbb->loadedBuffers = (GISTNodeBuffer **)
214  repalloc(gfbb->loadedBuffers,
215  gfbb->loadedBuffersLen * sizeof(GISTNodeBuffer *));
216  }
217 
218  gfbb->loadedBuffers[gfbb->loadedBuffersCount] = nodeBuffer;
219  gfbb->loadedBuffersCount++;
220 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
GISTNodeBuffer ** loadedBuffers
Definition: gist_private.h:372

◆ gistAllocateNewPageBuffer()

static GISTNodeBufferPage * gistAllocateNewPageBuffer ( GISTBuildBuffers gfbb)
static

Definition at line 186 of file gistbuildbuffers.c.

References BUFFER_PAGE_DATA_OFFSET, GISTBuildBuffers::context, InvalidBlockNumber, MemoryContextAllocZero(), PAGE_FREE_SPACE, and GISTNodeBufferPage::prev.

Referenced by gistLoadNodeBuffer(), and gistPushItupToNodeBuffer().

187 {
188  GISTNodeBufferPage *pageBuffer;
189 
190  pageBuffer = (GISTNodeBufferPage *) MemoryContextAllocZero(gfbb->context,
191  BLCKSZ);
192  pageBuffer->prev = InvalidBlockNumber;
193 
194  /* Set page free space */
195  PAGE_FREE_SPACE(pageBuffer) = BLCKSZ - BUFFER_PAGE_DATA_OFFSET;
196  return pageBuffer;
197 }
#define PAGE_FREE_SPACE(nbp)
Definition: gist_private.h:55
MemoryContext context
Definition: gist_private.h:338
#define BUFFER_PAGE_DATA_OFFSET
Definition: gist_private.h:53
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:814
BlockNumber prev
Definition: gist_private.h:48
#define InvalidBlockNumber
Definition: block.h:33

◆ gistBuffersGetFreeBlock()

static long gistBuffersGetFreeBlock ( GISTBuildBuffers gfbb)
static

Definition at line 473 of file gistbuildbuffers.c.

References GISTBuildBuffers::freeBlocks, GISTBuildBuffers::nFileBlocks, and GISTBuildBuffers::nFreeBlocks.

Referenced by gistPushItupToNodeBuffer(), and gistUnloadNodeBuffer().

474 {
475  /*
476  * If there are multiple free blocks, we select the one appearing last in
477  * freeBlocks[]. If there are none, assign the next block at the end of
478  * the file (causing the file to be extended).
479  */
480  if (gfbb->nFreeBlocks > 0)
481  return gfbb->freeBlocks[--gfbb->nFreeBlocks];
482  else
483  return gfbb->nFileBlocks++;
484 }

◆ gistBuffersReleaseBlock()

static void gistBuffersReleaseBlock ( GISTBuildBuffers gfbb,
long  blocknum 
)
static

Definition at line 490 of file gistbuildbuffers.c.

References GISTBuildBuffers::freeBlocks, GISTBuildBuffers::freeBlocksLen, GISTBuildBuffers::nFreeBlocks, and repalloc().

Referenced by gistLoadNodeBuffer(), and gistPopItupFromNodeBuffer().

491 {
492  int ndx;
493 
494  /* Enlarge freeBlocks array if full. */
495  if (gfbb->nFreeBlocks >= gfbb->freeBlocksLen)
496  {
497  gfbb->freeBlocksLen *= 2;
498  gfbb->freeBlocks = (long *) repalloc(gfbb->freeBlocks,
499  gfbb->freeBlocksLen *
500  sizeof(long));
501  }
502 
503  /* Add blocknum to array */
504  ndx = gfbb->nFreeBlocks++;
505  gfbb->freeBlocks[ndx] = blocknum;
506 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044

◆ gistFreeBuildBuffers()

void gistFreeBuildBuffers ( GISTBuildBuffers gfbb)

Definition at line 512 of file gistbuildbuffers.c.

References BufFileClose(), and GISTBuildBuffers::pfile.

Referenced by gistbuild().

513 {
514  /* Close buffers file. */
515  BufFileClose(gfbb->pfile);
516 
517  /* All other things will be freed on memory context release */
518 }
void BufFileClose(BufFile *file)
Definition: buffile.c:391

◆ gistGetItupFromPage()

static void gistGetItupFromPage ( GISTNodeBufferPage pageBuffer,
IndexTuple item 
)
static

Definition at line 316 of file gistbuildbuffers.c.

References Assert, BUFFER_PAGE_DATA_OFFSET, IndexTupleSize, MAXALIGN, PAGE_FREE_SPACE, PAGE_IS_EMPTY, and palloc().

Referenced by gistPopItupFromNodeBuffer().

317 {
318  IndexTuple ptr;
319  Size itupsz;
320 
321  Assert(!PAGE_IS_EMPTY(pageBuffer)); /* Page shouldn't be empty */
322 
323  /* Get pointer to last index tuple */
324  ptr = (IndexTuple) ((char *) pageBuffer
326  + PAGE_FREE_SPACE(pageBuffer));
327  itupsz = IndexTupleSize(ptr);
328 
329  /* Make a copy of the tuple */
330  *itup = (IndexTuple) palloc(itupsz);
331  memcpy(*itup, ptr, itupsz);
332 
333  /* Mark the space used by the tuple as free */
334  PAGE_FREE_SPACE(pageBuffer) += MAXALIGN(itupsz);
335 }
#define PAGE_FREE_SPACE(nbp)
Definition: gist_private.h:55
#define BUFFER_PAGE_DATA_OFFSET
Definition: gist_private.h:53
IndexTupleData * IndexTuple
Definition: itup.h:53
#define Assert(condition)
Definition: c.h:732
size_t Size
Definition: c.h:466
#define PAGE_IS_EMPTY(nbp)
Definition: gist_private.h:57
#define MAXALIGN(LEN)
Definition: c.h:685
void * palloc(Size size)
Definition: mcxt.c:924
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ gistGetNodeBuffer()

GISTNodeBuffer* gistGetNodeBuffer ( GISTBuildBuffers gfbb,
GISTSTATE giststate,
BlockNumber  nodeBlocknum,
int  level 
)

Definition at line 118 of file gistbuildbuffers.c.

References GISTNodeBuffer::blocksCount, GISTBuildBuffers::buffersOnLevels, GISTBuildBuffers::buffersOnLevelsLen, GISTBuildBuffers::context, HASH_ENTER, hash_search(), i, InvalidBlockNumber, GISTNodeBuffer::isTemp, lcons(), GISTNodeBuffer::level, MemoryContextSwitchTo(), NIL, GISTBuildBuffers::nodeBuffersTab, GISTNodeBuffer::pageBlocknum, GISTNodeBuffer::pageBuffer, GISTNodeBuffer::queuedForEmptying, and repalloc().

Referenced by gistProcessItup(), and gistRelocateBuildBuffersOnSplit().

120 {
121  GISTNodeBuffer *nodeBuffer;
122  bool found;
123 
124  /* Find node buffer in hash table */
125  nodeBuffer = (GISTNodeBuffer *) hash_search(gfbb->nodeBuffersTab,
126  (const void *) &nodeBlocknum,
127  HASH_ENTER,
128  &found);
129  if (!found)
130  {
131  /*
132  * Node buffer wasn't found. Initialize the new buffer as empty.
133  */
135 
136  /* nodeBuffer->nodeBlocknum is the hash key and was filled in already */
137  nodeBuffer->blocksCount = 0;
138  nodeBuffer->pageBlocknum = InvalidBlockNumber;
139  nodeBuffer->pageBuffer = NULL;
140  nodeBuffer->queuedForEmptying = false;
141  nodeBuffer->isTemp = false;
142  nodeBuffer->level = level;
143 
144  /*
145  * Add this buffer to the list of buffers on this level. Enlarge
146  * buffersOnLevels array if needed.
147  */
148  if (level >= gfbb->buffersOnLevelsLen)
149  {
150  int i;
151 
152  gfbb->buffersOnLevels =
153  (List **) repalloc(gfbb->buffersOnLevels,
154  (level + 1) * sizeof(List *));
155 
156  /* initialize the enlarged portion */
157  for (i = gfbb->buffersOnLevelsLen; i <= level; i++)
158  gfbb->buffersOnLevels[i] = NIL;
159  gfbb->buffersOnLevelsLen = level + 1;
160  }
161 
162  /*
163  * Prepend the new buffer to the list of buffers on this level. It's
164  * not arbitrary that the new buffer is put to the beginning of the
165  * list: in the final emptying phase we loop through all buffers at
166  * each level, and flush them. If a page is split during the emptying,
167  * it's more efficient to flush the new splitted pages first, before
168  * moving on to pre-existing pages on the level. The buffers just
169  * created during the page split are likely still in cache, so
170  * flushing them immediately is more efficient than putting them to
171  * the end of the queue.
172  */
173  gfbb->buffersOnLevels[level] = lcons(nodeBuffer,
174  gfbb->buffersOnLevels[level]);
175 
176  MemoryContextSwitchTo(oldcxt);
177  }
178 
179  return nodeBuffer;
180 }
#define NIL
Definition: pg_list.h:65
BlockNumber pageBlocknum
Definition: gist_private.h:300
MemoryContext context
Definition: gist_private.h:338
List ** buffersOnLevels
Definition: gist_private.h:365
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
GISTNodeBufferPage * pageBuffer
Definition: gist_private.h:301
bool queuedForEmptying
Definition: gist_private.h:304
List * lcons(void *datum, List *list)
Definition: list.c:453
#define InvalidBlockNumber
Definition: block.h:33
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
int i
Definition: pg_list.h:50

◆ gistInitBuildBuffers()

GISTBuildBuffers* gistInitBuildBuffers ( int  pagesPerBuffer,
int  levelStep,
int  maxLevel 
)

Definition at line 48 of file gistbuildbuffers.c.

References GISTBuildBuffers::bufferEmptyingQueue, GISTBuildBuffers::buffersOnLevels, GISTBuildBuffers::buffersOnLevelsLen, BufFileCreateTemp(), GISTBuildBuffers::context, CurrentMemoryContext, HASHCTL::entrysize, GISTBuildBuffers::freeBlocks, GISTBuildBuffers::freeBlocksLen, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASHCTL::hcxt, HASHCTL::keysize, GISTBuildBuffers::levelStep, GISTBuildBuffers::loadedBuffers, GISTBuildBuffers::loadedBuffersCount, GISTBuildBuffers::loadedBuffersLen, GISTBuildBuffers::nFileBlocks, GISTBuildBuffers::nFreeBlocks, NIL, GISTBuildBuffers::nodeBuffersTab, GISTBuildBuffers::pagesPerBuffer, palloc(), GISTBuildBuffers::pfile, and GISTBuildBuffers::rootlevel.

Referenced by gistInitBuffering().

49 {
50  GISTBuildBuffers *gfbb;
51  HASHCTL hashCtl;
52 
53  gfbb = palloc(sizeof(GISTBuildBuffers));
54  gfbb->pagesPerBuffer = pagesPerBuffer;
55  gfbb->levelStep = levelStep;
56 
57  /*
58  * Create a temporary file to hold buffer pages that are swapped out of
59  * memory.
60  */
61  gfbb->pfile = BufFileCreateTemp(false);
62  gfbb->nFileBlocks = 0;
63 
64  /* Initialize free page management. */
65  gfbb->nFreeBlocks = 0;
66  gfbb->freeBlocksLen = 32;
67  gfbb->freeBlocks = (long *) palloc(gfbb->freeBlocksLen * sizeof(long));
68 
69  /*
70  * Current memory context will be used for all in-memory data structures
71  * of buffers which are persistent during buffering build.
72  */
74 
75  /*
76  * nodeBuffersTab hash is association between index blocks and it's
77  * buffers.
78  */
79  memset(&hashCtl, 0, sizeof(hashCtl));
80  hashCtl.keysize = sizeof(BlockNumber);
81  hashCtl.entrysize = sizeof(GISTNodeBuffer);
82  hashCtl.hcxt = CurrentMemoryContext;
83  gfbb->nodeBuffersTab = hash_create("gistbuildbuffers",
84  1024,
85  &hashCtl,
87 
88  gfbb->bufferEmptyingQueue = NIL;
89 
90  /*
91  * Per-level node buffers lists for final buffers emptying process. Node
92  * buffers are inserted here when they are created.
93  */
94  gfbb->buffersOnLevelsLen = 1;
95  gfbb->buffersOnLevels = (List **) palloc(sizeof(List *) *
96  gfbb->buffersOnLevelsLen);
97  gfbb->buffersOnLevels[0] = NIL;
98 
99  /*
100  * Block numbers of node buffers which last pages are currently loaded
101  * into main memory.
102  */
103  gfbb->loadedBuffersLen = 32;
105  sizeof(GISTNodeBuffer *));
106  gfbb->loadedBuffersCount = 0;
107 
108  gfbb->rootlevel = maxLevel;
109 
110  return gfbb;
111 }
#define NIL
Definition: pg_list.h:65
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
MemoryContext context
Definition: gist_private.h:338
List ** buffersOnLevels
Definition: gist_private.h:365
Size entrysize
Definition: hsearch.h:73
uint32 BlockNumber
Definition: block.h:31
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:184
List * bufferEmptyingQueue
Definition: gist_private.h:354
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define HASH_BLOBS
Definition: hsearch.h:88
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72
GISTNodeBuffer ** loadedBuffers
Definition: gist_private.h:372
void * palloc(Size size)
Definition: mcxt.c:924
Definition: pg_list.h:50

◆ gistLoadNodeBuffer()

static void gistLoadNodeBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer 
)
static

Definition at line 226 of file gistbuildbuffers.c.

References GISTNodeBuffer::blocksCount, gistAddLoadedBuffer(), gistAllocateNewPageBuffer(), gistBuffersReleaseBlock(), InvalidBlockNumber, GISTNodeBuffer::pageBlocknum, GISTNodeBuffer::pageBuffer, GISTBuildBuffers::pfile, and ReadTempFileBlock().

Referenced by gistPopItupFromNodeBuffer(), and gistPushItupToNodeBuffer().

227 {
228  /* Check if we really should load something */
229  if (!nodeBuffer->pageBuffer && nodeBuffer->blocksCount > 0)
230  {
231  /* Allocate memory for page */
232  nodeBuffer->pageBuffer = gistAllocateNewPageBuffer(gfbb);
233 
234  /* Read block from temporary file */
235  ReadTempFileBlock(gfbb->pfile, nodeBuffer->pageBlocknum,
236  nodeBuffer->pageBuffer);
237 
238  /* Mark file block as free */
239  gistBuffersReleaseBlock(gfbb, nodeBuffer->pageBlocknum);
240 
241  /* Mark node buffer as loaded */
242  gistAddLoadedBuffer(gfbb, nodeBuffer);
243  nodeBuffer->pageBlocknum = InvalidBlockNumber;
244  }
245 }
BlockNumber pageBlocknum
Definition: gist_private.h:300
static GISTNodeBufferPage * gistAllocateNewPageBuffer(GISTBuildBuffers *gfbb)
static void gistBuffersReleaseBlock(GISTBuildBuffers *gfbb, long blocknum)
GISTNodeBufferPage * pageBuffer
Definition: gist_private.h:301
#define InvalidBlockNumber
Definition: block.h:33
static void gistAddLoadedBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
static void ReadTempFileBlock(BufFile *file, long blknum, void *ptr)

◆ gistPlaceItupToPage()

static void gistPlaceItupToPage ( GISTNodeBufferPage pageBuffer,
IndexTuple  item 
)
static

Definition at line 293 of file gistbuildbuffers.c.

References Assert, BUFFER_PAGE_DATA_OFFSET, IndexTupleSize, MAXALIGN, and PAGE_FREE_SPACE.

Referenced by gistPushItupToNodeBuffer().

294 {
295  Size itupsz = IndexTupleSize(itup);
296  char *ptr;
297 
298  /* There should be enough of space. */
299  Assert(PAGE_FREE_SPACE(pageBuffer) >= MAXALIGN(itupsz));
300 
301  /* Reduce free space value of page to reserve a spot for the tuple. */
302  PAGE_FREE_SPACE(pageBuffer) -= MAXALIGN(itupsz);
303 
304  /* Get pointer to the spot we reserved (ie. end of free space). */
305  ptr = (char *) pageBuffer + BUFFER_PAGE_DATA_OFFSET
306  + PAGE_FREE_SPACE(pageBuffer);
307 
308  /* Copy the index tuple there. */
309  memcpy(ptr, itup, itupsz);
310 }
#define PAGE_FREE_SPACE(nbp)
Definition: gist_private.h:55
#define BUFFER_PAGE_DATA_OFFSET
Definition: gist_private.h:53
#define Assert(condition)
Definition: c.h:732
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:685
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ gistPopItupFromNodeBuffer()

bool gistPopItupFromNodeBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer,
IndexTuple itup 
)

Definition at line 411 of file gistbuildbuffers.c.

References Assert, GISTNodeBuffer::blocksCount, gistBuffersReleaseBlock(), gistGetItupFromPage(), gistLoadNodeBuffer(), InvalidBlockNumber, PAGE_IS_EMPTY, GISTNodeBuffer::pageBuffer, GISTBuildBuffers::pfile, pfree(), GISTNodeBufferPage::prev, and ReadTempFileBlock().

Referenced by gistProcessEmptyingQueue(), and gistRelocateBuildBuffersOnSplit().

413 {
414  /*
415  * If node buffer is empty then return false.
416  */
417  if (nodeBuffer->blocksCount <= 0)
418  return false;
419 
420  /* Load last page of node buffer if needed */
421  if (!nodeBuffer->pageBuffer)
422  gistLoadNodeBuffer(gfbb, nodeBuffer);
423 
424  /*
425  * Get index tuple from last non-empty page.
426  */
427  gistGetItupFromPage(nodeBuffer->pageBuffer, itup);
428 
429  /*
430  * If we just removed the last tuple from the page, fetch previous page on
431  * this node buffer (if any).
432  */
433  if (PAGE_IS_EMPTY(nodeBuffer->pageBuffer))
434  {
435  BlockNumber prevblkno;
436 
437  /*
438  * blocksCount includes the page in pageBuffer, so decrease it now.
439  */
440  nodeBuffer->blocksCount--;
441 
442  /*
443  * If there's more pages, fetch previous one.
444  */
445  prevblkno = nodeBuffer->pageBuffer->prev;
446  if (prevblkno != InvalidBlockNumber)
447  {
448  /* There is a previous page. Fetch it. */
449  Assert(nodeBuffer->blocksCount > 0);
450  ReadTempFileBlock(gfbb->pfile, prevblkno, nodeBuffer->pageBuffer);
451 
452  /*
453  * Now that we've read the block in memory, we can release its
454  * on-disk block for reuse.
455  */
456  gistBuffersReleaseBlock(gfbb, prevblkno);
457  }
458  else
459  {
460  /* No more pages. Free memory. */
461  Assert(nodeBuffer->blocksCount == 0);
462  pfree(nodeBuffer->pageBuffer);
463  nodeBuffer->pageBuffer = NULL;
464  }
465  }
466  return true;
467 }
uint32 BlockNumber
Definition: block.h:31
static void gistBuffersReleaseBlock(GISTBuildBuffers *gfbb, long blocknum)
static void gistLoadNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
GISTNodeBufferPage * pageBuffer
Definition: gist_private.h:301
void pfree(void *pointer)
Definition: mcxt.c:1031
static void gistGetItupFromPage(GISTNodeBufferPage *pageBuffer, IndexTuple *item)
BlockNumber prev
Definition: gist_private.h:48
#define Assert(condition)
Definition: c.h:732
#define InvalidBlockNumber
Definition: block.h:33
#define PAGE_IS_EMPTY(nbp)
Definition: gist_private.h:57
static void ReadTempFileBlock(BufFile *file, long blknum, void *ptr)

◆ gistPushItupToNodeBuffer()

void gistPushItupToNodeBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer,
IndexTuple  itup 
)

Definition at line 341 of file gistbuildbuffers.c.

References DataPageDeleteStack::blkno, GISTNodeBuffer::blocksCount, BUFFER_HALF_FILLED, GISTBuildBuffers::bufferEmptyingQueue, GISTBuildBuffers::context, gistAddLoadedBuffer(), gistAllocateNewPageBuffer(), gistBuffersGetFreeBlock(), gistLoadNodeBuffer(), gistPlaceItupToPage(), lcons(), MAXALIGN, MemoryContextSwitchTo(), offsetof, PAGE_FREE_SPACE, PAGE_NO_SPACE, GISTNodeBuffer::pageBuffer, GISTBuildBuffers::pfile, GISTNodeBufferPage::prev, GISTNodeBuffer::queuedForEmptying, and WriteTempFileBlock().

Referenced by gistProcessItup(), and gistRelocateBuildBuffersOnSplit().

343 {
344  /*
345  * Most part of memory operations will be in buffering build persistent
346  * context. So, let's switch to it.
347  */
349 
350  /*
351  * If the buffer is currently empty, create the first page.
352  */
353  if (nodeBuffer->blocksCount == 0)
354  {
355  nodeBuffer->pageBuffer = gistAllocateNewPageBuffer(gfbb);
356  nodeBuffer->blocksCount = 1;
357  gistAddLoadedBuffer(gfbb, nodeBuffer);
358  }
359 
360  /* Load last page of node buffer if it wasn't in memory already */
361  if (!nodeBuffer->pageBuffer)
362  gistLoadNodeBuffer(gfbb, nodeBuffer);
363 
364  /*
365  * Check if there is enough space on the last page for the tuple.
366  */
367  if (PAGE_NO_SPACE(nodeBuffer->pageBuffer, itup))
368  {
369  /*
370  * Nope. Swap previous block to disk and allocate a new one.
371  */
372  BlockNumber blkno;
373 
374  /* Write filled page to the disk */
375  blkno = gistBuffersGetFreeBlock(gfbb);
376  WriteTempFileBlock(gfbb->pfile, blkno, nodeBuffer->pageBuffer);
377 
378  /*
379  * Reset the in-memory page as empty, and link the previous block to
380  * the new page by storing its block number in the prev-link.
381  */
382  PAGE_FREE_SPACE(nodeBuffer->pageBuffer) =
383  BLCKSZ - MAXALIGN(offsetof(GISTNodeBufferPage, tupledata));
384  nodeBuffer->pageBuffer->prev = blkno;
385 
386  /* We've just added one more page */
387  nodeBuffer->blocksCount++;
388  }
389 
390  gistPlaceItupToPage(nodeBuffer->pageBuffer, itup);
391 
392  /*
393  * If the buffer just overflowed, add it to the emptying queue.
394  */
395  if (BUFFER_HALF_FILLED(nodeBuffer, gfbb) && !nodeBuffer->queuedForEmptying)
396  {
397  gfbb->bufferEmptyingQueue = lcons(nodeBuffer,
398  gfbb->bufferEmptyingQueue);
399  nodeBuffer->queuedForEmptying = true;
400  }
401 
402  /* Restore memory context */
403  MemoryContextSwitchTo(oldcxt);
404 }
#define PAGE_FREE_SPACE(nbp)
Definition: gist_private.h:55
static void gistPlaceItupToPage(GISTNodeBufferPage *pageBuffer, IndexTuple item)
static GISTNodeBufferPage * gistAllocateNewPageBuffer(GISTBuildBuffers *gfbb)
MemoryContext context
Definition: gist_private.h:338
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define BUFFER_HALF_FILLED(nodeBuffer, gfbb)
Definition: gist_private.h:321
uint32 BlockNumber
Definition: block.h:31
static void gistLoadNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
GISTNodeBufferPage * pageBuffer
Definition: gist_private.h:301
static long gistBuffersGetFreeBlock(GISTBuildBuffers *gfbb)
List * bufferEmptyingQueue
Definition: gist_private.h:354
#define PAGE_NO_SPACE(nbp, itup)
Definition: gist_private.h:59
bool queuedForEmptying
Definition: gist_private.h:304
static void WriteTempFileBlock(BufFile *file, long blknum, void *ptr)
List * lcons(void *datum, List *list)
Definition: list.c:453
BlockNumber prev
Definition: gist_private.h:48
#define MAXALIGN(LEN)
Definition: c.h:685
static void gistAddLoadedBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
#define offsetof(type, field)
Definition: c.h:655

◆ gistRelocateBuildBuffersOnSplit()

void gistRelocateBuildBuffersOnSplit ( GISTBuildBuffers gfbb,
GISTSTATE giststate,
Relation  r,
int  level,
Buffer  buffer,
List splitinfo 
)

Definition at line 538 of file gistbuildbuffers.c.

References Assert, GISTNodeBuffer::blocksCount, GISTPageSplitInfo::buf, BufferGetBlockNumber(), GISTPageSplitInfo::downlink, RelocationBufferInfo::entry, GIST_ROOT_BLKNO, gistDeCompressAtt(), gistgetadjusted(), gistGetNodeBuffer(), gistpenalty(), gistPopItupFromNodeBuffer(), gistPushItupToNodeBuffer(), HASH_FIND, hash_search(), i, INDEX_MAX_KEYS, InvalidBlockNumber, RelocationBufferInfo::isnull, GISTNodeBuffer::isTemp, LEVEL_HAS_BUFFERS, lfirst, list_length(), TupleDescData::natts, RelocationBufferInfo::nodeBuffer, GISTBuildBuffers::nodeBuffersTab, GISTNodeBuffer::pageBlocknum, GISTNodeBuffer::pageBuffer, palloc(), pfree(), RelationData::rd_att, and RelocationBufferInfo::splitinfo.

Referenced by gistbufferinginserttuples().

541 {
542  RelocationBufferInfo *relocationBuffersInfos;
543  bool found;
544  GISTNodeBuffer *nodeBuffer;
545  BlockNumber blocknum;
546  IndexTuple itup;
547  int splitPagesCount = 0,
548  i;
549  GISTENTRY entry[INDEX_MAX_KEYS];
550  bool isnull[INDEX_MAX_KEYS];
551  GISTNodeBuffer oldBuf;
552  ListCell *lc;
553 
554  /* If the splitted page doesn't have buffers, we have nothing to do. */
555  if (!LEVEL_HAS_BUFFERS(level, gfbb))
556  return;
557 
558  /*
559  * Get the node buffer of the splitted page.
560  */
561  blocknum = BufferGetBlockNumber(buffer);
562  nodeBuffer = hash_search(gfbb->nodeBuffersTab, &blocknum,
563  HASH_FIND, &found);
564  if (!found)
565  {
566  /* The page has no buffer, so we have nothing to do. */
567  return;
568  }
569 
570  /*
571  * Make a copy of the old buffer, as we're going reuse it as the buffer
572  * for the new left page, which is on the same block as the old page.
573  * That's not true for the root page, but that's fine because we never
574  * have a buffer on the root page anyway. The original algorithm as
575  * described by Arge et al did, but it's of no use, as you might as well
576  * read the tuples straight from the heap instead of the root buffer.
577  */
578  Assert(blocknum != GIST_ROOT_BLKNO);
579  memcpy(&oldBuf, nodeBuffer, sizeof(GISTNodeBuffer));
580  oldBuf.isTemp = true;
581 
582  /* Reset the old buffer, used for the new left page from now on */
583  nodeBuffer->blocksCount = 0;
584  nodeBuffer->pageBuffer = NULL;
585  nodeBuffer->pageBlocknum = InvalidBlockNumber;
586 
587  /*
588  * Allocate memory for information about relocation buffers.
589  */
590  splitPagesCount = list_length(splitinfo);
591  relocationBuffersInfos =
593  splitPagesCount);
594 
595  /*
596  * Fill relocation buffers information for node buffers of pages produced
597  * by split.
598  */
599  i = 0;
600  foreach(lc, splitinfo)
601  {
603  GISTNodeBuffer *newNodeBuffer;
604 
605  /* Decompress parent index tuple of node buffer page. */
606  gistDeCompressAtt(giststate, r,
607  si->downlink, NULL, (OffsetNumber) 0,
608  relocationBuffersInfos[i].entry,
609  relocationBuffersInfos[i].isnull);
610 
611  /*
612  * Create a node buffer for the page. The leftmost half is on the same
613  * block as the old page before split, so for the leftmost half this
614  * will return the original buffer. The tuples on the original buffer
615  * were relinked to the temporary buffer, so the original one is now
616  * empty.
617  */
618  newNodeBuffer = gistGetNodeBuffer(gfbb, giststate, BufferGetBlockNumber(si->buf), level);
619 
620  relocationBuffersInfos[i].nodeBuffer = newNodeBuffer;
621  relocationBuffersInfos[i].splitinfo = si;
622 
623  i++;
624  }
625 
626  /*
627  * Loop through all index tuples in the buffer of the page being split,
628  * moving them to buffers for the new pages. We try to move each tuple to
629  * the page that will result in the lowest penalty for the leading column
630  * or, in the case of a tie, the lowest penalty for the earliest column
631  * that is not tied.
632  *
633  * The page searching logic is very similar to gistchoose().
634  */
635  while (gistPopItupFromNodeBuffer(gfbb, &oldBuf, &itup))
636  {
637  float best_penalty[INDEX_MAX_KEYS];
638  int i,
639  which;
640  IndexTuple newtup;
641  RelocationBufferInfo *targetBufferInfo;
642 
643  gistDeCompressAtt(giststate, r,
644  itup, NULL, (OffsetNumber) 0, entry, isnull);
645 
646  /* default to using first page (shouldn't matter) */
647  which = 0;
648 
649  /*
650  * best_penalty[j] is the best penalty we have seen so far for column
651  * j, or -1 when we haven't yet examined column j. Array entries to
652  * the right of the first -1 are undefined.
653  */
654  best_penalty[0] = -1;
655 
656  /*
657  * Loop over possible target pages, looking for one to move this tuple
658  * to.
659  */
660  for (i = 0; i < splitPagesCount; i++)
661  {
662  RelocationBufferInfo *splitPageInfo = &relocationBuffersInfos[i];
663  bool zero_penalty;
664  int j;
665 
666  zero_penalty = true;
667 
668  /* Loop over index attributes. */
669  for (j = 0; j < r->rd_att->natts; j++)
670  {
671  float usize;
672 
673  /* Compute penalty for this column. */
674  usize = gistpenalty(giststate, j,
675  &splitPageInfo->entry[j],
676  splitPageInfo->isnull[j],
677  &entry[j], isnull[j]);
678  if (usize > 0)
679  zero_penalty = false;
680 
681  if (best_penalty[j] < 0 || usize < best_penalty[j])
682  {
683  /*
684  * New best penalty for column. Tentatively select this
685  * page as the target, and record the best penalty. Then
686  * reset the next column's penalty to "unknown" (and
687  * indirectly, the same for all the ones to its right).
688  * This will force us to adopt this page's penalty values
689  * as the best for all the remaining columns during
690  * subsequent loop iterations.
691  */
692  which = i;
693  best_penalty[j] = usize;
694 
695  if (j < r->rd_att->natts - 1)
696  best_penalty[j + 1] = -1;
697  }
698  else if (best_penalty[j] == usize)
699  {
700  /*
701  * The current page is exactly as good for this column as
702  * the best page seen so far. The next iteration of this
703  * loop will compare the next column.
704  */
705  }
706  else
707  {
708  /*
709  * The current page is worse for this column than the best
710  * page seen so far. Skip the remaining columns and move
711  * on to the next page, if any.
712  */
713  zero_penalty = false; /* so outer loop won't exit */
714  break;
715  }
716  }
717 
718  /*
719  * If we find a page with zero penalty for all columns, there's no
720  * need to examine remaining pages; just break out of the loop and
721  * return it.
722  */
723  if (zero_penalty)
724  break;
725  }
726 
727  /* OK, "which" is the page index to push the tuple to */
728  targetBufferInfo = &relocationBuffersInfos[which];
729 
730  /* Push item to selected node buffer */
731  gistPushItupToNodeBuffer(gfbb, targetBufferInfo->nodeBuffer, itup);
732 
733  /* Adjust the downlink for this page, if needed. */
734  newtup = gistgetadjusted(r, targetBufferInfo->splitinfo->downlink,
735  itup, giststate);
736  if (newtup)
737  {
738  gistDeCompressAtt(giststate, r,
739  newtup, NULL, (OffsetNumber) 0,
740  targetBufferInfo->entry,
741  targetBufferInfo->isnull);
742 
743  targetBufferInfo->splitinfo->downlink = newtup;
744  }
745  }
746 
747  pfree(relocationBuffersInfos);
748 }
GISTPageSplitInfo * splitinfo
bool isnull[INDEX_MAX_KEYS]
BlockNumber pageBlocknum
Definition: gist_private.h:300
void gistPushItupToNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer, IndexTuple itup)
void gistDeCompressAtt(GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p, OffsetNumber o, GISTENTRY *attdata, bool *isnull)
Definition: gistutil.c:296
uint32 BlockNumber
Definition: block.h:31
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
IndexTuple gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
Definition: gistutil.c:316
#define LEVEL_HAS_BUFFERS(nlevel, gfbb)
Definition: gist_private.h:316
GISTENTRY entry[INDEX_MAX_KEYS]
uint16 OffsetNumber
Definition: off.h:24
GISTNodeBufferPage * pageBuffer
Definition: gist_private.h:301
void pfree(void *pointer)
Definition: mcxt.c:1031
float gistpenalty(GISTSTATE *giststate, int attno, GISTENTRY *orig, bool isNullOrig, GISTENTRY *add, bool isNullAdd)
Definition: gistutil.c:716
IndexTuple downlink
Definition: gist_private.h:410
GISTNodeBuffer * gistGetNodeBuffer(GISTBuildBuffers *gfbb, GISTSTATE *giststate, BlockNumber nodeBlocknum, int level)
TupleDesc rd_att
Definition: rel.h:84
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
#define INDEX_MAX_KEYS
#define InvalidBlockNumber
Definition: block.h:33
static int list_length(const List *l)
Definition: pg_list.h:169
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
void * palloc(Size size)
Definition: mcxt.c:924
GISTNodeBuffer * nodeBuffer
int i
#define GIST_ROOT_BLKNO
Definition: gist_private.h:259
bool gistPopItupFromNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer, IndexTuple *itup)

◆ gistUnloadNodeBuffer()

static void gistUnloadNodeBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer 
)
static

Definition at line 251 of file gistbuildbuffers.c.

References DataPageDeleteStack::blkno, gistBuffersGetFreeBlock(), GISTNodeBuffer::pageBlocknum, GISTNodeBuffer::pageBuffer, GISTBuildBuffers::pfile, pfree(), and WriteTempFileBlock().

Referenced by gistUnloadNodeBuffers().

252 {
253  /* Check if we have something to write */
254  if (nodeBuffer->pageBuffer)
255  {
256  BlockNumber blkno;
257 
258  /* Get free file block */
259  blkno = gistBuffersGetFreeBlock(gfbb);
260 
261  /* Write block to the temporary file */
262  WriteTempFileBlock(gfbb->pfile, blkno, nodeBuffer->pageBuffer);
263 
264  /* Free memory of that page */
265  pfree(nodeBuffer->pageBuffer);
266  nodeBuffer->pageBuffer = NULL;
267 
268  /* Save block number */
269  nodeBuffer->pageBlocknum = blkno;
270  }
271 }
BlockNumber pageBlocknum
Definition: gist_private.h:300
uint32 BlockNumber
Definition: block.h:31
GISTNodeBufferPage * pageBuffer
Definition: gist_private.h:301
void pfree(void *pointer)
Definition: mcxt.c:1031
static long gistBuffersGetFreeBlock(GISTBuildBuffers *gfbb)
static void WriteTempFileBlock(BufFile *file, long blknum, void *ptr)

◆ gistUnloadNodeBuffers()

void gistUnloadNodeBuffers ( GISTBuildBuffers gfbb)

Definition at line 277 of file gistbuildbuffers.c.

References gistUnloadNodeBuffer(), i, GISTBuildBuffers::loadedBuffers, and GISTBuildBuffers::loadedBuffersCount.

Referenced by gistProcessEmptyingQueue().

278 {
279  int i;
280 
281  /* Unload all the buffers that have a page loaded in memory. */
282  for (i = 0; i < gfbb->loadedBuffersCount; i++)
283  gistUnloadNodeBuffer(gfbb, gfbb->loadedBuffers[i]);
284 
285  /* Now there are no node buffers with loaded last page */
286  gfbb->loadedBuffersCount = 0;
287 }
static void gistUnloadNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
GISTNodeBuffer ** loadedBuffers
Definition: gist_private.h:372
int i

◆ ReadTempFileBlock()

static void ReadTempFileBlock ( BufFile file,
long  blknum,
void *  ptr 
)
static

Definition at line 758 of file gistbuildbuffers.c.

References BufFileRead(), BufFileSeekBlock(), elog, and ERROR.

Referenced by gistLoadNodeBuffer(), and gistPopItupFromNodeBuffer().

759 {
760  if (BufFileSeekBlock(file, blknum) != 0)
761  elog(ERROR, "could not seek temporary file: %m");
762  if (BufFileRead(file, ptr, BLCKSZ) != BLCKSZ)
763  elog(ERROR, "could not read temporary file: %m");
764 }
#define ERROR
Definition: elog.h:43
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:752
#define elog(elevel,...)
Definition: elog.h:226
size_t BufFileRead(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:528

◆ WriteTempFileBlock()

static void WriteTempFileBlock ( BufFile file,
long  blknum,
void *  ptr 
)
static

Definition at line 767 of file gistbuildbuffers.c.

References BufFileSeekBlock(), BufFileWrite(), elog, ereport, errcode_for_file_access(), errmsg(), and ERROR.

Referenced by gistPushItupToNodeBuffer(), and gistUnloadNodeBuffer().

768 {
769  if (BufFileSeekBlock(file, blknum) != 0)
770  elog(ERROR, "could not seek temporary file: %m");
771  if (BufFileWrite(file, ptr, BLCKSZ) != BLCKSZ)
772  {
773  /*
774  * the other errors in Read/WriteTempFileBlock shouldn't happen, but
775  * an error at write can easily happen if you run out of disk space.
776  */
777  ereport(ERROR,
779  errmsg("could not write block %ld of temporary file: %m",
780  blknum)));
781  }
782 }
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:593
#define ereport(elevel, rest)
Definition: elog.h:141
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:752
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
size_t BufFileWrite(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:575