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

197{
198 /* Never add a temporary buffer to the array */
199 if (nodeBuffer->isTemp)
200 return;
201
202 /* Enlarge the array if needed */
203 if (gfbb->loadedBuffersCount >= gfbb->loadedBuffersLen)
204 {
205 gfbb->loadedBuffersLen *= 2;
206 gfbb->loadedBuffers = (GISTNodeBuffer **)
208 gfbb->loadedBuffersLen * sizeof(GISTNodeBuffer *));
209 }
210
211 gfbb->loadedBuffers[gfbb->loadedBuffersCount] = nodeBuffer;
212 gfbb->loadedBuffersCount++;
213}
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1610
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 179 of file gistbuildbuffers.c.

180{
181 GISTNodeBufferPage *pageBuffer;
182
184 BLCKSZ);
185 pageBuffer->prev = InvalidBlockNumber;
186
187 /* Set page free space */
188 PAGE_FREE_SPACE(pageBuffer) = BLCKSZ - BUFFER_PAGE_DATA_OFFSET;
189 return pageBuffer;
190}
#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:1263
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 466 of file gistbuildbuffers.c.

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

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

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

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

Referenced by gistLoadNodeBuffer(), and gistPopItupFromNodeBuffer().

◆ gistFreeBuildBuffers()

void gistFreeBuildBuffers ( GISTBuildBuffers gfbb)

Definition at line 505 of file gistbuildbuffers.c.

506{
507 /* Close buffers file. */
508 BufFileClose(gfbb->pfile);
509
510 /* All other things will be freed on memory context release */
511}
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 309 of file gistbuildbuffers.c.

310{
311 IndexTuple ptr;
312 Size itupsz;
313
314 Assert(!PAGE_IS_EMPTY(pageBuffer)); /* Page shouldn't be empty */
315
316 /* Get pointer to last index tuple */
317 ptr = (IndexTuple) ((char *) pageBuffer
319 + PAGE_FREE_SPACE(pageBuffer));
320 itupsz = IndexTupleSize(ptr);
321
322 /* Make a copy of the tuple */
323 *itup = (IndexTuple) palloc(itupsz);
324 memcpy(*itup, ptr, itupsz);
325
326 /* Mark the space used by the tuple as free */
327 PAGE_FREE_SPACE(pageBuffer) += MAXALIGN(itupsz);
328}
#define MAXALIGN(LEN)
Definition: c.h:824
size_t Size
Definition: c.h:624
#define PAGE_IS_EMPTY(nbp)
Definition: gist_private.h:57
Assert(PointerIsAligned(start, uint64))
IndexTupleData * IndexTuple
Definition: itup.h:53
static Size IndexTupleSize(const IndexTupleData *itup)
Definition: itup.h:71
void * palloc(Size size)
Definition: mcxt.c:1365

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

113{
114 GISTNodeBuffer *nodeBuffer;
115 bool found;
116
117 /* Find node buffer in hash table */
118 nodeBuffer = (GISTNodeBuffer *) hash_search(gfbb->nodeBuffersTab,
119 &nodeBlocknum,
121 &found);
122 if (!found)
123 {
124 /*
125 * Node buffer wasn't found. Initialize the new buffer as empty.
126 */
128
129 /* nodeBuffer->nodeBlocknum is the hash key and was filled in already */
130 nodeBuffer->blocksCount = 0;
131 nodeBuffer->pageBlocknum = InvalidBlockNumber;
132 nodeBuffer->pageBuffer = NULL;
133 nodeBuffer->queuedForEmptying = false;
134 nodeBuffer->isTemp = false;
135 nodeBuffer->level = level;
136
137 /*
138 * Add this buffer to the list of buffers on this level. Enlarge
139 * buffersOnLevels array if needed.
140 */
141 if (level >= gfbb->buffersOnLevelsLen)
142 {
143 int i;
144
145 gfbb->buffersOnLevels =
146 (List **) repalloc(gfbb->buffersOnLevels,
147 (level + 1) * sizeof(List *));
148
149 /* initialize the enlarged portion */
150 for (i = gfbb->buffersOnLevelsLen; i <= level; i++)
151 gfbb->buffersOnLevels[i] = NIL;
152 gfbb->buffersOnLevelsLen = level + 1;
153 }
154
155 /*
156 * Prepend the new buffer to the list of buffers on this level. It's
157 * not arbitrary that the new buffer is put to the beginning of the
158 * list: in the final emptying phase we loop through all buffers at
159 * each level, and flush them. If a page is split during the emptying,
160 * it's more efficient to flush the new split pages first, before
161 * moving on to pre-existing pages on the level. The buffers just
162 * created during the page split are likely still in cache, so
163 * flushing them immediately is more efficient than putting them to
164 * the end of the queue.
165 */
166 gfbb->buffersOnLevels[level] = lcons(nodeBuffer,
167 gfbb->buffersOnLevels[level]);
168
169 MemoryContextSwitchTo(oldcxt);
170 }
171
172 return nodeBuffer;
173}
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:952
@ HASH_ENTER
Definition: hsearch.h:114
int i
Definition: isn.c:77
List * lcons(void *datum, List *list)
Definition: list.c:495
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define NIL
Definition: pg_list.h:68
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
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 = palloc_array(long, gfbb->freeBlocksLen);
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
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;
91 gfbb->buffersOnLevels[0] = NIL;
92
93 /*
94 * Block numbers of node buffers which last pages are currently loaded
95 * into main memory.
96 */
97 gfbb->loadedBuffersLen = 32;
99 gfbb->loadedBuffersCount = 0;
100
101 gfbb->rootlevel = maxLevel;
102
103 return gfbb;
104}
uint32 BlockNumber
Definition: block.h:31
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:193
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:358
#define palloc_object(type)
Definition: fe_memutils.h:74
#define palloc_array(type, count)
Definition: fe_memutils.h:76
#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:160
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_array, palloc_object, GISTBuildBuffers::pfile, and GISTBuildBuffers::rootlevel.

Referenced by gistInitBuffering().

◆ gistLoadNodeBuffer()

static void gistLoadNodeBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer 
)
static

Definition at line 219 of file gistbuildbuffers.c.

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

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

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

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

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

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

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

Referenced by gistbufferinginserttuples().

◆ gistUnloadNodeBuffer()

static void gistUnloadNodeBuffer ( GISTBuildBuffers gfbb,
GISTNodeBuffer nodeBuffer 
)
static

Definition at line 244 of file gistbuildbuffers.c.

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

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

Referenced by gistUnloadNodeBuffers().

◆ gistUnloadNodeBuffers()

void gistUnloadNodeBuffers ( GISTBuildBuffers gfbb)

Definition at line 270 of file gistbuildbuffers.c.

271{
272 int i;
273
274 /* Unload all the buffers that have a page loaded in memory. */
275 for (i = 0; i < gfbb->loadedBuffersCount; i++)
277
278 /* Now there are no node buffers with loaded last page */
279 gfbb->loadedBuffersCount = 0;
280}
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 746 of file gistbuildbuffers.c.

747{
748 if (BufFileSeekBlock(file, blknum) != 0)
749 elog(ERROR, "could not seek to block %ld in temporary file", blknum);
750 BufFileReadExact(file, ptr, BLCKSZ);
751}
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:226

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

755{
756 if (BufFileSeekBlock(file, blknum) != 0)
757 elog(ERROR, "could not seek to block %ld in temporary file", blknum);
758 BufFileWrite(file, ptr, BLCKSZ);
759}
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().