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 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, 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 202 of file gistbuildbuffers.c.

203 {
204  /* Never add a temporary buffer to the array */
205  if (nodeBuffer->isTemp)
206  return;
207 
208  /* Enlarge the array if needed */
209  if (gfbb->loadedBuffersCount >= gfbb->loadedBuffersLen)
210  {
211  gfbb->loadedBuffersLen *= 2;
212  gfbb->loadedBuffers = (GISTNodeBuffer **)
213  repalloc(gfbb->loadedBuffers,
214  gfbb->loadedBuffersLen * sizeof(GISTNodeBuffer *));
215  }
216 
217  gfbb->loadedBuffers[gfbb->loadedBuffersCount] = nodeBuffer;
218  gfbb->loadedBuffersCount++;
219 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1321
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 185 of file gistbuildbuffers.c.

186 {
187  GISTNodeBufferPage *pageBuffer;
188 
189  pageBuffer = (GISTNodeBufferPage *) MemoryContextAllocZero(gfbb->context,
190  BLCKSZ);
191  pageBuffer->prev = InvalidBlockNumber;
192 
193  /* Set page free space */
194  PAGE_FREE_SPACE(pageBuffer) = BLCKSZ - BUFFER_PAGE_DATA_OFFSET;
195  return pageBuffer;
196 }
#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:1037
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 472 of file gistbuildbuffers.c.

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

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 489 of file gistbuildbuffers.c.

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

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

Referenced by gistLoadNodeBuffer(), and gistPopItupFromNodeBuffer().

◆ gistFreeBuildBuffers()

void gistFreeBuildBuffers ( GISTBuildBuffers gfbb)

Definition at line 511 of file gistbuildbuffers.c.

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

References BufFileClose(), and GISTBuildBuffers::pfile.

Referenced by gistbuild().

◆ gistGetItupFromPage()

static void gistGetItupFromPage ( GISTNodeBufferPage pageBuffer,
IndexTuple itup 
)
static

Definition at line 315 of file gistbuildbuffers.c.

316 {
317  IndexTuple ptr;
318  Size itupsz;
319 
320  Assert(!PAGE_IS_EMPTY(pageBuffer)); /* Page shouldn't be empty */
321 
322  /* Get pointer to last index tuple */
323  ptr = (IndexTuple) ((char *) pageBuffer
325  + PAGE_FREE_SPACE(pageBuffer));
326  itupsz = IndexTupleSize(ptr);
327 
328  /* Make a copy of the tuple */
329  *itup = (IndexTuple) palloc(itupsz);
330  memcpy(*itup, ptr, itupsz);
331 
332  /* Mark the space used by the tuple as free */
333  PAGE_FREE_SPACE(pageBuffer) += MAXALIGN(itupsz);
334 }
#define MAXALIGN(LEN)
Definition: c.h:747
size_t Size
Definition: c.h:541
#define PAGE_IS_EMPTY(nbp)
Definition: gist_private.h:57
IndexTupleData * IndexTuple
Definition: itup.h:53
#define IndexTupleSize(itup)
Definition: itup.h:70
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc(Size size)
Definition: mcxt.c:1199

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 117 of file gistbuildbuffers.c.

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

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 48 of file gistbuildbuffers.c.

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  hashCtl.keysize = sizeof(BlockNumber);
80  hashCtl.entrysize = sizeof(GISTNodeBuffer);
81  hashCtl.hcxt = CurrentMemoryContext;
82  gfbb->nodeBuffersTab = hash_create("gistbuildbuffers",
83  1024,
84  &hashCtl,
86 
87  gfbb->bufferEmptyingQueue = NIL;
88 
89  /*
90  * Per-level node buffers lists for final buffers emptying process. Node
91  * buffers are inserted here when they are created.
92  */
93  gfbb->buffersOnLevelsLen = 1;
94  gfbb->buffersOnLevels = (List **) palloc(sizeof(List *) *
95  gfbb->buffersOnLevelsLen);
96  gfbb->buffersOnLevels[0] = NIL;
97 
98  /*
99  * Block numbers of node buffers which last pages are currently loaded
100  * into main memory.
101  */
102  gfbb->loadedBuffersLen = 32;
104  sizeof(GISTNodeBuffer *));
105  gfbb->loadedBuffersCount = 0;
106 
107  gfbb->rootlevel = maxLevel;
108 
109  return gfbb;
110 }
uint32 BlockNumber
Definition: block.h:31
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:188
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
#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:124
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 225 of file gistbuildbuffers.c.

226 {
227  /* Check if we really should load something */
228  if (!nodeBuffer->pageBuffer && nodeBuffer->blocksCount > 0)
229  {
230  /* Allocate memory for page */
231  nodeBuffer->pageBuffer = gistAllocateNewPageBuffer(gfbb);
232 
233  /* Read block from temporary file */
234  ReadTempFileBlock(gfbb->pfile, nodeBuffer->pageBlocknum,
235  nodeBuffer->pageBuffer);
236 
237  /* Mark file block as free */
238  gistBuffersReleaseBlock(gfbb, nodeBuffer->pageBlocknum);
239 
240  /* Mark node buffer as loaded */
241  gistAddLoadedBuffer(gfbb, nodeBuffer);
242  nodeBuffer->pageBlocknum = InvalidBlockNumber;
243  }
244 }
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 292 of file gistbuildbuffers.c.

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

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 410 of file gistbuildbuffers.c.

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

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 340 of file gistbuildbuffers.c.

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

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 537 of file gistbuildbuffers.c.

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

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

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

Referenced by gistUnloadNodeBuffers().

◆ gistUnloadNodeBuffers()

void gistUnloadNodeBuffers ( GISTBuildBuffers gfbb)

Definition at line 276 of file gistbuildbuffers.c.

277 {
278  int i;
279 
280  /* Unload all the buffers that have a page loaded in memory. */
281  for (i = 0; i < gfbb->loadedBuffersCount; i++)
282  gistUnloadNodeBuffer(gfbb, gfbb->loadedBuffers[i]);
283 
284  /* Now there are no node buffers with loaded last page */
285  gfbb->loadedBuffersCount = 0;
286 }
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 754 of file gistbuildbuffers.c.

755 {
756  size_t nread;
757 
758  if (BufFileSeekBlock(file, blknum) != 0)
759  elog(ERROR, "could not seek to block %ld in temporary file", blknum);
760  nread = BufFileRead(file, ptr, BLCKSZ);
761  if (nread != BLCKSZ)
762  elog(ERROR, "could not read temporary file: read only %zu of %zu bytes",
763  nread, (size_t) BLCKSZ);
764 }
size_t BufFileRead(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:582
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:800
#define ERROR
Definition: elog.h:35

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

Referenced by gistLoadNodeBuffer(), and gistPopItupFromNodeBuffer().

◆ WriteTempFileBlock()

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

Definition at line 767 of file gistbuildbuffers.c.

768 {
769  if (BufFileSeekBlock(file, blknum) != 0)
770  elog(ERROR, "could not seek to block %ld in temporary file", blknum);
771  BufFileWrite(file, ptr, BLCKSZ);
772 }
void BufFileWrite(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:625

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

Referenced by gistPushItupToNodeBuffer(), and gistUnloadNodeBuffer().