PostgreSQL Source Code  git master
gist.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * gist.c
4  * interface routines for the postgres GiST index access method.
5  *
6  *
7  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/access/gist/gist.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/gist_private.h"
18 #include "access/gistscan.h"
19 #include "catalog/pg_collation.h"
20 #include "miscadmin.h"
21 #include "storage/lmgr.h"
22 #include "storage/predicate.h"
23 #include "nodes/execnodes.h"
24 #include "utils/builtins.h"
25 #include "utils/index_selfuncs.h"
26 #include "utils/memutils.h"
27 #include "utils/rel.h"
28 
29 
30 /* non-export function prototypes */
31 static void gistfixsplit(GISTInsertState *state, GISTSTATE *giststate);
33  GISTSTATE *giststate, IndexTuple tuple, OffsetNumber oldoffnum);
35  GISTSTATE *giststate,
36  IndexTuple *tuples, int ntup, OffsetNumber oldoffnum,
38  bool unlockbuf, bool unlockleftchild);
40  GISTSTATE *giststate, List *splitinfo, bool unlockbuf);
41 static void gistprunepage(Relation rel, Page page, Buffer buffer,
42  Relation heapRel);
43 
44 
45 #define ROTATEDIST(d) do { \
46  SplitedPageLayout *tmp=(SplitedPageLayout*)palloc0(sizeof(SplitedPageLayout)); \
47  tmp->block.blkno = InvalidBlockNumber; \
48  tmp->buffer = InvalidBuffer; \
49  tmp->next = (d); \
50  (d)=tmp; \
51 } while(0)
52 
53 
54 /*
55  * GiST handler function: return IndexAmRoutine with access method parameters
56  * and callbacks.
57  */
58 Datum
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 }
104 
105 /*
106  * Create and return a temporary memory context for use by GiST. We
107  * _always_ invoke user-provided methods in a temporary memory
108  * context, so that memory leaks in those functions cannot cause
109  * problems. Also, we use some additional temporary contexts in the
110  * GiST code itself, to avoid the need to do some awkward manual
111  * memory management.
112  */
115 {
117  "GiST temporary context",
119 }
120 
121 /*
122  * gistbuildempty() -- build an empty gist index in the initialization fork
123  */
124 void
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 }
143 
144 /*
145  * gistinsert -- wrapper for GiST tuple insertion.
146  *
147  * This is the public interface routine for tuple insertion in GiSTs.
148  * It doesn't do any work; just locks the relation and passes the buck.
149  */
150 bool
151 gistinsert(Relation r, Datum *values, bool *isnull,
152  ItemPointer ht_ctid, Relation heapRel,
153  IndexUniqueCheck checkUnique,
154  IndexInfo *indexInfo)
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 }
184 
185 
186 /*
187  * Place tuples from 'itup' to 'buffer'. If 'oldoffnum' is valid, the tuple
188  * at that offset is atomically removed along with inserting the new tuples.
189  * This is used to replace a tuple with a new one.
190  *
191  * If 'leftchildbuf' is valid, we're inserting the downlink for the page
192  * to the right of 'leftchildbuf', or updating the downlink for 'leftchildbuf'.
193  * F_FOLLOW_RIGHT flag on 'leftchildbuf' is cleared and NSN is set.
194  *
195  * If 'markfollowright' is true and the page is split, the left child is
196  * marked with F_FOLLOW_RIGHT flag. That is the normal case. During buffered
197  * index build, however, there is no concurrent access and the page splitting
198  * is done in a slightly simpler fashion, and false is passed.
199  *
200  * If there is not enough room on the page, it is split. All the split
201  * pages are kept pinned and locked and returned in *splitinfo, the caller
202  * is responsible for inserting the downlinks for them. However, if
203  * 'buffer' is the root page and it needs to be split, gistplacetopage()
204  * performs the split as one atomic operation, and *splitinfo is set to NIL.
205  * In that case, we continue to hold the root page locked, and the child
206  * pages are released; note that new tuple(s) are *not* on the root page
207  * but in one of the new child pages.
208  *
209  * If 'newblkno' is not NULL, returns the block number of page the first
210  * new/updated tuple was inserted to. Usually it's the given page, but could
211  * be its right sibling if the page was split.
212  *
213  * Returns 'true' if the page was split, 'false' otherwise.
214  */
215 bool
216 gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
217  Buffer buffer,
218  IndexTuple *itup, int ntup, OffsetNumber oldoffnum,
219  BlockNumber *newblkno,
220  Buffer leftchildbuf,
221  List **splitinfo,
222  bool markfollowright,
223  Relation heapRel,
224  bool is_build)
225 {
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 }
616 
617 /*
618  * Workhouse routine for doing insertion into a GiST index. Note that
619  * this routine assumes it is invoked in a short-lived memory context,
620  * so it does not bother releasing palloc'd allocations.
621  */
622 void
624  GISTSTATE *giststate, Relation heapRel, bool is_build)
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 }
887 
888 /*
889  * Traverse the tree to find path from root page to specified "child" block.
890  *
891  * returns a new insertion stack, starting from the parent of "child", up
892  * to the root. *downlinkoffnum is set to the offset of the downlink in the
893  * direct parent of child.
894  *
895  * To prevent deadlocks, this should lock only one page at a time.
896  */
897 static GISTInsertStack *
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;
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 }
1004 
1005 /*
1006  * Updates the stack so that child->parent is the correct parent of the
1007  * child. child->parent must be exclusively locked on entry, and will
1008  * remain so at exit, but it might not be the same page anymore.
1009  */
1010 static void
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 }
1095 
1096 /*
1097  * Form a downlink pointer for the page in 'buf'.
1098  */
1099 static IndexTuple
1101  GISTInsertStack *stack)
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 }
1154 
1155 
1156 /*
1157  * Complete the incomplete split of state->stack->page.
1158  */
1159 static void
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 }
1207 
1208 /*
1209  * Insert or replace a tuple in stack->buffer. If 'oldoffnum' is valid, the
1210  * tuple at 'oldoffnum' is replaced, otherwise the tuple is inserted as new.
1211  * 'stack' represents the path from the root to the page being updated.
1212  *
1213  * The caller must hold an exclusive lock on stack->buffer. The lock is still
1214  * held on return, but the page might not contain the inserted tuple if the
1215  * page was split. The function returns true if the page was split, false
1216  * otherwise.
1217  */
1218 static bool
1220  GISTSTATE *giststate, IndexTuple tuple, OffsetNumber oldoffnum)
1221 {
1222  return gistinserttuples(state, stack, giststate, &tuple, 1, oldoffnum,
1223  InvalidBuffer, InvalidBuffer, false, false);
1224 }
1225 
1226 /* ----------------
1227  * An extended workhorse version of gistinserttuple(). This version allows
1228  * inserting multiple tuples, or replacing a single tuple with multiple tuples.
1229  * This is used to recursively update the downlinks in the parent when a page
1230  * is split.
1231  *
1232  * If leftchild and rightchild are valid, we're inserting/replacing the
1233  * downlink for rightchild, and leftchild is its left sibling. We clear the
1234  * F_FOLLOW_RIGHT flag and update NSN on leftchild, atomically with the
1235  * insertion of the downlink.
1236  *
1237  * To avoid holding locks for longer than necessary, when recursing up the
1238  * tree to update the parents, the locking is a bit peculiar here. On entry,
1239  * the caller must hold an exclusive lock on stack->buffer, as well as
1240  * leftchild and rightchild if given. On return:
1241  *
1242  * - Lock on stack->buffer is released, if 'unlockbuf' is true. The page is
1243  * always kept pinned, however.
1244  * - Lock on 'leftchild' is released, if 'unlockleftchild' is true. The page
1245  * is kept pinned.
1246  * - Lock and pin on 'rightchild' are always released.
1247  *
1248  * Returns 'true' if the page had to be split. Note that if the page was
1249  * split, the inserted/updated tuples might've been inserted to a right
1250  * sibling of stack->buffer instead of stack->buffer itself.
1251  */
1252 static bool
1254  GISTSTATE *giststate,
1255  IndexTuple *tuples, int ntup, OffsetNumber oldoffnum,
1257  bool unlockbuf, bool unlockleftchild)
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  */
1284  if (BufferIsValid(rightchild))
1285  UnlockReleaseBuffer(rightchild);
1286  if (BufferIsValid(leftchild) && unlockleftchild)
1287  LockBuffer(leftchild, GIST_UNLOCK);
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 }
1302 
1303 /*
1304  * Finish an incomplete split by inserting/updating the downlinks in parent
1305  * page. 'splitinfo' contains all the child pages involved in the split,
1306  * from left-to-right.
1307  *
1308  * On entry, the caller must hold a lock on stack->buffer and all the child
1309  * pages in 'splitinfo'. If 'unlockbuf' is true, the lock on stack->buffer is
1310  * released on return. The child pages are always unlocked and unpinned.
1311  */
1312 static void
1314  GISTSTATE *giststate, List *splitinfo, bool unlockbuf)
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 }
1392 
1393 /*
1394  * gistSplit -- split a page in the tree and fill struct
1395  * used for XLOG and real writes buffers. Function is recursive, ie
1396  * it will split page until keys will fit in every page.
1397  */
1400  Page page,
1401  IndexTuple *itup, /* contains compressed entry */
1402  int len,
1403  GISTSTATE *giststate)
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 }
1481 
1482 /*
1483  * Create a GISTSTATE and fill it with information about the index
1484  */
1485 GISTSTATE *
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 }
1612 
1613 void
1615 {
1616  /* It's sufficient to delete the scanCxt */
1617  MemoryContextDelete(giststate->scanCxt);
1618 }
1619 
1620 /*
1621  * gistprunepage() -- try to remove LP_DEAD items from the given page.
1622  * Function assumes that buffer is exclusively locked.
1623  */
1624 static void
1625 gistprunepage(Relation rel, Page page, Buffer buffer, Relation heapRel)
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 }
#define GistFollowRight(page)
Definition: gist.h:153
ambeginscan_function ambeginscan
Definition: amapi.h:221
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define NIL
Definition: pg_list.h:65
BlockNumber blkno
Definition: gist_private.h:225
#define GistPageGetNSN(page)
Definition: gist.h:157
MemoryContext ii_Context
Definition: execnodes.h:177
ambulkdelete_function ambulkdelete
Definition: amapi.h:213
bool amcanmulticol
Definition: amapi.h:183
static bool gistinserttuple(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, IndexTuple tuple, OffsetNumber oldoffnum)
Definition: gist.c:1219
uint16 amsupport
Definition: amapi.h:173
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:169
#define GistBuildLSN
Definition: gist.h:58
#define GistPageIsDeleted(page)
Definition: gist.h:143
int errhint(const char *fmt,...)
Definition: elog.c:974
#define GIST_FETCH_PROC
Definition: gist.h:37
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1009
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:794
Datum spl_lattr[INDEX_MAX_KEYS]
Definition: gist_private.h:254
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:410
static void gistfixsplit(GISTInsertState *state, GISTSTATE *giststate)
Definition: gist.c:1160
uint32 TransactionId
Definition: c.h:507
amgettuple_function amgettuple
Definition: amapi.h:223
BlockNumber blkno
Definition: gist_private.h:201
FmgrInfo fetchFn[INDEX_MAX_KEYS]
Definition: gist_private.h:94
IndexBulkDeleteResult * gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: gistvacuum.c:101
bool amcanorderbyop
Definition: amapi.h:177
#define GistClearPageHasGarbage(page)
Definition: gist.h:151
amproperty_function amproperty
Definition: amapi.h:218
#define GIST_EQUAL_PROC
Definition: gist.h:35
void PageIndexTupleDelete(Page page, OffsetNumber offnum)
Definition: bufpage.c:726
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
MemoryContext createTempGistContext(void)
Definition: gist.c:114
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gist_private.h:97
FmgrInfo compressFn[INDEX_MAX_KEYS]
Definition: gist_private.h:88
IndexBulkDeleteResult * gistbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: gistvacuum.c:83
Datum gisthandler(PG_FUNCTION_ARGS)
Definition: gist.c:59
Relation heapRel
Definition: gist_private.h:269
FmgrInfo equalFn[INDEX_MAX_KEYS]
Definition: gist_private.h:92
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
ItemPointerData t_tid
Definition: itup.h:37
void gistfillbuffer(Page page, IndexTuple *itup, int len, OffsetNumber off)
Definition: gistutil.c:34
amparallelrescan_function amparallelrescan
Definition: amapi.h:232
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
bool amstorage
Definition: amapi.h:191
bool gistgettuple(IndexScanDesc scan, ScanDirection dir)
Definition: gistget.c:628
OffsetNumber * spl_left
Definition: gist.h:113
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Pointer Item
Definition: item.h:17
#define GistPageSetNSN(page, val)
Definition: gist.h:158
#define InvalidBuffer
Definition: buf.h:25
GIST_SPLITVEC splitVector
Definition: gist_private.h:252
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 START_CRIT_SECTION()
Definition: miscadmin.h:132
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
bool ampredlocks
Definition: amapi.h:195
#define GistTupleIsInvalid(itup)
Definition: gist_private.h:303
#define GIST_UNLOCK
Definition: gist_private.h:44
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
#define GistPageHasGarbage(page)
Definition: gist.h:149
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3353
#define P_NEW
Definition: bufmgr.h:81
aminsert_function aminsert
Definition: amapi.h:212
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:88
int spl_nleft
Definition: gist.h:114
#define LOG
Definition: elog.h:26
bytea * gistoptions(Datum reloptions, bool validate)
Definition: gistutil.c:909
IndexTupleData * list
Definition: gist_private.h:209
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
Oid amkeytype
Definition: amapi.h:201
#define OidIsValid(objectId)
Definition: c.h:638
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
IndexTuple gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
Definition: gistutil.c:316
void CheckForSerializableConflictIn(Relation relation, HeapTuple tuple, Buffer buffer)
Definition: predicate.c:4442
bool amoptionalkey
Definition: amapi.h:185
void gistSplitByKey(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate, GistSplitVector *v, int attno)
Definition: gistsplit.c:623
amvalidate_function amvalidate
Definition: amapi.h:220
Datum spl_rattr[INDEX_MAX_KEYS]
Definition: gist_private.h:258
XLogRecPtr gistGetFakeLSN(Relation rel)
Definition: gistutil.c:1027
#define lsecond(l)
Definition: pg_list.h:200
XLogRecPtr gistXLogDelete(Buffer buffer, OffsetNumber *todelete, int ntodelete, TransactionId latestRemovedXid)
Definition: gistxlog.c:673
bool gistinsert(Relation r, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: gist.c:151
uint16 OffsetNumber
Definition: off.h:24
Definition: type.h:89
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:388
gistxlogPage block
Definition: gist_private.h:208
#define list_make1(x1)
Definition: pg_list.h:227
IndexBuildResult * gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: gistbuild.c:116
IndexUniqueCheck
Definition: genam.h:112
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
amgetbitmap_function amgetbitmap
Definition: amapi.h:224
FmgrInfo consistentFn[INDEX_MAX_KEYS]
Definition: gist_private.h:86
#define linitial(l)
Definition: pg_list.h:195
#define GIST_PICKSPLIT_PROC
Definition: gist.h:34
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3376
Oid * rd_indcollation
Definition: rel.h:168
#define ERROR
Definition: elog.h:43
static IndexTuple gistformdownlink(Relation rel, Buffer buf, GISTSTATE *giststate, GISTInsertStack *stack)
Definition: gist.c:1100
ambuild_function ambuild
Definition: amapi.h:210
#define GIST_COMPRESS_PROC
Definition: gist.h:31
#define GIST_MAX_SPLIT_PAGES
Definition: gist_private.h:39
amoptions_function amoptions
Definition: amapi.h:217
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
void gistdoinsert(Relation r, IndexTuple itup, Size freespace, GISTSTATE *giststate, Relation heapRel, bool is_build)
Definition: gist.c:623
int spl_nright
Definition: gist.h:119
bool amcaninclude
Definition: amapi.h:199
#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
BlockNumber blkno
Definition: ginvacuum.c:119
amcostestimate_function amcostestimate
Definition: amapi.h:216
IndexTuple downlink
Definition: gist_private.h:428
bool amcanunique
Definition: amapi.h:181
void gistrescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys)
Definition: gistscan.c:131
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
Definition: bufpage.c:1065
void gistcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:6126
GISTInsertStack * stack
Definition: gist_private.h:273
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:509
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2876
static char * buf
Definition: pg_test_fsync.c:68
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:214
amendscan_function amendscan
Definition: amapi.h:225
#define memmove(d, s, c)
Definition: c.h:1238
bool amcanbackward
Definition: amapi.h:179
void check_stack_depth(void)
Definition: postgres.c:3262
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
int errdetail(const char *fmt,...)
Definition: elog.c:860
FmgrInfo picksplitFn[INDEX_MAX_KEYS]
Definition: gist_private.h:91
#define InvalidTransactionId
Definition: transam.h:31
bool gistnospace(Page page, IndexTuple *itvec, int len, OffsetNumber todelete, Size freespace)
Definition: gistutil.c:59
#define RelationGetRelationName(relation)
Definition: rel.h:450
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
struct SplitedPageLayout * next
Definition: gist_private.h:215
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
void freeGISTstate(GISTSTATE *giststate)
Definition: gist.c:1614
#define ereport(elevel, rest)
Definition: elog.h:141
amrescan_function amrescan
Definition: amapi.h:222
bool amcanparallel
Definition: amapi.h:197
FmgrInfo decompressFn[INDEX_MAX_KEYS]
Definition: gist_private.h:89
int64 gistgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: gistget.c:762
List * lappend(List *list, void *datum)
Definition: list.c:321
void * ii_AmCache
Definition: execnodes.h:176
TupleDesc leafTupdesc
Definition: gist_private.h:80
OffsetNumber gistchoose(Relation r, Page p, IndexTuple it, GISTSTATE *giststate)
Definition: gistutil.c:374
void gistbuildempty(Relation index)
Definition: gist.c:125
#define GistPageIsLeaf(page)
Definition: gist.h:140
#define GistTupleSetValid(itup)
Definition: gist_private.h:304
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
OffsetNumber downlinkoffnum
Definition: gist_private.h:243
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
static void gistFindCorrectParent(Relation r, GISTInsertStack *child)
Definition: gist.c:1011
bool amsearchnulls
Definition: amapi.h:189
#define GiSTPageSize
Definition: gist_private.h:475
#define GistClearFollowRight(page)
Definition: gist.h:155
IndexTuple * gistjoinvector(IndexTuple *itvec, int *len, IndexTuple *additvec, int addlen)
Definition: gistutil.c:114
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 * palloc0(Size size)
Definition: mcxt.c:955
uintptr_t Datum
Definition: postgres.h:367
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
#define ROTATEDIST(d)
Definition: gist.c:45
bool amclusterable
Definition: amapi.h:193
#define XLogStandbyInfoActive()
Definition: xlog.h:195
TupleDesc rd_att
Definition: rel.h:84
GISTSTATE * initGISTstate(Relation index)
Definition: gist.c:1486
struct DataPageDeleteStack * child
Definition: ginvacuum.c:116
bool amsearcharray
Definition: amapi.h:187
SplitedPageLayout * gistSplit(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate)
Definition: gist.c:1399
#define rightchild(x)
Definition: fsmpage.c:30
#define InvalidOffsetNumber
Definition: off.h:26
static GISTInsertStack * gistFindPath(Relation r, BlockNumber child, OffsetNumber *downlinkoffnum)
Definition: gist.c:898
#define InvalidOid
Definition: postgres_ext.h:36
TupleDesc nonLeafTupdesc
Definition: gist_private.h:81
XLogRecPtr gistXLogUpdate(Buffer buffer, OffsetNumber *todelete, int ntodelete, IndexTuple *itup, int ituplen, Buffer leftchildbuf)
Definition: gistxlog.c:632
Oid fn_oid
Definition: fmgr.h:59
#define GIST_CONSISTENT_PROC
Definition: gist.h:29
bool gistfitpage(IndexTuple *itvec, int len)
Definition: gistutil.c:79
#define GIST_UNION_PROC
Definition: gist.h:30
bool gistcanreturn(Relation index, int attno)
Definition: gistget.c:815
#define GistPageGetOpaque(page)
Definition: gist.h:138
List * lcons(void *datum, List *list)
Definition: list.c:453
#define makeNode(_type_)
Definition: nodes.h:573
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
struct DataPageDeleteStack * parent
Definition: ginvacuum.c:117
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:835
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:771
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:596
#define INDEX_MAX_KEYS
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:466
IndexTupleData * gistfillitupvec(IndexTuple *vec, int veclen, int *memlen)
Definition: gistutil.c:127
#define GIST_PENALTY_PROC
Definition: gist.h:33
#define InvalidBlockNumber
Definition: block.h:33
static int list_length(const List *l)
Definition: pg_list.h:169
void XLogEnsureRecordSpace(int max_block_id, int ndatas)
Definition: xloginsert.c:146
#define leftchild(x)
Definition: fsmpage.c:29
#define GIST_DISTANCE_PROC
Definition: gist.h:36
OffsetNumber * spl_right
Definition: gist.h:118
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
#define GIST_SHARE
Definition: gist_private.h:42
ammarkpos_function ammarkpos
Definition: amapi.h:226
bool amcanorder
Definition: amapi.h:175
ambuildphasename_function ambuildphasename
Definition: amapi.h:219
FmgrInfo distanceFn[INDEX_MAX_KEYS]
Definition: gist_private.h:93
#define ItemPointerSetBlockNumber(pointer, blockNumber)
Definition: itemptr.h:138
#define GistMarkFollowRight(page)
Definition: gist.h:154
#define RelationNeedsWAL(relation)
Definition: rel.h:518
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:230
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
XLogRecPtr GistNSN
Definition: gist.h:51
#define PageGetLSN(page)
Definition: bufpage.h:366
#define GISTNProcs
Definition: gist.h:38
static Datum values[MAXATTR]
Definition: bootstrap.c:167
uint16 amstrategies
Definition: amapi.h:171
static void gistfinishsplit(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, List *splitinfo, bool unlockbuf)
Definition: gist.c:1313
bool spl_risnull[INDEX_MAX_KEYS]
Definition: gist_private.h:260
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
bool gistvalidate(Oid opclassoid)
Definition: gistvalidate.c:34
#define MaxIndexTuplesPerPage
Definition: itup.h:145
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define F_LEAF
Definition: gist.h:43
#define elog(elevel,...)
Definition: elog.h:226
ambuildempty_function ambuildempty
Definition: amapi.h:211
int i
#define GIST_ROOT_BLKNO
Definition: gist_private.h:277
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
MemoryContext scanCxt
Definition: gist_private.h:77
void GISTInitBuffer(Buffer b, uint32 f)
Definition: gistutil.c:749
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:575
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
#define GIST_DECOMPRESS_PROC
Definition: gist.h:32
IndexScanDesc gistbeginscan(Relation r, int nkeys, int norderbys)
Definition: gistscan.c:77
void gistendscan(IndexScanDesc scan)
Definition: gistscan.c:353
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
bool spl_lisnull[INDEX_MAX_KEYS]
Definition: gist_private.h:256
FmgrInfo unionFn[INDEX_MAX_KEYS]
Definition: gist_private.h:87
#define GIST_EXCLUSIVE
Definition: gist_private.h:43
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
Definition: pg_list.h:50
bool gistproperty(Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
Definition: gistutil.c:944
int Buffer
Definition: buf.h:23
struct GISTInsertStack * parent
Definition: gist_private.h:246
amcanreturn_function amcanreturn
Definition: amapi.h:215
Buffer gistNewBuffer(Relation r)
Definition: gistutil.c:810
void PredicateLockPageSplit(Relation relation, BlockNumber oldblkno, BlockNumber newblkno)
Definition: predicate.c:3118
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define IndexTupleSize(itup)
Definition: itup.h:71
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:231
List * list_delete_first(List *list)
Definition: list.c:857
TransactionId index_compute_xid_horizon_for_tuples(Relation irel, Relation hrel, Buffer ibuf, OffsetNumber *itemnos, int nitems)
Definition: genam.c:281
amrestrpos_function amrestrpos
Definition: amapi.h:227
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:760