PostgreSQL Source Code  git master
gistbuildbuffers.c File Reference
#include "postgres.h"
#include "access/gist_private.h"
#include "storage/buffile.h"
#include "storage/bufmgr.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 itup)
 
static void gistGetItupFromPage (GISTNodeBufferPage *pageBuffer, IndexTuple *itup)
 
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, const 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 198 of file gistbuildbuffers.c.

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

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

Referenced by gistLoadNodeBuffer(), and gistPushItupToNodeBuffer().

◆ gistAllocateNewPageBuffer()

static GISTNodeBufferPage * gistAllocateNewPageBuffer ( GISTBuildBuffers gfbb)
static

Definition at line 181 of file gistbuildbuffers.c.

182 {
183  GISTNodeBufferPage *pageBuffer;
184 
185  pageBuffer = (GISTNodeBufferPage *) MemoryContextAllocZero(gfbb->context,
186  BLCKSZ);
187  pageBuffer->prev = InvalidBlockNumber;
188 
189  /* Set page free space */
190  PAGE_FREE_SPACE(pageBuffer) = BLCKSZ - BUFFER_PAGE_DATA_OFFSET;
191  return pageBuffer;
192 }
#define InvalidBlockNumber
Definition: block.h:33
#define PAGE_FREE_SPACE(nbp)
Definition: gist_private.h:55
#define BUFFER_PAGE_DATA_OFFSET
Definition: gist_private.h:53
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1214
MemoryContext context
Definition: gist_private.h:341
BlockNumber prev
Definition: gist_private.h:48

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

Referenced by gistLoadNodeBuffer(), and gistPushItupToNodeBuffer().

◆ gistBuffersGetFreeBlock()

static long gistBuffersGetFreeBlock ( GISTBuildBuffers gfbb)
static

Definition at line 468 of file gistbuildbuffers.c.

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

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

Referenced by gistPushItupToNodeBuffer(), and gistUnloadNodeBuffer().

◆ gistBuffersReleaseBlock()

static void gistBuffersReleaseBlock ( GISTBuildBuffers gfbb,
long  blocknum 
)
static

Definition at line 485 of file gistbuildbuffers.c.

486 {
487  int ndx;
488 
489  /* Enlarge freeBlocks array if full. */
490  if (gfbb->nFreeBlocks >= gfbb->freeBlocksLen)
491  {
492  gfbb->freeBlocksLen *= 2;
493  gfbb->freeBlocks = (long *) repalloc(gfbb->freeBlocks,
494  gfbb->freeBlocksLen *
495  sizeof(long));
496  }
497 
498  /* Add blocknum to array */
499  ndx = gfbb->nFreeBlocks++;
500  gfbb->freeBlocks[ndx] = blocknum;
501 }

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

Referenced by gistLoadNodeBuffer(), and gistPopItupFromNodeBuffer().

◆ gistFreeBuildBuffers()

void gistFreeBuildBuffers ( GISTBuildBuffers gfbb)

Definition at line 507 of file gistbuildbuffers.c.

508 {
509  /* Close buffers file. */
510  BufFileClose(gfbb->pfile);
511 
512  /* All other things will be freed on memory context release */
513 }
void BufFileClose(BufFile *file)
Definition: buffile.c:412

References BufFileClose(), and GISTBuildBuffers::pfile.

Referenced by gistbuild().

◆ gistGetItupFromPage()

static void gistGetItupFromPage ( GISTNodeBufferPage pageBuffer,
IndexTuple itup 
)
static

Definition at line 311 of file gistbuildbuffers.c.

312 {
313  IndexTuple ptr;
314  Size itupsz;
315 
316  Assert(!PAGE_IS_EMPTY(pageBuffer)); /* Page shouldn't be empty */
317 
318  /* Get pointer to last index tuple */
319  ptr = (IndexTuple) ((char *) pageBuffer
321  + PAGE_FREE_SPACE(pageBuffer));
322  itupsz = IndexTupleSize(ptr);
323 
324  /* Make a copy of the tuple */
325  *itup = (IndexTuple) palloc(itupsz);
326  memcpy(*itup, ptr, itupsz);
327 
328  /* Mark the space used by the tuple as free */
329  PAGE_FREE_SPACE(pageBuffer) += MAXALIGN(itupsz);
330 }
#define MAXALIGN(LEN)
Definition: c.h:811
#define Assert(condition)
Definition: c.h:858
size_t Size
Definition: c.h:605
#define PAGE_IS_EMPTY(nbp)
Definition: gist_private.h:57
IndexTupleData * IndexTuple
Definition: itup.h:53
#define IndexTupleSize(itup)
Definition: itup.h:70
void * palloc(Size size)
Definition: mcxt.c:1316

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

Referenced by gistPopItupFromNodeBuffer().

◆ gistGetNodeBuffer()

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

Definition at line 113 of file gistbuildbuffers.c.

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

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().

◆ gistInitBuildBuffers()

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

Definition at line 44 of file gistbuildbuffers.c.

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

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().

◆ gistLoadNodeBuffer()

static void gistLoadNodeBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer 
)
static

Definition at line 221 of file gistbuildbuffers.c.

