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-2020, 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 "commands/vacuum.h"
21 #include "miscadmin.h"
22 #include "nodes/execnodes.h"
23 #include "storage/lmgr.h"
24 #include "storage/predicate.h"
25 #include "utils/builtins.h"
26 #include "utils/index_selfuncs.h"
27 #include "utils/memutils.h"
28 #include "utils/rel.h"
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->amoptsprocnum = GIST_OPTIONS_PROC;
66  amroutine->amcanorder = false;
67  amroutine->amcanorderbyop = true;
68  amroutine->amcanbackward = false;
69  amroutine->amcanunique = false;
70  amroutine->amcanmulticol = true;
71  amroutine->amoptionalkey = true;
72  amroutine->amsearcharray = false;
73  amroutine->amsearchnulls = true;
74  amroutine->amstorage = true;
75  amroutine->amclusterable = true;
76  amroutine->ampredlocks = true;
77  amroutine->amcanparallel = false;
78  amroutine->amcaninclude = true;
79  amroutine->amusemaintenanceworkmem = false;
80  amroutine->amparallelvacuumoptions =
82  amroutine->amkeytype = InvalidOid;
83 
84  amroutine->ambuild = gistbuild;
85  amroutine->ambuildempty = gistbuildempty;
86  amroutine->aminsert = gistinsert;
87  amroutine->ambulkdelete = gistbulkdelete;
89  amroutine->amcanreturn = gistcanreturn;
90  amroutine->amcostestimate = gistcostestimate;
91  amroutine->amoptions = gistoptions;
92  amroutine->amproperty = gistproperty;
93  amroutine->ambuildphasename = NULL;
94  amroutine->amvalidate = gistvalidate;
95  amroutine->ambeginscan = gistbeginscan;
96  amroutine->amrescan = gistrescan;
97  amroutine->amgettuple = gistgettuple;
98  amroutine->amgetbitmap = gistgetbitmap;
99  amroutine->amendscan = gistendscan;
100  amroutine->ammarkpos = NULL;
101  amroutine->amrestrpos = NULL;
102  amroutine->amestimateparallelscan = NULL;
103  amroutine->aminitparallelscan = NULL;
104  amroutine->amparallelrescan = NULL;
105 
106  PG_RETURN_POINTER(amroutine);
107 }
108 
109 /*
110  * Create and return a temporary memory context for use by GiST. We
111  * _always_ invoke user-provided methods in a temporary memory
112  * context, so that memory leaks in those functions cannot cause
113  * problems. Also, we use some additional temporary contexts in the
114  * GiST code itself, to avoid the need to do some awkward manual
115  * memory management.
116  */
119 {
121  "GiST temporary context",
123 }
124 
125 /*
126  * gistbuildempty() -- build an empty gist index in the initialization fork
127  */
128 void
130 {
131  Buffer buffer;
132 
133  /* Initialize the root page */
134  buffer = ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
136 
137  /* Initialize and xlog buffer */
139  GISTInitBuffer(buffer, F_LEAF);
140  MarkBufferDirty(buffer);
141  log_newpage_buffer(buffer, true);
143 
144  /* Unlock and release the buffer */
145  UnlockReleaseBuffer(buffer);
146 }
147 
148 /*
149  * gistinsert -- wrapper for GiST tuple insertion.
150  *
151  * This is the public interface routine for tuple insertion in GiSTs.
152  * It doesn't do any work; just locks the relation and passes the buck.
153  */
154 bool
155 gistinsert(Relation r, Datum *values, bool *isnull,
156  ItemPointer ht_ctid, Relation heapRel,
157  IndexUniqueCheck checkUnique,
158  IndexInfo *indexInfo)
159 {
160  GISTSTATE *giststate = (GISTSTATE *) indexInfo->ii_AmCache;
161  IndexTuple itup;
162  MemoryContext oldCxt;
163 
164  /* Initialize GISTSTATE cache if first call in this statement */
165  if (giststate == NULL)
166  {
167  oldCxt = MemoryContextSwitchTo(indexInfo->ii_Context);
168  giststate = initGISTstate(r);
169  giststate->tempCxt = createTempGistContext();
170  indexInfo->ii_AmCache = (void *) giststate;
171  MemoryContextSwitchTo(oldCxt);
172  }
173 
174  oldCxt = MemoryContextSwitchTo(giststate->tempCxt);
175 
176  itup = gistFormTuple(giststate, r,
177  values, isnull, true /* size is currently bogus */ );
178  itup->t_tid = *ht_ctid;
179 
180  gistdoinsert(r, itup, 0, giststate, heapRel, false);
181 
182  /* cleanup */
183  MemoryContextSwitchTo(oldCxt);
184  MemoryContextReset(giststate->tempCxt);
185 
186  return false;
187 }
188 
189 
190 /*
191  * Place tuples from 'itup' to 'buffer'. If 'oldoffnum' is valid, the tuple
192  * at that offset is atomically removed along with inserting the new tuples.
193  * This is used to replace a tuple with a new one.
194  *
195  * If 'leftchildbuf' is valid, we're inserting the downlink for the page
196  * to the right of 'leftchildbuf', or updating the downlink for 'leftchildbuf'.
197  * F_FOLLOW_RIGHT flag on 'leftchildbuf' is cleared and NSN is set.
198  *
199  * If 'markfollowright' is true and the page is split, the left child is
200  * marked with F_FOLLOW_RIGHT flag. That is the normal case. During buffered
201  * index build, however, there is no concurrent access and the page splitting
202  * is done in a slightly simpler fashion, and false is passed.
203  *
204  * If there is not enough room on the page, it is split. All the split
205  * pages are kept pinned and locked and returned in *splitinfo, the caller
206  * is responsible for inserting the downlinks for them. However, if
207  * 'buffer' is the root page and it needs to be split, gistplacetopage()
208  * performs the split as one atomic operation, and *splitinfo is set to NIL.
209  * In that case, we continue to hold the root page locked, and the child
210  * pages are released; note that new tuple(s) are *not* on the root page
211  * but in one of the new child pages.
212  *
213  * If 'newblkno' is not NULL, returns the block number of page the first
214  * new/updated tuple was inserted to. Usually it's the given page, but could
215  * be its right sibling if the page was split.
216  *
217  * Returns 'true' if the page was split, 'false' otherwise.
218  */
219 bool
220 gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
221  Buffer buffer,
222  IndexTuple *itup, int ntup, OffsetNumber oldoffnum,
223  BlockNumber *newblkno,
224  Buffer leftchildbuf,
225  List **splitinfo,
226  bool markfollowright,
227  Relation heapRel,
228  bool is_build)
229 {
231  Page page = BufferGetPage(buffer);
232  bool is_leaf = (GistPageIsLeaf(page)) ? true : false;
233  XLogRecPtr recptr;
234  int i;
235  bool is_split;
236 
237  /*
238  * Refuse to modify a page that's incompletely split. This should not
239  * happen because we finish any incomplete splits while we walk down the
240  * tree. However, it's remotely possible that another concurrent inserter
241  * splits a parent page, and errors out before completing the split. We
242  * will just throw an error in that case, and leave any split we had in
243  * progress unfinished too. The next insert that comes along will clean up
244  * the mess.
245  */
246  if (GistFollowRight(page))
247  elog(ERROR, "concurrent GiST page split was incomplete");
248 
249  *splitinfo = NIL;
250 
251  /*
252  * if isupdate, remove old key: This node's key has been modified, either
253  * because a child split occurred or because we needed to adjust our key
254  * for an insert in a child node. Therefore, remove the old version of
255  * this node's key.
256  *
257  * for WAL replay, in the non-split case we handle this by setting up a
258  * one-element todelete array; in the split case, it's handled implicitly
259  * because the tuple vector passed to gistSplit won't include this tuple.
260  */
261  is_split = gistnospace(page, itup, ntup, oldoffnum, freespace);
262 
263  /*
264  * If leaf page is full, try at first to delete dead tuples. And then
265  * check again.
266  */
267  if (is_split && GistPageIsLeaf(page) && GistPageHasGarbage(page))
268  {
269  gistprunepage(rel, page, buffer, heapRel);
270  is_split = gistnospace(page, itup, ntup, oldoffnum, freespace);
271  }
272 
273  if (is_split)
274  {
275  /* no space for insertion */
276  IndexTuple *itvec;
277  int tlen;
278  SplitedPageLayout *dist = NULL,
279  *ptr;
280  BlockNumber oldrlink = InvalidBlockNumber;
281  GistNSN oldnsn = 0;
282  SplitedPageLayout rootpg;
283  bool is_rootsplit;
284  int npage;
285 
286  is_rootsplit = (blkno == GIST_ROOT_BLKNO);
287 
288  /*
289  * Form index tuples vector to split. If we're replacing an old tuple,
290  * remove the old version from the vector.
291  */
292  itvec = gistextractpage(page, &tlen);
293  if (OffsetNumberIsValid(oldoffnum))
294  {
295  /* on inner page we should remove old tuple */
296  int pos = oldoffnum - FirstOffsetNumber;
297 
298  tlen--;
299  if (pos != tlen)
300  memmove(itvec + pos, itvec + pos + 1, sizeof(IndexTuple) * (tlen - pos));
301  }
302  itvec = gistjoinvector(itvec, &tlen, itup, ntup);
303  dist = gistSplit(rel, page, itvec, tlen, giststate);
304 
305  /*
306  * Check that split didn't produce too many pages.
307  */
308  npage = 0;
309  for (ptr = dist; ptr; ptr = ptr->next)
310  npage++;
311  /* in a root split, we'll add one more page to the list below */
312  if (is_rootsplit)
313  npage++;
314  if (npage > GIST_MAX_SPLIT_PAGES)
315  elog(ERROR, "GiST page split into too many halves (%d, maximum %d)",
316  npage, GIST_MAX_SPLIT_PAGES);
317 
318  /*
319  * Set up pages to work with. Allocate new buffers for all but the
320  * leftmost page. The original page becomes the new leftmost page, and
321  * is just replaced with the new contents.
322  *
323  * For a root-split, allocate new buffers for all child pages, the
324  * original page is overwritten with new root page containing
325  * downlinks to the new child pages.
326  */
327  ptr = dist;
328  if (!is_rootsplit)
329  {
330  /* save old rightlink and NSN */
331  oldrlink = GistPageGetOpaque(page)->rightlink;
332  oldnsn = GistPageGetNSN(page);
333 
334  dist->buffer = buffer;
335  dist->block.blkno = BufferGetBlockNumber(buffer);
337 
338  /* clean all flags except F_LEAF */
339  GistPageGetOpaque(dist->page)->flags = (is_leaf) ? F_LEAF : 0;
340 
341  ptr = ptr->next;
342  }
343  for (; ptr; ptr = ptr->next)
344  {
345  /* Allocate new page */
346  ptr->buffer = gistNewBuffer(rel);
347  GISTInitBuffer(ptr->buffer, (is_leaf) ? F_LEAF : 0);
348  ptr->page = BufferGetPage(ptr->buffer);
349  ptr->block.blkno = BufferGetBlockNumber(ptr->buffer);
351  BufferGetBlockNumber(buffer),
352  BufferGetBlockNumber(ptr->buffer));
353  }
354 
355  /*
356  * Now that we know which blocks the new pages go to, set up downlink
357  * tuples to point to them.
358  */
359  for (ptr = dist; ptr; ptr = ptr->next)
360  {
361  ItemPointerSetBlockNumber(&(ptr->itup->t_tid), ptr->block.blkno);
362  GistTupleSetValid(ptr->itup);
363  }
364 
365  /*
366  * If this is a root split, we construct the new root page with the
367  * downlinks here directly, instead of requiring the caller to insert
368  * them. Add the new root page to the list along with the child pages.
369  */
370  if (is_rootsplit)
371  {
372  IndexTuple *downlinks;
373  int ndownlinks = 0;
374  int i;
375 
376  rootpg.buffer = buffer;
378  GistPageGetOpaque(rootpg.page)->flags = 0;
379 
380  /* Prepare a vector of all the downlinks */
381  for (ptr = dist; ptr; ptr = ptr->next)
382  ndownlinks++;
383  downlinks = palloc(sizeof(IndexTuple) * ndownlinks);
384  for (i = 0, ptr = dist; ptr; ptr = ptr->next)
385  downlinks[i++] = ptr->itup;
386 
387  rootpg.block.blkno = GIST_ROOT_BLKNO;
388  rootpg.block.num = ndownlinks;
389  rootpg.list = gistfillitupvec(downlinks, ndownlinks,
390  &(rootpg.lenlist));
391  rootpg.itup = NULL;
392 
393  rootpg.next = dist;
394  dist = &rootpg;
395  }
396  else
397  {
398  /* Prepare split-info to be returned to caller */
399  for (ptr = dist; ptr; ptr = ptr->next)
400  {
402 
403  si->buf = ptr->buffer;
404  si->downlink = ptr->itup;
405  *splitinfo = lappend(*splitinfo, si);
406  }
407  }
408 
409  /*
410  * Fill all pages. All the pages are new, ie. freshly allocated empty
411  * pages, or a temporary copy of the old page.
412  */
413  for (ptr = dist; ptr; ptr = ptr->next)
414  {
415  char *data = (char *) (ptr->list);
416 
417  for (i = 0; i < ptr->block.num; i++)
418  {
419  IndexTuple thistup = (IndexTuple) data;
420 
421  if (PageAddItem(ptr->page, (Item) data, IndexTupleSize(thistup), i + FirstOffsetNumber, false, false) == InvalidOffsetNumber)
422  elog(ERROR, "failed to add item to index page in \"%s\"", RelationGetRelationName(rel));
423 
424  /*
425  * If this is the first inserted/updated tuple, let the caller
426  * know which page it landed on.
427  */
428  if (newblkno && ItemPointerEquals(&thistup->t_tid, &(*itup)->t_tid))
429  *newblkno = ptr->block.blkno;
430 
431  data += IndexTupleSize(thistup);
432  }
433 
434  /* Set up rightlinks */
435  if (ptr->next && ptr->block.blkno != GIST_ROOT_BLKNO)
436  GistPageGetOpaque(ptr->page)->rightlink =
437  ptr->next->block.blkno;
438  else
439  GistPageGetOpaque(ptr->page)->rightlink = oldrlink;
440 
441  /*
442  * Mark the all but the right-most page with the follow-right
443  * flag. It will be cleared as soon as the downlink is inserted
444  * into the parent, but this ensures that if we error out before
445  * that, the index is still consistent. (in buffering build mode,
446  * any error will abort the index build anyway, so this is not
447  * needed.)
448  */
449  if (ptr->next && !is_rootsplit && markfollowright)
450  GistMarkFollowRight(ptr->page);
451  else
452  GistClearFollowRight(ptr->page);
453 
454  /*
455  * Copy the NSN of the original page to all pages. The
456  * F_FOLLOW_RIGHT flags ensure that scans will follow the
457  * rightlinks until the downlinks are inserted.
458  */
459  GistPageSetNSN(ptr->page, oldnsn);
460  }
461 
462  /*
463  * gistXLogSplit() needs to WAL log a lot of pages, prepare WAL
464  * insertion for that. NB: The number of pages and data segments
465  * specified here must match the calculations in gistXLogSplit()!
466  */
467  if (!is_build && RelationNeedsWAL(rel))
468  XLogEnsureRecordSpace(npage, 1 + npage * 2);
469 
471 
472  /*
473  * Must mark buffers dirty before XLogInsert, even though we'll still
474  * be changing their opaque fields below.
475  */
476  for (ptr = dist; ptr; ptr = ptr->next)
477  MarkBufferDirty(ptr->buffer);
478  if (BufferIsValid(leftchildbuf))
479  MarkBufferDirty(leftchildbuf);
480 
481  /*
482  * The first page in the chain was a temporary working copy meant to
483  * replace the old page. Copy it over the old page.
484  */
486  dist->page = BufferGetPage(dist->buffer);
487 
488  /*
489  * Write the WAL record.
490  *
491  * If we're building a new index, however, we don't WAL-log changes
492  * yet. The LSN-NSN interlock between parent and child requires that
493  * LSNs never move backwards, so set the LSNs to a value that's
494  * smaller than any real or fake unlogged LSN that might be generated
495  * later. (There can't be any concurrent scans during index build, so
496  * we don't need to be able to detect concurrent splits yet.)
497  */
498  if (is_build)
499  recptr = GistBuildLSN;
500  else
501  {
502  if (RelationNeedsWAL(rel))
503  recptr = gistXLogSplit(is_leaf,
504  dist, oldrlink, oldnsn, leftchildbuf,
505  markfollowright);
506  else
507  recptr = gistGetFakeLSN(rel);
508  }
509 
510  for (ptr = dist; ptr; ptr = ptr->next)
511  PageSetLSN(ptr->page, recptr);
512 
513  /*
514  * Return the new child buffers to the caller.
515  *
516  * If this was a root split, we've already inserted the downlink
517  * pointers, in the form of a new root page. Therefore we can release
518  * all the new buffers, and keep just the root page locked.
519  */
520  if (is_rootsplit)
521  {
522  for (ptr = dist->next; ptr; ptr = ptr->next)
523  UnlockReleaseBuffer(ptr->buffer);
524  }
525  }
526  else
527  {
528  /*
529  * Enough space. We always get here if ntup==0.
530  */
532 
533  /*
534  * Delete old tuple if any, then insert new tuple(s) if any. If
535  * possible, use the fast path of PageIndexTupleOverwrite.
536  */
537  if (OffsetNumberIsValid(oldoffnum))
538  {
539  if (ntup == 1)
540  {
541  /* One-for-one replacement, so use PageIndexTupleOverwrite */
542  if (!PageIndexTupleOverwrite(page, oldoffnum, (Item) *itup,
543  IndexTupleSize(*itup)))
544  elog(ERROR, "failed to add item to index page in \"%s\"",
546  }
547  else
548  {
549  /* Delete old, then append new tuple(s) to page */
550  PageIndexTupleDelete(page, oldoffnum);
551  gistfillbuffer(page, itup, ntup, InvalidOffsetNumber);
552  }
553  }
554  else
555  {
556  /* Just append new tuples at the end of the page */
557  gistfillbuffer(page, itup, ntup, InvalidOffsetNumber);
558  }
559 
560  MarkBufferDirty(buffer);
561 
562  if (BufferIsValid(leftchildbuf))
563  MarkBufferDirty(leftchildbuf);
564 
565  if (is_build)
566  recptr = GistBuildLSN;
567  else
568  {
569  if (RelationNeedsWAL(rel))
570  {
571  OffsetNumber ndeloffs = 0,
572  deloffs[1];
573 
574  if (OffsetNumberIsValid(oldoffnum))
575  {
576  deloffs[0] = oldoffnum;
577  ndeloffs = 1;
578  }
579 
580  recptr = gistXLogUpdate(buffer,
581  deloffs, ndeloffs, itup, ntup,
582  leftchildbuf);
583  }
584  else
585  recptr = gistGetFakeLSN(rel);
586  }
587  PageSetLSN(page, recptr);
588 
589  if (newblkno)
590  *newblkno = blkno;
591  }
592 
593  /*
594  * If we inserted the downlink for a child page, set NSN and clear
595  * F_FOLLOW_RIGHT flag on the left child, so that concurrent scans know to
596  * follow the rightlink if and only if they looked at the parent page
597  * before we inserted the downlink.
598  *
599  * Note that we do this *after* writing the WAL record. That means that
600  * the possible full page image in the WAL record does not include these
601  * changes, and they must be replayed even if the page is restored from
602  * the full page image. There's a chicken-and-egg problem: if we updated
603  * the child pages first, we wouldn't know the recptr of the WAL record
604  * we're about to write.
605  */
606  if (BufferIsValid(leftchildbuf))
607  {
608  Page leftpg = BufferGetPage(leftchildbuf);
609 
610  GistPageSetNSN(leftpg, recptr);
611  GistClearFollowRight(leftpg);
612 
613  PageSetLSN(leftpg, recptr);
614  }
615 
617 
618  return is_split;
619 }
620 
621 /*
622  * Workhouse routine for doing insertion into a GiST index. Note that
623  * this routine assumes it is invoked in a short-lived memory context,
624  * so it does not bother releasing palloc'd allocations.
625  */
626 void
628  GISTSTATE *giststate, Relation heapRel, bool is_build)
629 {
630  ItemId iid;
631  IndexTuple idxtuple;
632  GISTInsertStack firststack;
633  GISTInsertStack *stack;
635  bool xlocked = false;
636 
637  memset(&state, 0, sizeof(GISTInsertState));
638  state.freespace = freespace;
639  state.r = r;
640  state.heapRel = heapRel;
641  state.is_build = is_build;
642 
643  /* Start from the root */
644  firststack.blkno = GIST_ROOT_BLKNO;
645  firststack.lsn = 0;
646  firststack.retry_from_parent = false;
647  firststack.parent = NULL;
648  firststack.downlinkoffnum = InvalidOffsetNumber;
649  state.stack = stack = &firststack;
650 
651  /*
652  * Walk down along the path of smallest penalty, updating the parent
653  * pointers with the key we're inserting as we go. If we crash in the
654  * middle, the tree is consistent, although the possible parent updates
655  * were a waste.
656  */
657  for (;;)
658  {
659  /*
660  * If we split an internal page while descending the tree, we have to
661  * retry at the parent. (Normally, the LSN-NSN interlock below would
662  * also catch this and cause us to retry. But LSNs are not updated
663  * during index build.)
664  */
665  while (stack->retry_from_parent)
666  {
667  if (xlocked)
668  LockBuffer(stack->buffer, GIST_UNLOCK);
669  xlocked = false;
670  ReleaseBuffer(stack->buffer);
671  state.stack = stack = stack->parent;
672  }
673 
674  if (XLogRecPtrIsInvalid(stack->lsn))
675  stack->buffer = ReadBuffer(state.r, stack->blkno);
676 
677  /*
678  * Be optimistic and grab shared lock first. Swap it for an exclusive
679  * lock later if we need to update the page.
680  */
681  if (!xlocked)
682  {
683  LockBuffer(stack->buffer, GIST_SHARE);
684  gistcheckpage(state.r, stack->buffer);
685  }
686 
687  stack->page = (Page) BufferGetPage(stack->buffer);
688  stack->lsn = xlocked ?
689  PageGetLSN(stack->page) : BufferGetLSNAtomic(stack->buffer);
690  Assert(!RelationNeedsWAL(state.r) || !XLogRecPtrIsInvalid(stack->lsn));
691 
692  /*
693  * If this page was split but the downlink was never inserted to the
694  * parent because the inserting backend crashed before doing that, fix
695  * that now.
696  */
697  if (GistFollowRight(stack->page))
698  {
699  if (!xlocked)
700  {
701  LockBuffer(stack->buffer, GIST_UNLOCK);
703  xlocked = true;
704  /* someone might've completed the split when we unlocked */
705  if (!GistFollowRight(stack->page))
706  continue;
707  }
708  gistfixsplit(&state, giststate);
709 
710  UnlockReleaseBuffer(stack->buffer);
711  xlocked = false;
712  state.stack = stack = stack->parent;
713  continue;
714  }
715 
716  if ((stack->blkno != GIST_ROOT_BLKNO &&
717  stack->parent->lsn < GistPageGetNSN(stack->page)) ||
718  GistPageIsDeleted(stack->page))
719  {
720  /*
721  * Concurrent split or page deletion detected. There's no
722  * guarantee that the downlink for this page is consistent with
723  * the tuple we're inserting anymore, so go back to parent and
724  * rechoose the best child.
725  */
726  UnlockReleaseBuffer(stack->buffer);
727  xlocked = false;
728  state.stack = stack = stack->parent;
729  continue;
730  }
731 
732  if (!GistPageIsLeaf(stack->page))
733  {
734  /*
735  * This is an internal page so continue to walk down the tree.
736  * Find the child node that has the minimum insertion penalty.
737  */
738  BlockNumber childblkno;
739  IndexTuple newtup;
740  GISTInsertStack *item;
741  OffsetNumber downlinkoffnum;
742 
743  downlinkoffnum = gistchoose(state.r, stack->page, itup, giststate);
744  iid = PageGetItemId(stack->page, downlinkoffnum);
745  idxtuple = (IndexTuple) PageGetItem(stack->page, iid);
746  childblkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
747 
748  /*
749  * Check that it's not a leftover invalid tuple from pre-9.1
750  */
751  if (GistTupleIsInvalid(idxtuple))
752  ereport(ERROR,
753  (errmsg("index \"%s\" contains an inner tuple marked as invalid",
755  errdetail("This is caused by an incomplete page split at crash recovery before upgrading to PostgreSQL 9.1."),
756  errhint("Please REINDEX it.")));
757 
758  /*
759  * Check that the key representing the target child node is
760  * consistent with the key we're inserting. Update it if it's not.
761  */
762  newtup = gistgetadjusted(state.r, idxtuple, itup, giststate);
763  if (newtup)
764  {
765  /*
766  * Swap shared lock for an exclusive one. Beware, the page may
767  * change while we unlock/lock the page...
768  */
769  if (!xlocked)
770  {
771  LockBuffer(stack->buffer, GIST_UNLOCK);
773  xlocked = true;
774  stack->page = (Page) BufferGetPage(stack->buffer);
775 
776  if (PageGetLSN(stack->page) != stack->lsn)
777  {
778  /* the page was changed while we unlocked it, retry */
779  continue;
780  }
781  }
782 
783  /*
784  * Update the tuple.
785  *
786  * We still hold the lock after gistinserttuple(), but it
787  * might have to split the page to make the updated tuple fit.
788  * In that case the updated tuple might migrate to the other
789  * half of the split, so we have to go back to the parent and
790  * descend back to the half that's a better fit for the new
791  * tuple.
792  */
793  if (gistinserttuple(&state, stack, giststate, newtup,
794  downlinkoffnum))
795  {
796  /*
797  * If this was a root split, the root page continues to be
798  * the parent and the updated tuple went to one of the
799  * child pages, so we just need to retry from the root
800  * page.
801  */
802  if (stack->blkno != GIST_ROOT_BLKNO)
803  {
804  UnlockReleaseBuffer(stack->buffer);
805  xlocked = false;
806  state.stack = stack = stack->parent;
807  }
808  continue;
809  }
810  }
811  LockBuffer(stack->buffer, GIST_UNLOCK);
812  xlocked = false;
813 
814  /* descend to the chosen child */
815  item = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
816  item->blkno = childblkno;
817  item->parent = stack;
818  item->downlinkoffnum = downlinkoffnum;
819  state.stack = stack = item;
820  }
821  else
822  {
823  /*
824  * Leaf page. Insert the new key. We've already updated all the
825  * parents on the way down, but we might have to split the page if
826  * it doesn't fit. gistinserttuple() will take care of that.
827  */
828 
829  /*
830  * Swap shared lock for an exclusive one. Be careful, the page may
831  * change while we unlock/lock the page...
832  */
833  if (!xlocked)
834  {
835  LockBuffer(stack->buffer, GIST_UNLOCK);
837  xlocked = true;
838  stack->page = (Page) BufferGetPage(stack->buffer);
839  stack->lsn = PageGetLSN(stack->page);
840 
841  if (stack->blkno == GIST_ROOT_BLKNO)
842  {
843  /*
844  * the only page that can become inner instead of leaf is
845  * the root page, so for root we should recheck it
846  */
847  if (!GistPageIsLeaf(stack->page))
848  {
849  /*
850  * very rare situation: during unlock/lock index with
851  * number of pages = 1 was increased
852  */
853  LockBuffer(stack->buffer, GIST_UNLOCK);
854  xlocked = false;
855  continue;
856  }
857 
858  /*
859  * we don't need to check root split, because checking
860  * leaf/inner is enough to recognize split for root
861  */
862  }
863  else if ((GistFollowRight(stack->page) ||
864  stack->parent->lsn < GistPageGetNSN(stack->page)) &&
865  GistPageIsDeleted(stack->page))
866  {
867  /*
868  * The page was split or deleted while we momentarily
869  * unlocked the page. Go back to parent.
870  */
871  UnlockReleaseBuffer(stack->buffer);
872  xlocked = false;
873  state.stack = stack = stack->parent;
874  continue;
875  }
876  }
877 
878  /* now state.stack->(page, buffer and blkno) points to leaf page */
879 
880  gistinserttuple(&state, stack, giststate, itup,
882  LockBuffer(stack->buffer, GIST_UNLOCK);
883 
884  /* Release any pins we might still hold before exiting */
885  for (; stack; stack = stack->parent)
886  ReleaseBuffer(stack->buffer);
887  break;
888  }
889  }
890 }
891 
892 /*
893  * Traverse the tree to find path from root page to specified "child" block.
894  *
895  * returns a new insertion stack, starting from the parent of "child", up
896  * to the root. *downlinkoffnum is set to the offset of the downlink in the
897  * direct parent of child.
898  *
899  * To prevent deadlocks, this should lock only one page at a time.
900  */
901 static GISTInsertStack *
903 {
904  Page page;
905  Buffer buffer;
906  OffsetNumber i,
907  maxoff;
908  ItemId iid;
909  IndexTuple idxtuple;
910  List *fifo;
911  GISTInsertStack *top,
912  *ptr;
914 
915  top = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
916  top->blkno = GIST_ROOT_BLKNO;
918 
919  fifo = list_make1(top);
920  while (fifo != NIL)
921  {
922  /* Get next page to visit */
923  top = linitial(fifo);
924  fifo = list_delete_first(fifo);
925 
926  buffer = ReadBuffer(r, top->blkno);
927  LockBuffer(buffer, GIST_SHARE);
928  gistcheckpage(r, buffer);
929  page = (Page) BufferGetPage(buffer);
930 
931  if (GistPageIsLeaf(page))
932  {
933  /*
934  * Because we scan the index top-down, all the rest of the pages
935  * in the queue must be leaf pages as well.
936  */
937  UnlockReleaseBuffer(buffer);
938  break;
939  }
940 
941  /* currently, internal pages are never deleted */
942  Assert(!GistPageIsDeleted(page));
943 
944  top->lsn = BufferGetLSNAtomic(buffer);
945 
946  /*
947  * If F_FOLLOW_RIGHT is set, the page to the right doesn't have a
948  * downlink. This should not normally happen..
949  */
950  if (GistFollowRight(page))
951  elog(ERROR, "concurrent GiST page split was incomplete");
952 
953  if (top->parent && top->parent->lsn < GistPageGetNSN(page) &&
954  GistPageGetOpaque(page)->rightlink != InvalidBlockNumber /* sanity check */ )
955  {
956  /*
957  * Page was split while we looked elsewhere. We didn't see the
958  * downlink to the right page when we scanned the parent, so add
959  * it to the queue now.
960  *
961  * Put the right page ahead of the queue, so that we visit it
962  * next. That's important, because if this is the lowest internal
963  * level, just above leaves, we might already have queued up some
964  * leaf pages, and we assume that there can't be any non-leaf
965  * pages behind leaf pages.
966  */
967  ptr = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
968  ptr->blkno = GistPageGetOpaque(page)->rightlink;
970  ptr->parent = top->parent;
971 
972  fifo = lcons(ptr, fifo);
973  }
974 
975  maxoff = PageGetMaxOffsetNumber(page);
976 
977  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
978  {
979  iid = PageGetItemId(page, i);
980  idxtuple = (IndexTuple) PageGetItem(page, iid);
981  blkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
982  if (blkno == child)
983  {
984  /* Found it! */
985  UnlockReleaseBuffer(buffer);
986  *downlinkoffnum = i;
987  return top;
988  }
989  else
990  {
991  /* Append this child to the list of pages to visit later */
992  ptr = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
993  ptr->blkno = blkno;
994  ptr->downlinkoffnum = i;
995  ptr->parent = top;
996 
997  fifo = lappend(fifo, ptr);
998  }
999  }
1000 
1001  UnlockReleaseBuffer(buffer);
1002  }
1003 
1004  elog(ERROR, "failed to re-find parent of a page in index \"%s\", block %u",
1005  RelationGetRelationName(r), child);
1006  return NULL; /* keep compiler quiet */
1007 }
1008 
1009 /*
1010  * Updates the stack so that child->parent is the correct parent of the
1011  * child. child->parent must be exclusively locked on entry, and will
1012  * remain so at exit, but it might not be the same page anymore.
1013  */
1014 static void
1016 {
1017  GISTInsertStack *parent = child->parent;
1018 
1019  gistcheckpage(r, parent->buffer);
1020  parent->page = (Page) BufferGetPage(parent->buffer);
1021 
1022  /* here we don't need to distinguish between split and page update */
1023  if (child->downlinkoffnum == InvalidOffsetNumber ||
1024  parent->lsn != PageGetLSN(parent->page))
1025  {
1026  /* parent is changed, look child in right links until found */
1027  OffsetNumber i,
1028  maxoff;
1029  ItemId iid;
1030  IndexTuple idxtuple;
1031  GISTInsertStack *ptr;
1032 
1033  while (true)
1034  {
1035  maxoff = PageGetMaxOffsetNumber(parent->page);
1036  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1037  {
1038  iid = PageGetItemId(parent->page, i);
1039  idxtuple = (IndexTuple) PageGetItem(parent->page, iid);
1040  if (ItemPointerGetBlockNumber(&(idxtuple->t_tid)) == child->blkno)
1041  {
1042  /* yes!!, found */
1043  child->downlinkoffnum = i;
1044  return;
1045  }
1046  }
1047 
1048  parent->blkno = GistPageGetOpaque(parent->page)->rightlink;
1049  UnlockReleaseBuffer(parent->buffer);
1050  if (parent->blkno == InvalidBlockNumber)
1051  {
1052  /*
1053  * End of chain and still didn't find parent. It's a very-very
1054  * rare situation when root splitted.
1055  */
1056  break;
1057  }
1058  parent->buffer = ReadBuffer(r, parent->blkno);
1059  LockBuffer(parent->buffer, GIST_EXCLUSIVE);
1060  gistcheckpage(r, parent->buffer);
1061  parent->page = (Page) BufferGetPage(parent->buffer);
1062  }
1063 
1064  /*
1065  * awful!!, we need search tree to find parent ... , but before we
1066  * should release all old parent
1067  */
1068 
1069  ptr = child->parent->parent; /* child->parent already released
1070  * above */
1071  while (ptr)
1072  {
1073  ReleaseBuffer(ptr->buffer);
1074  ptr = ptr->parent;
1075  }
1076 
1077  /* ok, find new path */
1078  ptr = parent = gistFindPath(r, child->blkno, &child->downlinkoffnum);
1079 
1080  /* read all buffers as expected by caller */
1081  /* note we don't lock them or gistcheckpage them here! */
1082  while (ptr)
1083  {
1084  ptr->buffer = ReadBuffer(r, ptr->blkno);
1085  ptr->page = (Page) BufferGetPage(ptr->buffer);
1086  ptr = ptr->parent;
1087  }
1088 
1089  /* install new chain of parents to stack */
1090  child->parent = parent;
1091 
1092  /* make recursive call to normal processing */
1094  gistFindCorrectParent(r, child);
1095  }
1096 }
1097 
1098 /*
1099  * Form a downlink pointer for the page in 'buf'.
1100  */
1101 static IndexTuple
1103  GISTInsertStack *stack)
1104 {
1105  Page page = BufferGetPage(buf);
1106  OffsetNumber maxoff;
1107  OffsetNumber offset;
1108  IndexTuple downlink = NULL;
1109 
1110  maxoff = PageGetMaxOffsetNumber(page);
1111  for (offset = FirstOffsetNumber; offset <= maxoff; offset = OffsetNumberNext(offset))
1112  {
1113  IndexTuple ituple = (IndexTuple)
1114  PageGetItem(page, PageGetItemId(page, offset));
1115 
1116  if (downlink == NULL)
1117  downlink = CopyIndexTuple(ituple);
1118  else
1119  {
1120  IndexTuple newdownlink;
1121 
1122  newdownlink = gistgetadjusted(rel, downlink, ituple,
1123  giststate);
1124  if (newdownlink)
1125  downlink = newdownlink;
1126  }
1127  }
1128 
1129  /*
1130  * If the page is completely empty, we can't form a meaningful downlink
1131  * for it. But we have to insert a downlink for the page. Any key will do,
1132  * as long as its consistent with the downlink of parent page, so that we
1133  * can legally insert it to the parent. A minimal one that matches as few
1134  * scans as possible would be best, to keep scans from doing useless work,
1135  * but we don't know how to construct that. So we just use the downlink of
1136  * the original page that was split - that's as far from optimal as it can
1137  * get but will do..
1138  */
1139  if (!downlink)
1140  {
1141  ItemId iid;
1142 
1144  gistFindCorrectParent(rel, stack);
1145  iid = PageGetItemId(stack->parent->page, stack->downlinkoffnum);
1146  downlink = (IndexTuple) PageGetItem(stack->parent->page, iid);
1147  downlink = CopyIndexTuple(downlink);
1148  LockBuffer(stack->parent->buffer, GIST_UNLOCK);
1149  }
1150 
1152  GistTupleSetValid(downlink);
1153 
1154  return downlink;
1155 }
1156 
1157 
1158 /*
1159  * Complete the incomplete split of state->stack->page.
1160  */
1161 static void
1163 {
1164  GISTInsertStack *stack = state->stack;
1165  Buffer buf;
1166  Page page;
1167  List *splitinfo = NIL;
1168 
1169  elog(LOG, "fixing incomplete split in index \"%s\", block %u",
1170  RelationGetRelationName(state->r), stack->blkno);
1171 
1172  Assert(GistFollowRight(stack->page));
1174 
1175  buf = stack->buffer;
1176 
1177  /*
1178  * Read the chain of split pages, following the rightlinks. Construct a
1179  * downlink tuple for each page.
1180  */
1181  for (;;)
1182  {
1184  IndexTuple downlink;
1185 
1186  page = BufferGetPage(buf);
1187 
1188  /* Form the new downlink tuples to insert to parent */
1189  downlink = gistformdownlink(state->r, buf, giststate, stack);
1190 
1191  si->buf = buf;
1192  si->downlink = downlink;
1193 
1194  splitinfo = lappend(splitinfo, si);
1195 
1196  if (GistFollowRight(page))
1197  {
1198  /* lock next page */
1199  buf = ReadBuffer(state->r, GistPageGetOpaque(page)->rightlink);
1200  LockBuffer(buf, GIST_EXCLUSIVE);
1201  }
1202  else
1203  break;
1204  }
1205 
1206  /* Insert the downlinks */
1207  gistfinishsplit(state, stack, giststate, splitinfo, false);
1208 }
1209 
1210 /*
1211  * Insert or replace a tuple in stack->buffer. If 'oldoffnum' is valid, the
1212  * tuple at 'oldoffnum' is replaced, otherwise the tuple is inserted as new.
1213  * 'stack' represents the path from the root to the page being updated.
1214  *
1215  * The caller must hold an exclusive lock on stack->buffer. The lock is still
1216  * held on return, but the page might not contain the inserted tuple if the
1217  * page was split. The function returns true if the page was split, false
1218  * otherwise.
1219  */
1220 static bool
1222  GISTSTATE *giststate, IndexTuple tuple, OffsetNumber oldoffnum)
1223 {
1224  return gistinserttuples(state, stack, giststate, &tuple, 1, oldoffnum,
1225  InvalidBuffer, InvalidBuffer, false, false);
1226 }
1227 
1228 /* ----------------
1229  * An extended workhorse version of gistinserttuple(). This version allows
1230  * inserting multiple tuples, or replacing a single tuple with multiple tuples.
1231  * This is used to recursively update the downlinks in the parent when a page
1232  * is split.
1233  *
1234  * If leftchild and rightchild are valid, we're inserting/replacing the
1235  * downlink for rightchild, and leftchild is its left sibling. We clear the
1236  * F_FOLLOW_RIGHT flag and update NSN on leftchild, atomically with the
1237  * insertion of the downlink.
1238  *
1239  * To avoid holding locks for longer than necessary, when recursing up the
1240  * tree to update the parents, the locking is a bit peculiar here. On entry,
1241  * the caller must hold an exclusive lock on stack->buffer, as well as
1242  * leftchild and rightchild if given. On return:
1243  *
1244  * - Lock on stack->buffer is released, if 'unlockbuf' is true. The page is
1245  * always kept pinned, however.
1246  * - Lock on 'leftchild' is released, if 'unlockleftchild' is true. The page
1247  * is kept pinned.
1248  * - Lock and pin on 'rightchild' are always released.
1249  *
1250  * Returns 'true' if the page had to be split. Note that if the page was
1251  * split, the inserted/updated tuples might've been inserted to a right
1252  * sibling of stack->buffer instead of stack->buffer itself.
1253  */
1254 static bool
1256  GISTSTATE *giststate,
1257  IndexTuple *tuples, int ntup, OffsetNumber oldoffnum,
1259  bool unlockbuf, bool unlockleftchild)
1260 {
1261  List *splitinfo;
1262  bool is_split;
1263 
1264  /*
1265  * Check for any rw conflicts (in serializable isolation level) just
1266  * before we intend to modify the page
1267  */
1269 
1270  /* Insert the tuple(s) to the page, splitting the page if necessary */
1271  is_split = gistplacetopage(state->r, state->freespace, giststate,
1272  stack->buffer,
1273  tuples, ntup,
1274  oldoffnum, NULL,
1275  leftchild,
1276  &splitinfo,
1277  true,
1278  state->heapRel,
1279  state->is_build);
1280 
1281  /*
1282  * Before recursing up in case the page was split, release locks on the
1283  * child pages. We don't need to keep them locked when updating the
1284  * parent.
1285  */
1286  if (BufferIsValid(rightchild))
1287  UnlockReleaseBuffer(rightchild);
1288  if (BufferIsValid(leftchild) && unlockleftchild)
1289  LockBuffer(leftchild, GIST_UNLOCK);
1290 
1291  /*
1292  * If we had to split, insert/update the downlinks in the parent. If the
1293  * caller requested us to release the lock on stack->buffer, tell
1294  * gistfinishsplit() to do that as soon as it's safe to do so. If we
1295  * didn't have to split, release it ourselves.
1296  */
1297  if (splitinfo)
1298  gistfinishsplit(state, stack, giststate, splitinfo, unlockbuf);
1299  else if (unlockbuf)
1300  LockBuffer(stack->buffer, GIST_UNLOCK);
1301 
1302  return is_split;
1303 }
1304 
1305 /*
1306  * Finish an incomplete split by inserting/updating the downlinks in parent
1307  * page. 'splitinfo' contains all the child pages involved in the split,
1308  * from left-to-right.
1309  *
1310  * On entry, the caller must hold a lock on stack->buffer and all the child
1311  * pages in 'splitinfo'. If 'unlockbuf' is true, the lock on stack->buffer is
1312  * released on return. The child pages are always unlocked and unpinned.
1313  */
1314 static void
1316  GISTSTATE *giststate, List *splitinfo, bool unlockbuf)
1317 {
1318  GISTPageSplitInfo *right;
1319  GISTPageSplitInfo *left;
1320  IndexTuple tuples[2];
1321 
1322  /* A split always contains at least two halves */
1323  Assert(list_length(splitinfo) >= 2);
1324 
1325  /*
1326  * We need to insert downlinks for each new page, and update the downlink
1327  * for the original (leftmost) page in the split. Begin at the rightmost
1328  * page, inserting one downlink at a time until there's only two pages
1329  * left. Finally insert the downlink for the last new page and update the
1330  * downlink for the original page as one operation.
1331  */
1333 
1334  /*
1335  * Insert downlinks for the siblings from right to left, until there are
1336  * only two siblings left.
1337  */
1338  for (int pos = list_length(splitinfo) - 1; pos > 1; pos--)
1339  {
1340  right = (GISTPageSplitInfo *) list_nth(splitinfo, pos);
1341  left = (GISTPageSplitInfo *) list_nth(splitinfo, pos - 1);
1342 
1343  gistFindCorrectParent(state->r, stack);
1344  if (gistinserttuples(state, stack->parent, giststate,
1345  &right->downlink, 1,
1347  left->buf, right->buf, false, false))
1348  {
1349  /*
1350  * If the parent page was split, the existing downlink might have
1351  * moved.
1352  */
1354  }
1355  /* gistinserttuples() released the lock on right->buf. */
1356  }
1357 
1358  right = (GISTPageSplitInfo *) lsecond(splitinfo);
1359  left = (GISTPageSplitInfo *) linitial(splitinfo);
1360 
1361  /*
1362  * Finally insert downlink for the remaining right page and update the
1363  * downlink for the original page to not contain the tuples that were
1364  * moved to the new pages.
1365  */
1366  tuples[0] = left->downlink;
1367  tuples[1] = right->downlink;
1368  gistFindCorrectParent(state->r, stack);
1369  if (gistinserttuples(state, stack->parent, giststate,
1370  tuples, 2,
1371  stack->downlinkoffnum,
1372  left->buf, right->buf,
1373  true, /* Unlock parent */
1374  unlockbuf /* Unlock stack->buffer if caller wants
1375  * that */
1376  ))
1377  {
1378  /*
1379  * If the parent page was split, the downlink might have moved.
1380  */
1382  }
1383 
1384  Assert(left->buf == stack->buffer);
1385 
1386  /*
1387  * If we split the page because we had to adjust the downlink on an
1388  * internal page, while descending the tree for inserting a new tuple,
1389  * then this might no longer be the correct page for the new tuple. The
1390  * downlink to this page might not cover the new tuple anymore, it might
1391  * need to go to the newly-created right sibling instead. Tell the caller
1392  * to walk back up the stack, to re-check at the parent which page to
1393  * insert to.
1394  *
1395  * Normally, the LSN-NSN interlock during the tree descend would also
1396  * detect that a concurrent split happened (by ourselves), and cause us to
1397  * retry at the parent. But that mechanism doesn't work during index
1398  * build, because we don't do WAL-logging, and don't update LSNs, during
1399  * index build.
1400  */
1401  stack->retry_from_parent = true;
1402 }
1403 
1404 /*
1405  * gistSplit -- split a page in the tree and fill struct
1406  * used for XLOG and real writes buffers. Function is recursive, ie
1407  * it will split page until keys will fit in every page.
1408  */
1411  Page page,
1412  IndexTuple *itup, /* contains compressed entry */
1413  int len,
1414  GISTSTATE *giststate)
1415 {
1416  IndexTuple *lvectup,
1417  *rvectup;
1418  GistSplitVector v;
1419  int i;
1420  SplitedPageLayout *res = NULL;
1421 
1422  /* this should never recurse very deeply, but better safe than sorry */
1424 
1425  /* there's no point in splitting an empty page */
1426  Assert(len > 0);
1427 
1428  /*
1429  * If a single tuple doesn't fit on a page, no amount of splitting will
1430  * help.
1431  */
1432  if (len == 1)
1433  ereport(ERROR,
1434  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1435  errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
1436  IndexTupleSize(itup[0]), GiSTPageSize,
1438 
1439  memset(v.spl_lisnull, true,
1440  sizeof(bool) * giststate->nonLeafTupdesc->natts);
1441  memset(v.spl_risnull, true,
1442  sizeof(bool) * giststate->nonLeafTupdesc->natts);
1443  gistSplitByKey(r, page, itup, len, giststate, &v, 0);
1444 
1445  /* form left and right vector */
1446  lvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
1447  rvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
1448 
1449  for (i = 0; i < v.splitVector.spl_nleft; i++)
1450  lvectup[i] = itup[v.splitVector.spl_left[i] - 1];
1451 
1452  for (i = 0; i < v.splitVector.spl_nright; i++)
1453  rvectup[i] = itup[v.splitVector.spl_right[i] - 1];
1454 
1455  /* finalize splitting (may need another split) */
1456  if (!gistfitpage(rvectup, v.splitVector.spl_nright))
1457  {
1458  res = gistSplit(r, page, rvectup, v.splitVector.spl_nright, giststate);
1459  }
1460  else
1461  {
1462  ROTATEDIST(res);
1463  res->block.num = v.splitVector.spl_nright;
1464  res->list = gistfillitupvec(rvectup, v.splitVector.spl_nright, &(res->lenlist));
1465  res->itup = gistFormTuple(giststate, r, v.spl_rattr, v.spl_risnull, false);
1466  }
1467 
1468  if (!gistfitpage(lvectup, v.splitVector.spl_nleft))
1469  {
1470  SplitedPageLayout *resptr,
1471  *subres;
1472 
1473  resptr = subres = gistSplit(r, page, lvectup, v.splitVector.spl_nleft, giststate);
1474 
1475  /* install on list's tail */
1476  while (resptr->next)
1477  resptr = resptr->next;
1478 
1479  resptr->next = res;
1480  res = subres;
1481  }
1482  else
1483  {
1484  ROTATEDIST(res);
1485  res->block.num = v.splitVector.spl_nleft;
1486  res->list = gistfillitupvec(lvectup, v.splitVector.spl_nleft, &(res->lenlist));
1487  res->itup = gistFormTuple(giststate, r, v.spl_lattr, v.spl_lisnull, false);
1488  }
1489 
1490  return res;
1491 }
1492 
1493 /*
1494  * Create a GISTSTATE and fill it with information about the index
1495  */
1496 GISTSTATE *
1498 {
1499  GISTSTATE *giststate;
1500  MemoryContext scanCxt;
1501  MemoryContext oldCxt;
1502  int i;
1503 
1504  /* safety check to protect fixed-size arrays in GISTSTATE */
1505  if (index->rd_att->natts > INDEX_MAX_KEYS)
1506  elog(ERROR, "numberOfAttributes %d > %d",
1507  index->rd_att->natts, INDEX_MAX_KEYS);
1508 
1509  /* Create the memory context that will hold the GISTSTATE */
1511  "GiST scan context",
1513  oldCxt = MemoryContextSwitchTo(scanCxt);
1514 
1515  /* Create and fill in the GISTSTATE */
1516  giststate = (GISTSTATE *) palloc(sizeof(GISTSTATE));
1517 
1518  giststate->scanCxt = scanCxt;
1519  giststate->tempCxt = scanCxt; /* caller must change this if needed */
1520  giststate->leafTupdesc = index->rd_att;
1521 
1522  /*
1523  * The truncated tupdesc for non-leaf index tuples, which doesn't contain
1524  * the INCLUDE attributes.
1525  *
1526  * It is used to form tuples during tuple adjustment and page split.
1527  * B-tree creates shortened tuple descriptor for every truncated tuple,
1528  * because it is doing this less often: it does not have to form truncated
1529  * tuples during page split. Also, B-tree is not adjusting tuples on
1530  * internal pages the way GiST does.
1531  */
1532  giststate->nonLeafTupdesc = CreateTupleDescCopyConstr(index->rd_att);
1533  giststate->nonLeafTupdesc->natts =
1535 
1536  for (i = 0; i < IndexRelationGetNumberOfKeyAttributes(index); i++)
1537  {
1538  fmgr_info_copy(&(giststate->consistentFn[i]),
1539  index_getprocinfo(index, i + 1, GIST_CONSISTENT_PROC),
1540  scanCxt);
1541  fmgr_info_copy(&(giststate->unionFn[i]),
1542  index_getprocinfo(index, i + 1, GIST_UNION_PROC),
1543  scanCxt);
1544 
1545  /* opclasses are not required to provide a Compress method */
1546  if (OidIsValid(index_getprocid(index, i + 1, GIST_COMPRESS_PROC)))
1547  fmgr_info_copy(&(giststate->compressFn[i]),
1548  index_getprocinfo(index, i + 1, GIST_COMPRESS_PROC),
1549  scanCxt);
1550  else
1551  giststate->compressFn[i].fn_oid = InvalidOid;
1552 
1553  /* opclasses are not required to provide a Decompress method */
1554  if (OidIsValid(index_getprocid(index, i + 1, GIST_DECOMPRESS_PROC)))
1555  fmgr_info_copy(&(giststate->decompressFn[i]),
1556  index_getprocinfo(index, i + 1, GIST_DECOMPRESS_PROC),
1557  scanCxt);
1558  else
1559  giststate->decompressFn[i].fn_oid = InvalidOid;
1560 
1561  fmgr_info_copy(&(giststate->penaltyFn[i]),
1562  index_getprocinfo(index, i + 1, GIST_PENALTY_PROC),
1563  scanCxt);
1564  fmgr_info_copy(&(giststate->picksplitFn[i]),
1565  index_getprocinfo(index, i + 1, GIST_PICKSPLIT_PROC),
1566  scanCxt);
1567  fmgr_info_copy(&(giststate->equalFn[i]),
1568  index_getprocinfo(index, i + 1, GIST_EQUAL_PROC),
1569  scanCxt);
1570 
1571  /* opclasses are not required to provide a Distance method */
1572  if (OidIsValid(index_getprocid(index, i + 1, GIST_DISTANCE_PROC)))
1573  fmgr_info_copy(&(giststate->distanceFn[i]),
1574  index_getprocinfo(index, i + 1, GIST_DISTANCE_PROC),
1575  scanCxt);
1576  else
1577  giststate->distanceFn[i].fn_oid = InvalidOid;
1578 
1579  /* opclasses are not required to provide a Fetch method */
1580  if (OidIsValid(index_getprocid(index, i + 1, GIST_FETCH_PROC)))
1581  fmgr_info_copy(&(giststate->fetchFn[i]),
1582  index_getprocinfo(index, i + 1, GIST_FETCH_PROC),
1583  scanCxt);
1584  else
1585  giststate->fetchFn[i].fn_oid = InvalidOid;
1586 
1587  /*
1588  * If the index column has a specified collation, we should honor that
1589  * while doing comparisons. However, we may have a collatable storage
1590  * type for a noncollatable indexed data type. If there's no index
1591  * collation then specify default collation in case the support
1592  * functions need collation. This is harmless if the support
1593  * functions don't care about collation, so we just do it
1594  * unconditionally. (We could alternatively call get_typcollation,
1595  * but that seems like expensive overkill --- there aren't going to be
1596  * any cases where a GiST storage type has a nondefault collation.)
1597  */
1598  if (OidIsValid(index->rd_indcollation[i]))
1599  giststate->supportCollation[i] = index->rd_indcollation[i];
1600  else
1601  giststate->supportCollation[i] = DEFAULT_COLLATION_OID;
1602  }
1603 
1604  /* No opclass information for INCLUDE attributes */
1605  for (; i < index->rd_att->natts; i++)
1606  {
1607  giststate->consistentFn[i].fn_oid = InvalidOid;
1608  giststate->unionFn[i].fn_oid = InvalidOid;
1609  giststate->compressFn[i].fn_oid = InvalidOid;
1610  giststate->decompressFn[i].fn_oid = InvalidOid;
1611  giststate->penaltyFn[i].fn_oid = InvalidOid;
1612  giststate->picksplitFn[i].fn_oid = InvalidOid;
1613  giststate->equalFn[i].fn_oid = InvalidOid;
1614  giststate->distanceFn[i].fn_oid = InvalidOid;
1615  giststate->fetchFn[i].fn_oid = InvalidOid;
1616  giststate->supportCollation[i] = InvalidOid;
1617  }
1618 
1619  MemoryContextSwitchTo(oldCxt);
1620 
1621  return giststate;
1622 }
1623 
1624 void
1626 {
1627  /* It's sufficient to delete the scanCxt */
1628  MemoryContextDelete(giststate->scanCxt);
1629 }
1630 
1631 /*
1632  * gistprunepage() -- try to remove LP_DEAD items from the given page.
1633  * Function assumes that buffer is exclusively locked.
1634  */
1635 static void
1636 gistprunepage(Relation rel, Page page, Buffer buffer, Relation heapRel)
1637 {
1639  int ndeletable = 0;
1640  OffsetNumber offnum,
1641  maxoff;
1642  TransactionId latestRemovedXid = InvalidTransactionId;
1643 
1644  Assert(GistPageIsLeaf(page));
1645 
1646  /*
1647  * Scan over all items to see which ones need to be deleted according to
1648  * LP_DEAD flags.
1649  */
1650  maxoff = PageGetMaxOffsetNumber(page);
1651  for (offnum = FirstOffsetNumber;
1652  offnum <= maxoff;
1653  offnum = OffsetNumberNext(offnum))
1654  {
1655  ItemId itemId = PageGetItemId(page, offnum);
1656 
1657  if (ItemIdIsDead(itemId))
1658  deletable[ndeletable++] = offnum;
1659  }
1660 
1662  latestRemovedXid =
1663  index_compute_xid_horizon_for_tuples(rel, heapRel, buffer,
1664  deletable, ndeletable);
1665 
1666  if (ndeletable > 0)
1667  {
1669 
1670  PageIndexMultiDelete(page, deletable, ndeletable);
1671 
1672  /*
1673  * Mark the page as not containing any LP_DEAD items. This is not
1674  * certainly true (there might be some that have recently been marked,
1675  * but weren't included in our target-item list), but it will almost
1676  * always be true and it doesn't seem worth an additional page scan to
1677  * check it. Remember that F_HAS_GARBAGE is only a hint anyway.
1678  */
1680 
1681  MarkBufferDirty(buffer);
1682 
1683  /* XLOG stuff */
1684  if (RelationNeedsWAL(rel))
1685  {
1686  XLogRecPtr recptr;
1687 
1688  recptr = gistXLogDelete(buffer,
1689  deletable, ndeletable,
1690  latestRemovedXid);
1691 
1692  PageSetLSN(page, recptr);
1693  }
1694  else
1695  PageSetLSN(page, gistGetFakeLSN(rel));
1696 
1697  END_CRIT_SECTION();
1698  }
1699 
1700  /*
1701  * Note: if we didn't find any LP_DEAD items, then the page's
1702  * F_HAS_GARBAGE hint bit is falsely set. We do not bother expending a
1703  * separate write to clear it, however. We will clear it when we split
1704  * the page.
1705  */
1706 }
#define GIST_OPTIONS_PROC
Definition: gist.h:39
#define GistFollowRight(page)
Definition: gist.h:173
ambeginscan_function ambeginscan
Definition: amapi.h:227
uint8 amparallelvacuumoptions
Definition: amapi.h:205
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define NIL
Definition: pg_list.h:65
BlockNumber blkno
Definition: gist_private.h:210
#define GistPageGetNSN(page)
Definition: gist.h:177
MemoryContext ii_Context
Definition: execnodes.h:179
ambulkdelete_function ambulkdelete
Definition: amapi.h:219
bool amcanmulticol
Definition: amapi.h:185
static bool gistinserttuple(GISTInsertState *state, GISTInsertStack *stack, GISTSTATE *giststate, IndexTuple tuple, OffsetNumber oldoffnum)
Definition: gist.c:1221
uint16 amsupport
Definition: amapi.h:173
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
#define GistBuildLSN
Definition: gist.h:60
#define GistPageIsDeleted(page)
Definition: gist.h:163
int errhint(const char *fmt,...)
Definition: elog.c:1071
#define GIST_FETCH_PROC
Definition: gist.h:38
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1014
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:800
Datum spl_lattr[INDEX_MAX_KEYS]
Definition: gist_private.h:239
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:403
static void gistfixsplit(GISTInsertState *state, GISTSTATE *giststate)
Definition: gist.c:1162
uint32 TransactionId
Definition: c.h:513
amgettuple_function amgettuple
Definition: amapi.h:229
BlockNumber blkno
Definition: gist_private.h:186
FmgrInfo fetchFn[INDEX_MAX_KEYS]
Definition: gist_private.h:94
IndexBulkDeleteResult * gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: gistvacuum.c:75
bool amcanorderbyop
Definition: amapi.h:179
#define GistClearPageHasGarbage(page)
Definition: gist.h:171
amproperty_function amproperty
Definition: amapi.h:224
#define GIST_EQUAL_PROC
Definition: gist.h:36
void PageIndexTupleDelete(Page page, OffsetNumber offnum)
Definition: bufpage.c:719
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
MemoryContext createTempGistContext(void)
Definition: gist.c:118
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:59
Datum gisthandler(PG_FUNCTION_ARGS)
Definition: gist.c:59
Relation heapRel
Definition: gist_private.h:254
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:652
ItemPointerData t_tid
Definition: itup.h:37
void gistfillbuffer(Page page, IndexTuple *itup, int len, OffsetNumber off)
Definition: gistutil.c:33
amparallelrescan_function amparallelrescan
Definition: amapi.h:238
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
bool amstorage
Definition: amapi.h:193
bool gistgettuple(IndexScanDesc scan, ScanDirection dir)
Definition: gistget.c:614
OffsetNumber * spl_left
Definition: gist.h:133
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Pointer Item
Definition: item.h:17
#define GistPageSetNSN(page, val)
Definition: gist.h:178
#define InvalidBuffer
Definition: buf.h:25
GIST_SPLITVEC splitVector
Definition: gist_private.h:237
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:220
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
bool ampredlocks
Definition: amapi.h:197
#define GistTupleIsInvalid(itup)
Definition: gist_private.h:288
#define GIST_UNLOCK
Definition: gist_private.h:44
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
#define GistPageHasGarbage(page)
Definition: gist.h:169
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3483
#define P_NEW
Definition: bufmgr.h:91
aminsert_function aminsert
Definition: amapi.h:218
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
int spl_nleft
Definition: gist.h:134
#define LOG
Definition: elog.h:26
bytea * gistoptions(Datum reloptions, bool validate)
Definition: gistutil.c:908
IndexTupleData * list
Definition: gist_private.h:194
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
Oid amkeytype
Definition: amapi.h:207
#define OidIsValid(objectId)
Definition: c.h:644
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
IndexTuple gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
Definition: gistutil.c:315
bool amoptionalkey
Definition: amapi.h:187
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:226
Datum spl_rattr[INDEX_MAX_KEYS]
Definition: gist_private.h:243
XLogRecPtr gistGetFakeLSN(Relation rel)
Definition: gistutil.c:1012
#define lsecond(l)
Definition: pg_list.h:200
XLogRecPtr gistXLogDelete(Buffer buffer, OffsetNumber *todelete, int ntodelete, TransactionId latestRemovedXid)
Definition: gistxlog.c:694
bool gistinsert(Relation r, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: gist.c:155
uint16 OffsetNumber
Definition: off.h:24
Definition: type.h:89
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:381
gistxlogPage block
Definition: gist_private.h:193
#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:521
IndexTuple * gistextractpage(Page page, int *len)
Definition: gistutil.c:94
amgetbitmap_function amgetbitmap
Definition: amapi.h:230
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:35
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
Oid * rd_indcollation
Definition: rel.h:199
#define ERROR
Definition: elog.h:43
static IndexTuple gistformdownlink(Relation rel, Buffer buf, GISTSTATE *giststate, GISTInsertStack *stack)
Definition: gist.c:1102
ambuild_function ambuild
Definition: amapi.h:216
#define GIST_COMPRESS_PROC
Definition: gist.h:32
#define GIST_MAX_SPLIT_PAGES
Definition: gist_private.h:39
amoptions_function amoptions
Definition: amapi.h:223
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:627
int spl_nright
Definition: gist.h:139
bool amcaninclude
Definition: amapi.h:201
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:612
MemoryContext tempCxt
Definition: gist_private.h:78
BlockNumber blkno
Definition: ginvacuum.c:119
amcostestimate_function amcostestimate
Definition: amapi.h:222
IndexTuple downlink
Definition: gist_private.h:421
bool amcanunique
Definition: amapi.h:183
void gistrescan(IndexScanDesc scan, ScanKey key, int nkeys, ScanKey orderbys, int norderbys)
Definition: gistscan.c:127
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
Definition: bufpage.c:1060
void gistcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:6482
GISTInsertStack * stack
Definition: gist_private.h:258
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:510
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2896
static char * buf
Definition: pg_test_fsync.c:67
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:220
amendscan_function amendscan
Definition: amapi.h:231
bool amcanbackward
Definition: amapi.h:181
void check_stack_depth(void)
Definition: postgres.c:3312
static void gistprunepage(Relation rel, Page page, Buffer buffer, Relation heapRel)
Definition: gist.c:1636
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
int errdetail(const char *fmt,...)
Definition: elog.c:957
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:58
#define RelationGetRelationName(relation)
Definition: rel.h:490
FmgrInfo penaltyFn[INDEX_MAX_KEYS]
Definition: gist_private.h:90
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:475
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:150
struct SplitedPageLayout * next
Definition: gist_private.h:200
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void freeGISTstate(GISTSTATE *giststate)
Definition: gist.c:1625
amrescan_function amrescan
Definition: amapi.h:228
bool amcanparallel
Definition: amapi.h:199
FmgrInfo decompressFn[INDEX_MAX_KEYS]
Definition: gist_private.h:89
int64 gistgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: gistget.c:745
List * lappend(List *list, void *datum)
Definition: list.c:321
void * ii_AmCache
Definition: execnodes.h:178
TupleDesc leafTupdesc
Definition: gist_private.h:80
OffsetNumber gistchoose(Relation r, Page p, IndexTuple it, GISTSTATE *giststate)
Definition: gistutil.c:373
void gistbuildempty(Relation index)
Definition: gist.c:129
#define GistPageIsLeaf(page)
Definition: gist.h:160
#define GistTupleSetValid(itup)
Definition: gist_private.h:289
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
OffsetNumber downlinkoffnum
Definition: gist_private.h:228
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
static void gistFindCorrectParent(Relation r, GISTInsertStack *child)
Definition: gist.c:1015
bool amsearchnulls
Definition: amapi.h:191
#define GiSTPageSize
Definition: gist_private.h:470
#define GistClearFollowRight(page)
Definition: gist.h:175
IndexTuple * gistjoinvector(IndexTuple *itvec, int *len, IndexTuple *additvec, int addlen)
Definition: gistutil.c:113
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:1255
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
#define ROTATEDIST(d)
Definition: gist.c:45
bool amclusterable
Definition: amapi.h:195
#define XLogStandbyInfoActive()
Definition: xlog.h:205
TupleDesc rd_att
Definition: rel.h:110
void CheckForSerializableConflictIn(Relation relation, ItemPointer tid, BlockNumber blkno)
Definition: predicate.c:4374
GISTSTATE * initGISTstate(Relation index)
Definition: gist.c:1497
struct DataPageDeleteStack * child
Definition: ginvacuum.c:116
bool amsearcharray
Definition: amapi.h:189
SplitedPageLayout * gistSplit(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate)
Definition: gist.c:1410
#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:902
#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:653
Oid fn_oid
Definition: fmgr.h:59
#define ereport(elevel,...)
Definition: elog.h:144
#define GIST_CONSISTENT_PROC
Definition: gist.h:30
bool amusemaintenanceworkmem
Definition: amapi.h:203
bool gistfitpage(IndexTuple *itvec, int len)
Definition: gistutil.c:78
#define GIST_UNION_PROC
Definition: gist.h:31
bool gistcanreturn(Relation index, int attno)
Definition: gistget.c:795
#define GistPageGetOpaque(page)
Definition: gist.h:158
List * lcons(void *datum, List *list)
Definition: list.c:453
#define makeNode(_type_)
Definition: nodes.h:577
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
#define VACUUM_OPTION_PARALLEL_COND_CLEANUP
Definition: vacuum.h:52
Definition: regguts.h:298
struct DataPageDeleteStack * parent
Definition: ginvacuum.c:117
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:828
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:770
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:606
#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:126
#define GIST_PENALTY_PROC
Definition: gist.h:34
#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:147
#define leftchild(x)
Definition: fsmpage.c:29
#define GIST_DISTANCE_PROC
Definition: gist.h:37
OffsetNumber * spl_right
Definition: gist.h:138
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define GIST_SHARE
Definition: gist_private.h:42
ammarkpos_function ammarkpos
Definition: amapi.h:232
bool amcanorder
Definition: amapi.h:177
ambuildphasename_function ambuildphasename
Definition: amapi.h:225
FmgrInfo distanceFn[INDEX_MAX_KEYS]
Definition: gist_private.h:93
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition: vacuum.h:45
#define ItemPointerSetBlockNumber(pointer, blockNumber)
Definition: itemptr.h:138
#define GistMarkFollowRight(page)
Definition: gist.h:174
#define RelationNeedsWAL(relation)
Definition: rel.h:562
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:236
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
XLogRecPtr GistNSN
Definition: gist.h:53
#define PageGetLSN(page)
Definition: bufpage.h:366
#define GISTNProcs
Definition: gist.h:40
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:1315
bool spl_risnull[INDEX_MAX_KEYS]
Definition: gist_private.h:245
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2633
bool gistvalidate(Oid opclassoid)
Definition: gistvalidate.c:34
#define MaxIndexTuplesPerPage
Definition: itup.h:145
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
uint16 amoptsprocnum
Definition: amapi.h:175
#define F_LEAF
Definition: gist.h:45
#define elog(elevel,...)
Definition: elog.h:214
ambuildempty_function ambuildempty
Definition: amapi.h:217
int i
#define GIST_ROOT_BLKNO
Definition: gist_private.h:262
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
MemoryContext scanCxt
Definition: gist_private.h:77
void GISTInitBuffer(Buffer b, uint32 f)
Definition: gistutil.c:748
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:574
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define GIST_DECOMPRESS_PROC
Definition: gist.h:33
IndexScanDesc gistbeginscan(Relation r, int nkeys, int norderbys)
Definition: gistscan.c:74
void gistendscan(IndexScanDesc scan)
Definition: gistscan.c:349
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
bool spl_lisnull[INDEX_MAX_KEYS]
Definition: gist_private.h:241
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:929
int Buffer
Definition: buf.h:23
struct GISTInsertStack * parent
Definition: gist_private.h:231
amcanreturn_function amcanreturn
Definition: amapi.h:221
Buffer gistNewBuffer(Relation r)
Definition: gistutil.c:809
void PredicateLockPageSplit(Relation relation, BlockNumber oldblkno, BlockNumber newblkno)
Definition: predicate.c:3098
#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:237
List * list_delete_first(List *list)
Definition: list.c:860
TransactionId index_compute_xid_horizon_for_tuples(Relation irel, Relation hrel, Buffer ibuf, OffsetNumber *itemnos, int nitems)
Definition: genam.c:285
amrestrpos_function amrestrpos
Definition: amapi.h:233
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:766