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 "storage/lmgr.h"
#include "storage/predicate.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 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:955
#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 114 of file gist.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, and CurrentMemoryContext.

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

115 {
117  "GiST temporary context",
119 }
#define AllocSetContextCreate
Definition: memutils.h:169
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:191
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38

◆ freeGISTstate()

void freeGISTstate ( GISTSTATE giststate)

Definition at line 1614 of file gist.c.

References MemoryContextDelete(), and GISTSTATE::scanCxt.

Referenced by gistbuild(), and gistendscan().

1615 {
1616  /* It's sufficient to delete the scanCxt */
1617  MemoryContextDelete(giststate->scanCxt);
1618 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
MemoryContext scanCxt
Definition: gist_private.h:77

◆ gistbuildempty()

void gistbuildempty ( Relation  index)

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

126 {
127  Buffer buffer;
128 
129  /* Initialize the root page */
130  buffer = ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
132 
133  /* Initialize and xlog buffer */
135  GISTInitBuffer(buffer, F_LEAF);
136  MarkBufferDirty(buffer);
137  log_newpage_buffer(buffer, true);
139 
140  /* Unlock and release the buffer */
141  UnlockReleaseBuffer(buffer);
142 }
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1009
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#define P_NEW
Definition: bufmgr.h:81
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:88
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3376
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
#define F_LEAF
Definition: gist.h:43
void GISTInitBuffer(Buffer b, uint32 f)
Definition: gistutil.c:749
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 623 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().

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

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

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

◆ gistfinishsplit()

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

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

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

◆ gistfixsplit()

static void gistfixsplit ( GISTInsertState state,
GISTSTATE giststate 
)
static

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

1161 {
1162  GISTInsertStack *stack = state->stack;
1163  Buffer buf;
1164  Page page;
1165  List *splitinfo = NIL;
1166 
1167  elog(LOG, "fixing incomplete split in index \"%s\", block %u",
1168  RelationGetRelationName(state->r), stack->blkno);
1169 
1170  Assert(GistFollowRight(stack->page));
1172 
1173  buf = stack->buffer;
1174 
1175  /*
1176  * Read the chain of split pages, following the rightlinks. Construct a
1177  * downlink tuple for each page.
1178  */
1179  for (;;)
1180  {
1182  IndexTuple downlink;
1183 
1184  page = BufferGetPage(buf);
1185 
1186  /* Form the new downlink tuples to insert to parent */
1187  downlink = gistformdownlink(state->r, buf, giststate, stack);
1188 
1189  si->buf = buf;
1190  si->downlink = downlink;
1191 
1192  splitinfo = lappend(splitinfo, si);
1193 
1194  if (GistFollowRight(page))
1195  {
1196  /* lock next page */
1197  buf = ReadBuffer(state->r, GistPageGetOpaque(page)->rightlink);
1198  LockBuffer(buf, GIST_EXCLUSIVE);
1199  }
1200  else
1201  break;
1202  }
1203 
1204  /* Insert the downlinks */
1205  gistfinishsplit(state, stack, giststate, splitinfo, false);
1206 }
#define GistFollowRight(page)
Definition: gist.h:153
#define NIL
Definition: pg_list.h:65
BlockNumber blkno
Definition: gist_private.h:207
#define LOG
Definition: elog.h:26
static IndexTuple gistformdownlink(Relation rel, Buffer buf, GISTSTATE *giststate, GISTInsertStack *stack)
Definition: gist.c:1100
IndexTuple downlink
Definition: gist_private.h:410
GISTInsertStack * stack
Definition: gist_private.h:255
static char * buf
Definition: pg_test_fsync.c:68
#define RelationGetRelationName(relation)
Definition: rel.h:450
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
List * lappend(List *list, void *datum)
Definition: list.c:321
OffsetNumber downlinkoffnum
Definition: gist_private.h:225
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
#define GistPageGetOpaque(page)
Definition: gist.h:138
#define Assert(condition)
Definition: c.h:732
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:596
static void gistfinishsplit(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, List *splitinfo, bool unlockbuf)
Definition: gist.c:1313
void * palloc(Size size)
Definition: mcxt.c:924
#define elog(elevel,...)
Definition: elog.h:226
#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 1100 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().

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

60 {
62 
63  amroutine->amstrategies = 0;
64  amroutine->amsupport = GISTNProcs;
65  amroutine->amcanorder = false;
66  amroutine->amcanorderbyop = true;
67  amroutine->amcanbackward = false;
68  amroutine->amcanunique = false;
69  amroutine->amcanmulticol = true;
70  amroutine->amoptionalkey = true;
71  amroutine->amsearcharray = false;
72  amroutine->amsearchnulls = true;
73  amroutine->amstorage = true;
74  amroutine->amclusterable = true;
75  amroutine->ampredlocks = true;
76  amroutine->amcanparallel = false;
77  amroutine->amcaninclude = true;
78  amroutine->amkeytype = InvalidOid;
79 
80  amroutine->ambuild = gistbuild;
81  amroutine->ambuildempty = gistbuildempty;
82  amroutine->aminsert = gistinsert;
83  amroutine->ambulkdelete = gistbulkdelete;
85  amroutine->amcanreturn = gistcanreturn;
86  amroutine->amcostestimate = gistcostestimate;
87  amroutine->amoptions = gistoptions;
88  amroutine->amproperty = gistproperty;
89  amroutine->ambuildphasename = NULL;
90  amroutine->amvalidate = gistvalidate;
91  amroutine->ambeginscan = gistbeginscan;
92  amroutine->amrescan = gistrescan;
93  amroutine->amgettuple = gistgettuple;
94  amroutine->amgetbitmap = gistgetbitmap;
95  amroutine->amendscan = gistendscan;
96  amroutine->ammarkpos = NULL;
97  amroutine->amrestrpos = NULL;
98  amroutine->amestimateparallelscan = NULL;
99  amroutine->aminitparallelscan = NULL;
100  amroutine->amparallelrescan = NULL;
101 
102  PG_RETURN_POINTER(amroutine);
103 }
ambeginscan_function ambeginscan
Definition: amapi.h:221
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
ambulkdelete_function ambulkdelete
Definition: amapi.h:213
bool amcanmulticol
Definition: amapi.h:183
uint16 amsupport
Definition: amapi.h:173
amgettuple_function amgettuple
Definition: amapi.h:223
IndexBulkDeleteResult * gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: gistvacuum.c:101
bool amcanorderbyop
Definition: amapi.h:177
amproperty_function amproperty
Definition: amapi.h:218
IndexBulkDeleteResult * gistbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: gistvacuum.c:83
amparallelrescan_function amparallelrescan
Definition: amapi.h:232
bool amstorage
Definition: amapi.h:191
bool gistgettuple(IndexScanDesc scan, ScanDirection dir)
Definition: gistget.c:608
bool ampredlocks
Definition: amapi.h:195
aminsert_function aminsert
Definition: amapi.h:212
bytea * gistoptions(Datum reloptions, bool validate)
Definition: gistutil.c:909
Oid amkeytype
Definition: amapi.h:201
bool amoptionalkey
Definition: amapi.h:185
amvalidate_function amvalidate
Definition: amapi.h:220
bool gistinsert(Relation r, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: gist.c:151
IndexBuildResult * gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: gistbuild.c:116
amgetbitmap_function amgetbitmap
Definition: amapi.h:224
ambuild_function ambuild
Definition: amapi.h:210
amoptions_function amoptions
Definition: amapi.h:217
bool amcaninclude
Definition: amapi.h:199
amcostestimate_function amcostestimate
Definition: amapi.h:216
bool amcanunique
Definition: amapi.h:181
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:6126
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:214
amendscan_function amendscan
Definition: amapi.h:225
bool amcanbackward
Definition: amapi.h:179
amrescan_function amrescan
Definition: amapi.h:222
bool amcanparallel
Definition: amapi.h:197
int64 gistgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: gistget.c:739
void gistbuildempty(Relation index)
Definition: gist.c:125
bool amsearchnulls
Definition: amapi.h:189
bool amclusterable
Definition: amapi.h:193
bool amsearcharray
Definition: amapi.h:187
#define InvalidOid
Definition: postgres_ext.h:36
bool gistcanreturn(Relation index, int attno)
Definition: gistget.c:789
#define makeNode(_type_)
Definition: nodes.h:572
ammarkpos_function ammarkpos
Definition: amapi.h:226
bool amcanorder
Definition: amapi.h:175
ambuildphasename_function ambuildphasename
Definition: amapi.h:219
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:230
#define GISTNProcs
Definition: gist.h:38
uint16 amstrategies
Definition: amapi.h:171
bool gistvalidate(Oid opclassoid)
Definition: gistvalidate.c:34
ambuildempty_function ambuildempty
Definition: amapi.h:211
IndexScanDesc gistbeginscan(Relation r, int nkeys, int norderbys)
Definition: gistscan.c:58
void gistendscan(IndexScanDesc scan)
Definition: gistscan.c:333
bool gistproperty(Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
Definition: gistutil.c:944
amcanreturn_function amcanreturn
Definition: amapi.h:215
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:231
amrestrpos_function amrestrpos
Definition: amapi.h:227

◆ gistinsert()

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

Definition at line 151 of file gist.c.

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

Referenced by gisthandler().

155 {
156  GISTSTATE *giststate = (GISTSTATE *) indexInfo->ii_AmCache;
157  IndexTuple itup;
158  MemoryContext oldCxt;
159 
160  /* Initialize GISTSTATE cache if first call in this statement */
161  if (giststate == NULL)
162  {
163  oldCxt = MemoryContextSwitchTo(indexInfo->ii_Context);
164  giststate = initGISTstate(r);
165  giststate->tempCxt = createTempGistContext();
166  indexInfo->ii_AmCache = (void *) giststate;
167  MemoryContextSwitchTo(oldCxt);
168  }
169 
170  oldCxt = MemoryContextSwitchTo(giststate->tempCxt);
171 
172  itup = gistFormTuple(giststate, r,
173  values, isnull, true /* size is currently bogus */ );
174  itup->t_tid = *ht_ctid;
175 
176  gistdoinsert(r, itup, 0, giststate, heapRel, false);
177 
178  /* cleanup */
179  MemoryContextSwitchTo(oldCxt);
180  MemoryContextReset(giststate->tempCxt);
181 
182  return false;
183 }
MemoryContext ii_Context
Definition: execnodes.h:177
MemoryContext createTempGistContext(void)
Definition: gist.c:114
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:623
MemoryContext tempCxt
Definition: gist_private.h:78
void * ii_AmCache
Definition: execnodes.h:176
GISTSTATE * initGISTstate(Relation index)
Definition: gist.c:1486
static Datum values[MAXATTR]
Definition: bootstrap.c:167
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:575

◆ gistinserttuple()

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

Definition at line 1219 of file gist.c.

References gistinserttuples(), and InvalidBuffer.

Referenced by gistdoinsert().

1221 {
1222  return gistinserttuples(state, stack, giststate, &tuple, 1, oldoffnum,
1223  InvalidBuffer, InvalidBuffer, false, false);
1224 }
#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:1253

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

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

Referenced by gistfinishsplit(), and gistinserttuple().

1258 {
1259  List *splitinfo;
1260  bool is_split;
1261 
1262  /*
1263  * Check for any rw conflicts (in serializable isolation level) just
1264  * before we intend to modify the page
1265  */
1266  CheckForSerializableConflictIn(state->r, NULL, stack->buffer);
1267 
1268  /* Insert the tuple(s) to the page, splitting the page if necessary */
1269  is_split = gistplacetopage(state->r, state->freespace, giststate,
1270  stack->buffer,
1271  tuples, ntup,
1272  oldoffnum, NULL,
1273  leftchild,
1274  &splitinfo,
1275  true,
1276  state->heapRel,
1277  state->is_build);
1278 
1279  /*
1280  * Before recursing up in case the page was split, release locks on the
1281  * child pages. We don't need to keep them locked when updating the
1282  * parent.
1283  */
1286  if (BufferIsValid(leftchild) && unlockleftchild)
1288 
1289  /*
1290  * If we had to split, insert/update the downlinks in the parent. If the
1291  * caller requested us to release the lock on stack->buffer, tell
1292  * gistfinishsplit() to do that as soon as it's safe to do so. If we
1293  * didn't have to split, release it ourselves.
1294  */
1295  if (splitinfo)
1296  gistfinishsplit(state, stack, giststate, splitinfo, unlockbuf);
1297  else if (unlockbuf)
1298  LockBuffer(stack->buffer, GIST_UNLOCK);
1299 
1300  return is_split;
1301 }
Relation heapRel
Definition: gist_private.h:251
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:216
#define GIST_UNLOCK
Definition: gist_private.h:44
void CheckForSerializableConflictIn(Relation relation, HeapTuple tuple, Buffer buffer)
Definition: predicate.c:4442
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3376
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
#define rightchild(x)
Definition: fsmpage.c:30
#define leftchild(x)
Definition: fsmpage.c:29
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
static void gistfinishsplit(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, List *splitinfo, bool unlockbuf)
Definition: gist.c:1313
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 216 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(), memmove, 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().

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

1626 {
1628  int ndeletable = 0;
1629  OffsetNumber offnum,
1630  maxoff;
1631  TransactionId latestRemovedXid = InvalidTransactionId;
1632 
1633  Assert(GistPageIsLeaf(page));
1634 
1635  /*
1636  * Scan over all items to see which ones need to be deleted according to
1637  * LP_DEAD flags.
1638  */
1639  maxoff = PageGetMaxOffsetNumber(page);
1640  for (offnum = FirstOffsetNumber;
1641  offnum <= maxoff;
1642  offnum = OffsetNumberNext(offnum))
1643  {
1644  ItemId itemId = PageGetItemId(page, offnum);
1645 
1646  if (ItemIdIsDead(itemId))
1647  deletable[ndeletable++] = offnum;
1648  }
1649 
1651  latestRemovedXid =
1652  index_compute_xid_horizon_for_tuples(rel, heapRel, buffer,
1653  deletable, ndeletable);
1654 
1655  if (ndeletable > 0)
1656  {
1658 
1659  PageIndexMultiDelete(page, deletable, ndeletable);
1660 
1661  /*
1662  * Mark the page as not containing any LP_DEAD items. This is not
1663  * certainly true (there might be some that have recently been marked,
1664  * but weren't included in our target-item list), but it will almost
1665  * always be true and it doesn't seem worth an additional page scan to
1666  * check it. Remember that F_HAS_GARBAGE is only a hint anyway.
1667  */
1669 
1670  MarkBufferDirty(buffer);
1671 
1672  /* XLOG stuff */
1673  if (RelationNeedsWAL(rel))
1674  {
1675  XLogRecPtr recptr;
1676 
1677  recptr = gistXLogDelete(buffer,
1678  deletable, ndeletable,
1679  latestRemovedXid);
1680 
1681  PageSetLSN(page, recptr);
1682  }
1683  else
1684  PageSetLSN(page, gistGetFakeLSN(rel));
1685 
1686  END_CRIT_SECTION();
1687  }
1688 
1689  /*
1690  * Note: if we didn't find any LP_DEAD items, then the page's
1691  * F_HAS_GARBAGE hint bit is falsely set. We do not bother expending a
1692  * separate write to clear it, however. We will clear it when we split
1693  * the page.
1694  */
1695 }
uint32 TransactionId
Definition: c.h:507
#define GistClearPageHasGarbage(page)
Definition: gist.h:151
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
#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:1027
XLogRecPtr gistXLogDelete(Buffer buffer, OffsetNumber *todelete, int ntodelete, TransactionId latestRemovedXid)
Definition: gistxlog.c:673
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
#define InvalidTransactionId
Definition: transam.h:31
#define GistPageIsLeaf(page)
Definition: gist.h:140
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
#define XLogStandbyInfoActive()
Definition: xlog.h:195
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:835
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define RelationNeedsWAL(relation)
Definition: rel.h:518
#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:281

◆ gistSplit()

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

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

1404 {
1405  IndexTuple *lvectup,
1406  *rvectup;
1407  GistSplitVector v;
1408  int i;
1409  SplitedPageLayout *res = NULL;
1410 
1411  /* this should never recurse very deeply, but better safe than sorry */
1413 
1414  /* there's no point in splitting an empty page */
1415  Assert(len > 0);
1416 
1417  /*
1418  * If a single tuple doesn't fit on a page, no amount of splitting will
1419  * help.
1420  */
1421  if (len == 1)
1422  ereport(ERROR,
1423  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1424  errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
1425  IndexTupleSize(itup[0]), GiSTPageSize,
1427 
1428  memset(v.spl_lisnull, true,
1429  sizeof(bool) * giststate->nonLeafTupdesc->natts);
1430  memset(v.spl_risnull, true,
1431  sizeof(bool) * giststate->nonLeafTupdesc->natts);
1432  gistSplitByKey(r, page, itup, len, giststate, &v, 0);
1433 
1434  /* form left and right vector */
1435  lvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
1436  rvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
1437 
1438  for (i = 0; i < v.splitVector.spl_nleft; i++)
1439  lvectup[i] = itup[v.splitVector.spl_left[i] - 1];
1440 
1441  for (i = 0; i < v.splitVector.spl_nright; i++)
1442  rvectup[i] = itup[v.splitVector.spl_right[i] - 1];
1443 
1444  /* finalize splitting (may need another split) */
1445  if (!gistfitpage(rvectup, v.splitVector.spl_nright))
1446  {
1447  res = gistSplit(r, page, rvectup, v.splitVector.spl_nright, giststate);
1448  }
1449  else
1450  {
1451  ROTATEDIST(res);
1452  res->block.num = v.splitVector.spl_nright;
1453  res->list = gistfillitupvec(rvectup, v.splitVector.spl_nright, &(res->lenlist));
1454  res->itup = gistFormTuple(giststate, r, v.spl_rattr, v.spl_risnull, false);
1455  }
1456 
1457  if (!gistfitpage(lvectup, v.splitVector.spl_nleft))
1458  {
1459  SplitedPageLayout *resptr,
1460  *subres;
1461 
1462  resptr = subres = gistSplit(r, page, lvectup, v.splitVector.spl_nleft, giststate);
1463 
1464  /* install on list's tail */
1465  while (resptr->next)
1466  resptr = resptr->next;
1467 
1468  resptr->next = res;
1469  res = subres;
1470  }
1471  else
1472  {
1473  ROTATEDIST(res);
1474  res->block.num = v.splitVector.spl_nleft;
1475  res->list = gistfillitupvec(lvectup, v.splitVector.spl_nleft, &(res->lenlist));
1476  res->itup = gistFormTuple(giststate, r, v.spl_lattr, v.spl_lisnull, false);
1477  }
1478 
1479  return res;
1480 }
Datum spl_lattr[INDEX_MAX_KEYS]
Definition: gist_private.h:236
OffsetNumber * spl_left
Definition: gist.h:113
GIST_SPLITVEC splitVector
Definition: gist_private.h:234
int errcode(int sqlerrcode)
Definition: elog.c:570
int spl_nleft
Definition: gist.h:114
IndexTupleData * list
Definition: gist_private.h:191
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:240
gistxlogPage block
Definition: gist_private.h:190
#define ERROR
Definition: elog.h:43
int spl_nright
Definition: gist.h:119
void check_stack_depth(void)
Definition: postgres.c:3262
#define RelationGetRelationName(relation)
Definition: rel.h:450
struct SplitedPageLayout * next
Definition: gist_private.h:197
#define ereport(elevel, rest)
Definition: elog.h:141
#define GiSTPageSize
Definition: gist_private.h:457
#define ROTATEDIST(d)
Definition: gist.c:45
SplitedPageLayout * gistSplit(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate)
Definition: gist.c:1399
TupleDesc nonLeafTupdesc
Definition: gist_private.h:81
bool gistfitpage(IndexTuple *itvec, int len)
Definition: gistutil.c:79
#define Assert(condition)
Definition: c.h:732
IndexTupleData * gistfillitupvec(IndexTuple *vec, int veclen, int *memlen)
Definition: gistutil.c:127
OffsetNumber * spl_right
Definition: gist.h:118
bool spl_risnull[INDEX_MAX_KEYS]
Definition: gist_private.h:242
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:575
bool spl_lisnull[INDEX_MAX_KEYS]
Definition: gist_private.h:238
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ initGISTstate()

GISTSTATE* initGISTstate ( Relation  index)

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

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