222 {
223  /* Check if we really should load something */
224  if (!nodeBuffer->pageBuffer && nodeBuffer->blocksCount > 0)
225  {
226  /* Allocate memory for page */
227  nodeBuffer->pageBuffer = gistAllocateNewPageBuffer(gfbb);
228 
229  /* Read block from temporary file */
230  ReadTempFileBlock(gfbb->pfile, nodeBuffer->pageBlocknum,
231  nodeBuffer->pageBuffer);
232 
233  /* Mark file block as free */
234  gistBuffersReleaseBlock(gfbb, nodeBuffer->pageBlocknum);
235 
236  /* Mark node buffer as loaded */
237  gistAddLoadedBuffer(gfbb, nodeBuffer);
238  nodeBuffer->pageBlocknum = InvalidBlockNumber;
239  }
240 }
static void gistBuffersReleaseBlock(GISTBuildBuffers *gfbb, long blocknum)
static void gistAddLoadedBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
static GISTNodeBufferPage * gistAllocateNewPageBuffer(GISTBuildBuffers *gfbb)
static void ReadTempFileBlock(BufFile *file, long blknum, void *ptr)

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

Referenced by gistPopItupFromNodeBuffer(), and gistPushItupToNodeBuffer().

◆ gistPlaceItupToPage()

static void gistPlaceItupToPage ( GISTNodeBufferPage pageBuffer,
IndexTuple  itup 
)
static

Definition at line 288 of file gistbuildbuffers.c.

289 {
290  Size itupsz = IndexTupleSize(itup);
291  char *ptr;
292 
293  /* There should be enough of space. */
294  Assert(PAGE_FREE_SPACE(pageBuffer) >= MAXALIGN(itupsz));
295 
296  /* Reduce free space value of page to reserve a spot for the tuple. */
297  PAGE_FREE_SPACE(pageBuffer) -= MAXALIGN(itupsz);
298 
299  /* Get pointer to the spot we reserved (ie. end of free space). */
300  ptr = (char *) pageBuffer + BUFFER_PAGE_DATA_OFFSET
301  + PAGE_FREE_SPACE(pageBuffer);
302 
303  /* Copy the index tuple there. */
304  memcpy(ptr, itup, itupsz);
305 }

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

Referenced by gistPushItupToNodeBuffer().

◆ gistPopItupFromNodeBuffer()

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

Definition at line 406 of file gistbuildbuffers.c.

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

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().

◆ gistPushItupToNodeBuffer()

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

Definition at line 336 of file gistbuildbuffers.c.

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

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

Referenced by gistProcessItup(), and gistRelocateBuildBuffersOnSplit().

◆ gistRelocateBuildBuffersOnSplit()

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

Definition at line 533 of file gistbuildbuffers.c.

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

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

Referenced by gistbufferinginserttuples().

◆ gistUnloadNodeBuffer()

static void gistUnloadNodeBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer 
)
static

Definition at line 246 of file gistbuildbuffers.c.

247 {
248  /* Check if we have something to write */
249  if (nodeBuffer->pageBuffer)
250  {
251  BlockNumber blkno;
252 
253  /* Get free file block */
254  blkno = gistBuffersGetFreeBlock(gfbb);
255 
256  /* Write block to the temporary file */
257  WriteTempFileBlock(gfbb->pfile, blkno, nodeBuffer->pageBuffer);
258 
259  /* Free memory of that page */
260  pfree(nodeBuffer->pageBuffer);
261  nodeBuffer->pageBuffer = NULL;
262 
263  /* Save block number */
264  nodeBuffer->pageBlocknum = blkno;
265  }
266 }

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

Referenced by gistUnloadNodeBuffers().

◆ gistUnloadNodeBuffers()

void gistUnloadNodeBuffers ( GISTBuildBuffers gfbb)

Definition at line 272 of file gistbuildbuffers.c.

273 {
274  int i;
275 
276  /* Unload all the buffers that have a page loaded in memory. */
277  for (i = 0; i < gfbb->loadedBuffersCount; i++)
278  gistUnloadNodeBuffer(gfbb, gfbb->loadedBuffers[i]);
279 
280  /* Now there are no node buffers with loaded last page */
281  gfbb->loadedBuffersCount = 0;
282 }
static void gistUnloadNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)

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

Referenced by gistProcessEmptyingQueue().

◆ ReadTempFileBlock()

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

Definition at line 750 of file gistbuildbuffers.c.

751 {
752  if (BufFileSeekBlock(file, blknum) != 0)
753  elog(ERROR, "could not seek to block %ld in temporary file", blknum);
754  BufFileReadExact(file, ptr, BLCKSZ);
755 }
int BufFileSeekBlock(BufFile *file, int64 blknum)
Definition: buffile.c:851
void BufFileReadExact(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:654
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224

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

Referenced by gistLoadNodeBuffer(), and gistPopItupFromNodeBuffer().

◆ WriteTempFileBlock()

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

Definition at line 758 of file gistbuildbuffers.c.

759 {
760  if (BufFileSeekBlock(file, blknum) != 0)
761  elog(ERROR, "could not seek to block %ld in temporary file", blknum);
762  BufFileWrite(file, ptr, BLCKSZ);
763 }
void BufFileWrite(BufFile *file, const void *ptr, size_t size)
Definition: buffile.c:676

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

Referenced by gistPushItupToNodeBuffer(), and gistUnloadNodeBuffer().