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