PostgreSQL Source Code  git master
gist.c File Reference
#include "postgres.h"
#include "access/gist_private.h"
#include "access/gistscan.h"
#include "catalog/pg_collation.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "utils/builtins.h"
#include "utils/index_selfuncs.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for gist.c:

Go to the source code of this file.

Macros

#define ROTATEDIST(d)
 

Functions

static void gistfixsplit (GISTInsertState *state, GISTSTATE *giststate)
 
static bool gistinserttuple (GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, IndexTuple tuple, OffsetNumber oldoffnum)
 
static bool gistinserttuples (GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, IndexTuple *tuples, int ntup, OffsetNumber oldoffnum, Buffer leftchild, Buffer rightchild, bool unlockbuf, bool unlockleftchild)
 
static void gistfinishsplit (GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, List *splitinfo, bool releasebuf)
 
static void gistvacuumpage (Relation rel, Page page, Buffer buffer)
 
Datum gisthandler (PG_FUNCTION_ARGS)
 
MemoryContext createTempGistContext (void)
 
void gistbuildempty (Relation index)
 
bool gistinsert (Relation r, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
 
bool gistplacetopage (Relation rel, Size freespace, GISTSTATE *giststate, Buffer buffer, IndexTuple *itup, int ntup, OffsetNumber oldoffnum, BlockNumber *newblkno, Buffer leftchildbuf, List **splitinfo, bool markfollowright)
 
void gistdoinsert (Relation r, IndexTuple itup, Size freespace, GISTSTATE *giststate)
 
static GISTInsertStackgistFindPath (Relation r, BlockNumber child, OffsetNumber *downlinkoffnum)
 
static void gistFindCorrectParent (Relation r, GISTInsertStack *child)
 
static IndexTuple gistformdownlink (Relation rel, Buffer buf, GISTSTATE *giststate, GISTInsertStack *stack)
 
SplitedPageLayoutgistSplit (Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate)
 
GISTSTATEinitGISTstate (Relation index)
 
void freeGISTstate (GISTSTATE *giststate)
 

Macro Definition Documentation

◆ ROTATEDIST

#define ROTATEDIST (   d)
Value:
do { \
SplitedPageLayout *tmp=(SplitedPageLayout*)palloc(sizeof(SplitedPageLayout)); \
memset(tmp,0,sizeof(SplitedPageLayout)); \
tmp->block.blkno = InvalidBlockNumber; \
tmp->buffer = InvalidBuffer; \
tmp->next = (d); \
(d)=tmp; \
} while(0)
#define InvalidBuffer
Definition: buf.h:25
#define InvalidBlockNumber
Definition: block.h:33
void * palloc(Size size)
Definition: mcxt.c:835

Definition at line 42 of file gist.c.

Referenced by gistSplit().

Function Documentation

◆ createTempGistContext()

MemoryContext createTempGistContext ( void  )

Definition at line 110 of file gist.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, and CurrentMemoryContext.

Referenced by gist_xlog_startup(), gistbeginscan(), gistbuild(), and gistinsert().

111 {
113  "GiST temporary context",
115 }
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:165

◆ freeGISTstate()

void freeGISTstate ( GISTSTATE giststate)

Definition at line 1523 of file gist.c.

References MemoryContextDelete(), and GISTSTATE::scanCxt.

Referenced by gistbuild(), and gistendscan().

1524 {
1525  /* It's sufficient to delete the scanCxt */
1526  MemoryContextDelete(giststate->scanCxt);
1527 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:198
MemoryContext scanCxt
Definition: gist_private.h:78

◆ gistbuildempty()

void gistbuildempty ( Relation  index)

Definition at line 121 of file gist.c.

References buffer, BUFFER_LOCK_EXCLUSIVE, END_CRIT_SECTION, F_LEAF, GISTInitBuffer(), INIT_FORKNUM, LockBuffer(), log_newpage_buffer(), MarkBufferDirty(), P_NEW, RBM_NORMAL, ReadBufferExtended(), START_CRIT_SECTION, and UnlockReleaseBuffer().

Referenced by gisthandler().

122 {
123  Buffer buffer;
124 
125  /* Initialize the root page */
126  buffer = ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
128 
129  /* Initialize and xlog buffer */
131  GISTInitBuffer(buffer, F_LEAF);
132  MarkBufferDirty(buffer);
133  log_newpage_buffer(buffer, true);
135 
136  /* Unlock and release the buffer */
137  UnlockReleaseBuffer(buffer);
138 }
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1009
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
#define P_NEW
Definition: bufmgr.h:82
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
#define F_LEAF
Definition: gist.h:42
void GISTInitBuffer(Buffer b, uint32 f)
Definition: gistutil.c:721
int Buffer
Definition: buf.h:23

◆ gistdoinsert()

void gistdoinsert ( Relation  r,
IndexTuple  itup,
Size  freespace,
GISTSTATE giststate 
)

Definition at line 601 of file gist.c.

References Assert, GISTInsertStack::blkno, GISTInsertStack::buffer, BufferGetLSNAtomic(), BufferGetPage, GISTInsertStack::downlinkoffnum, ereport, errdetail(), errhint(), errmsg(), ERROR, GISTInsertState::freespace, GIST_EXCLUSIVE, GIST_ROOT_BLKNO, GIST_SHARE, GIST_UNLOCK, gistcheckpage(), gistchoose(), gistfixsplit(), GistFollowRight, gistgetadjusted(), gistinserttuple(), GistPageGetNSN, GistPageIsLeaf, GistTupleIsInvalid, InvalidOffsetNumber, ItemPointerGetBlockNumber, LockBuffer(), GISTInsertStack::lsn, GISTInsertStack::page, PageGetItem, PageGetItemId, PageGetLSN, palloc0(), GISTInsertStack::parent, GISTInsertState::r, ReadBuffer(), RelationGetRelationName, RelationNeedsWAL, ReleaseBuffer(), GISTInsertState::stack, IndexTupleData::t_tid, UnlockReleaseBuffer(), and XLogRecPtrIsInvalid.

Referenced by gistBuildCallback(), and gistinsert().

602 {
603  ItemId iid;
604  IndexTuple idxtuple;
605  GISTInsertStack firststack;
606  GISTInsertStack *stack;
608  bool xlocked = false;
609 
610  memset(&state, 0, sizeof(GISTInsertState));
611  state.freespace = freespace;
612  state.r = r;
613 
614  /* Start from the root */
615  firststack.blkno = GIST_ROOT_BLKNO;
616  firststack.lsn = 0;
617  firststack.parent = NULL;
618  firststack.downlinkoffnum = InvalidOffsetNumber;
619  state.stack = stack = &firststack;
620 
621  /*
622  * Walk down along the path of smallest penalty, updating the parent
623  * pointers with the key we're inserting as we go. If we crash in the
624  * middle, the tree is consistent, although the possible parent updates
625  * were a waste.
626  */
627  for (;;)
628  {
629  if (XLogRecPtrIsInvalid(stack->lsn))
630  stack->buffer = ReadBuffer(state.r, stack->blkno);
631 
632  /*
633  * Be optimistic and grab shared lock first. Swap it for an exclusive
634  * lock later if we need to update the page.
635  */
636  if (!xlocked)
637  {
638  LockBuffer(stack->buffer, GIST_SHARE);
639  gistcheckpage(state.r, stack->buffer);
640  }
641 
642  stack->page = (Page) BufferGetPage(stack->buffer);
643  stack->lsn = xlocked ?
644  PageGetLSN(stack->page) : BufferGetLSNAtomic(stack->buffer);
645  Assert(!RelationNeedsWAL(state.r) || !XLogRecPtrIsInvalid(stack->lsn));
646 
647  /*
648  * If this page was split but the downlink was never inserted to the
649  * parent because the inserting backend crashed before doing that, fix
650  * that now.
651  */
652  if (GistFollowRight(stack->page))
653  {
654  if (!xlocked)
655  {
656  LockBuffer(stack->buffer, GIST_UNLOCK);
658  xlocked = true;
659  /* someone might've completed the split when we unlocked */
660  if (!GistFollowRight(stack->page))
661  continue;
662  }
663  gistfixsplit(&state, giststate);
664 
665  UnlockReleaseBuffer(stack->buffer);
666  xlocked = false;
667  state.stack = stack = stack->parent;
668  continue;
669  }
670 
671  if (stack->blkno != GIST_ROOT_BLKNO &&
672  stack->parent->lsn < GistPageGetNSN(stack->page))
673  {
674  /*
675  * Concurrent split detected. There's no guarantee that the
676  * downlink for this page is consistent with the tuple we're
677  * inserting anymore, so go back to parent and rechoose the best
678  * child.
679  */
680  UnlockReleaseBuffer(stack->buffer);
681  xlocked = false;
682  state.stack = stack = stack->parent;
683  continue;
684  }
685 
686  if (!GistPageIsLeaf(stack->page))
687  {
688  /*
689  * This is an internal page so continue to walk down the tree.
690  * Find the child node that has the minimum insertion penalty.
691  */
692  BlockNumber childblkno;
693  IndexTuple newtup;
694  GISTInsertStack *item;
695  OffsetNumber downlinkoffnum;
696 
697  downlinkoffnum = gistchoose(state.r, stack->page, itup, giststate);
698  iid = PageGetItemId(stack->page, downlinkoffnum);
699  idxtuple = (IndexTuple) PageGetItem(stack->page, iid);
700  childblkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
701 
702  /*
703  * Check that it's not a leftover invalid tuple from pre-9.1
704  */
705  if (GistTupleIsInvalid(idxtuple))
706  ereport(ERROR,
707  (errmsg("index \"%s\" contains an inner tuple marked as invalid",
709  errdetail("This is caused by an incomplete page split at crash recovery before upgrading to PostgreSQL 9.1."),
710  errhint("Please REINDEX it.")));
711 
712  /*
713  * Check that the key representing the target child node is
714  * consistent with the key we're inserting. Update it if it's not.
715  */
716  newtup = gistgetadjusted(state.r, idxtuple, itup, giststate);
717  if (newtup)
718  {
719  /*
720  * Swap shared lock for an exclusive one. Beware, the page may
721  * change while we unlock/lock the page...
722  */
723  if (!xlocked)
724  {
725  LockBuffer(stack->buffer, GIST_UNLOCK);
727  xlocked = true;
728  stack->page = (Page) BufferGetPage(stack->buffer);
729 
730  if (PageGetLSN(stack->page) != stack->lsn)
731  {
732  /* the page was changed while we unlocked it, retry */
733  continue;
734  }
735  }
736 
737  /*
738  * Update the tuple.
739  *
740  * We still hold the lock after gistinserttuple(), but it
741  * might have to split the page to make the updated tuple fit.
742  * In that case the updated tuple might migrate to the other
743  * half of the split, so we have to go back to the parent and
744  * descend back to the half that's a better fit for the new
745  * tuple.
746  */
747  if (gistinserttuple(&state, stack, giststate, newtup,
748  downlinkoffnum))
749  {
750  /*
751  * If this was a root split, the root page continues to be
752  * the parent and the updated tuple went to one of the
753  * child pages, so we just need to retry from the root
754  * page.
755  */
756  if (stack->blkno != GIST_ROOT_BLKNO)
757  {
758  UnlockReleaseBuffer(stack->buffer);
759  xlocked = false;
760  state.stack = stack = stack->parent;
761  }
762  continue;
763  }
764  }
765  LockBuffer(stack->buffer, GIST_UNLOCK);
766  xlocked = false;
767 
768  /* descend to the chosen child */
769  item = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
770  item->blkno = childblkno;
771  item->parent = stack;
772  item->downlinkoffnum = downlinkoffnum;
773  state.stack = stack = item;
774  }
775  else
776  {
777  /*
778  * Leaf page. Insert the new key. We've already updated all the
779  * parents on the way down, but we might have to split the page if
780  * it doesn't fit. gistinserthere() will take care of that.
781  */
782 
783  /*
784  * Swap shared lock for an exclusive one. Be careful, the page may
785  * change while we unlock/lock the page...
786  */
787  if (!xlocked)
788  {
789  LockBuffer(stack->buffer, GIST_UNLOCK);
791  xlocked = true;
792  stack->page = (Page) BufferGetPage(stack->buffer);
793  stack->lsn = PageGetLSN(stack->page);
794 
795  if (stack->blkno == GIST_ROOT_BLKNO)
796  {
797  /*
798  * the only page that can become inner instead of leaf is
799  * the root page, so for root we should recheck it
800  */
801  if (!GistPageIsLeaf(stack->page))
802  {
803  /*
804  * very rare situation: during unlock/lock index with
805  * number of pages = 1 was increased
806  */
807  LockBuffer(stack->buffer, GIST_UNLOCK);
808  xlocked = false;
809  continue;
810  }
811 
812  /*
813  * we don't need to check root split, because checking
814  * leaf/inner is enough to recognize split for root
815  */
816  }
817  else if (GistFollowRight(stack->page) ||
818  stack->parent->lsn < GistPageGetNSN(stack->page))
819  {
820  /*
821  * The page was split while we momentarily unlocked the
822  * page. Go back to parent.
823  */
824  UnlockReleaseBuffer(stack->buffer);
825  xlocked = false;
826  state.stack = stack = stack->parent;
827  continue;
828  }
829  }
830 
831  /* now state.stack->(page, buffer and blkno) points to leaf page */
832 
833  gistinserttuple(&state, stack, giststate, itup,
835  LockBuffer(stack->buffer, GIST_UNLOCK);
836 
837  /* Release any pins we might still hold before exiting */
838  for (; stack; stack = stack->parent)
839  ReleaseBuffer(stack->buffer);
840  break;
841  }
842  }
843 }
#define GistFollowRight(page)
Definition: gist.h:147
BlockNumber blkno
Definition: gist_private.h:206
#define GistPageGetNSN(page)
Definition: gist.h:151
static bool gistinserttuple(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, IndexTuple tuple, OffsetNumber oldoffnum)
Definition: gist.c:1173
int errhint(const char *fmt,...)
Definition: elog.c:987
static void gistfixsplit(GISTInsertState *state, GISTSTATE *giststate)
Definition: gist.c:1114
ItemPointerData t_tid
Definition: itup.h:37
#define GistTupleIsInvalid(itup)
Definition: gist_private.h:275
#define GIST_UNLOCK
Definition: gist_private.h:45
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
IndexTuple gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
Definition: gistutil.c:314
uint16 OffsetNumber
Definition: off.h:24
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
GISTInsertStack * stack
Definition: gist_private.h:245
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2832
IndexTupleData * IndexTuple
Definition: itup.h:53
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define RelationGetRelationName(relation)
Definition: rel.h:445
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
OffsetNumber gistchoose(Relation r, Page p, IndexTuple it, GISTSTATE *giststate)
Definition: gistutil.c:372
#define GistPageIsLeaf(page)
Definition: gist.h:132
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
OffsetNumber downlinkoffnum
Definition: gist_private.h:217
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
void * palloc0(Size size)
Definition: mcxt.c:864
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define InvalidOffsetNumber
Definition: off.h:26
#define Assert(condition)
Definition: c.h:680
Definition: regguts.h:298
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:743
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define GIST_SHARE
Definition: gist_private.h:43
#define RelationNeedsWAL(relation)
Definition: rel.h:514
#define PageGetLSN(page)
Definition: bufpage.h:362
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define GIST_ROOT_BLKNO
Definition: gist_private.h:249
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
#define GIST_EXCLUSIVE
Definition: gist_private.h:44
struct GISTInsertStack * parent
Definition: gist_private.h:220
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74

◆ gistFindCorrectParent()

static void gistFindCorrectParent ( Relation  r,
GISTInsertStack child 
)
static

Definition at line 965 of file gist.c.

References GISTInsertStack::blkno, GISTInsertStack::buffer, BufferGetPage, GISTInsertStack::downlinkoffnum, FirstOffsetNumber, GIST_EXCLUSIVE, gistcheckpage(), gistFindPath(), GistPageGetOpaque, i, InvalidBlockNumber, InvalidOffsetNumber, ItemPointerGetBlockNumber, LockBuffer(), GISTInsertStack::lsn, OffsetNumberNext, GISTInsertStack::page, PageGetItem, PageGetItemId, PageGetLSN, PageGetMaxOffsetNumber, DataPageDeleteStack::parent, GISTInsertStack::parent, ReadBuffer(), ReleaseBuffer(), IndexTupleData::t_tid, and UnlockReleaseBuffer().

Referenced by gistfinishsplit(), and gistformdownlink().

966 {
967  GISTInsertStack *parent = child->parent;
968 
969  gistcheckpage(r, parent->buffer);
970  parent->page = (Page) BufferGetPage(parent->buffer);
971 
972  /* here we don't need to distinguish between split and page update */
973  if (child->downlinkoffnum == InvalidOffsetNumber ||
974  parent->lsn != PageGetLSN(parent->page))
975  {
976  /* parent is changed, look child in right links until found */
977  OffsetNumber i,
978  maxoff;
979  ItemId iid;
980  IndexTuple idxtuple;
981  GISTInsertStack *ptr;
982 
983  while (true)
984  {
985  maxoff = PageGetMaxOffsetNumber(parent->page);
986  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
987  {
988  iid = PageGetItemId(parent->page, i);
989  idxtuple = (IndexTuple) PageGetItem(parent->page, iid);
990  if (ItemPointerGetBlockNumber(&(idxtuple->t_tid)) == child->blkno)
991  {
992  /* yes!!, found */
993  child->downlinkoffnum = i;
994  return;
995  }
996  }
997 
998  parent->blkno = GistPageGetOpaque(parent->page)->rightlink;
999  UnlockReleaseBuffer(parent->buffer);
1000  if (parent->blkno == InvalidBlockNumber)
1001  {
1002  /*
1003  * End of chain and still didn't find parent. It's a very-very
1004  * rare situation when root splited.
1005  */
1006  break;
1007  }
1008  parent->buffer = ReadBuffer(r, parent->blkno);
1009  LockBuffer(parent->buffer, GIST_EXCLUSIVE);
1010  gistcheckpage(r, parent->buffer);
1011  parent->page = (Page) BufferGetPage(parent->buffer);
1012  }
1013 
1014  /*
1015  * awful!!, we need search tree to find parent ... , but before we
1016  * should release all old parent
1017  */
1018 
1019  ptr = child->parent->parent; /* child->parent already released
1020  * above */
1021  while (ptr)
1022  {
1023  ReleaseBuffer(ptr->buffer);
1024  ptr = ptr->parent;
1025  }
1026 
1027  /* ok, find new path */
1028  ptr = parent = gistFindPath(r, child->blkno, &child->downlinkoffnum);
1029 
1030  /* read all buffers as expected by caller */
1031  /* note we don't lock them or gistcheckpage them here! */
1032  while (ptr)
1033  {
1034  ptr->buffer = ReadBuffer(r, ptr->blkno);
1035  ptr->page = (Page) BufferGetPage(ptr->buffer);
1036  ptr = ptr->parent;
1037  }
1038 
1039  /* install new chain of parents to stack */
1040  child->parent = parent;
1041 
1042  /* make recursive call to normal processing */
1044  gistFindCorrectParent(r, child);
1045  }
1046 
1047  return;
1048 }
BlockNumber blkno
Definition: gist_private.h:206
ItemPointerData t_tid
Definition: itup.h:37
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
OffsetNumber downlinkoffnum
Definition: gist_private.h:217
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
static void gistFindCorrectParent(Relation r, GISTInsertStack *child)
Definition: gist.c:965
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define InvalidOffsetNumber
Definition: off.h:26
static GISTInsertStack * gistFindPath(Relation r, BlockNumber child, OffsetNumber *downlinkoffnum)
Definition: gist.c:855
#define GistPageGetOpaque(page)
Definition: gist.h:130
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:743
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define InvalidBlockNumber
Definition: block.h:33
#define PageGetLSN(page)
Definition: bufpage.h:362
int i
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
#define GIST_EXCLUSIVE
Definition: gist_private.h:44
struct GISTInsertStack * parent
Definition: gist_private.h:220
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74

◆ gistFindPath()

static GISTInsertStack* gistFindPath ( Relation  r,
BlockNumber  child,
OffsetNumber downlinkoffnum 
)
static

Definition at line 855 of file gist.c.

References DataPageDeleteStack::blkno, GISTInsertStack::blkno, buffer, BufferGetLSNAtomic(), BufferGetPage, GISTInsertStack::downlinkoffnum, elog, ERROR, FirstOffsetNumber, GIST_ROOT_BLKNO, GIST_SHARE, gistcheckpage(), GistFollowRight, GistPageGetNSN, GistPageGetOpaque, GistPageIsLeaf, i, InvalidBlockNumber, InvalidOffsetNumber, ItemPointerGetBlockNumber, lappend(), lcons(), linitial, list_delete_first(), list_make1, LockBuffer(), GISTInsertStack::lsn, NIL, OffsetNumberNext, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, palloc0(), GISTInsertStack::parent, ReadBuffer(), RelationGetRelationName, IndexTupleData::t_tid, and UnlockReleaseBuffer().

Referenced by gistFindCorrectParent().

856 {
857  Page page;
858  Buffer buffer;
859  OffsetNumber i,
860  maxoff;
861  ItemId iid;
862  IndexTuple idxtuple;
863  List *fifo;
864  GISTInsertStack *top,
865  *ptr;
866  BlockNumber blkno;
867 
868  top = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
869  top->blkno = GIST_ROOT_BLKNO;
871 
872  fifo = list_make1(top);
873  while (fifo != NIL)
874  {
875  /* Get next page to visit */
876  top = linitial(fifo);
877  fifo = list_delete_first(fifo);
878 
879  buffer = ReadBuffer(r, top->blkno);
880  LockBuffer(buffer, GIST_SHARE);
881  gistcheckpage(r, buffer);
882  page = (Page) BufferGetPage(buffer);
883 
884  if (GistPageIsLeaf(page))
885  {
886  /*
887  * Because we scan the index top-down, all the rest of the pages
888  * in the queue must be leaf pages as well.
889  */
890  UnlockReleaseBuffer(buffer);
891  break;
892  }
893 
894  top->lsn = BufferGetLSNAtomic(buffer);
895 
896  /*
897  * If F_FOLLOW_RIGHT is set, the page to the right doesn't have a
898  * downlink. This should not normally happen..
899  */
900  if (GistFollowRight(page))
901  elog(ERROR, "concurrent GiST page split was incomplete");
902 
903  if (top->parent && top->parent->lsn < GistPageGetNSN(page) &&
904  GistPageGetOpaque(page)->rightlink != InvalidBlockNumber /* sanity check */ )
905  {
906  /*
907  * Page was split while we looked elsewhere. We didn't see the
908  * downlink to the right page when we scanned the parent, so add
909  * it to the queue now.
910  *
911  * Put the right page ahead of the queue, so that we visit it
912  * next. That's important, because if this is the lowest internal
913  * level, just above leaves, we might already have queued up some
914  * leaf pages, and we assume that there can't be any non-leaf
915  * pages behind leaf pages.
916  */
917  ptr = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
918  ptr->blkno = GistPageGetOpaque(page)->rightlink;
920  ptr->parent = top->parent;
921 
922  fifo = lcons(ptr, fifo);
923  }
924 
925  maxoff = PageGetMaxOffsetNumber(page);
926 
927  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
928  {
929  iid = PageGetItemId(page, i);
930  idxtuple = (IndexTuple) PageGetItem(page, iid);
931  blkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
932  if (blkno == child)
933  {
934  /* Found it! */
935  UnlockReleaseBuffer(buffer);
936  *downlinkoffnum = i;
937  return top;
938  }
939  else
940  {
941  /* Append this child to the list of pages to visit later */
942  ptr = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
943  ptr->blkno = blkno;
944  ptr->downlinkoffnum = i;
945  ptr->parent = top;
946 
947  fifo = lappend(fifo, ptr);
948  }
949  }
950 
951  UnlockReleaseBuffer(buffer);
952  }
953 
954  elog(ERROR, "failed to re-find parent of a page in index \"%s\", block %u",
955  RelationGetRelationName(r), child);
956  return NULL; /* keep compiler quiet */
957 }
#define GistFollowRight(page)
Definition: gist.h:147
#define NIL
Definition: pg_list.h:69
BlockNumber blkno
Definition: gist_private.h:206
#define GistPageGetNSN(page)
Definition: gist.h:151
ItemPointerData t_tid
Definition: itup.h:37
uint32 BlockNumber
Definition: block.h:31
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
#define list_make1(x1)
Definition: pg_list.h:139
#define linitial(l)
Definition: pg_list.h:111
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2832
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
#define RelationGetRelationName(relation)
Definition: rel.h:445
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
List * lappend(List *list, void *datum)
Definition: list.c:128
#define GistPageIsLeaf(page)
Definition: gist.h:132
OffsetNumber downlinkoffnum
Definition: gist_private.h:217
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
void * palloc0(Size size)
Definition: mcxt.c:864
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define InvalidOffsetNumber
Definition: off.h:26
#define GistPageGetOpaque(page)
Definition: gist.h:130
List * lcons(void *datum, List *list)
Definition: list.c:259
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:743
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define InvalidBlockNumber
Definition: block.h:33
#define GIST_SHARE
Definition: gist_private.h:43
int i
#define GIST_ROOT_BLKNO
Definition: gist_private.h:249
#define elog
Definition: elog.h:219
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
Definition: pg_list.h:45
int Buffer
Definition: buf.h:23
struct GISTInsertStack * parent
Definition: gist_private.h:220
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
List * list_delete_first(List *list)
Definition: list.c:666

◆ gistfinishsplit()

static void gistfinishsplit ( GISTInsertState state,
GISTInsertStack stack,
GISTSTATE giststate,
List splitinfo,
bool  releasebuf 
)
static

Definition at line 1259 of file gist.c.

References Assert, GISTPageSplitInfo::buf, GISTInsertStack::buffer, GISTPageSplitInfo::downlink, GISTInsertStack::downlinkoffnum, GIST_EXCLUSIVE, gistFindCorrectParent(), gistinserttuples(), InvalidOffsetNumber, lcons(), lfirst, linitial, list_delete_first(), list_length(), LockBuffer(), lsecond, NIL, GISTInsertStack::parent, and GISTInsertState::r.

Referenced by gistfixsplit(), and gistinserttuples().

1261 {
1262  ListCell *lc;
1263  List *reversed;
1264  GISTPageSplitInfo *right;
1265  GISTPageSplitInfo *left;
1266  IndexTuple tuples[2];
1267 
1268  /* A split always contains at least two halves */
1269  Assert(list_length(splitinfo) >= 2);
1270 
1271  /*
1272  * We need to insert downlinks for each new page, and update the downlink
1273  * for the original (leftmost) page in the split. Begin at the rightmost
1274  * page, inserting one downlink at a time until there's only two pages
1275  * left. Finally insert the downlink for the last new page and update the
1276  * downlink for the original page as one operation.
1277  */
1278 
1279  /* for convenience, create a copy of the list in reverse order */
1280  reversed = NIL;
1281  foreach(lc, splitinfo)
1282  {
1283  reversed = lcons(lfirst(lc), reversed);
1284  }
1285 
1287  gistFindCorrectParent(state->r, stack);
1288 
1289  /*
1290  * insert downlinks for the siblings from right to left, until there are
1291  * only two siblings left.
1292  */
1293  while (list_length(reversed) > 2)
1294  {
1295  right = (GISTPageSplitInfo *) linitial(reversed);
1296  left = (GISTPageSplitInfo *) lsecond(reversed);
1297 
1298  if (gistinserttuples(state, stack->parent, giststate,
1299  &right->downlink, 1,
1301  left->buf, right->buf, false, false))
1302  {
1303  /*
1304  * If the parent page was split, need to relocate the original
1305  * parent pointer.
1306  */
1307  gistFindCorrectParent(state->r, stack);
1308  }
1309  /* gistinserttuples() released the lock on right->buf. */
1310  reversed = list_delete_first(reversed);
1311  }
1312 
1313  right = (GISTPageSplitInfo *) linitial(reversed);
1314  left = (GISTPageSplitInfo *) lsecond(reversed);
1315 
1316  /*
1317  * Finally insert downlink for the remaining right page and update the
1318  * downlink for the original page to not contain the tuples that were
1319  * moved to the new pages.
1320  */
1321  tuples[0] = left->downlink;
1322  tuples[1] = right->downlink;
1323  gistinserttuples(state, stack->parent, giststate,
1324  tuples, 2,
1325  stack->downlinkoffnum,
1326  left->buf, right->buf,
1327  true, /* Unlock parent */
1328  unlockbuf /* Unlock stack->buffer if caller wants that */
1329  );
1330  Assert(left->buf == stack->buffer);
1331 }
#define NIL
Definition: pg_list.h:69
#define lsecond(l)
Definition: pg_list.h:116
#define linitial(l)
Definition: pg_list.h:111
IndexTuple downlink
Definition: gist_private.h:398
OffsetNumber downlinkoffnum
Definition: gist_private.h:217
static void gistFindCorrectParent(Relation r, GISTInsertStack *child)
Definition: gist.c:965
static bool gistinserttuples(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, IndexTuple *tuples, int ntup, OffsetNumber oldoffnum, Buffer leftchild, Buffer rightchild, bool unlockbuf, bool unlockleftchild)
Definition: gist.c:1207
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define InvalidOffsetNumber
Definition: off.h:26
List * lcons(void *datum, List *list)
Definition: list.c:259
#define Assert(condition)
Definition: c.h:680
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
#define GIST_EXCLUSIVE
Definition: gist_private.h:44
Definition: pg_list.h:45
struct GISTInsertStack * parent
Definition: gist_private.h:220
List * list_delete_first(List *list)
Definition: list.c:666

◆ gistfixsplit()

static void gistfixsplit ( GISTInsertState state,
GISTSTATE giststate 
)
static

Definition at line 1114 of file gist.c.

References Assert, GISTInsertStack::blkno, buf, GISTPageSplitInfo::buf, GISTInsertStack::buffer, BufferGetPage, GISTPageSplitInfo::downlink, GISTInsertStack::downlinkoffnum, elog, GIST_EXCLUSIVE, gistfinishsplit(), GistFollowRight, gistformdownlink(), GistPageGetOpaque, lappend(), LockBuffer(), LOG, NIL, OffsetNumberIsValid, GISTInsertStack::page, palloc(), GISTInsertState::r, ReadBuffer(), RelationGetRelationName, and GISTInsertState::stack.

Referenced by gistdoinsert().

1115 {
1116  GISTInsertStack *stack = state->stack;
1117  Buffer buf;
1118  Page page;
1119  List *splitinfo = NIL;
1120 
1121  elog(LOG, "fixing incomplete split in index \"%s\", block %u",
1122  RelationGetRelationName(state->r), stack->blkno);
1123 
1124  Assert(GistFollowRight(stack->page));
1126 
1127  buf = stack->buffer;
1128 
1129  /*
1130  * Read the chain of split pages, following the rightlinks. Construct a
1131  * downlink tuple for each page.
1132  */
1133  for (;;)
1134  {
1136  IndexTuple downlink;
1137 
1138  page = BufferGetPage(buf);
1139 
1140  /* Form the new downlink tuples to insert to parent */
1141  downlink = gistformdownlink(state->r, buf, giststate, stack);
1142 
1143  si->buf = buf;
1144  si->downlink = downlink;
1145 
1146  splitinfo = lappend(splitinfo, si);
1147 
1148  if (GistFollowRight(page))
1149  {
1150  /* lock next page */
1151  buf = ReadBuffer(state->r, GistPageGetOpaque(page)->rightlink);
1152  LockBuffer(buf, GIST_EXCLUSIVE);
1153  }
1154  else
1155  break;
1156  }
1157 
1158  /* Insert the downlinks */
1159  gistfinishsplit(state, stack, giststate, splitinfo, false);
1160 }
#define GistFollowRight(page)
Definition: gist.h:147
#define NIL
Definition: pg_list.h:69
BlockNumber blkno
Definition: gist_private.h:206
#define LOG
Definition: elog.h:26
static void gistfinishsplit(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, List *splitinfo, bool releasebuf)
Definition: gist.c:1259
static IndexTuple gistformdownlink(Relation rel, Buffer buf, GISTSTATE *giststate, GISTInsertStack *stack)
Definition: gist.c:1054
IndexTuple downlink
Definition: gist_private.h:398
GISTInsertStack * stack
Definition: gist_private.h:245
static char * buf
Definition: pg_test_fsync.c:67
#define RelationGetRelationName(relation)
Definition: rel.h:445
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
List * lappend(List *list, void *datum)
Definition: list.c:128
OffsetNumber downlinkoffnum
Definition: gist_private.h:217
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define GistPageGetOpaque(page)
Definition: gist.h:130
#define Assert(condition)
Definition: c.h:680
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
void * palloc(Size size)
Definition: mcxt.c:835
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
#define elog
Definition: elog.h:219
#define GIST_EXCLUSIVE
Definition: gist_private.h:44
Definition: pg_list.h:45
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74

◆ gistformdownlink()

static IndexTuple gistformdownlink ( Relation  rel,
Buffer  buf,
GISTSTATE giststate,
GISTInsertStack stack 
)
static

Definition at line 1054 of file gist.c.

References GISTInsertStack::buffer, BufferGetBlockNumber(), BufferGetPage, CopyIndexTuple(), GISTInsertStack::downlinkoffnum, FirstOffsetNumber, GIST_EXCLUSIVE, GIST_UNLOCK, gistFindCorrectParent(), gistgetadjusted(), GistTupleSetValid, ItemPointerSetBlockNumber, LockBuffer(), OffsetNumberNext, GISTInsertStack::page, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, GISTInsertStack::parent, and IndexTupleData::t_tid.

Referenced by gistfixsplit().

1056 {
1057  Page page = BufferGetPage(buf);
1058  OffsetNumber maxoff;
1059  OffsetNumber offset;
1060  IndexTuple downlink = NULL;
1061 
1062  maxoff = PageGetMaxOffsetNumber(page);
1063  for (offset = FirstOffsetNumber; offset <= maxoff; offset = OffsetNumberNext(offset))
1064  {
1065  IndexTuple ituple = (IndexTuple)
1066  PageGetItem(page, PageGetItemId(page, offset));
1067 
1068  if (downlink == NULL)
1069  downlink = CopyIndexTuple(ituple);
1070  else
1071  {
1072  IndexTuple newdownlink;
1073 
1074  newdownlink = gistgetadjusted(rel, downlink, ituple,
1075  giststate);
1076  if (newdownlink)
1077  downlink = newdownlink;
1078  }
1079  }
1080 
1081  /*
1082  * If the page is completely empty, we can't form a meaningful downlink
1083  * for it. But we have to insert a downlink for the page. Any key will do,
1084  * as long as its consistent with the downlink of parent page, so that we
1085  * can legally insert it to the parent. A minimal one that matches as few
1086  * scans as possible would be best, to keep scans from doing useless work,
1087  * but we don't know how to construct that. So we just use the downlink of
1088  * the original page that was split - that's as far from optimal as it can
1089  * get but will do..
1090  */
1091  if (!downlink)
1092  {
1093  ItemId iid;
1094 
1096  gistFindCorrectParent(rel, stack);
1097  iid = PageGetItemId(stack->parent->page, stack->downlinkoffnum);
1098  downlink = (IndexTuple) PageGetItem(stack->parent->page, iid);
1099  downlink = CopyIndexTuple(downlink);
1100  LockBuffer(stack->parent->buffer, GIST_UNLOCK);
1101  }
1102 
1104  GistTupleSetValid(downlink);
1105 
1106  return downlink;
1107 }
ItemPointerData t_tid
Definition: itup.h:37
#define GIST_UNLOCK
Definition: gist_private.h:45
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
IndexTuple gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
Definition: gistutil.c:314
uint16 OffsetNumber
Definition: off.h:24
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:438
static char * buf
Definition: pg_test_fsync.c:67
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define GistTupleSetValid(itup)
Definition: gist_private.h:276
OffsetNumber downlinkoffnum
Definition: gist_private.h:217
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
static void gistFindCorrectParent(Relation r, GISTInsertStack *child)
Definition: gist.c:965
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define ItemPointerSetBlockNumber(pointer, blockNumber)
Definition: itemptr.h:116
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
#define GIST_EXCLUSIVE
Definition: gist_private.h:44
struct GISTInsertStack * parent
Definition: gist_private.h:220
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74

◆ gisthandler()

Datum gisthandler ( PG_FUNCTION_ARGS  )

Definition at line 57 of file gist.c.

References IndexAmRoutine::ambeginscan, IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, IndexAmRoutine::ambulkdelete, IndexAmRoutine::amcanbackward, IndexAmRoutine::amcanmulticol, IndexAmRoutine::amcanorder, IndexAmRoutine::amcanorderbyop, IndexAmRoutine::amcanparallel, IndexAmRoutine::amcanreturn, IndexAmRoutine::amcanunique, IndexAmRoutine::amclusterable, IndexAmRoutine::amcostestimate, IndexAmRoutine::amendscan, IndexAmRoutine::amestimateparallelscan, IndexAmRoutine::amgetbitmap, IndexAmRoutine::amgettuple, IndexAmRoutine::aminitparallelscan, IndexAmRoutine::aminsert, IndexAmRoutine::amkeytype, IndexAmRoutine::ammarkpos, IndexAmRoutine::amoptionalkey, IndexAmRoutine::amoptions, IndexAmRoutine::amparallelrescan, IndexAmRoutine::ampredlocks, IndexAmRoutine::amproperty, IndexAmRoutine::amrescan, IndexAmRoutine::amrestrpos, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, IndexAmRoutine::amstorage, IndexAmRoutine::amstrategies, IndexAmRoutine::amsupport, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, gistbeginscan(), gistbuild(), gistbuildempty(), gistbulkdelete(), gistcanreturn(), gistcostestimate(), gistendscan(), gistgetbitmap(), gistgettuple(), gistinsert(), GISTNProcs, gistoptions(), gistproperty(), gistrescan(), gistvacuumcleanup(), gistvalidate(), InvalidOid, makeNode, and PG_RETURN_POINTER.

58 {
60 
61  amroutine->amstrategies = 0;
62  amroutine->amsupport = GISTNProcs;
63  amroutine->amcanorder = false;
64  amroutine->amcanorderbyop = true;
65  amroutine->amcanbackward = false;
66  amroutine->amcanunique = false;
67  amroutine->amcanmulticol = true;
68  amroutine->amoptionalkey = true;
69  amroutine->amsearcharray = false;
70  amroutine->amsearchnulls = true;
71  amroutine->amstorage = true;
72  amroutine->amclusterable = true;
73  amroutine->ampredlocks = false;
74  amroutine->amcanparallel = false;
75  amroutine->amkeytype = InvalidOid;
76 
77  amroutine->ambuild = gistbuild;
78  amroutine->ambuildempty = gistbuildempty;
79  amroutine->aminsert = gistinsert;
80  amroutine->ambulkdelete = gistbulkdelete;
82  amroutine->amcanreturn = gistcanreturn;
83  amroutine->amcostestimate = gistcostestimate;
84  amroutine->amoptions = gistoptions;
85  amroutine->amproperty = gistproperty;
86  amroutine->amvalidate = gistvalidate;
87  amroutine->ambeginscan = gistbeginscan;
88  amroutine->amrescan = gistrescan;
89  amroutine->amgettuple = gistgettuple;
90  amroutine->amgetbitmap = gistgetbitmap;
91  amroutine->amendscan = gistendscan;
92  amroutine->ammarkpos = NULL;
93  amroutine->amrestrpos = NULL;
94  amroutine->amestimateparallelscan = NULL;
95  amroutine->aminitparallelscan = NULL;
96  amroutine->amparallelrescan = NULL;
97 
98  PG_RETURN_POINTER(amroutine);
99 }
ambeginscan_function ambeginscan
Definition: amapi.h:208
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
ambulkdelete_function ambulkdelete
Definition: amapi.h:201
bool amcanmulticol
Definition: amapi.h:179
uint16 amsupport
Definition: amapi.h:169
amgettuple_function amgettuple
Definition: amapi.h:210
IndexBulkDeleteResult * gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: gistvacuum.c:29
bool amcanorderbyop
Definition: amapi.h:173
amproperty_function amproperty
Definition: amapi.h:206
IndexBulkDeleteResult * gistbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: gistvacuum.c:139
amparallelrescan_function amparallelrescan
Definition: amapi.h:219
bool amstorage
Definition: amapi.h:187
bool gistgettuple(IndexScanDesc scan, ScanDirection dir)
Definition: gistget.c:627
bool ampredlocks
Definition: amapi.h:191
aminsert_function aminsert
Definition: amapi.h:200
bytea * gistoptions(Datum reloptions, bool validate)
Definition: gistutil.c:836
Oid amkeytype
Definition: amapi.h:195
bool amoptionalkey
Definition: amapi.h:181
amvalidate_function amvalidate
Definition: amapi.h:207
bool gistinsert(Relation r, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: gist.c:147
IndexBuildResult * gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: gistbuild.c:114
amgetbitmap_function amgetbitmap
Definition: amapi.h:211
ambuild_function ambuild
Definition: amapi.h:198
amoptions_function amoptions
Definition: amapi.h:205
amcostestimate_function amcostestimate
Definition: amapi.h:204
bool amcanunique
Definition: amapi.h:177
void gistrescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys)
Definition: gistscan.c:111
void gistcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7221
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:202
amendscan_function amendscan
Definition: amapi.h:212
bool amcanbackward
Definition: amapi.h:175
amrescan_function amrescan
Definition: amapi.h:209
bool amcanparallel
Definition: amapi.h:193
int64 gistgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: gistget.c:758
void gistbuildempty(Relation index)
Definition: gist.c:121
bool amsearchnulls
Definition: amapi.h:185
bool amclusterable
Definition: amapi.h:189
bool amsearcharray
Definition: amapi.h:183
#define InvalidOid
Definition: postgres_ext.h:36
bool gistcanreturn(Relation index, int attno)
Definition: gistget.c:807
#define makeNode(_type_)
Definition: nodes.h:560
ammarkpos_function ammarkpos
Definition: amapi.h:213
bool amcanorder
Definition: amapi.h:171
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:217
#define GISTNProcs
Definition: gist.h:37
uint16 amstrategies
Definition: amapi.h:167
bool gistvalidate(Oid opclassoid)
Definition: gistvalidate.c:34
ambuildempty_function ambuildempty
Definition: amapi.h:199
IndexScanDesc gistbeginscan(Relation r, int nkeys, int norderbys)
Definition: gistscan.c:58
void gistendscan(IndexScanDesc scan)
Definition: gistscan.c:322
bool gistproperty(Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
Definition: gistutil.c:871
amcanreturn_function amcanreturn
Definition: amapi.h:203
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:218
amrestrpos_function amrestrpos
Definition: amapi.h:214

◆ gistinsert()

bool gistinsert ( Relation  r,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
IndexInfo indexInfo 
)

Definition at line 147 of file gist.c.

References createTempGistContext(), gistdoinsert(), gistFormTuple(), IndexInfo::ii_AmCache, IndexInfo::ii_Context, initGISTstate(), MemoryContextReset(), MemoryContextSwitchTo(), and GISTSTATE::tempCxt.

Referenced by gisthandler().

151 {
152  GISTSTATE *giststate = (GISTSTATE *) indexInfo->ii_AmCache;
153  IndexTuple itup;
154  MemoryContext oldCxt;
155 
156  /* Initialize GISTSTATE cache if first call in this statement */
157  if (giststate == NULL)
158  {
159  oldCxt = MemoryContextSwitchTo(indexInfo->ii_Context);
160  giststate = initGISTstate(r);
161  giststate->tempCxt = createTempGistContext();
162  indexInfo->ii_AmCache = (void *) giststate;
163  MemoryContextSwitchTo(oldCxt);
164  }
165 
166  oldCxt = MemoryContextSwitchTo(giststate->tempCxt);
167 
168  itup = gistFormTuple(giststate, r,
169  values, isnull, true /* size is currently bogus */ );
170  itup->t_tid = *ht_ctid;
171 
172  gistdoinsert(r, itup, 0, giststate);
173 
174  /* cleanup */
175  MemoryContextSwitchTo(oldCxt);
176  MemoryContextReset(giststate->tempCxt);
177 
178  return false;
179 }
MemoryContext ii_Context
Definition: execnodes.h:162
MemoryContext createTempGistContext(void)
Definition: gist.c:110
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:134
void gistdoinsert(Relation r, IndexTuple itup, Size freespace, GISTSTATE *giststate)
Definition: gist.c:601
MemoryContext tempCxt
Definition: gist_private.h:79
void * ii_AmCache
Definition: execnodes.h:161
GISTSTATE * initGISTstate(Relation index)
Definition: gist.c:1424
static Datum values[MAXATTR]
Definition: bootstrap.c:164
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:572

◆ gistinserttuple()

static bool gistinserttuple ( GISTInsertState state,
GISTInsertStack stack,
GISTSTATE giststate,
IndexTuple  tuple,
OffsetNumber  oldoffnum 
)
static

Definition at line 1173 of file gist.c.

References gistinserttuples(), and InvalidBuffer.

Referenced by gistdoinsert().

1175 {
1176  return gistinserttuples(state, stack, giststate, &tuple, 1, oldoffnum,
1177  InvalidBuffer, InvalidBuffer, false, false);
1178 }
#define InvalidBuffer
Definition: buf.h:25
static bool gistinserttuples(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, IndexTuple *tuples, int ntup, OffsetNumber oldoffnum, Buffer leftchild, Buffer rightchild, bool unlockbuf, bool unlockleftchild)
Definition: gist.c:1207

◆ gistinserttuples()

static bool gistinserttuples ( GISTInsertState state,
GISTInsertStack stack,
GISTSTATE giststate,
IndexTuple tuples,
int  ntup,
OffsetNumber  oldoffnum,
Buffer  leftchild,
Buffer  rightchild,
bool  unlockbuf,
bool  unlockleftchild 
)
static

Definition at line 1207 of file gist.c.

References GISTInsertStack::buffer, BufferIsValid, GISTInsertState::freespace, GIST_UNLOCK, gistfinishsplit(), gistplacetopage(), LockBuffer(), GISTInsertState::r, and UnlockReleaseBuffer().

Referenced by gistfinishsplit(), and gistinserttuple().

1212 {
1213  List *splitinfo;
1214  bool is_split;
1215 
1216  /* Insert the tuple(s) to the page, splitting the page if necessary */
1217  is_split = gistplacetopage(state->r, state->freespace, giststate,
1218  stack->buffer,
1219  tuples, ntup,
1220  oldoffnum, NULL,
1221  leftchild,
1222  &splitinfo,
1223  true);
1224 
1225  /*
1226  * Before recursing up in case the page was split, release locks on the
1227  * child pages. We don't need to keep them locked when updating the
1228  * parent.
1229  */
1232  if (BufferIsValid(leftchild) && unlockleftchild)
1234 
1235  /*
1236  * If we had to split, insert/update the downlinks in the parent. If the
1237  * caller requested us to release the lock on stack->buffer, tell
1238  * gistfinishsplit() to do that as soon as it's safe to do so. If we
1239  * didn't have to split, release it ourselves.
1240  */
1241  if (splitinfo)
1242  gistfinishsplit(state, stack, giststate, splitinfo, unlockbuf);
1243  else if (unlockbuf)
1244  LockBuffer(stack->buffer, GIST_UNLOCK);
1245 
1246  return is_split;
1247 }
#define GIST_UNLOCK
Definition: gist_private.h:45
static void gistfinishsplit(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, List *splitinfo, bool releasebuf)
Definition: gist.c:1259
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
bool gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate, Buffer buffer, IndexTuple *itup, int ntup, OffsetNumber oldoffnum, BlockNumber *newblkno, Buffer leftchildbuf, List **splitinfo, bool markfollowright)
Definition: gist.c:212
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define rightchild(x)
Definition: fsmpage.c:30
#define leftchild(x)
Definition: fsmpage.c:29
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
Definition: pg_list.h:45

◆ gistplacetopage()

bool gistplacetopage ( Relation  rel,
Size  freespace,
GISTSTATE giststate,
Buffer  buffer,
IndexTuple itup,
int  ntup,
OffsetNumber  oldoffnum,
BlockNumber newblkno,
Buffer  leftchildbuf,
List **  splitinfo,
bool  markfollowright 
)

Definition at line 212 of file gist.c.

References DataPageDeleteStack::blkno, gistxlogPage::blkno, SplitedPageLayout::block, GISTPageSplitInfo::buf, SplitedPageLayout::buffer, buffer, BufferGetBlockNumber(), BufferGetPage, BufferIsValid, GISTPageSplitInfo::downlink, elog, END_CRIT_SECTION, ERROR, F_LEAF, FirstOffsetNumber, GIST_MAX_SPLIT_PAGES, GIST_ROOT_BLKNO, GistClearFollowRight, gistextractpage(), gistfillbuffer(), gistfillitupvec(), GistFollowRight, gistGetFakeLSN(), GISTInitBuffer(), gistjoinvector(), GistMarkFollowRight, gistNewBuffer(), gistnospace(), GistPageGetNSN, GistPageGetOpaque, GistPageHasGarbage, GistPageIsLeaf, GistPageSetNSN, gistSplit(), GistTupleSetValid, gistvacuumpage(), gistXLogSplit(), gistXLogUpdate(), i, IndexTupleSize, InvalidBlockNumber, InvalidOffsetNumber, ItemPointerEquals(), ItemPointerSetBlockNumber, SplitedPageLayout::itup, lappend(), SplitedPageLayout::lenlist, SplitedPageLayout::list, MarkBufferDirty(), memmove, SplitedPageLayout::next, NIL, gistxlogPage::num, OffsetNumberIsValid, SplitedPageLayout::page, PageAddItem, PageGetTempPageCopySpecial(), PageIndexTupleDelete(), PageIndexTupleOverwrite(), PageRestoreTempPage(), PageSetLSN, palloc(), RelationGetRelationName, RelationNeedsWAL, START_CRIT_SECTION, IndexTupleData::t_tid, UnlockReleaseBuffer(), and XLogEnsureRecordSpace().

Referenced by gistbufferinginserttuples(), and gistinserttuples().

219 {
221  Page page = BufferGetPage(buffer);
222  bool is_leaf = (GistPageIsLeaf(page)) ? true : false;
223  XLogRecPtr recptr;
224  int i;
225  bool is_split;
226 
227  /*
228  * Refuse to modify a page that's incompletely split. This should not
229  * happen because we finish any incomplete splits while we walk down the
230  * tree. However, it's remotely possible that another concurrent inserter
231  * splits a parent page, and errors out before completing the split. We
232  * will just throw an error in that case, and leave any split we had in
233  * progress unfinished too. The next insert that comes along will clean up
234  * the mess.
235  */
236  if (GistFollowRight(page))
237  elog(ERROR, "concurrent GiST page split was incomplete");
238 
239  *splitinfo = NIL;
240 
241  /*
242  * if isupdate, remove old key: This node's key has been modified, either
243  * because a child split occurred or because we needed to adjust our key
244  * for an insert in a child node. Therefore, remove the old version of
245  * this node's key.
246  *
247  * for WAL replay, in the non-split case we handle this by setting up a
248  * one-element todelete array; in the split case, it's handled implicitly
249  * because the tuple vector passed to gistSplit won't include this tuple.
250  */
251  is_split = gistnospace(page, itup, ntup, oldoffnum, freespace);
252 
253  /*
254  * If leaf page is full, try at first to delete dead tuples. And then
255  * check again.
256  */
257  if (is_split && GistPageIsLeaf(page) && GistPageHasGarbage(page))
258  {
259  gistvacuumpage(rel, page, buffer);
260  is_split = gistnospace(page, itup, ntup, oldoffnum, freespace);
261  }
262 
263  if (is_split)
264  {
265  /* no space for insertion */
266  IndexTuple *itvec;
267  int tlen;
268  SplitedPageLayout *dist = NULL,
269  *ptr;
270  BlockNumber oldrlink = InvalidBlockNumber;
271  GistNSN oldnsn = 0;
272  SplitedPageLayout rootpg;
273  bool is_rootsplit;
274  int npage;
275 
276  is_rootsplit = (blkno == GIST_ROOT_BLKNO);
277 
278  /*
279  * Form index tuples vector to split. If we're replacing an old tuple,
280  * remove the old version from the vector.
281  */
282  itvec = gistextractpage(page, &tlen);
283  if (OffsetNumberIsValid(oldoffnum))
284  {
285  /* on inner page we should remove old tuple */
286  int pos = oldoffnum - FirstOffsetNumber;
287 
288  tlen--;
289  if (pos != tlen)
290  memmove(itvec + pos, itvec + pos + 1, sizeof(IndexTuple) * (tlen - pos));
291  }
292  itvec = gistjoinvector(itvec, &tlen, itup, ntup);
293  dist = gistSplit(rel, page, itvec, tlen, giststate);
294 
295  /*
296  * Check that split didn't produce too many pages.
297  */
298  npage = 0;
299  for (ptr = dist; ptr; ptr = ptr->next)
300  npage++;
301  /* in a root split, we'll add one more page to the list below */
302  if (is_rootsplit)
303  npage++;
304  if (npage > GIST_MAX_SPLIT_PAGES)
305  elog(ERROR, "GiST page split into too many halves (%d, maximum %d)",
306  npage, GIST_MAX_SPLIT_PAGES);
307 
308  /*
309  * Set up pages to work with. Allocate new buffers for all but the
310  * leftmost page. The original page becomes the new leftmost page, and
311  * is just replaced with the new contents.
312  *
313  * For a root-split, allocate new buffers for all child pages, the
314  * original page is overwritten with new root page containing
315  * downlinks to the new child pages.
316  */
317  ptr = dist;
318  if (!is_rootsplit)
319  {
320  /* save old rightlink and NSN */
321  oldrlink = GistPageGetOpaque(page)->rightlink;
322  oldnsn = GistPageGetNSN(page);
323 
324  dist->buffer = buffer;
327 
328  /* clean all flags except F_LEAF */
329  GistPageGetOpaque(dist->page)->flags = (is_leaf) ? F_LEAF : 0;
330 
331  ptr = ptr->next;
332  }
333  for (; ptr; ptr = ptr->next)
334  {
335  /* Allocate new page */
336  ptr->buffer = gistNewBuffer(rel);
337  GISTInitBuffer(ptr->buffer, (is_leaf) ? F_LEAF : 0);
338  ptr->page = BufferGetPage(ptr->buffer);
339  ptr->block.blkno = BufferGetBlockNumber(ptr->buffer);
340  }
341 
342  /*
343  * Now that we know which blocks the new pages go to, set up downlink
344  * tuples to point to them.
345  */
346  for (ptr = dist; ptr; ptr = ptr->next)
347  {
348  ItemPointerSetBlockNumber(&(ptr->itup->t_tid), ptr->block.blkno);
349  GistTupleSetValid(ptr->itup);
350  }
351 
352  /*
353  * If this is a root split, we construct the new root page with the
354  * downlinks here directly, instead of requiring the caller to insert
355  * them. Add the new root page to the list along with the child pages.
356  */
357  if (is_rootsplit)
358  {
359  IndexTuple *downlinks;
360  int ndownlinks = 0;
361  int i;
362 
363  rootpg.buffer = buffer;
365  GistPageGetOpaque(rootpg.page)->flags = 0;
366 
367  /* Prepare a vector of all the downlinks */
368  for (ptr = dist; ptr; ptr = ptr->next)
369  ndownlinks++;
370  downlinks = palloc(sizeof(IndexTuple) * ndownlinks);
371  for (i = 0, ptr = dist; ptr; ptr = ptr->next)
372  downlinks[i++] = ptr->itup;
373 
374  rootpg.block.blkno = GIST_ROOT_BLKNO;
375  rootpg.block.num = ndownlinks;
376  rootpg.list = gistfillitupvec(downlinks, ndownlinks,
377  &(rootpg.lenlist));
378  rootpg.itup = NULL;
379 
380  rootpg.next = dist;
381  dist = &rootpg;
382  }
383  else
384  {
385  /* Prepare split-info to be returned to caller */
386  for (ptr = dist; ptr; ptr = ptr->next)
387  {
389 
390  si->buf = ptr->buffer;
391  si->downlink = ptr->itup;
392  *splitinfo = lappend(*splitinfo, si);
393  }
394  }
395 
396  /*
397  * Fill all pages. All the pages are new, ie. freshly allocated empty
398  * pages, or a temporary copy of the old page.
399  */
400  for (ptr = dist; ptr; ptr = ptr->next)
401  {
402  char *data = (char *) (ptr->list);
403 
404  for (i = 0; i < ptr->block.num; i++)
405  {
406  IndexTuple thistup = (IndexTuple) data;
407 
408  if (PageAddItem(ptr->page, (Item) data, IndexTupleSize(thistup), i + FirstOffsetNumber, false, false) == InvalidOffsetNumber)
409  elog(ERROR, "failed to add item to index page in \"%s\"", RelationGetRelationName(rel));
410 
411  /*
412  * If this is the first inserted/updated tuple, let the caller
413  * know which page it landed on.
414  */
415  if (newblkno && ItemPointerEquals(&thistup->t_tid, &(*itup)->t_tid))
416  *newblkno = ptr->block.blkno;
417 
418  data += IndexTupleSize(thistup);
419  }
420 
421  /* Set up rightlinks */
422  if (ptr->next && ptr->block.blkno != GIST_ROOT_BLKNO)
423  GistPageGetOpaque(ptr->page)->rightlink =
424  ptr->next->block.blkno;
425  else
426  GistPageGetOpaque(ptr->page)->rightlink = oldrlink;
427 
428  /*
429  * Mark the all but the right-most page with the follow-right
430  * flag. It will be cleared as soon as the downlink is inserted
431  * into the parent, but this ensures that if we error out before
432  * that, the index is still consistent. (in buffering build mode,
433  * any error will abort the index build anyway, so this is not
434  * needed.)
435  */
436  if (ptr->next && !is_rootsplit && markfollowright)
437  GistMarkFollowRight(ptr->page);
438  else
439  GistClearFollowRight(ptr->page);
440 
441  /*
442  * Copy the NSN of the original page to all pages. The
443  * F_FOLLOW_RIGHT flags ensure that scans will follow the
444  * rightlinks until the downlinks are inserted.
445  */
446  GistPageSetNSN(ptr->page, oldnsn);
447  }
448 
449  /*
450  * gistXLogSplit() needs to WAL log a lot of pages, prepare WAL
451  * insertion for that. NB: The number of pages and data segments
452  * specified here must match the calculations in gistXLogSplit()!
453  */
454  if (RelationNeedsWAL(rel))
455  XLogEnsureRecordSpace(npage, 1 + npage * 2);
456 
458 
459  /*
460  * Must mark buffers dirty before XLogInsert, even though we'll still
461  * be changing their opaque fields below.
462  */
463  for (ptr = dist; ptr; ptr = ptr->next)
464  MarkBufferDirty(ptr->buffer);
465  if (BufferIsValid(leftchildbuf))
466  MarkBufferDirty(leftchildbuf);
467 
468  /*
469  * The first page in the chain was a temporary working copy meant to
470  * replace the old page. Copy it over the old page.
471  */
473  dist->page = BufferGetPage(dist->buffer);
474 
475  /* Write the WAL record */
476  if (RelationNeedsWAL(rel))
477  recptr = gistXLogSplit(is_leaf,
478  dist, oldrlink, oldnsn, leftchildbuf,
479  markfollowright);
480  else
481  recptr = gistGetFakeLSN(rel);
482 
483  for (ptr = dist; ptr; ptr = ptr->next)
484  {
485  PageSetLSN(ptr->page, recptr);
486  }
487 
488  /*
489  * Return the new child buffers to the caller.
490  *
491  * If this was a root split, we've already inserted the downlink
492  * pointers, in the form of a new root page. Therefore we can release
493  * all the new buffers, and keep just the root page locked.
494  */
495  if (is_rootsplit)
496  {
497  for (ptr = dist->next; ptr; ptr = ptr->next)
498  UnlockReleaseBuffer(ptr->buffer);
499  }
500  }
501  else
502  {
503  /*
504  * Enough space. We always get here if ntup==0.
505  */
507 
508  /*
509  * Delete old tuple if any, then insert new tuple(s) if any. If
510  * possible, use the fast path of PageIndexTupleOverwrite.
511  */
512  if (OffsetNumberIsValid(oldoffnum))
513  {
514  if (ntup == 1)
515  {
516  /* One-for-one replacement, so use PageIndexTupleOverwrite */
517  if (!PageIndexTupleOverwrite(page, oldoffnum, (Item) *itup,
518  IndexTupleSize(*itup)))
519  elog(ERROR, "failed to add item to index page in \"%s\"",
521  }
522  else
523  {
524  /* Delete old, then append new tuple(s) to page */
525  PageIndexTupleDelete(page, oldoffnum);
526  gistfillbuffer(page, itup, ntup, InvalidOffsetNumber);
527  }
528  }
529  else
530  {
531  /* Just append new tuples at the end of the page */
532  gistfillbuffer(page, itup, ntup, InvalidOffsetNumber);
533  }
534 
536 
537  if (BufferIsValid(leftchildbuf))
538  MarkBufferDirty(leftchildbuf);
539 
540  if (RelationNeedsWAL(rel))
541  {
542  OffsetNumber ndeloffs = 0,
543  deloffs[1];
544 
545  if (OffsetNumberIsValid(oldoffnum))
546  {
547  deloffs[0] = oldoffnum;
548  ndeloffs = 1;
549  }
550 
551  recptr = gistXLogUpdate(buffer,
552  deloffs, ndeloffs, itup, ntup,
553  leftchildbuf);
554 
555  PageSetLSN(page, recptr);
556  }
557  else
558  {
559  recptr = gistGetFakeLSN(rel);
560  PageSetLSN(page, recptr);
561  }
562 
563  if (newblkno)
564  *newblkno = blkno;
565  }
566 
567  /*
568  * If we inserted the downlink for a child page, set NSN and clear
569  * F_FOLLOW_RIGHT flag on the left child, so that concurrent scans know to
570  * follow the rightlink if and only if they looked at the parent page
571  * before we inserted the downlink.
572  *
573  * Note that we do this *after* writing the WAL record. That means that
574  * the possible full page image in the WAL record does not include these
575  * changes, and they must be replayed even if the page is restored from
576  * the full page image. There's a chicken-and-egg problem: if we updated
577  * the child pages first, we wouldn't know the recptr of the WAL record
578  * we're about to write.
579  */
580  if (BufferIsValid(leftchildbuf))
581  {
582  Page leftpg = BufferGetPage(leftchildbuf);
583 
584  GistPageSetNSN(leftpg, recptr);
585  GistClearFollowRight(leftpg);
586 
587  PageSetLSN(leftpg, recptr);
588  }
589 
591 
592  return is_split;
593 }
#define GistFollowRight(page)
Definition: gist.h:147
#define NIL
Definition: pg_list.h:69
#define GistPageGetNSN(page)
Definition: gist.h:151
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:407
BlockNumber blkno
Definition: gist_private.h:182
void PageIndexTupleDelete(Page page, OffsetNumber offnum)
Definition: bufpage.c:723
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
ItemPointerData t_tid
Definition: itup.h:37
void gistfillbuffer(Page page, IndexTuple *itup, int len, OffsetNumber off)
Definition: gistutil.c:33
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
Pointer Item
Definition: item.h:17
#define GistPageSetNSN(page, val)
Definition: gist.h:152
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:412
#define GistPageHasGarbage(page)
Definition: gist.h:143
uint32 BlockNumber
Definition: block.h:31
IndexTupleData * list
Definition: gist_private.h:190
XLogRecPtr gistGetFakeLSN(Relation rel)
Definition: gistutil.c:980
uint16 OffsetNumber
Definition: off.h:24
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:385
gistxlogPage block
Definition: gist_private.h:189
XLogRecPtr gistXLogSplit(bool page_is_leaf, SplitedPageLayout *dist, BlockNumber origrlink, GistNSN orignsn, Buffer leftchildbuf, bool markfollowright)
Definition: gistxlog.c:393
IndexTuple * gistextractpage(Page page, int *len)
Definition: gistutil.c:94
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
#define GIST_MAX_SPLIT_PAGES
Definition: gist_private.h:40
IndexTuple downlink
Definition: gist_private.h:398
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
Definition: bufpage.c:1062
#define memmove(d, s, c)
Definition: c.h:1069
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
bool gistnospace(Page page, IndexTuple *itvec, int len, OffsetNumber todelete, Size freespace)
Definition: gistutil.c:58
#define RelationGetRelationName(relation)
Definition: rel.h:445
struct SplitedPageLayout * next
Definition: gist_private.h:196
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
static void gistvacuumpage(Relation rel, Page page, Buffer buffer)
Definition: gist.c:1534
List * lappend(List *list, void *datum)
Definition: list.c:128
#define GistPageIsLeaf(page)
Definition: gist.h:132
#define GistTupleSetValid(itup)
Definition: gist_private.h:276
#define GistClearFollowRight(page)
Definition: gist.h:149
IndexTuple * gistjoinvector(IndexTuple *itvec, int *len, IndexTuple *additvec, int addlen)
Definition: gistutil.c:113
SplitedPageLayout * gistSplit(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate)
Definition: gist.c:1339
#define InvalidOffsetNumber
Definition: off.h:26
XLogRecPtr gistXLogUpdate(Buffer buffer, OffsetNumber *todelete, int ntodelete, IndexTuple *itup, int ituplen, Buffer leftchildbuf)
Definition: gistxlog.c:457
#define GistPageGetOpaque(page)
Definition: gist.h:130
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
IndexTupleData * gistfillitupvec(IndexTuple *vec, int veclen, int *memlen)
Definition: gistutil.c:126
#define InvalidBlockNumber
Definition: block.h:33
void XLogEnsureRecordSpace(int max_block_id, int ndatas)
Definition: xloginsert.c:146
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define ItemPointerSetBlockNumber(pointer, blockNumber)
Definition: itemptr.h:116
#define GistMarkFollowRight(page)
Definition: gist.h:148
#define RelationNeedsWAL(relation)
Definition: rel.h:514
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
XLogRecPtr GistNSN
Definition: gist.h:50
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
void * palloc(Size size)
Definition: mcxt.c:835
#define F_LEAF
Definition: gist.h:42
int i
#define GIST_ROOT_BLKNO
Definition: gist_private.h:249
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
void GISTInitBuffer(Buffer b, uint32 f)
Definition: gistutil.c:721
#define elog
Definition: elog.h:219
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
Buffer gistNewBuffer(Relation r)
Definition: gistutil.c:782
Pointer Page
Definition: bufpage.h:74
#define IndexTupleSize(itup)
Definition: itup.h:70

◆ gistSplit()

SplitedPageLayout* gistSplit ( Relation  r,
Page  page,
IndexTuple itup,
int  len,
GISTSTATE giststate 
)

Definition at line 1339 of file gist.c.

References Assert, SplitedPageLayout::block, check_stack_depth(), ereport, errcode(), errmsg(), ERROR, gistfillitupvec(), gistfitpage(), gistFormTuple(), GiSTPageSize, gistSplit(), gistSplitByKey(), i, IndexTupleSize, SplitedPageLayout::itup, SplitedPageLayout::lenlist, SplitedPageLayout::list, tupleDesc::natts, SplitedPageLayout::next, gistxlogPage::num, palloc(), RelationGetRelationName, ROTATEDIST, GistSplitVector::spl_lattr, GIST_SPLITVEC::spl_left, GistSplitVector::spl_lisnull, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GistSplitVector::spl_rattr, GIST_SPLITVEC::spl_right, GistSplitVector::spl_risnull, GistSplitVector::splitVector, and GISTSTATE::tupdesc.

Referenced by gistplacetopage(), and gistSplit().

1344 {
1345  IndexTuple *lvectup,
1346  *rvectup;
1347  GistSplitVector v;
1348  int i;
1349  SplitedPageLayout *res = NULL;
1350 
1351  /* this should never recurse very deeply, but better safe than sorry */
1353 
1354  /* there's no point in splitting an empty page */
1355  Assert(len > 0);
1356 
1357  /*
1358  * If a single tuple doesn't fit on a page, no amount of splitting will
1359  * help.
1360  */
1361  if (len == 1)
1362  ereport(ERROR,
1363  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1364  errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
1365  IndexTupleSize(itup[0]), GiSTPageSize,
1367 
1368  memset(v.spl_lisnull, true, sizeof(bool) * giststate->tupdesc->natts);
1369  memset(v.spl_risnull, true, sizeof(bool) * giststate->tupdesc->natts);
1370  gistSplitByKey(r, page, itup, len, giststate, &v, 0);
1371 
1372  /* form left and right vector */
1373  lvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
1374  rvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
1375 
1376  for (i = 0; i < v.splitVector.spl_nleft; i++)
1377  lvectup[i] = itup[v.splitVector.spl_left[i] - 1];
1378 
1379  for (i = 0; i < v.splitVector.spl_nright; i++)
1380  rvectup[i] = itup[v.splitVector.spl_right[i] - 1];
1381 
1382  /* finalize splitting (may need another split) */
1383  if (!gistfitpage(rvectup, v.splitVector.spl_nright))
1384  {
1385  res = gistSplit(r, page, rvectup, v.splitVector.spl_nright, giststate);
1386  }
1387  else
1388  {
1389  ROTATEDIST(res);
1390  res->block.num = v.splitVector.spl_nright;
1391  res->list = gistfillitupvec(rvectup, v.splitVector.spl_nright, &(res->lenlist));
1392  res->itup = gistFormTuple(giststate, r, v.spl_rattr, v.spl_risnull, false);
1393  }
1394 
1395  if (!gistfitpage(lvectup, v.splitVector.spl_nleft))
1396  {
1397  SplitedPageLayout *resptr,
1398  *subres;
1399 
1400  resptr = subres = gistSplit(r, page, lvectup, v.splitVector.spl_nleft, giststate);
1401 
1402  /* install on list's tail */
1403  while (resptr->next)
1404  resptr = resptr->next;
1405 
1406  resptr->next = res;
1407  res = subres;
1408  }
1409  else
1410  {
1411  ROTATEDIST(res);
1412  res->block.num = v.splitVector.spl_nleft;
1413  res->list = gistfillitupvec(lvectup, v.splitVector.spl_nleft, &(res->lenlist));
1414  res->itup = gistFormTuple(giststate, r, v.spl_lattr, v.spl_lisnull, false);
1415  }
1416 
1417  return res;
1418 }
TupleDesc tupdesc
Definition: gist_private.h:81
Datum spl_lattr[INDEX_MAX_KEYS]
Definition: gist_private.h:228
OffsetNumber * spl_left
Definition: gist.h:105
GIST_SPLITVEC splitVector
Definition: gist_private.h:226
int errcode(int sqlerrcode)
Definition: elog.c:575
int spl_nleft
Definition: gist.h:106
IndexTupleData * list
Definition: gist_private.h:190
void gistSplitByKey(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate, GistSplitVector *v, int attno)
Definition: gistsplit.c:623
Datum spl_rattr[INDEX_MAX_KEYS]
Definition: gist_private.h:232
int natts
Definition: tupdesc.h:79
gistxlogPage block
Definition: gist_private.h:189
#define ERROR
Definition: elog.h:43
int spl_nright
Definition: gist.h:111
void check_stack_depth(void)
Definition: postgres.c:3154
#define RelationGetRelationName(relation)
Definition: rel.h:445
struct SplitedPageLayout * next
Definition: gist_private.h:196
#define ereport(elevel, rest)
Definition: elog.h:122
#define GiSTPageSize
Definition: gist_private.h:432
#define ROTATEDIST(d)
Definition: gist.c:42
SplitedPageLayout * gistSplit(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate)
Definition: gist.c:1339
bool gistfitpage(IndexTuple *itvec, int len)
Definition: gistutil.c:78
#define Assert(condition)
Definition: c.h:680
IndexTupleData * gistfillitupvec(IndexTuple *vec, int veclen, int *memlen)
Definition: gistutil.c:126
OffsetNumber * spl_right
Definition: gist.h:110
bool spl_risnull[INDEX_MAX_KEYS]
Definition: gist_private.h:234
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:572
bool spl_lisnull[INDEX_MAX_KEYS]
Definition: gist_private.h:230
#define IndexTupleSize(itup)
Definition: itup.h:70

◆ gistvacuumpage()

static void gistvacuumpage ( Relation  rel,
Page  page,
Buffer  buffer 
)
static

Definition at line 1534 of file gist.c.

References Assert, END_CRIT_SECTION, FirstOffsetNumber, GistClearPageHasGarbage, gistGetFakeLSN(), GistPageIsLeaf, gistXLogUpdate(), InvalidBuffer, ItemIdIsDead, MarkBufferDirty(), MaxIndexTuplesPerPage, OffsetNumberNext, PageGetItemId, PageGetMaxOffsetNumber, PageIndexMultiDelete(), PageSetLSN, RelationNeedsWAL, and START_CRIT_SECTION.

Referenced by gistplacetopage().

1535 {
1537  int ndeletable = 0;
1538  OffsetNumber offnum,
1539  maxoff;
1540 
1541  Assert(GistPageIsLeaf(page));
1542 
1543  /*
1544  * Scan over all items to see which ones need to be deleted according to
1545  * LP_DEAD flags.
1546  */
1547  maxoff = PageGetMaxOffsetNumber(page);
1548  for (offnum = FirstOffsetNumber;
1549  offnum <= maxoff;
1550  offnum = OffsetNumberNext(offnum))
1551  {
1552  ItemId itemId = PageGetItemId(page, offnum);
1553 
1554  if (ItemIdIsDead(itemId))
1555  deletable[ndeletable++] = offnum;
1556  }
1557 
1558  if (ndeletable > 0)
1559  {
1561 
1562  PageIndexMultiDelete(page, deletable, ndeletable);
1563 
1564  /*
1565  * Mark the page as not containing any LP_DEAD items. This is not
1566  * certainly true (there might be some that have recently been marked,
1567  * but weren't included in our target-item list), but it will almost
1568  * always be true and it doesn't seem worth an additional page scan to
1569  * check it. Remember that F_HAS_GARBAGE is only a hint anyway.
1570  */
1572 
1574 
1575  /* XLOG stuff */
1576  if (RelationNeedsWAL(rel))
1577  {
1578  XLogRecPtr recptr;
1579 
1580  recptr = gistXLogUpdate(buffer,
1581  deletable, ndeletable,
1582  NULL, 0, InvalidBuffer);
1583 
1584  PageSetLSN(page, recptr);
1585  }
1586  else
1587  PageSetLSN(page, gistGetFakeLSN(rel));
1588 
1589  END_CRIT_SECTION();
1590  }
1591 
1592  /*
1593  * Note: if we didn't find any LP_DEAD items, then the page's
1594  * F_HAS_GARBAGE hint bit is falsely set. We do not bother expending a
1595  * separate write to clear it, however. We will clear it when we split
1596  * the page.
1597  */
1598 }
#define GistClearPageHasGarbage(page)
Definition: gist.h:145
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
#define InvalidBuffer
Definition: buf.h:25
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
XLogRecPtr gistGetFakeLSN(Relation rel)
Definition: gistutil.c:980
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
#define GistPageIsLeaf(page)
Definition: gist.h:132
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
XLogRecPtr gistXLogUpdate(Buffer buffer, OffsetNumber *todelete, int ntodelete, IndexTuple *itup, int ituplen, Buffer leftchildbuf)
Definition: gistxlog.c:457
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:680
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:832
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define RelationNeedsWAL(relation)
Definition: rel.h:514
#define MaxIndexTuplesPerPage
Definition: itup.h:137
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364

◆ initGISTstate()

GISTSTATE* initGISTstate ( Relation  index)

Definition at line 1424 of file gist.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, GISTSTATE::compressFn, GISTSTATE::consistentFn, CurrentMemoryContext, GISTSTATE::decompressFn, DEFAULT_COLLATION_OID, GISTSTATE::distanceFn, elog, GISTSTATE::equalFn, ERROR, GISTSTATE::fetchFn, fmgr_info_copy(), FmgrInfo::fn_oid, GIST_COMPRESS_PROC, GIST_CONSISTENT_PROC, GIST_DECOMPRESS_PROC, GIST_DISTANCE_PROC, GIST_EQUAL_PROC, GIST_FETCH_PROC, GIST_PENALTY_PROC, GIST_PICKSPLIT_PROC, GIST_UNION_PROC, i, index_getprocid(), index_getprocinfo(), INDEX_MAX_KEYS, InvalidOid, MemoryContextSwitchTo(), tupleDesc::natts, OidIsValid, palloc(), GISTSTATE::penaltyFn, GISTSTATE::picksplitFn, RelationData::rd_att, RelationData::rd_indcollation, GISTSTATE::scanCxt, GISTSTATE::supportCollation, GISTSTATE::tempCxt, GISTSTATE::tupdesc, and GISTSTATE::unionFn.

Referenced by gistbeginscan(), gistbuild(), and gistinsert().

1425 {
1426  GISTSTATE *giststate;
1427  MemoryContext scanCxt;
1428  MemoryContext oldCxt;
1429  int i;
1430 
1431  /* safety check to protect fixed-size arrays in GISTSTATE */
1432  if (index->rd_att->natts > INDEX_MAX_KEYS)
1433  elog(ERROR, "numberOfAttributes %d > %d",
1434  index->rd_att->natts, INDEX_MAX_KEYS);
1435 
1436  /* Create the memory context that will hold the GISTSTATE */
1438  "GiST scan context",
1440  oldCxt = MemoryContextSwitchTo(scanCxt);
1441 
1442  /* Create and fill in the GISTSTATE */
1443  giststate = (GISTSTATE *) palloc(sizeof(GISTSTATE));
1444 
1445  giststate->scanCxt = scanCxt;
1446  giststate->tempCxt = scanCxt; /* caller must change this if needed */
1447  giststate->tupdesc = index->rd_att;
1448 
1449  for (i = 0; i < index->rd_att->natts; i++)
1450  {
1451  fmgr_info_copy(&(giststate->consistentFn[i]),
1452  index_getprocinfo(index, i + 1, GIST_CONSISTENT_PROC),
1453  scanCxt);
1454  fmgr_info_copy(&(giststate->unionFn[i]),
1455  index_getprocinfo(index, i + 1, GIST_UNION_PROC),
1456  scanCxt);
1457 
1458  /* opclasses are not required to provide a Compress method */
1459  if (OidIsValid(index_getprocid(index, i + 1, GIST_COMPRESS_PROC)))
1460  fmgr_info_copy(&(giststate->compressFn[i]),
1461  index_getprocinfo(index, i + 1, GIST_COMPRESS_PROC),
1462  scanCxt);
1463  else
1464  giststate->compressFn[i].fn_oid = InvalidOid;
1465 
1466  /* opclasses are not required to provide a Decompress method */
1467  if (OidIsValid(index_getprocid(index, i + 1, GIST_DECOMPRESS_PROC)))
1468  fmgr_info_copy(&(giststate->decompressFn[i]),
1469  index_getprocinfo(index, i + 1, GIST_DECOMPRESS_PROC),
1470  scanCxt);
1471  else
1472  giststate->decompressFn[i].fn_oid = InvalidOid;
1473 
1474  fmgr_info_copy(&(giststate->penaltyFn[i]),
1475  index_getprocinfo(index, i + 1, GIST_PENALTY_PROC),
1476  scanCxt);
1477  fmgr_info_copy(&(giststate->picksplitFn[i]),
1478  index_getprocinfo(index, i + 1, GIST_PICKSPLIT_PROC),
1479  scanCxt);
1480  fmgr_info_copy(&(giststate->equalFn[i]),
1481  index_getprocinfo(index, i + 1, GIST_EQUAL_PROC),
1482  scanCxt);
1483 
1484  /* opclasses are not required to provide a Distance method */
1485  if (OidIsValid(index_getprocid(index, i + 1, GIST_DISTANCE_PROC)))
1486  fmgr_info_copy(&(giststate->distanceFn[i]),
1487  index_getprocinfo(index, i + 1, GIST_DISTANCE_PROC),
1488  scanCxt);
1489  else
1490  giststate->distanceFn[i].fn_oid = InvalidOid;
1491 
1492  /* opclasses are not required to provide a Fetch method */
1493  if (OidIsValid(index_getprocid(index, i + 1, GIST_FETCH_PROC)))
1494  fmgr_info_copy(&(giststate->fetchFn[i]),
1495  index_getprocinfo(index, i + 1, GIST_FETCH_PROC),
1496  scanCxt);
1497  else
1498  giststate->fetchFn[i].fn_oid = InvalidOid;
1499 
1500  /*
1501  * If the index column has a specified collation, we should honor that
1502  * while doing comparisons. However, we may have a collatable storage
1503  * type for a noncollatable indexed data type. If there's no index
1504  * collation then specify default collation in case the support
1505  * functions need collation. This is harmless if the support
1506  * functions don't care about collation, so we just do it
1507  * unconditionally. (We could alternatively call get_typcollation,
1508  * but that seems like expensive overkill --- there aren't going to be
1509  * any cases where a GiST storage type has a nondefault collation.)
1510  */
1511  if (OidIsValid(index->rd_indcollation[i]))
1512  giststate->supportCollation[i] = index->rd_indcollation[i];
1513  else
1515  }
1516 
1517  MemoryContextSwitchTo(oldCxt);
1518 
1519  return giststate;
1520 }
TupleDesc tupdesc
Definition: gist_private.h:81
#define GIST_FETCH_PROC
Definition: gist.h:36
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
FmgrInfo fetchFn[INDEX_MAX_KEYS]
Definition: gist_private.h:93
#define GIST_EQUAL_PROC
Definition: gist.h:34
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gist_private.h:96
FmgrInfo compressFn[INDEX_MAX_KEYS]
Definition: gist_private.h:87
FmgrInfo equalFn[INDEX_MAX_KEYS]
Definition: gist_private.h:91
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define OidIsValid(objectId)
Definition: c.h:586
int natts
Definition: tupdesc.h:79
FmgrInfo consistentFn[INDEX_MAX_KEYS]
Definition: gist_private.h:85
#define GIST_PICKSPLIT_PROC
Definition: gist.h:33
Oid * rd_indcollation
Definition: rel.h:193
#define ERROR
Definition: elog.h:43
#define GIST_COMPRESS_PROC
Definition: gist.h:30
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:519
MemoryContext tempCxt
Definition: gist_private.h:79
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
FmgrInfo picksplitFn[INDEX_MAX_KEYS]
Definition: gist_private.h:90
FmgrInfo penaltyFn[INDEX_MAX_KEYS]
Definition: gist_private.h:89
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
FmgrInfo decompressFn[INDEX_MAX_KEYS]
Definition: gist_private.h:88
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:165
TupleDesc rd_att
Definition: rel.h:115
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define GIST_CONSISTENT_PROC
Definition: gist.h:28
#define GIST_UNION_PROC
Definition: gist.h:29
#define INDEX_MAX_KEYS
#define GIST_PENALTY_PROC
Definition: gist.h:32
#define GIST_DISTANCE_PROC
Definition: gist.h:35
FmgrInfo distanceFn[INDEX_MAX_KEYS]
Definition: gist_private.h:92
void * palloc(Size size)
Definition: mcxt.c:835
int i
MemoryContext scanCxt
Definition: gist_private.h:78
#define GIST_DECOMPRESS_PROC
Definition: gist.h:31
#define elog
Definition: elog.h:219
FmgrInfo unionFn[INDEX_MAX_KEYS]
Definition: gist_private.h:86
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:821