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 "commands/vacuum.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "storage/lmgr.h"
#include "storage/predicate.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 unlockbuf)
 
static void gistprunepage (Relation rel, Page page, Buffer buffer, Relation heapRel)
 
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, Relation heapRel, bool is_build)
 
void gistdoinsert (Relation r, IndexTuple itup, Size freespace, GISTSTATE *giststate, Relation heapRel, bool is_build)
 
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*)palloc0(sizeof(SplitedPageLayout)); \
tmp->block.blkno = InvalidBlockNumber; \
tmp->buffer = InvalidBuffer; \
tmp->next = (d); \
(d)=tmp; \
} while(0)
#define InvalidBuffer
Definition: buf.h:25
void * palloc0(Size size)
Definition: mcxt.c:980
#define InvalidBlockNumber
Definition: block.h:33

Definition at line 45 of file gist.c.

Referenced by gistSplit().

Function Documentation

◆ createTempGistContext()

MemoryContext createTempGistContext ( void  )

Definition at line 119 of file gist.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, and CurrentMemoryContext.

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

120 {
122  "GiST temporary context",
124 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38

◆ freeGISTstate()

void freeGISTstate ( GISTSTATE giststate)

Definition at line 1626 of file gist.c.

References MemoryContextDelete(), and GISTSTATE::scanCxt.

Referenced by gistbuild(), and gistendscan().

1627 {
1628  /* It's sufficient to delete the scanCxt */
1629  MemoryContextDelete(giststate->scanCxt);
1630 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
MemoryContext scanCxt
Definition: gist_private.h:77

◆ gistbuildempty()

void gistbuildempty ( Relation  index)

Definition at line 130 of file gist.c.

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

131 {
132  Buffer buffer;
133 
134  /* Initialize the root page */
135  buffer = ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
137 
138  /* Initialize and xlog buffer */
140  GISTInitBuffer(buffer, F_LEAF);
141  MarkBufferDirty(buffer);
142  log_newpage_buffer(buffer, true);
144 
145  /* Unlock and release the buffer */
146  UnlockReleaseBuffer(buffer);
147 }
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1033
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:653
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#define P_NEW
Definition: bufmgr.h:91
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
#define F_LEAF
Definition: gist.h:45
void GISTInitBuffer(Buffer b, uint32 f)
Definition: gistutil.c:748
int Buffer
Definition: buf.h:23

◆ gistdoinsert()

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

Definition at line 628 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, GistPageIsDeleted, GistPageIsLeaf, GistTupleIsInvalid, GISTInsertState::heapRel, InvalidOffsetNumber, GISTInsertState::is_build, ItemPointerGetBlockNumber, LockBuffer(), GISTInsertStack::lsn, GISTInsertStack::page, PageGetItem, PageGetItemId, PageGetLSN, palloc0(), GISTInsertStack::parent, GISTInsertState::r, ReadBuffer(), RelationGetRelationName, RelationNeedsWAL, ReleaseBuffer(), GISTInsertStack::retry_from_parent, GISTInsertState::stack, IndexTupleData::t_tid, UnlockReleaseBuffer(), and XLogRecPtrIsInvalid.

Referenced by gistBuildCallback(), and gistinsert().

630 {
631  ItemId iid;
632  IndexTuple idxtuple;
633  GISTInsertStack firststack;
634  GISTInsertStack *stack;
636  bool xlocked = false;
637 
638  memset(&state, 0, sizeof(GISTInsertState));
639  state.freespace = freespace;
640  state.r = r;
641  state.heapRel = heapRel;
642  state.is_build = is_build;
643 
644  /* Start from the root */
645  firststack.blkno = GIST_ROOT_BLKNO;
646  firststack.lsn = 0;
647  firststack.retry_from_parent = false;
648  firststack.parent = NULL;
649  firststack.downlinkoffnum = InvalidOffsetNumber;
650  state.stack = stack = &firststack;
651 
652  /*
653  * Walk down along the path of smallest penalty, updating the parent
654  * pointers with the key we're inserting as we go. If we crash in the
655  * middle, the tree is consistent, although the possible parent updates
656  * were a waste.
657  */
658  for (;;)
659  {
660  /*
661  * If we split an internal page while descending the tree, we have to
662  * retry at the parent. (Normally, the LSN-NSN interlock below would
663  * also catch this and cause us to retry. But LSNs are not updated
664  * during index build.)
665  */
666  while (stack->retry_from_parent)
667  {
668  if (xlocked)
669  LockBuffer(stack->buffer, GIST_UNLOCK);
670  xlocked = false;
671  ReleaseBuffer(stack->buffer);
672  state.stack = stack = stack->parent;
673  }
674 
675  if (XLogRecPtrIsInvalid(stack->lsn))
676  stack->buffer = ReadBuffer(state.r, stack->blkno);
677 
678  /*
679  * Be optimistic and grab shared lock first. Swap it for an exclusive
680  * lock later if we need to update the page.
681  */
682  if (!xlocked)
683  {
684  LockBuffer(stack->buffer, GIST_SHARE);
685  gistcheckpage(state.r, stack->buffer);
686  }
687 
688  stack->page = (Page) BufferGetPage(stack->buffer);
689  stack->lsn = xlocked ?
690  PageGetLSN(stack->page) : BufferGetLSNAtomic(stack->buffer);
691  Assert(!RelationNeedsWAL(state.r) || !XLogRecPtrIsInvalid(stack->lsn));
692 
693  /*
694  * If this page was split but the downlink was never inserted to the
695  * parent because the inserting backend crashed before doing that, fix
696  * that now.
697  */
698  if (GistFollowRight(stack->page))
699  {
700  if (!xlocked)
701  {
702  LockBuffer(stack->buffer, GIST_UNLOCK);
704  xlocked = true;
705  /* someone might've completed the split when we unlocked */
706  if (!GistFollowRight(stack->page))
707  continue;
708  }
709  gistfixsplit(&state, giststate);
710 
711  UnlockReleaseBuffer(stack->buffer);
712  xlocked = false;
713  state.stack = stack = stack->parent;
714  continue;
715  }
716 
717  if ((stack->blkno != GIST_ROOT_BLKNO &&
718  stack->parent->lsn < GistPageGetNSN(stack->page)) ||
719  GistPageIsDeleted(stack->page))
720  {
721  /*
722  * Concurrent split or page deletion detected. There's no
723  * guarantee that the downlink for this page is consistent with
724  * the tuple we're inserting anymore, so go back to parent and
725  * rechoose the best child.
726  */
727  UnlockReleaseBuffer(stack->buffer);
728  xlocked = false;
729  state.stack = stack = stack->parent;
730  continue;
731  }
732 
733  if (!GistPageIsLeaf(stack->page))
734  {
735  /*
736  * This is an internal page so continue to walk down the tree.
737  * Find the child node that has the minimum insertion penalty.
738  */
739  BlockNumber childblkno;
740  IndexTuple newtup;
741  GISTInsertStack *item;
742  OffsetNumber downlinkoffnum;
743 
744  downlinkoffnum = gistchoose(state.r, stack->page, itup, giststate);
745  iid = PageGetItemId(stack->page, downlinkoffnum);
746  idxtuple = (IndexTuple) PageGetItem(stack->page, iid);
747  childblkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
748 
749  /*
750  * Check that it's not a leftover invalid tuple from pre-9.1
751  */
752  if (GistTupleIsInvalid(idxtuple))
753  ereport(ERROR,
754  (errmsg("index \"%s\" contains an inner tuple marked as invalid",
756  errdetail("This is caused by an incomplete page split at crash recovery before upgrading to PostgreSQL 9.1."),
757  errhint("Please REINDEX it.")));
758 
759  /*
760  * Check that the key representing the target child node is
761  * consistent with the key we're inserting. Update it if it's not.
762  */
763  newtup = gistgetadjusted(state.r, idxtuple, itup, giststate);
764  if (newtup)
765  {
766  /*
767  * Swap shared lock for an exclusive one. Beware, the page may
768  * change while we unlock/lock the page...
769  */
770  if (!xlocked)
771  {
772  LockBuffer(stack->buffer, GIST_UNLOCK);
774  xlocked = true;
775  stack->page = (Page) BufferGetPage(stack->buffer);
776 
777  if (PageGetLSN(stack->page) != stack->lsn)
778  {
779  /* the page was changed while we unlocked it, retry */
780  continue;
781  }
782  }
783 
784  /*
785  * Update the tuple.
786  *
787  * We still hold the lock after gistinserttuple(), but it
788  * might have to split the page to make the updated tuple fit.
789  * In that case the updated tuple might migrate to the other
790  * half of the split, so we have to go back to the parent and
791  * descend back to the half that's a better fit for the new
792  * tuple.
793  */
794  if (gistinserttuple(&state, stack, giststate, newtup,
795  downlinkoffnum))
796  {
797  /*
798  * If this was a root split, the root page continues to be
799  * the parent and the updated tuple went to one of the
800  * child pages, so we just need to retry from the root
801  * page.
802  */
803  if (stack->blkno != GIST_ROOT_BLKNO)
804  {
805  UnlockReleaseBuffer(stack->buffer);
806  xlocked = false;
807  state.stack = stack = stack->parent;
808  }
809  continue;
810  }
811  }
812  LockBuffer(stack->buffer, GIST_UNLOCK);
813  xlocked = false;
814 
815  /* descend to the chosen child */
816  item = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
817  item->blkno = childblkno;
818  item->parent = stack;
819  item->downlinkoffnum = downlinkoffnum;
820  state.stack = stack = item;
821  }
822  else
823  {
824  /*
825  * Leaf page. Insert the new key. We've already updated all the
826  * parents on the way down, but we might have to split the page if
827  * it doesn't fit. gistinserttuple() will take care of that.
828  */
829 
830  /*
831  * Swap shared lock for an exclusive one. Be careful, the page may
832  * change while we unlock/lock the page...
833  */
834  if (!xlocked)
835  {
836  LockBuffer(stack->buffer, GIST_UNLOCK);
838  xlocked = true;
839  stack->page = (Page) BufferGetPage(stack->buffer);
840  stack->lsn = PageGetLSN(stack->page);
841 
842  if (stack->blkno == GIST_ROOT_BLKNO)
843  {
844  /*
845  * the only page that can become inner instead of leaf is
846  * the root page, so for root we should recheck it
847  */
848  if (!GistPageIsLeaf(stack->page))
849  {
850  /*
851  * very rare situation: during unlock/lock index with
852  * number of pages = 1 was increased
853  */
854  LockBuffer(stack->buffer, GIST_UNLOCK);
855  xlocked = false;
856  continue;
857  }
858 
859  /*
860  * we don't need to check root split, because checking
861  * leaf/inner is enough to recognize split for root
862  */
863  }
864  else if ((GistFollowRight(stack->page) ||
865  stack->parent->lsn < GistPageGetNSN(stack->page)) &&
866  GistPageIsDeleted(stack->page))
867  {
868  /*
869  * The page was split or deleted while we momentarily
870  * unlocked the page. Go back to parent.
871  */
872  UnlockReleaseBuffer(stack->buffer);
873  xlocked = false;
874  state.stack = stack = stack->parent;
875  continue;
876  }
877  }
878 
879  /* now state.stack->(page, buffer and blkno) points to leaf page */
880 
881  gistinserttuple(&state, stack, giststate, itup,
883  LockBuffer(stack->buffer, GIST_UNLOCK);
884 
885  /* Release any pins we might still hold before exiting */
886  for (; stack; stack = stack->parent)
887  ReleaseBuffer(stack->buffer);
888  break;
889  }
890  }
891 }
#define GistFollowRight(page)
Definition: gist.h:173
BlockNumber blkno
Definition: gist_private.h:210
#define GistPageGetNSN(page)
Definition: gist.h:177
static bool gistinserttuple(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, IndexTuple tuple, OffsetNumber oldoffnum)
Definition: gist.c:1222
#define GistPageIsDeleted(page)
Definition: gist.h:163
int errhint(const char *fmt,...)
Definition: elog.c:1071
static void gistfixsplit(GISTInsertState *state, GISTSTATE *giststate)
Definition: gist.c:1163
Relation heapRel
Definition: gist_private.h:254
ItemPointerData t_tid
Definition: itup.h:37
#define GistTupleIsInvalid(itup)
Definition: gist_private.h:288
#define GIST_UNLOCK
Definition: gist_private.h:44
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3518
IndexTuple gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
Definition: gistutil.c:315
uint16 OffsetNumber
Definition: off.h:24
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define ERROR
Definition: elog.h:43
GISTInsertStack * stack
Definition: gist_private.h:258
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2931
IndexTupleData * IndexTuple
Definition: itup.h:53
int errdetail(const char *fmt,...)
Definition: elog.c:957
#define RelationGetRelationName(relation)
Definition: rel.h:490
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
OffsetNumber gistchoose(Relation r, Page p, IndexTuple it, GISTSTATE *giststate)
Definition: gistutil.c:373
#define GistPageIsLeaf(page)
Definition: gist.h:160
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
OffsetNumber downlinkoffnum
Definition: gist_private.h:228
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void * palloc0(Size size)
Definition: mcxt.c:980
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
#define InvalidOffsetNumber
Definition: off.h:26
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
Definition: regguts.h:298
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:770
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
#define GIST_SHARE
Definition: gist_private.h:42
#define RelationNeedsWAL(relation)
Definition: rel.h:562
#define PageGetLSN(page)
Definition: bufpage.h:366
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define GIST_ROOT_BLKNO
Definition: gist_private.h:262
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
#define GIST_EXCLUSIVE
Definition: gist_private.h:43
struct GISTInsertStack * parent
Definition: gist_private.h:231
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78

◆ gistFindCorrectParent()

static void gistFindCorrectParent ( Relation  r,
GISTInsertStack child 
)
static

Definition at line 1016 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().

1017 {
1018  GISTInsertStack *parent = child->parent;
1019 
1020  gistcheckpage(r, parent->buffer);
1021  parent->page = (Page) BufferGetPage(parent->buffer);
1022 
1023  /* here we don't need to distinguish between split and page update */
1024  if (child->downlinkoffnum == InvalidOffsetNumber ||
1025  parent->lsn != PageGetLSN(parent->page))
1026  {
1027  /* parent is changed, look child in right links until found */
1028  OffsetNumber i,
1029  maxoff;
1030  ItemId iid;
1031  IndexTuple idxtuple;
1032  GISTInsertStack *ptr;
1033 
1034  while (true)
1035  {
1036  maxoff = PageGetMaxOffsetNumber(parent->page);
1037  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1038  {
1039  iid = PageGetItemId(parent->page, i);
1040  idxtuple = (IndexTuple) PageGetItem(parent->page, iid);
1041  if (ItemPointerGetBlockNumber(&(idxtuple->t_tid)) == child->blkno)
1042  {
1043  /* yes!!, found */
1044  child->downlinkoffnum = i;
1045  return;
1046  }
1047  }
1048 
1049  parent->blkno = GistPageGetOpaque(parent->page)->rightlink;
1050  UnlockReleaseBuffer(parent->buffer);
1051  if (parent->blkno == InvalidBlockNumber)
1052  {
1053  /*
1054  * End of chain and still didn't find parent. It's a very-very
1055  * rare situation when root splitted.
1056  */
1057  break;
1058  }
1059  parent->buffer = ReadBuffer(r, parent->blkno);
1060  LockBuffer(parent->buffer, GIST_EXCLUSIVE);
1061  gistcheckpage(r, parent->buffer);
1062  parent->page = (Page) BufferGetPage(parent->buffer);
1063  }
1064 
1065  /*
1066  * awful!!, we need search tree to find parent ... , but before we
1067  * should release all old parent
1068  */
1069 
1070  ptr = child->parent->parent; /* child->parent already released
1071  * above */
1072  while (ptr)
1073  {
1074  ReleaseBuffer(ptr->buffer);
1075  ptr = ptr->parent;
1076  }
1077 
1078  /* ok, find new path */
1079  ptr = parent = gistFindPath(r, child->blkno, &child->downlinkoffnum);
1080 
1081  /* read all buffers as expected by caller */
1082  /* note we don't lock them or gistcheckpage them here! */
1083  while (ptr)
1084  {
1085  ptr->buffer = ReadBuffer(r, ptr->blkno);
1086  ptr->page = (Page) BufferGetPage(ptr->buffer);
1087  ptr = ptr->parent;
1088  }
1089 
1090  /* install new chain of parents to stack */
1091  child->parent = parent;
1092 
1093  /* make recursive call to normal processing */
1095  gistFindCorrectParent(r, child);
1096  }
1097 }
BlockNumber blkno
Definition: gist_private.h:210
ItemPointerData t_tid
Definition: itup.h:37
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3518
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
uint16 OffsetNumber
Definition: off.h:24
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
OffsetNumber downlinkoffnum
Definition: gist_private.h:228
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
static void gistFindCorrectParent(Relation r, GISTInsertStack *child)
Definition: gist.c:1016
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
#define InvalidOffsetNumber
Definition: off.h:26
static GISTInsertStack * gistFindPath(Relation r, BlockNumber child, OffsetNumber *downlinkoffnum)
Definition: gist.c:903
#define GistPageGetOpaque(page)
Definition: gist.h:158
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:770
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define InvalidBlockNumber
Definition: block.h:33
#define PageGetLSN(page)
Definition: bufpage.h:366
int i
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
#define GIST_EXCLUSIVE
Definition: gist_private.h:43
struct GISTInsertStack * parent
Definition: gist_private.h:231
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78

◆ gistFindPath()

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

Definition at line 903 of file gist.c.

References Assert, DataPageDeleteStack::blkno, GISTInsertStack::blkno, BufferGetLSNAtomic(), BufferGetPage, GISTInsertStack::downlinkoffnum, elog, ERROR, FirstOffsetNumber, GIST_ROOT_BLKNO, GIST_SHARE, gistcheckpage(), GistFollowRight, GistPageGetNSN, GistPageGetOpaque, GistPageIsDeleted, 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().

904 {
905  Page page;
906  Buffer buffer;
907  OffsetNumber i,
908  maxoff;
909  ItemId iid;
910  IndexTuple idxtuple;
911  List *fifo;
912  GISTInsertStack *top,
913  *ptr;
914  BlockNumber blkno;
915 
916  top = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
917  top->blkno = GIST_ROOT_BLKNO;
919 
920  fifo = list_make1(top);
921  while (fifo != NIL)
922  {
923  /* Get next page to visit */
924  top = linitial(fifo);
925  fifo = list_delete_first(fifo);
926 
927  buffer = ReadBuffer(r, top->blkno);
928  LockBuffer(buffer, GIST_SHARE);
929  gistcheckpage(r, buffer);
930  page = (Page) BufferGetPage(buffer);
931 
932  if (GistPageIsLeaf(page))
933  {
934  /*
935  * Because we scan the index top-down, all the rest of the pages
936  * in the queue must be leaf pages as well.
937  */
938  UnlockReleaseBuffer(buffer);
939  break;
940  }
941 
942  /* currently, internal pages are never deleted */
943  Assert(!GistPageIsDeleted(page));
944 
945  top->lsn = BufferGetLSNAtomic(buffer);
946 
947  /*
948  * If F_FOLLOW_RIGHT is set, the page to the right doesn't have a
949  * downlink. This should not normally happen..
950  */
951  if (GistFollowRight(page))
952  elog(ERROR, "concurrent GiST page split was incomplete");
953 
954  if (top->parent && top->parent->lsn < GistPageGetNSN(page) &&
955  GistPageGetOpaque(page)->rightlink != InvalidBlockNumber /* sanity check */ )
956  {
957  /*
958  * Page was split while we looked elsewhere. We didn't see the
959  * downlink to the right page when we scanned the parent, so add
960  * it to the queue now.
961  *
962  * Put the right page ahead of the queue, so that we visit it
963  * next. That's important, because if this is the lowest internal
964  * level, just above leaves, we might already have queued up some
965  * leaf pages, and we assume that there can't be any non-leaf
966  * pages behind leaf pages.
967  */
968  ptr = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
969  ptr->blkno = GistPageGetOpaque(page)->rightlink;
971  ptr->parent = top->parent;
972 
973  fifo = lcons(ptr, fifo);
974  }
975 
976  maxoff = PageGetMaxOffsetNumber(page);
977 
978  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
979  {
980  iid = PageGetItemId(page, i);
981  idxtuple = (IndexTuple) PageGetItem(page, iid);
982  blkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
983  if (blkno == child)
984  {
985  /* Found it! */
986  UnlockReleaseBuffer(buffer);
987  *downlinkoffnum = i;
988  return top;
989  }
990  else
991  {
992  /* Append this child to the list of pages to visit later */
993  ptr = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
994  ptr->blkno = blkno;
995  ptr->downlinkoffnum = i;
996  ptr->parent = top;
997 
998  fifo = lappend(fifo, ptr);
999  }
1000  }
1001 
1002  UnlockReleaseBuffer(buffer);
1003  }
1004 
1005  elog(ERROR, "failed to re-find parent of a page in index \"%s\", block %u",
1006  RelationGetRelationName(r), child);
1007  return NULL; /* keep compiler quiet */
1008 }
#define GistFollowRight(page)
Definition: gist.h:173
#define NIL
Definition: pg_list.h:65
BlockNumber blkno
Definition: gist_private.h:210
#define GistPageGetNSN(page)
Definition: gist.h:177
#define GistPageIsDeleted(page)
Definition: gist.h:163
ItemPointerData t_tid
Definition: itup.h:37
uint32 BlockNumber
Definition: block.h:31
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
uint16 OffsetNumber
Definition: off.h:24
#define list_make1(x1)
Definition: pg_list.h:227
#define linitial(l)
Definition: pg_list.h:195
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define ERROR
Definition: elog.h:43
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2931
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
#define RelationGetRelationName(relation)
Definition: rel.h:490
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
List * lappend(List *list, void *datum)
Definition: list.c:321
#define GistPageIsLeaf(page)
Definition: gist.h:160
OffsetNumber downlinkoffnum
Definition: gist_private.h:228
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void * palloc0(Size size)
Definition: mcxt.c:980
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
#define InvalidOffsetNumber
Definition: off.h:26
#define GistPageGetOpaque(page)
Definition: gist.h:158
List * lcons(void *datum, List *list)
Definition: list.c:453
#define Assert(condition)
Definition: c.h:745
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:770
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define InvalidBlockNumber
Definition: block.h:33
#define GIST_SHARE
Definition: gist_private.h:42
#define elog(elevel,...)
Definition: elog.h:214
int i
#define GIST_ROOT_BLKNO
Definition: gist_private.h:262
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
Definition: pg_list.h:50
int Buffer
Definition: buf.h:23
struct GISTInsertStack * parent
Definition: gist_private.h:231
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
List * list_delete_first(List *list)
Definition: list.c:860

◆ gistfinishsplit()

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

Definition at line 1316 of file gist.c.

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

Referenced by gistfixsplit(), and gistinserttuples().

1318 {
1319  GISTPageSplitInfo *right;
1320  GISTPageSplitInfo *left;
1321  IndexTuple tuples[2];
1322 
1323  /* A split always contains at least two halves */
1324  Assert(list_length(splitinfo) >= 2);
1325 
1326  /*
1327  * We need to insert downlinks for each new page, and update the downlink
1328  * for the original (leftmost) page in the split. Begin at the rightmost
1329  * page, inserting one downlink at a time until there's only two pages
1330  * left. Finally insert the downlink for the last new page and update the
1331  * downlink for the original page as one operation.
1332  */
1334 
1335  /*
1336  * Insert downlinks for the siblings from right to left, until there are
1337  * only two siblings left.
1338  */
1339  for (int pos = list_length(splitinfo) - 1; pos > 1; pos--)
1340  {
1341  right = (GISTPageSplitInfo *) list_nth(splitinfo, pos);
1342  left = (GISTPageSplitInfo *) list_nth(splitinfo, pos - 1);
1343 
1344  gistFindCorrectParent(state->r, stack);
1345  if (gistinserttuples(state, stack->parent, giststate,
1346  &right->downlink, 1,
1348  left->buf, right->buf, false, false))
1349  {
1350  /*
1351  * If the parent page was split, the existing downlink might have
1352  * moved.
1353  */
1355  }
1356  /* gistinserttuples() released the lock on right->buf. */
1357  }
1358 
1359  right = (GISTPageSplitInfo *) lsecond(splitinfo);
1360  left = (GISTPageSplitInfo *) linitial(splitinfo);
1361 
1362  /*
1363  * Finally insert downlink for the remaining right page and update the
1364  * downlink for the original page to not contain the tuples that were
1365  * moved to the new pages.
1366  */
1367  tuples[0] = left->downlink;
1368  tuples[1] = right->downlink;
1369  gistFindCorrectParent(state->r, stack);
1370  if (gistinserttuples(state, stack->parent, giststate,
1371  tuples, 2,
1372  stack->downlinkoffnum,
1373  left->buf, right->buf,
1374  true, /* Unlock parent */
1375  unlockbuf /* Unlock stack->buffer if caller wants
1376  * that */
1377  ))
1378  {
1379  /*
1380  * If the parent page was split, the downlink might have moved.
1381  */
1383  }
1384 
1385  Assert(left->buf == stack->buffer);
1386 
1387  /*
1388  * If we split the page because we had to adjust the downlink on an
1389  * internal page, while descending the tree for inserting a new tuple,
1390  * then this might no longer be the correct page for the new tuple. The
1391  * downlink to this page might not cover the new tuple anymore, it might
1392  * need to go to the newly-created right sibling instead. Tell the caller
1393  * to walk back up the stack, to re-check at the parent which page to
1394  * insert to.
1395  *
1396  * Normally, the LSN-NSN interlock during the tree descend would also
1397  * detect that a concurrent split happened (by ourselves), and cause us to
1398  * retry at the parent. But that mechanism doesn't work during index
1399  * build, because we don't do WAL-logging, and don't update LSNs, during
1400  * index build.
1401  */
1402  stack->retry_from_parent = true;
1403 }
#define lsecond(l)
Definition: pg_list.h:200
#define linitial(l)
Definition: pg_list.h:195
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
IndexTuple downlink
Definition: gist_private.h:421
OffsetNumber downlinkoffnum
Definition: gist_private.h:228
static void gistFindCorrectParent(Relation r, GISTInsertStack *child)
Definition: gist.c:1016
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:1256
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
#define InvalidOffsetNumber
Definition: off.h:26
#define Assert(condition)
Definition: c.h:745
static int list_length(const List *l)
Definition: pg_list.h:169
#define GIST_EXCLUSIVE
Definition: gist_private.h:43
struct GISTInsertStack * parent
Definition: gist_private.h:231

◆ gistfixsplit()

static void gistfixsplit ( GISTInsertState state,
GISTSTATE giststate 
)
static

Definition at line 1163 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().

1164 {
1165  GISTInsertStack *stack = state->stack;
1166  Buffer buf;
1167  Page page;
1168  List *splitinfo = NIL;
1169 
1170  elog(LOG, "fixing incomplete split in index \"%s\", block %u",
1171  RelationGetRelationName(state->r), stack->blkno);
1172 
1173  Assert(GistFollowRight(stack->page));
1175 
1176  buf = stack->buffer;
1177 
1178  /*
1179  * Read the chain of split pages, following the rightlinks. Construct a
1180  * downlink tuple for each page.
1181  */
1182  for (;;)
1183  {
1185  IndexTuple downlink;
1186 
1187  page = BufferGetPage(buf);
1188 
1189  /* Form the new downlink tuples to insert to parent */
1190  downlink = gistformdownlink(state->r, buf, giststate, stack);
1191 
1192  si->buf = buf;
1193  si->downlink = downlink;
1194 
1195  splitinfo = lappend(splitinfo, si);
1196 
1197  if (GistFollowRight(page))
1198  {
1199  /* lock next page */
1200  buf = ReadBuffer(state->r, GistPageGetOpaque(page)->rightlink);
1201  LockBuffer(buf, GIST_EXCLUSIVE);
1202  }
1203  else
1204  break;
1205  }
1206 
1207  /* Insert the downlinks */
1208  gistfinishsplit(state, stack, giststate, splitinfo, false);
1209 }
#define GistFollowRight(page)
Definition: gist.h:173
#define NIL
Definition: pg_list.h:65
BlockNumber blkno
Definition: gist_private.h:210
#define LOG
Definition: elog.h:26
static IndexTuple gistformdownlink(Relation rel, Buffer buf, GISTSTATE *giststate, GISTInsertStack *stack)
Definition: gist.c:1103
IndexTuple downlink
Definition: gist_private.h:421
GISTInsertStack * stack
Definition: gist_private.h:258
static char * buf
Definition: pg_test_fsync.c:67
#define RelationGetRelationName(relation)
Definition: rel.h:490
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
List * lappend(List *list, void *datum)
Definition: list.c:321
OffsetNumber downlinkoffnum
Definition: gist_private.h:228
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
#define GistPageGetOpaque(page)
Definition: gist.h:158
#define Assert(condition)
Definition: c.h:745
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
static void gistfinishsplit(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, List *splitinfo, bool unlockbuf)
Definition: gist.c:1316
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
#define GIST_EXCLUSIVE
Definition: gist_private.h:43
Definition: pg_list.h:50
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78

◆ gistformdownlink()

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

Definition at line 1103 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().

1105 {
1106  Page page = BufferGetPage(buf);
1107  OffsetNumber maxoff;
1108  OffsetNumber offset;
1109  IndexTuple downlink = NULL;
1110 
1111  maxoff = PageGetMaxOffsetNumber(page);
1112  for (offset = FirstOffsetNumber; offset <= maxoff; offset = OffsetNumberNext(offset))
1113  {
1114  IndexTuple ituple = (IndexTuple)
1115  PageGetItem(page, PageGetItemId(page, offset));
1116 
1117  if (downlink == NULL)
1118  downlink = CopyIndexTuple(ituple);
1119  else
1120  {
1121  IndexTuple newdownlink;
1122 
1123  newdownlink = gistgetadjusted(rel, downlink, ituple,
1124  giststate);
1125  if (newdownlink)
1126  downlink = newdownlink;
1127  }
1128  }
1129 
1130  /*
1131  * If the page is completely empty, we can't form a meaningful downlink
1132  * for it. But we have to insert a downlink for the page. Any key will do,
1133  * as long as its consistent with the downlink of parent page, so that we
1134  * can legally insert it to the parent. A minimal one that matches as few
1135  * scans as possible would be best, to keep scans from doing useless work,
1136  * but we don't know how to construct that. So we just use the downlink of
1137  * the original page that was split - that's as far from optimal as it can
1138  * get but will do..
1139  */
1140  if (!downlink)
1141  {
1142  ItemId iid;
1143 
1145  gistFindCorrectParent(rel, stack);
1146  iid = PageGetItemId(stack->parent->page, stack->downlinkoffnum);
1147  downlink = (IndexTuple) PageGetItem(stack->parent->page, iid);
1148  downlink = CopyIndexTuple(downlink);
1149  LockBuffer(stack->parent->buffer, GIST_UNLOCK);
1150  }
1151 
1153  GistTupleSetValid(downlink);
1154 
1155  return downlink;
1156 }
ItemPointerData t_tid
Definition: itup.h:37
#define GIST_UNLOCK
Definition: gist_private.h:44
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
IndexTuple gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
Definition: gistutil.c:315
uint16 OffsetNumber
Definition: off.h:24
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:510
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:169
#define GistTupleSetValid(itup)
Definition: gist_private.h:289
OffsetNumber downlinkoffnum
Definition: gist_private.h:228
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
static void gistFindCorrectParent(Relation r, GISTInsertStack *child)
Definition: gist.c:1016
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define ItemPointerSetBlockNumber(pointer, blockNumber)
Definition: itemptr.h:138
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2668
#define GIST_EXCLUSIVE
Definition: gist_private.h:43
struct GISTInsertStack * parent
Definition: gist_private.h:231
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78

◆ gisthandler()

Datum gisthandler ( PG_FUNCTION_ARGS  )

Definition at line 59 of file gist.c.

References IndexAmRoutine::amadjustmembers, IndexAmRoutine::ambeginscan, IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, IndexAmRoutine::ambuildphasename, IndexAmRoutine::ambulkdelete, IndexAmRoutine::amcanbackward, IndexAmRoutine::amcaninclude, 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::amoptsprocnum, IndexAmRoutine::amparallelrescan, IndexAmRoutine::amparallelvacuumoptions, IndexAmRoutine::ampredlocks, IndexAmRoutine::amproperty, IndexAmRoutine::amrescan, IndexAmRoutine::amrestrpos, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, IndexAmRoutine::amstorage, IndexAmRoutine::amstrategies, IndexAmRoutine::amsupport, IndexAmRoutine::amusemaintenanceworkmem, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, GIST_OPTIONS_PROC, gistadjustmembers(), gistbeginscan(), gistbuild(), gistbuildempty(), gistbulkdelete(), gistcanreturn(), gistcostestimate(), gistendscan(), gistgetbitmap(), gistgettuple(), gistinsert(), GISTNProcs, gistoptions(), gistproperty(), gistrescan(), gistvacuumcleanup(), gistvalidate(), InvalidOid, makeNode, PG_RETURN_POINTER, VACUUM_OPTION_PARALLEL_BULKDEL, and VACUUM_OPTION_PARALLEL_COND_CLEANUP.

60 {
62 
63  amroutine->amstrategies = 0;
64  amroutine->amsupport = GISTNProcs;
65  amroutine->amoptsprocnum = GIST_OPTIONS_PROC;
66  amroutine->amcanorder = false;
67  amroutine->amcanorderbyop = true;
68  amroutine->amcanbackward = false;
69  amroutine->amcanunique = false;
70  amroutine->amcanmulticol = true;
71  amroutine->amoptionalkey = true;
72  amroutine->amsearcharray = false;
73  amroutine->amsearchnulls = true;
74  amroutine->amstorage = true;
75  amroutine->amclusterable = true;
76  amroutine->ampredlocks = true;
77  amroutine->amcanparallel = false;
78  amroutine->amcaninclude = true;
79  amroutine->amusemaintenanceworkmem = false;
80  amroutine->amparallelvacuumoptions =
82  amroutine->amkeytype = InvalidOid;
83 
84  amroutine->ambuild = gistbuild;
85  amroutine->ambuildempty = gistbuildempty;
86  amroutine->aminsert = gistinsert;
87  amroutine->ambulkdelete = gistbulkdelete;
89  amroutine->amcanreturn = gistcanreturn;
90  amroutine->amcostestimate = gistcostestimate;
91  amroutine->amoptions = gistoptions;
92  amroutine->amproperty = gistproperty;
93  amroutine->ambuildphasename = NULL;
94  amroutine->amvalidate = gistvalidate;
96  amroutine->ambeginscan = gistbeginscan;
97  amroutine->amrescan = gistrescan;
98  amroutine->amgettuple = gistgettuple;
99  amroutine->amgetbitmap = gistgetbitmap;
100  amroutine->amendscan = gistendscan;
101  amroutine->ammarkpos = NULL;
102  amroutine->amrestrpos = NULL;
103  amroutine->amestimateparallelscan = NULL;
104  amroutine->aminitparallelscan = NULL;
105  amroutine->amparallelrescan = NULL;
106 
107  PG_RETURN_POINTER(amroutine);
108 }
#define GIST_OPTIONS_PROC
Definition: gist.h:39
ambeginscan_function ambeginscan
Definition: amapi.h:270
uint8 amparallelvacuumoptions
Definition: amapi.h:247
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
ambulkdelete_function ambulkdelete
Definition: amapi.h:261
bool amcanmulticol
Definition: amapi.h:227
uint16 amsupport
Definition: amapi.h:215
amgettuple_function amgettuple
Definition: amapi.h:272
IndexBulkDeleteResult * gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: gistvacuum.c:75
bool amcanorderbyop
Definition: amapi.h:221
amproperty_function amproperty
Definition: amapi.h:266
IndexBulkDeleteResult * gistbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: gistvacuum.c:59
amparallelrescan_function amparallelrescan
Definition: amapi.h:281
bool amstorage
Definition: amapi.h:235
bool gistgettuple(IndexScanDesc scan, ScanDirection dir)
Definition: gistget.c:614
bool ampredlocks
Definition: amapi.h:239
aminsert_function aminsert
Definition: amapi.h:260
void gistadjustmembers(Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
Definition: gistvalidate.c:287
bytea * gistoptions(Datum reloptions, bool validate)
Definition: gistutil.c:908
Oid amkeytype
Definition: amapi.h:249
bool amoptionalkey
Definition: amapi.h:229
amvalidate_function amvalidate
Definition: amapi.h:268
bool gistinsert(Relation r, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: gist.c:156
IndexBuildResult * gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: gistbuild.c:116
amgetbitmap_function amgetbitmap
Definition: amapi.h:273
ambuild_function ambuild
Definition: amapi.h:258
amoptions_function amoptions
Definition: amapi.h:265
bool amcaninclude
Definition: amapi.h:243
amcostestimate_function amcostestimate
Definition: amapi.h:264
bool amcanunique
Definition: amapi.h:225
void gistrescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys)
Definition: gistscan.c:127
void gistcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:6587
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:262
amendscan_function amendscan
Definition: amapi.h:274
bool amcanbackward
Definition: amapi.h:223
amrescan_function amrescan
Definition: amapi.h:271
bool amcanparallel
Definition: amapi.h:241
int64 gistgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: gistget.c:745
void gistbuildempty(Relation index)
Definition: gist.c:130
bool amsearchnulls
Definition: amapi.h:233
bool amclusterable
Definition: amapi.h:237
bool amsearcharray
Definition: amapi.h:231
#define InvalidOid
Definition: postgres_ext.h:36
bool amusemaintenanceworkmem
Definition: amapi.h:245
bool gistcanreturn(Relation index, int attno)
Definition: gistget.c:795
#define makeNode(_type_)
Definition: nodes.h:577
amadjustmembers_function amadjustmembers
Definition: amapi.h:269
#define VACUUM_OPTION_PARALLEL_COND_CLEANUP
Definition: vacuum.h:52
ammarkpos_function ammarkpos
Definition: amapi.h:275
bool amcanorder
Definition: amapi.h:219
ambuildphasename_function ambuildphasename
Definition: amapi.h:267
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition: vacuum.h:45
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:279
#define GISTNProcs
Definition: gist.h:40
uint16 amstrategies
Definition: amapi.h:213
bool gistvalidate(Oid opclassoid)
Definition: gistvalidate.c:34
uint16 amoptsprocnum
Definition: amapi.h:217
ambuildempty_function ambuildempty
Definition: amapi.h:259
IndexScanDesc gistbeginscan(Relation r, int nkeys, int norderbys)
Definition: gistscan.c:74
void gistendscan(IndexScanDesc scan)
Definition: gistscan.c:349
bool gistproperty(Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
Definition: gistutil.c:929
amcanreturn_function amcanreturn
Definition: amapi.h:263
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:280
amrestrpos_function amrestrpos
Definition: amapi.h:276

◆ gistinsert()

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

Definition at line 156 of file gist.c.

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

Referenced by gisthandler().

160 {
161  GISTSTATE *giststate = (GISTSTATE *) indexInfo->ii_AmCache;
162  IndexTuple itup;
163  MemoryContext oldCxt;
164 
165  /* Initialize GISTSTATE cache if first call in this statement */
166  if (giststate == NULL)
167  {
168  oldCxt = MemoryContextSwitchTo(indexInfo->ii_Context);
169  giststate = initGISTstate(r);
170  giststate->tempCxt = createTempGistContext();
171  indexInfo->ii_AmCache = (void *) giststate;
172  MemoryContextSwitchTo(oldCxt);
173  }
174 
175  oldCxt = MemoryContextSwitchTo(giststate->tempCxt);
176 
177  itup = gistFormTuple(giststate, r,
178  values, isnull, true /* size is currently bogus */ );
179  itup->t_tid = *ht_ctid;
180 
181  gistdoinsert(r, itup, 0, giststate, heapRel, false);
182 
183  /* cleanup */
184  MemoryContextSwitchTo(oldCxt);
185  MemoryContextReset(giststate->tempCxt);
186 
187  return false;
188 }
MemoryContext ii_Context
Definition: execnodes.h:179
MemoryContext createTempGistContext(void)
Definition: gist.c:119
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
void gistdoinsert(Relation r, IndexTuple itup, Size freespace, GISTSTATE *giststate, Relation heapRel, bool is_build)
Definition: gist.c:628
MemoryContext tempCxt
Definition: gist_private.h:78
void * ii_AmCache
Definition: execnodes.h:178
GISTSTATE * initGISTstate(Relation index)
Definition: gist.c:1498
static Datum values[MAXATTR]
Definition: bootstrap.c:167
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:574

◆ gistinserttuple()

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

Definition at line 1222 of file gist.c.

References gistinserttuples(), and InvalidBuffer.

Referenced by gistdoinsert().

1224 {
1225  return gistinserttuples(state, stack, giststate, &tuple, 1, oldoffnum,
1226  InvalidBuffer, InvalidBuffer, false, false);
1227 }
#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:1256

◆ 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 1256 of file gist.c.

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

Referenced by gistfinishsplit(), and gistinserttuple().

1261 {
1262  List *splitinfo;
1263  bool is_split;
1264 
1265  /*
1266  * Check for any rw conflicts (in serializable isolation level) just
1267  * before we intend to modify the page
1268  */
1270 
1271  /* Insert the tuple(s) to the page, splitting the page if necessary */
1272  is_split = gistplacetopage(state->r, state->freespace, giststate,
1273  stack->buffer,
1274  tuples, ntup,
1275  oldoffnum, NULL,
1276  leftchild,
1277  &splitinfo,
1278  true,
1279  state->heapRel,
1280  state->is_build);
1281 
1282  /*
1283  * Before recursing up in case the page was split, release locks on the
1284  * child pages. We don't need to keep them locked when updating the
1285  * parent.
1286  */
1289  if (BufferIsValid(leftchild) && unlockleftchild)
1291 
1292  /*
1293  * If we had to split, insert/update the downlinks in the parent. If the
1294  * caller requested us to release the lock on stack->buffer, tell
1295  * gistfinishsplit() to do that as soon as it's safe to do so. If we
1296  * didn't have to split, release it ourselves.
1297  */
1298  if (splitinfo)
1299  gistfinishsplit(state, stack, giststate, splitinfo, unlockbuf);
1300  else if (unlockbuf)
1301  LockBuffer(stack->buffer, GIST_UNLOCK);
1302 
1303  return is_split;
1304 }
Relation heapRel
Definition: gist_private.h:254
bool gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate, Buffer buffer, IndexTuple *itup, int ntup, OffsetNumber oldoffnum, BlockNumber *newblkno, Buffer leftchildbuf, List **splitinfo, bool markfollowright, Relation heapRel, bool is_build)
Definition: gist.c:221
#define GIST_UNLOCK
Definition: gist_private.h:44
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
void CheckForSerializableConflictIn(Relation relation, ItemPointer tid, BlockNumber blkno)
Definition: predicate.c:4377
#define rightchild(x)
Definition: fsmpage.c:30
#define leftchild(x)
Definition: fsmpage.c:29
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
static void gistfinishsplit(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, List *splitinfo, bool unlockbuf)
Definition: gist.c:1316
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2668
Definition: pg_list.h:50

◆ 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,
Relation  heapRel,
bool  is_build 
)

Definition at line 221 of file gist.c.

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

Referenced by gistbufferinginserttuples(), and gistinserttuples().

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

◆ gistprunepage()

static void gistprunepage ( Relation  rel,
Page  page,
Buffer  buffer,
Relation  heapRel 
)
static

Definition at line 1637 of file gist.c.

References Assert, END_CRIT_SECTION, FirstOffsetNumber, GistClearPageHasGarbage, gistGetFakeLSN(), GistPageIsLeaf, gistXLogDelete(), index_compute_xid_horizon_for_tuples(), InvalidTransactionId, ItemIdIsDead, MarkBufferDirty(), MaxIndexTuplesPerPage, OffsetNumberNext, PageGetItemId, PageGetMaxOffsetNumber, PageIndexMultiDelete(), PageSetLSN, RelationNeedsWAL, START_CRIT_SECTION, and XLogStandbyInfoActive.

Referenced by gistplacetopage().

1638 {
1640  int ndeletable = 0;
1641  OffsetNumber offnum,
1642  maxoff;
1643  TransactionId latestRemovedXid = InvalidTransactionId;
1644 
1645  Assert(GistPageIsLeaf(page));
1646 
1647  /*
1648  * Scan over all items to see which ones need to be deleted according to
1649  * LP_DEAD flags.
1650  */
1651  maxoff = PageGetMaxOffsetNumber(page);
1652  for (offnum = FirstOffsetNumber;
1653  offnum <= maxoff;
1654  offnum = OffsetNumberNext(offnum))
1655  {
1656  ItemId itemId = PageGetItemId(page, offnum);
1657 
1658  if (ItemIdIsDead(itemId))
1659  deletable[ndeletable++] = offnum;
1660  }
1661 
1663  latestRemovedXid =
1664  index_compute_xid_horizon_for_tuples(rel, heapRel, buffer,
1665  deletable, ndeletable);
1666 
1667  if (ndeletable > 0)
1668  {
1670 
1671  PageIndexMultiDelete(page, deletable, ndeletable);
1672 
1673  /*
1674  * Mark the page as not containing any LP_DEAD items. This is not
1675  * certainly true (there might be some that have recently been marked,
1676  * but weren't included in our target-item list), but it will almost
1677  * always be true and it doesn't seem worth an additional page scan to
1678  * check it. Remember that F_HAS_GARBAGE is only a hint anyway.
1679  */
1681 
1682  MarkBufferDirty(buffer);
1683 
1684  /* XLOG stuff */
1685  if (RelationNeedsWAL(rel))
1686  {
1687  XLogRecPtr recptr;
1688 
1689  recptr = gistXLogDelete(buffer,
1690  deletable, ndeletable,
1691  latestRemovedXid);
1692 
1693  PageSetLSN(page, recptr);
1694  }
1695  else
1696  PageSetLSN(page, gistGetFakeLSN(rel));
1697 
1698  END_CRIT_SECTION();
1699  }
1700 
1701  /*
1702  * Note: if we didn't find any LP_DEAD items, then the page's
1703  * F_HAS_GARBAGE hint bit is falsely set. We do not bother expending a
1704  * separate write to clear it, however. We will clear it when we split
1705  * the page.
1706  */
1707 }
uint32 TransactionId
Definition: c.h:520
#define GistClearPageHasGarbage(page)
Definition: gist.h:171
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
XLogRecPtr gistGetFakeLSN(Relation rel)
Definition: gistutil.c:1012
XLogRecPtr gistXLogDelete(Buffer buffer, OffsetNumber *todelete, int ntodelete, TransactionId latestRemovedXid)
Definition: gistxlog.c:694
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
#define InvalidTransactionId
Definition: transam.h:31
#define GistPageIsLeaf(page)
Definition: gist.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
#define XLogStandbyInfoActive()
Definition: xlog.h:205
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:828
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define RelationNeedsWAL(relation)
Definition: rel.h:562
#define MaxIndexTuplesPerPage
Definition: itup.h:145
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
TransactionId index_compute_xid_horizon_for_tuples(Relation irel, Relation hrel, Buffer ibuf, OffsetNumber *itemnos, int nitems)
Definition: genam.c:285

◆ gistSplit()

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

Definition at line 1411 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, TupleDescData::natts, SplitedPageLayout::next, GISTSTATE::nonLeafTupdesc, 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, and GistSplitVector::splitVector.

Referenced by gistplacetopage(), and gistSplit().

1416 {
1417  IndexTuple *lvectup,
1418  *rvectup;
1419  GistSplitVector v;
1420  int i;
1421  SplitedPageLayout *res = NULL;
1422 
1423  /* this should never recurse very deeply, but better safe than sorry */
1425 
1426  /* there's no point in splitting an empty page */
1427  Assert(len > 0);
1428 
1429  /*
1430  * If a single tuple doesn't fit on a page, no amount of splitting will
1431  * help.
1432  */
1433  if (len == 1)
1434  ereport(ERROR,
1435  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1436  errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
1437  IndexTupleSize(itup[0]), GiSTPageSize,
1439 
1440  memset(v.spl_lisnull, true,
1441  sizeof(bool) * giststate->nonLeafTupdesc->natts);
1442  memset(v.spl_risnull, true,
1443  sizeof(bool) * giststate->nonLeafTupdesc->natts);
1444  gistSplitByKey(r, page, itup, len, giststate, &v, 0);
1445 
1446  /* form left and right vector */
1447  lvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
1448  rvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
1449 
1450  for (i = 0; i < v.splitVector.spl_nleft; i++)
1451  lvectup[i] = itup[v.splitVector.spl_left[i] - 1];
1452 
1453  for (i = 0; i < v.splitVector.spl_nright; i++)
1454  rvectup[i] = itup[v.splitVector.spl_right[i] - 1];
1455 
1456  /* finalize splitting (may need another split) */
1457  if (!gistfitpage(rvectup, v.splitVector.spl_nright))
1458  {
1459  res = gistSplit(r, page, rvectup, v.splitVector.spl_nright, giststate);
1460  }
1461  else
1462  {
1463  ROTATEDIST(res);
1464  res->block.num = v.splitVector.spl_nright;
1465  res->list = gistfillitupvec(rvectup, v.splitVector.spl_nright, &(res->lenlist));
1466  res->itup = gistFormTuple(giststate, r, v.spl_rattr, v.spl_risnull, false);
1467  }
1468 
1469  if (!gistfitpage(lvectup, v.splitVector.spl_nleft))
1470  {
1471  SplitedPageLayout *resptr,
1472  *subres;
1473 
1474  resptr = subres = gistSplit(r, page, lvectup, v.splitVector.spl_nleft, giststate);
1475 
1476  /* install on list's tail */
1477  while (resptr->next)
1478  resptr = resptr->next;
1479 
1480  resptr->next = res;
1481  res = subres;
1482  }
1483  else
1484  {
1485  ROTATEDIST(res);
1486  res->block.num = v.splitVector.spl_nleft;
1487  res->list = gistfillitupvec(lvectup, v.splitVector.spl_nleft, &(res->lenlist));
1488  res->itup = gistFormTuple(giststate, r, v.spl_lattr, v.spl_lisnull, false);
1489  }
1490 
1491  return res;
1492 }
Datum spl_lattr[INDEX_MAX_KEYS]
Definition: gist_private.h:239
OffsetNumber * spl_left
Definition: gist.h:133
GIST_SPLITVEC splitVector
Definition: gist_private.h:237
int errcode(int sqlerrcode)
Definition: elog.c:610
int spl_nleft
Definition: gist.h:134
IndexTupleData * list
Definition: gist_private.h:194
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:243
gistxlogPage block
Definition: gist_private.h:193
#define ERROR
Definition: elog.h:43
int spl_nright
Definition: gist.h:139
void check_stack_depth(void)
Definition: postgres.c:3312
#define RelationGetRelationName(relation)
Definition: rel.h:490
struct SplitedPageLayout * next
Definition: gist_private.h:200
#define GiSTPageSize
Definition: gist_private.h:474
#define ROTATEDIST(d)
Definition: gist.c:45
SplitedPageLayout * gistSplit(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate)
Definition: gist.c:1411
TupleDesc nonLeafTupdesc
Definition: gist_private.h:81
#define ereport(elevel,...)
Definition: elog.h:144
bool gistfitpage(IndexTuple *itvec, int len)
Definition: gistutil.c:78
#define Assert(condition)
Definition: c.h:745
IndexTupleData * gistfillitupvec(IndexTuple *vec, int veclen, int *memlen)
Definition: gistutil.c:126
OffsetNumber * spl_right
Definition: gist.h:138
bool spl_risnull[INDEX_MAX_KEYS]
Definition: gist_private.h:245
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:574
bool spl_lisnull[INDEX_MAX_KEYS]
Definition: gist_private.h:241
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ initGISTstate()

GISTSTATE* initGISTstate ( Relation  index)

Definition at line 1498 of file gist.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, GISTSTATE::compressFn, GISTSTATE::consistentFn, CreateTupleDescCopyConstr(), CurrentMemoryContext, GISTSTATE::decompressFn, 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, IndexRelationGetNumberOfKeyAttributes, InvalidOid, GISTSTATE::leafTupdesc, MemoryContextSwitchTo(), TupleDescData::natts, GISTSTATE::nonLeafTupdesc, OidIsValid, palloc(), GISTSTATE::penaltyFn, GISTSTATE::picksplitFn, RelationData::rd_att, RelationData::rd_indcollation, GISTSTATE::scanCxt, GISTSTATE::supportCollation, GISTSTATE::tempCxt, and GISTSTATE::unionFn.

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

1499 {
1500  GISTSTATE *giststate;
1501  MemoryContext scanCxt;
1502  MemoryContext oldCxt;
1503  int i;
1504 
1505  /* safety check to protect fixed-size arrays in GISTSTATE */
1506  if (index->rd_att->natts > INDEX_MAX_KEYS)
1507  elog(ERROR, "numberOfAttributes %d > %d",
1508  index->rd_att->natts, INDEX_MAX_KEYS);
1509 
1510  /* Create the memory context that will hold the GISTSTATE */
1512  "GiST scan context",
1514  oldCxt = MemoryContextSwitchTo(scanCxt);
1515 
1516  /* Create and fill in the GISTSTATE */
1517  giststate = (GISTSTATE *) palloc(sizeof(GISTSTATE));
1518 
1519  giststate->scanCxt = scanCxt;
1520  giststate->tempCxt = scanCxt; /* caller must change this if needed */
1521  giststate->leafTupdesc = index->rd_att;
1522 
1523  /*
1524  * The truncated tupdesc for non-leaf index tuples, which doesn't contain
1525  * the INCLUDE attributes.
1526  *
1527  * It is used to form tuples during tuple adjustment and page split.
1528  * B-tree creates shortened tuple descriptor for every truncated tuple,
1529  * because it is doing this less often: it does not have to form truncated
1530  * tuples during page split. Also, B-tree is not adjusting tuples on
1531  * internal pages the way GiST does.
1532  */
1533  giststate->nonLeafTupdesc = CreateTupleDescCopyConstr(index->rd_att);
1534  giststate->nonLeafTupdesc->natts =
1536 
1537  for (i = 0; i < IndexRelationGetNumberOfKeyAttributes(index); i++)
1538  {
1539  fmgr_info_copy(&(giststate->consistentFn[i]),
1540  index_getprocinfo(index, i + 1, GIST_CONSISTENT_PROC),
1541  scanCxt);
1542  fmgr_info_copy(&(giststate->unionFn[i]),
1543  index_getprocinfo(index, i + 1, GIST_UNION_PROC),
1544  scanCxt);
1545 
1546  /* opclasses are not required to provide a Compress method */
1547  if (OidIsValid(index_getprocid(index, i + 1, GIST_COMPRESS_PROC)))
1548  fmgr_info_copy(&(giststate->compressFn[i]),
1549  index_getprocinfo(index, i + 1, GIST_COMPRESS_PROC),
1550  scanCxt);
1551  else
1552  giststate->compressFn[i].fn_oid = InvalidOid;
1553 
1554  /* opclasses are not required to provide a Decompress method */
1555  if (OidIsValid(index_getprocid(index, i + 1, GIST_DECOMPRESS_PROC)))
1556  fmgr_info_copy(&(giststate->decompressFn[i]),
1557  index_getprocinfo(index, i + 1, GIST_DECOMPRESS_PROC),
1558  scanCxt);
1559  else
1560  giststate->decompressFn[i].fn_oid = InvalidOid;
1561 
1562  fmgr_info_copy(&(giststate->penaltyFn[i]),
1563  index_getprocinfo(index, i + 1, GIST_PENALTY_PROC),
1564  scanCxt);
1565  fmgr_info_copy(&(giststate->picksplitFn[i]),
1566  index_getprocinfo(index, i + 1, GIST_PICKSPLIT_PROC),
1567  scanCxt);
1568  fmgr_info_copy(&(giststate->equalFn[i]),
1569  index_getprocinfo(index, i + 1, GIST_EQUAL_PROC),
1570  scanCxt);
1571 
1572  /* opclasses are not required to provide a Distance method */
1573  if (OidIsValid(index_getprocid(index, i + 1, GIST_DISTANCE_PROC)))
1574  fmgr_info_copy(&(giststate->distanceFn[i]),
1575  index_getprocinfo(index, i + 1, GIST_DISTANCE_PROC),
1576  scanCxt);
1577  else
1578  giststate->distanceFn[i].fn_oid = InvalidOid;
1579 
1580  /* opclasses are not required to provide a Fetch method */
1581  if (OidIsValid(index_getprocid(index, i + 1, GIST_FETCH_PROC)))
1582  fmgr_info_copy(&(giststate->fetchFn[i]),
1583  index_getprocinfo(index, i + 1, GIST_FETCH_PROC),
1584  scanCxt);
1585  else
1586  giststate->fetchFn[i].fn_oid = InvalidOid;
1587 
1588  /*
1589  * If the index column has a specified collation, we should honor that
1590  * while doing comparisons. However, we may have a collatable storage
1591  * type for a noncollatable indexed data type. If there's no index
1592  * collation then specify default collation in case the support
1593  * functions need collation. This is harmless if the support
1594  * functions don't care about collation, so we just do it
1595  * unconditionally. (We could alternatively call get_typcollation,
1596  * but that seems like expensive overkill --- there aren't going to be
1597  * any cases where a GiST storage type has a nondefault collation.)
1598  */
1599  if (OidIsValid(index->rd_indcollation[i]))
1600  giststate->supportCollation[i] = index->rd_indcollation[i];
1601  else
1602  giststate->supportCollation[i] = DEFAULT_COLLATION_OID;
1603  }
1604 
1605  /* No opclass information for INCLUDE attributes */
1606  for (; i < index->rd_att->natts; i++)
1607  {
1608  giststate->consistentFn[i].fn_oid = InvalidOid;
1609  giststate->unionFn[i].fn_oid = InvalidOid;
1610  giststate->compressFn[i].fn_oid = InvalidOid;
1611  giststate->decompressFn[i].fn_oid = InvalidOid;
1612  giststate->penaltyFn[i].fn_oid = InvalidOid;
1613  giststate->picksplitFn[i].fn_oid = InvalidOid;
1614  giststate->equalFn[i].fn_oid = InvalidOid;
1615  giststate->distanceFn[i].fn_oid = InvalidOid;
1616  giststate->fetchFn[i].fn_oid = InvalidOid;
1617  giststate->supportCollation[i] = InvalidOid;
1618  }
1619 
1620  MemoryContextSwitchTo(oldCxt);
1621 
1622  return giststate;
1623 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define GIST_FETCH_PROC
Definition: gist.h:38
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:800
FmgrInfo fetchFn[INDEX_MAX_KEYS]
Definition: gist_private.h:94
#define GIST_EQUAL_PROC
Definition: gist.h:36
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gist_private.h:97
FmgrInfo compressFn[INDEX_MAX_KEYS]
Definition: gist_private.h:88
FmgrInfo equalFn[INDEX_MAX_KEYS]
Definition: gist_private.h:92
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define OidIsValid(objectId)
Definition: c.h:651
FmgrInfo consistentFn[INDEX_MAX_KEYS]
Definition: gist_private.h:86
#define GIST_PICKSPLIT_PROC
Definition: gist.h:35
Oid * rd_indcollation
Definition: rel.h:199
#define ERROR
Definition: elog.h:43
#define GIST_COMPRESS_PROC
Definition: gist.h:32
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:612
MemoryContext tempCxt
Definition: gist_private.h:78
FmgrInfo picksplitFn[INDEX_MAX_KEYS]
Definition: gist_private.h:91
FmgrInfo penaltyFn[INDEX_MAX_KEYS]
Definition: gist_private.h:90
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:475
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:150
FmgrInfo decompressFn[INDEX_MAX_KEYS]
Definition: gist_private.h:89
TupleDesc leafTupdesc
Definition: gist_private.h:80
TupleDesc rd_att
Definition: rel.h:110
#define InvalidOid
Definition: postgres_ext.h:36
TupleDesc nonLeafTupdesc
Definition: gist_private.h:81
Oid fn_oid
Definition: fmgr.h:59
#define GIST_CONSISTENT_PROC
Definition: gist.h:30
#define GIST_UNION_PROC
Definition: gist.h:31
#define INDEX_MAX_KEYS
#define GIST_PENALTY_PROC
Definition: gist.h:34
#define GIST_DISTANCE_PROC
Definition: gist.h:37
FmgrInfo distanceFn[INDEX_MAX_KEYS]
Definition: gist_private.h:93
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
int i
MemoryContext scanCxt
Definition: gist_private.h:77
#define GIST_DECOMPRESS_PROC
Definition: gist.h:33
FmgrInfo unionFn[INDEX_MAX_KEYS]
Definition: gist_private.h:87
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:766