PostgreSQL Source Code  git master
spgutils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * spgutils.c
4  * various support functions for SP-GiST
5  *
6  *
7  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/access/spgist/spgutils.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "access/amvalidate.h"
19 #include "access/htup_details.h"
20 #include "access/reloptions.h"
21 #include "access/spgist_private.h"
23 #include "access/transam.h"
24 #include "access/xact.h"
25 #include "catalog/pg_amop.h"
26 #include "commands/vacuum.h"
27 #include "nodes/nodeFuncs.h"
28 #include "parser/parse_coerce.h"
29 #include "storage/bufmgr.h"
30 #include "storage/indexfsm.h"
31 #include "storage/lmgr.h"
32 #include "utils/builtins.h"
33 #include "utils/catcache.h"
34 #include "utils/index_selfuncs.h"
35 #include "utils/lsyscache.h"
36 #include "utils/syscache.h"
37 
38 
39 /*
40  * SP-GiST handler function: return IndexAmRoutine with access method parameters
41  * and callbacks.
42  */
43 Datum
45 {
47 
48  amroutine->amstrategies = 0;
49  amroutine->amsupport = SPGISTNProc;
51  amroutine->amcanorder = false;
52  amroutine->amcanorderbyop = true;
53  amroutine->amcanbackward = false;
54  amroutine->amcanunique = false;
55  amroutine->amcanmulticol = false;
56  amroutine->amoptionalkey = true;
57  amroutine->amsearcharray = false;
58  amroutine->amsearchnulls = true;
59  amroutine->amstorage = true;
60  amroutine->amclusterable = false;
61  amroutine->ampredlocks = false;
62  amroutine->amcanparallel = false;
63  amroutine->amcaninclude = true;
64  amroutine->amusemaintenanceworkmem = false;
65  amroutine->amsummarizing = false;
66  amroutine->amparallelvacuumoptions =
68  amroutine->amkeytype = InvalidOid;
69 
70  amroutine->ambuild = spgbuild;
71  amroutine->ambuildempty = spgbuildempty;
72  amroutine->aminsert = spginsert;
73  amroutine->ambulkdelete = spgbulkdelete;
74  amroutine->amvacuumcleanup = spgvacuumcleanup;
75  amroutine->amcanreturn = spgcanreturn;
76  amroutine->amcostestimate = spgcostestimate;
77  amroutine->amoptions = spgoptions;
78  amroutine->amproperty = spgproperty;
79  amroutine->ambuildphasename = NULL;
80  amroutine->amvalidate = spgvalidate;
81  amroutine->amadjustmembers = spgadjustmembers;
82  amroutine->ambeginscan = spgbeginscan;
83  amroutine->amrescan = spgrescan;
84  amroutine->amgettuple = spggettuple;
85  amroutine->amgetbitmap = spggetbitmap;
86  amroutine->amendscan = spgendscan;
87  amroutine->ammarkpos = NULL;
88  amroutine->amrestrpos = NULL;
89  amroutine->amestimateparallelscan = NULL;
90  amroutine->aminitparallelscan = NULL;
91  amroutine->amparallelrescan = NULL;
92 
93  PG_RETURN_POINTER(amroutine);
94 }
95 
96 /*
97  * GetIndexInputType
98  * Determine the nominal input data type for an index column
99  *
100  * We define the "nominal" input type as the associated opclass's opcintype,
101  * or if that is a polymorphic type, the base type of the heap column or
102  * expression that is the index's input. The reason for preferring the
103  * opcintype is that non-polymorphic opclasses probably don't want to hear
104  * about binary-compatible input types. For instance, if a text opclass
105  * is being used with a varchar heap column, we want to report "text" not
106  * "varchar". Likewise, opclasses don't want to hear about domain types,
107  * so if we do consult the actual input type, we make sure to flatten domains.
108  *
109  * At some point maybe this should go somewhere else, but it's not clear
110  * if any other index AMs have a use for it.
111  */
112 static Oid
114 {
115  Oid opcintype;
116  AttrNumber heapcol;
117  List *indexprs;
118  ListCell *indexpr_item;
119 
120  Assert(index->rd_index != NULL);
121  Assert(indexcol > 0 && indexcol <= index->rd_index->indnkeyatts);
122  opcintype = index->rd_opcintype[indexcol - 1];
123  if (!IsPolymorphicType(opcintype))
124  return opcintype;
125  heapcol = index->rd_index->indkey.values[indexcol - 1];
126  if (heapcol != 0) /* Simple index column? */
127  return getBaseType(get_atttype(index->rd_index->indrelid, heapcol));
128 
129  /*
130  * If the index expressions are already cached, skip calling
131  * RelationGetIndexExpressions, as it will make a copy which is overkill.
132  * We're not going to modify the trees, and we're not going to do anything
133  * that would invalidate the relcache entry before we're done.
134  */
135  if (index->rd_indexprs)
136  indexprs = index->rd_indexprs;
137  else
139  indexpr_item = list_head(indexprs);
140  for (int i = 1; i <= index->rd_index->indnkeyatts; i++)
141  {
142  if (index->rd_index->indkey.values[i - 1] == 0)
143  {
144  /* expression column */
145  if (indexpr_item == NULL)
146  elog(ERROR, "wrong number of index expressions");
147  if (i == indexcol)
148  return getBaseType(exprType((Node *) lfirst(indexpr_item)));
149  indexpr_item = lnext(indexprs, indexpr_item);
150  }
151  }
152  elog(ERROR, "wrong number of index expressions");
153  return InvalidOid; /* keep compiler quiet */
154 }
155 
156 /* Fill in a SpGistTypeDesc struct with info about the specified data type */
157 static void
159 {
160  HeapTuple tp;
161  Form_pg_type typtup;
162 
163  desc->type = type;
165  if (!HeapTupleIsValid(tp))
166  elog(ERROR, "cache lookup failed for type %u", type);
167  typtup = (Form_pg_type) GETSTRUCT(tp);
168  desc->attlen = typtup->typlen;
169  desc->attbyval = typtup->typbyval;
170  desc->attalign = typtup->typalign;
171  desc->attstorage = typtup->typstorage;
172  ReleaseSysCache(tp);
173 }
174 
175 /*
176  * Fetch local cache of AM-specific info about the index, initializing it
177  * if necessary
178  */
179 SpGistCache *
181 {
182  SpGistCache *cache;
183 
184  if (index->rd_amcache == NULL)
185  {
186  Oid atttype;
187  spgConfigIn in;
188  FmgrInfo *procinfo;
189  Buffer metabuffer;
190  SpGistMetaPageData *metadata;
191 
192  cache = MemoryContextAllocZero(index->rd_indexcxt,
193  sizeof(SpGistCache));
194 
195  /* SPGiST must have one key column and can also have INCLUDE columns */
198 
199  /*
200  * Get the actual (well, nominal) data type of the key column. We
201  * pass this to the opclass config function so that polymorphic
202  * opclasses are possible.
203  */
204  atttype = GetIndexInputType(index, spgKeyColumn + 1);
205 
206  /* Call the config function to get config info for the opclass */
207  in.attType = atttype;
208 
210  FunctionCall2Coll(procinfo,
211  index->rd_indcollation[spgKeyColumn],
212  PointerGetDatum(&in),
213  PointerGetDatum(&cache->config));
214 
215  /*
216  * If leafType isn't specified, use the declared index column type,
217  * which index.c will have derived from the opclass's opcintype.
218  * (Although we now make spgvalidate.c warn if these aren't the same,
219  * old user-defined opclasses may not set the STORAGE parameter
220  * correctly, so believe leafType if it's given.)
221  */
222  if (!OidIsValid(cache->config.leafType))
223  {
224  cache->config.leafType =
226 
227  /*
228  * If index column type is binary-coercible to atttype (for
229  * example, it's a domain over atttype), treat it as plain atttype
230  * to avoid thinking we need to compress.
231  */
232  if (cache->config.leafType != atttype &&
233  IsBinaryCoercible(cache->config.leafType, atttype))
234  cache->config.leafType = atttype;
235  }
236 
237  /* Get the information we need about each relevant datatype */
238  fillTypeDesc(&cache->attType, atttype);
239 
240  if (cache->config.leafType != atttype)
241  {
243  ereport(ERROR,
244  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
245  errmsg("compress method must be defined when leaf type is different from input type")));
246 
247  fillTypeDesc(&cache->attLeafType, cache->config.leafType);
248  }
249  else
250  {
251  /* Save lookups in this common case */
252  cache->attLeafType = cache->attType;
253  }
254 
255  fillTypeDesc(&cache->attPrefixType, cache->config.prefixType);
256  fillTypeDesc(&cache->attLabelType, cache->config.labelType);
257 
258  /* Last, get the lastUsedPages data from the metapage */
259  metabuffer = ReadBuffer(index, SPGIST_METAPAGE_BLKNO);
260  LockBuffer(metabuffer, BUFFER_LOCK_SHARE);
261 
262  metadata = SpGistPageGetMeta(BufferGetPage(metabuffer));
263 
264  if (metadata->magicNumber != SPGIST_MAGIC_NUMBER)
265  elog(ERROR, "index \"%s\" is not an SP-GiST index",
267 
268  cache->lastUsedPages = metadata->lastUsedPages;
269 
270  UnlockReleaseBuffer(metabuffer);
271 
272  index->rd_amcache = (void *) cache;
273  }
274  else
275  {
276  /* assume it's up to date */
277  cache = (SpGistCache *) index->rd_amcache;
278  }
279 
280  return cache;
281 }
282 
283 /*
284  * Compute a tuple descriptor for leaf tuples or index-only-scan result tuples.
285  *
286  * We can use the relcache's tupdesc as-is in many cases, and it's always
287  * OK so far as any INCLUDE columns are concerned. However, the entry for
288  * the key column has to match leafType in the first case or attType in the
289  * second case. While the relcache's tupdesc *should* show leafType, this
290  * might not hold for legacy user-defined opclasses, since before v14 they
291  * were not allowed to declare their true storage type in CREATE OPCLASS.
292  * Also, attType can be different from what is in the relcache.
293  *
294  * This function gives back either a pointer to the relcache's tupdesc
295  * if that is suitable, or a palloc'd copy that's been adjusted to match
296  * the specified key column type. We can avoid doing any catalog lookups
297  * here by insisting that the caller pass an SpGistTypeDesc not just an OID.
298  */
299 TupleDesc
301 {
302  TupleDesc outTupDesc;
303  Form_pg_attribute att;
304 
305  if (keyType->type ==
307  outTupDesc = RelationGetDescr(index);
308  else
309  {
311  att = TupleDescAttr(outTupDesc, spgKeyColumn);
312  /* It's sufficient to update the type-dependent fields of the column */
313  att->atttypid = keyType->type;
314  att->atttypmod = -1;
315  att->attlen = keyType->attlen;
316  att->attbyval = keyType->attbyval;
317  att->attalign = keyType->attalign;
318  att->attstorage = keyType->attstorage;
319  /* We shouldn't need to bother with making these valid: */
320  att->attcompression = InvalidCompressionMethod;
321  att->attcollation = InvalidOid;
322  /* In case we changed typlen, we'd better reset following offsets */
323  for (int i = spgFirstIncludeColumn; i < outTupDesc->natts; i++)
324  TupleDescAttr(outTupDesc, i)->attcacheoff = -1;
325  }
326  return outTupDesc;
327 }
328 
329 /* Initialize SpGistState for working with the given index */
330 void
332 {
333  SpGistCache *cache;
334 
335  state->index = index;
336 
337  /* Get cached static information about index */
338  cache = spgGetCache(index);
339 
340  state->config = cache->config;
341  state->attType = cache->attType;
342  state->attLeafType = cache->attLeafType;
343  state->attPrefixType = cache->attPrefixType;
344  state->attLabelType = cache->attLabelType;
345 
346  /* Ensure we have a valid descriptor for leaf tuples */
347  state->leafTupDesc = getSpGistTupleDesc(state->index, &state->attLeafType);
348 
349  /* Make workspace for constructing dead tuples */
350  state->deadTupleStorage = palloc0(SGDTSIZE);
351 
352  /* Set XID to use in redirection tuples */
353  state->myXid = GetTopTransactionIdIfAny();
354 
355  /* Assume we're not in an index build (spgbuild will override) */
356  state->isBuild = false;
357 }
358 
359 /*
360  * Allocate a new page (either by recycling, or by extending the index file).
361  *
362  * The returned buffer is already pinned and exclusive-locked.
363  * Caller is responsible for initializing the page by calling SpGistInitBuffer.
364  */
365 Buffer
367 {
368  Buffer buffer;
369 
370  /* First, try to get a page from FSM */
371  for (;;)
372  {
374 
375  if (blkno == InvalidBlockNumber)
376  break; /* nothing known to FSM */
377 
378  /*
379  * The fixed pages shouldn't ever be listed in FSM, but just in case
380  * one is, ignore it.
381  */
382  if (SpGistBlockIsFixed(blkno))
383  continue;
384 
385  buffer = ReadBuffer(index, blkno);
386 
387  /*
388  * We have to guard against the possibility that someone else already
389  * recycled this page; the buffer may be locked if so.
390  */
391  if (ConditionalLockBuffer(buffer))
392  {
393  Page page = BufferGetPage(buffer);
394 
395  if (PageIsNew(page))
396  return buffer; /* OK to use, if never initialized */
397 
398  if (SpGistPageIsDeleted(page) || PageIsEmpty(page))
399  return buffer; /* OK to use */
400 
402  }
403 
404  /* Can't use it, so release buffer and try again */
405  ReleaseBuffer(buffer);
406  }
407 
408  buffer = ExtendBufferedRel(BMR_REL(index), MAIN_FORKNUM, NULL,
409  EB_LOCK_FIRST);
410 
411  return buffer;
412 }
413 
414 /*
415  * Update index metapage's lastUsedPages info from local cache, if possible
416  *
417  * Updating meta page isn't critical for index working, so
418  * 1 use ConditionalLockBuffer to improve concurrency
419  * 2 don't WAL-log metabuffer changes to decrease WAL traffic
420  */
421 void
423 {
424  SpGistCache *cache = (SpGistCache *) index->rd_amcache;
425 
426  if (cache != NULL)
427  {
428  Buffer metabuffer;
429 
430  metabuffer = ReadBuffer(index, SPGIST_METAPAGE_BLKNO);
431 
432  if (ConditionalLockBuffer(metabuffer))
433  {
434  Page metapage = BufferGetPage(metabuffer);
435  SpGistMetaPageData *metadata = SpGistPageGetMeta(metapage);
436 
437  metadata->lastUsedPages = cache->lastUsedPages;
438 
439  /*
440  * Set pd_lower just past the end of the metadata. This is
441  * essential, because without doing so, metadata will be lost if
442  * xlog.c compresses the page. (We must do this here because
443  * pre-v11 versions of PG did not set the metapage's pd_lower
444  * correctly, so a pg_upgraded index might contain the wrong
445  * value.)
446  */
447  ((PageHeader) metapage)->pd_lower =
448  ((char *) metadata + sizeof(SpGistMetaPageData)) - (char *) metapage;
449 
450  MarkBufferDirty(metabuffer);
451  UnlockReleaseBuffer(metabuffer);
452  }
453  else
454  {
455  ReleaseBuffer(metabuffer);
456  }
457  }
458 }
459 
460 /* Macro to select proper element of lastUsedPages cache depending on flags */
461 /* Masking flags with SPGIST_CACHED_PAGES is just for paranoia's sake */
462 #define GET_LUP(c, f) (&(c)->lastUsedPages.cachedPage[((unsigned int) (f)) % SPGIST_CACHED_PAGES])
463 
464 /*
465  * Allocate and initialize a new buffer of the type and parity specified by
466  * flags. The returned buffer is already pinned and exclusive-locked.
467  *
468  * When requesting an inner page, if we get one with the wrong parity,
469  * we just release the buffer and try again. We will get a different page
470  * because GetFreeIndexPage will have marked the page used in FSM. The page
471  * is entered in our local lastUsedPages cache, so there's some hope of
472  * making use of it later in this session, but otherwise we rely on VACUUM
473  * to eventually re-enter the page in FSM, making it available for recycling.
474  * Note that such a page does not get marked dirty here, so unless it's used
475  * fairly soon, the buffer will just get discarded and the page will remain
476  * as it was on disk.
477  *
478  * When we return a buffer to the caller, the page is *not* entered into
479  * the lastUsedPages cache; we expect the caller will do so after it's taken
480  * whatever space it will use. This is because after the caller has used up
481  * some space, the page might have less space than whatever was cached already
482  * so we'd rather not trash the old cache entry.
483  */
484 static Buffer
486 {
487  SpGistCache *cache = spgGetCache(index);
488  uint16 pageflags = 0;
489 
490  if (GBUF_REQ_LEAF(flags))
491  pageflags |= SPGIST_LEAF;
492  if (GBUF_REQ_NULLS(flags))
493  pageflags |= SPGIST_NULLS;
494 
495  for (;;)
496  {
497  Buffer buffer;
498 
499  buffer = SpGistNewBuffer(index);
500  SpGistInitBuffer(buffer, pageflags);
501 
502  if (pageflags & SPGIST_LEAF)
503  {
504  /* Leaf pages have no parity concerns, so just use it */
505  return buffer;
506  }
507  else
508  {
509  BlockNumber blkno = BufferGetBlockNumber(buffer);
510  int blkFlags = GBUF_INNER_PARITY(blkno);
511 
512  if ((flags & GBUF_PARITY_MASK) == blkFlags)
513  {
514  /* Page has right parity, use it */
515  return buffer;
516  }
517  else
518  {
519  /* Page has wrong parity, record it in cache and try again */
520  if (pageflags & SPGIST_NULLS)
521  blkFlags |= GBUF_NULLS;
522  cache->lastUsedPages.cachedPage[blkFlags].blkno = blkno;
523  cache->lastUsedPages.cachedPage[blkFlags].freeSpace =
525  UnlockReleaseBuffer(buffer);
526  }
527  }
528  }
529 }
530 
531 /*
532  * Get a buffer of the type and parity specified by flags, having at least
533  * as much free space as indicated by needSpace. We use the lastUsedPages
534  * cache to assign the same buffer previously requested when possible.
535  * The returned buffer is already pinned and exclusive-locked.
536  *
537  * *isNew is set true if the page was initialized here, false if it was
538  * already valid.
539  */
540 Buffer
541 SpGistGetBuffer(Relation index, int flags, int needSpace, bool *isNew)
542 {
543  SpGistCache *cache = spgGetCache(index);
544  SpGistLastUsedPage *lup;
545 
546  /* Bail out if even an empty page wouldn't meet the demand */
547  if (needSpace > SPGIST_PAGE_CAPACITY)
548  elog(ERROR, "desired SPGiST tuple size is too big");
549 
550  /*
551  * If possible, increase the space request to include relation's
552  * fillfactor. This ensures that when we add unrelated tuples to a page,
553  * we try to keep 100-fillfactor% available for adding tuples that are
554  * related to the ones already on it. But fillfactor mustn't cause an
555  * error for requests that would otherwise be legal.
556  */
557  needSpace += SpGistGetTargetPageFreeSpace(index);
558  needSpace = Min(needSpace, SPGIST_PAGE_CAPACITY);
559 
560  /* Get the cache entry for this flags setting */
561  lup = GET_LUP(cache, flags);
562 
563  /* If we have nothing cached, just turn it over to allocNewBuffer */
564  if (lup->blkno == InvalidBlockNumber)
565  {
566  *isNew = true;
567  return allocNewBuffer(index, flags);
568  }
569 
570  /* fixed pages should never be in cache */
572 
573  /* If cached freeSpace isn't enough, don't bother looking at the page */
574  if (lup->freeSpace >= needSpace)
575  {
576  Buffer buffer;
577  Page page;
578 
579  buffer = ReadBuffer(index, lup->blkno);
580 
581  if (!ConditionalLockBuffer(buffer))
582  {
583  /*
584  * buffer is locked by another process, so return a new buffer
585  */
586  ReleaseBuffer(buffer);
587  *isNew = true;
588  return allocNewBuffer(index, flags);
589  }
590 
591  page = BufferGetPage(buffer);
592 
593  if (PageIsNew(page) || SpGistPageIsDeleted(page) || PageIsEmpty(page))
594  {
595  /* OK to initialize the page */
596  uint16 pageflags = 0;
597 
598  if (GBUF_REQ_LEAF(flags))
599  pageflags |= SPGIST_LEAF;
600  if (GBUF_REQ_NULLS(flags))
601  pageflags |= SPGIST_NULLS;
602  SpGistInitBuffer(buffer, pageflags);
603  lup->freeSpace = PageGetExactFreeSpace(page) - needSpace;
604  *isNew = true;
605  return buffer;
606  }
607 
608  /*
609  * Check that page is of right type and has enough space. We must
610  * recheck this since our cache isn't necessarily up to date.
611  */
612  if ((GBUF_REQ_LEAF(flags) ? SpGistPageIsLeaf(page) : !SpGistPageIsLeaf(page)) &&
614  {
615  int freeSpace = PageGetExactFreeSpace(page);
616 
617  if (freeSpace >= needSpace)
618  {
619  /* Success, update freespace info and return the buffer */
620  lup->freeSpace = freeSpace - needSpace;
621  *isNew = false;
622  return buffer;
623  }
624  }
625 
626  /*
627  * fallback to allocation of new buffer
628  */
629  UnlockReleaseBuffer(buffer);
630  }
631 
632  /* No success with cache, so return a new buffer */
633  *isNew = true;
634  return allocNewBuffer(index, flags);
635 }
636 
637 /*
638  * Update lastUsedPages cache when done modifying a page.
639  *
640  * We update the appropriate cache entry if it already contained this page
641  * (its freeSpace is likely obsolete), or if this page has more space than
642  * whatever we had cached.
643  */
644 void
646 {
647  SpGistCache *cache = spgGetCache(index);
648  SpGistLastUsedPage *lup;
649  int freeSpace;
650  Page page = BufferGetPage(buffer);
651  BlockNumber blkno = BufferGetBlockNumber(buffer);
652  int flags;
653 
654  /* Never enter fixed pages (root pages) in cache, though */
655  if (SpGistBlockIsFixed(blkno))
656  return;
657 
658  if (SpGistPageIsLeaf(page))
659  flags = GBUF_LEAF;
660  else
661  flags = GBUF_INNER_PARITY(blkno);
662  if (SpGistPageStoresNulls(page))
663  flags |= GBUF_NULLS;
664 
665  lup = GET_LUP(cache, flags);
666 
667  freeSpace = PageGetExactFreeSpace(page);
668  if (lup->blkno == InvalidBlockNumber || lup->blkno == blkno ||
669  lup->freeSpace < freeSpace)
670  {
671  lup->blkno = blkno;
672  lup->freeSpace = freeSpace;
673  }
674 }
675 
676 /*
677  * Initialize an SPGiST page to empty, with specified flags
678  */
679 void
681 {
682  SpGistPageOpaque opaque;
683 
684  PageInit(page, BLCKSZ, sizeof(SpGistPageOpaqueData));
685  opaque = SpGistPageGetOpaque(page);
686  opaque->flags = f;
687  opaque->spgist_page_id = SPGIST_PAGE_ID;
688 }
689 
690 /*
691  * Initialize a buffer's page to empty, with specified flags
692  */
693 void
695 {
696  Assert(BufferGetPageSize(b) == BLCKSZ);
698 }
699 
700 /*
701  * Initialize metadata page
702  */
703 void
705 {
706  SpGistMetaPageData *metadata;
707  int i;
708 
710  metadata = SpGistPageGetMeta(page);
711  memset(metadata, 0, sizeof(SpGistMetaPageData));
712  metadata->magicNumber = SPGIST_MAGIC_NUMBER;
713 
714  /* initialize last-used-page cache to empty */
715  for (i = 0; i < SPGIST_CACHED_PAGES; i++)
717 
718  /*
719  * Set pd_lower just past the end of the metadata. This is essential,
720  * because without doing so, metadata will be lost if xlog.c compresses
721  * the page.
722  */
723  ((PageHeader) page)->pd_lower =
724  ((char *) metadata + sizeof(SpGistMetaPageData)) - (char *) page;
725 }
726 
727 /*
728  * reloptions processing for SPGiST
729  */
730 bytea *
731 spgoptions(Datum reloptions, bool validate)
732 {
733  static const relopt_parse_elt tab[] = {
734  {"fillfactor", RELOPT_TYPE_INT, offsetof(SpGistOptions, fillfactor)},
735  };
736 
737  return (bytea *) build_reloptions(reloptions, validate,
739  sizeof(SpGistOptions),
740  tab, lengthof(tab));
741 }
742 
743 /*
744  * Get the space needed to store a non-null datum of the indicated type
745  * in an inner tuple (that is, as a prefix or node label).
746  * Note the result is already rounded up to a MAXALIGN boundary.
747  * Here we follow the convention that pass-by-val types are just stored
748  * in their Datum representation (compare memcpyInnerDatum).
749  */
750 unsigned int
752 {
753  unsigned int size;
754 
755  if (att->attbyval)
756  size = sizeof(Datum);
757  else if (att->attlen > 0)
758  size = att->attlen;
759  else
760  size = VARSIZE_ANY(datum);
761 
762  return MAXALIGN(size);
763 }
764 
765 /*
766  * Copy the given non-null datum to *target, in the inner-tuple case
767  */
768 static void
769 memcpyInnerDatum(void *target, SpGistTypeDesc *att, Datum datum)
770 {
771  unsigned int size;
772 
773  if (att->attbyval)
774  {
775  memcpy(target, &datum, sizeof(Datum));
776  }
777  else
778  {
779  size = (att->attlen > 0) ? att->attlen : VARSIZE_ANY(datum);
780  memcpy(target, DatumGetPointer(datum), size);
781  }
782 }
783 
784 /*
785  * Compute space required for a leaf tuple holding the given data.
786  *
787  * This must match the size-calculation portion of spgFormLeafTuple.
788  */
789 Size
791  Datum *datums, bool *isnulls)
792 {
793  Size size;
794  Size data_size;
795  bool needs_null_mask = false;
796  int natts = tupleDescriptor->natts;
797 
798  /*
799  * Decide whether we need a nulls bitmask.
800  *
801  * If there is only a key attribute (natts == 1), never use a bitmask, for
802  * compatibility with the pre-v14 layout of leaf tuples. Otherwise, we
803  * need one if any attribute is null.
804  */
805  if (natts > 1)
806  {
807  for (int i = 0; i < natts; i++)
808  {
809  if (isnulls[i])
810  {
811  needs_null_mask = true;
812  break;
813  }
814  }
815  }
816 
817  /*
818  * Calculate size of the data part; same as for heap tuples.
819  */
820  data_size = heap_compute_data_size(tupleDescriptor, datums, isnulls);
821 
822  /*
823  * Compute total size.
824  */
825  size = SGLTHDRSZ(needs_null_mask);
826  size += data_size;
827  size = MAXALIGN(size);
828 
829  /*
830  * Ensure that we can replace the tuple with a dead tuple later. This test
831  * is unnecessary when there are any non-null attributes, but be safe.
832  */
833  if (size < SGDTSIZE)
834  size = SGDTSIZE;
835 
836  return size;
837 }
838 
839 /*
840  * Construct a leaf tuple containing the given heap TID and datum values
841  */
844  Datum *datums, bool *isnulls)
845 {
846  SpGistLeafTuple tup;
847  TupleDesc tupleDescriptor = state->leafTupDesc;
848  Size size;
849  Size hoff;
850  Size data_size;
851  bool needs_null_mask = false;
852  int natts = tupleDescriptor->natts;
853  char *tp; /* ptr to tuple data */
854  uint16 tupmask = 0; /* unused heap_fill_tuple output */
855 
856  /*
857  * Decide whether we need a nulls bitmask.
858  *
859  * If there is only a key attribute (natts == 1), never use a bitmask, for
860  * compatibility with the pre-v14 layout of leaf tuples. Otherwise, we
861  * need one if any attribute is null.
862  */
863  if (natts > 1)
864  {
865  for (int i = 0; i < natts; i++)
866  {
867  if (isnulls[i])
868  {
869  needs_null_mask = true;
870  break;
871  }
872  }
873  }
874 
875  /*
876  * Calculate size of the data part; same as for heap tuples.
877  */
878  data_size = heap_compute_data_size(tupleDescriptor, datums, isnulls);
879 
880  /*
881  * Compute total size.
882  */
883  hoff = SGLTHDRSZ(needs_null_mask);
884  size = hoff + data_size;
885  size = MAXALIGN(size);
886 
887  /*
888  * Ensure that we can replace the tuple with a dead tuple later. This test
889  * is unnecessary when there are any non-null attributes, but be safe.
890  */
891  if (size < SGDTSIZE)
892  size = SGDTSIZE;
893 
894  /* OK, form the tuple */
895  tup = (SpGistLeafTuple) palloc0(size);
896 
897  tup->size = size;
899  tup->heapPtr = *heapPtr;
900 
901  tp = (char *) tup + hoff;
902 
903  if (needs_null_mask)
904  {
905  bits8 *bp; /* ptr to null bitmap in tuple */
906 
907  /* Set nullmask presence bit in SpGistLeafTuple header */
908  SGLT_SET_HASNULLMASK(tup, true);
909  /* Fill the data area and null mask */
910  bp = (bits8 *) ((char *) tup + sizeof(SpGistLeafTupleData));
911  heap_fill_tuple(tupleDescriptor, datums, isnulls, tp, data_size,
912  &tupmask, bp);
913  }
914  else if (natts > 1 || !isnulls[spgKeyColumn])
915  {
916  /* Fill data area only */
917  heap_fill_tuple(tupleDescriptor, datums, isnulls, tp, data_size,
918  &tupmask, (bits8 *) NULL);
919  }
920  /* otherwise we have no data, nor a bitmap, to fill */
921 
922  return tup;
923 }
924 
925 /*
926  * Construct a node (to go into an inner tuple) containing the given label
927  *
928  * Note that the node's downlink is just set invalid here. Caller will fill
929  * it in later.
930  */
933 {
934  SpGistNodeTuple tup;
935  unsigned int size;
936  unsigned short infomask = 0;
937 
938  /* compute space needed (note result is already maxaligned) */
939  size = SGNTHDRSZ;
940  if (!isnull)
941  size += SpGistGetInnerTypeSize(&state->attLabelType, label);
942 
943  /*
944  * Here we make sure that the size will fit in the field reserved for it
945  * in t_info.
946  */
947  if ((size & INDEX_SIZE_MASK) != size)
948  ereport(ERROR,
949  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
950  errmsg("index row requires %zu bytes, maximum size is %zu",
951  (Size) size, (Size) INDEX_SIZE_MASK)));
952 
953  tup = (SpGistNodeTuple) palloc0(size);
954 
955  if (isnull)
956  infomask |= INDEX_NULL_MASK;
957  /* we don't bother setting the INDEX_VAR_MASK bit */
958  infomask |= size;
959  tup->t_info = infomask;
960 
961  /* The TID field will be filled in later */
963 
964  if (!isnull)
965  memcpyInnerDatum(SGNTDATAPTR(tup), &state->attLabelType, label);
966 
967  return tup;
968 }
969 
970 /*
971  * Construct an inner tuple containing the given prefix and node array
972  */
974 spgFormInnerTuple(SpGistState *state, bool hasPrefix, Datum prefix,
975  int nNodes, SpGistNodeTuple *nodes)
976 {
977  SpGistInnerTuple tup;
978  unsigned int size;
979  unsigned int prefixSize;
980  int i;
981  char *ptr;
982 
983  /* Compute size needed */
984  if (hasPrefix)
985  prefixSize = SpGistGetInnerTypeSize(&state->attPrefixType, prefix);
986  else
987  prefixSize = 0;
988 
989  size = SGITHDRSZ + prefixSize;
990 
991  /* Note: we rely on node tuple sizes to be maxaligned already */
992  for (i = 0; i < nNodes; i++)
993  size += IndexTupleSize(nodes[i]);
994 
995  /*
996  * Ensure that we can replace the tuple with a dead tuple later. This
997  * test is unnecessary given current tuple layouts, but let's be safe.
998  */
999  if (size < SGDTSIZE)
1000  size = SGDTSIZE;
1001 
1002  /*
1003  * Inner tuple should be small enough to fit on a page
1004  */
1005  if (size > SPGIST_PAGE_CAPACITY - sizeof(ItemIdData))
1006  ereport(ERROR,
1007  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1008  errmsg("SP-GiST inner tuple size %zu exceeds maximum %zu",
1009  (Size) size,
1010  SPGIST_PAGE_CAPACITY - sizeof(ItemIdData)),
1011  errhint("Values larger than a buffer page cannot be indexed.")));
1012 
1013  /*
1014  * Check for overflow of header fields --- probably can't fail if the
1015  * above succeeded, but let's be paranoid
1016  */
1017  if (size > SGITMAXSIZE ||
1018  prefixSize > SGITMAXPREFIXSIZE ||
1019  nNodes > SGITMAXNNODES)
1020  elog(ERROR, "SPGiST inner tuple header field is too small");
1021 
1022  /* OK, form the tuple */
1023  tup = (SpGistInnerTuple) palloc0(size);
1024 
1025  tup->nNodes = nNodes;
1026  tup->prefixSize = prefixSize;
1027  tup->size = size;
1028 
1029  if (hasPrefix)
1030  memcpyInnerDatum(SGITDATAPTR(tup), &state->attPrefixType, prefix);
1031 
1032  ptr = (char *) SGITNODEPTR(tup);
1033 
1034  for (i = 0; i < nNodes; i++)
1035  {
1036  SpGistNodeTuple node = nodes[i];
1037 
1038  memcpy(ptr, node, IndexTupleSize(node));
1039  ptr += IndexTupleSize(node);
1040  }
1041 
1042  return tup;
1043 }
1044 
1045 /*
1046  * Construct a "dead" tuple to replace a tuple being deleted.
1047  *
1048  * The state can be SPGIST_REDIRECT, SPGIST_DEAD, or SPGIST_PLACEHOLDER.
1049  * For a REDIRECT tuple, a pointer (blkno+offset) must be supplied, and
1050  * the xid field is filled in automatically.
1051  *
1052  * This is called in critical sections, so we don't use palloc; the tuple
1053  * is built in preallocated storage. It should be copied before another
1054  * call with different parameters can occur.
1055  */
1058  BlockNumber blkno, OffsetNumber offnum)
1059 {
1060  SpGistDeadTuple tuple = (SpGistDeadTuple) state->deadTupleStorage;
1061 
1062  tuple->tupstate = tupstate;
1063  tuple->size = SGDTSIZE;
1065 
1066  if (tupstate == SPGIST_REDIRECT)
1067  {
1068  ItemPointerSet(&tuple->pointer, blkno, offnum);
1070  tuple->xid = state->myXid;
1071  }
1072  else
1073  {
1074  ItemPointerSetInvalid(&tuple->pointer);
1075  tuple->xid = InvalidTransactionId;
1076  }
1077 
1078  return tuple;
1079 }
1080 
1081 /*
1082  * Convert an SPGiST leaf tuple into Datum/isnull arrays.
1083  *
1084  * The caller must allocate sufficient storage for the output arrays.
1085  * (INDEX_MAX_KEYS entries should be enough.)
1086  */
1087 void
1089  Datum *datums, bool *isnulls, bool keyColumnIsNull)
1090 {
1091  bool hasNullsMask = SGLT_GET_HASNULLMASK(tup);
1092  char *tp; /* ptr to tuple data */
1093  bits8 *bp; /* ptr to null bitmap in tuple */
1094 
1095  if (keyColumnIsNull && tupleDescriptor->natts == 1)
1096  {
1097  /*
1098  * Trivial case: there is only the key attribute and we're in a nulls
1099  * tree. The hasNullsMask bit in the tuple header should not be set
1100  * (and thus we can't use index_deform_tuple_internal), but
1101  * nonetheless the result is NULL.
1102  *
1103  * Note: currently this is dead code, because noplace calls this when
1104  * there is only the key attribute. But we should cover the case.
1105  */
1106  Assert(!hasNullsMask);
1107 
1108  datums[spgKeyColumn] = (Datum) 0;
1109  isnulls[spgKeyColumn] = true;
1110  return;
1111  }
1112 
1113  tp = (char *) tup + SGLTHDRSZ(hasNullsMask);
1114  bp = (bits8 *) ((char *) tup + sizeof(SpGistLeafTupleData));
1115 
1116  index_deform_tuple_internal(tupleDescriptor,
1117  datums, isnulls,
1118  tp, bp, hasNullsMask);
1119 
1120  /*
1121  * Key column isnull value from the tuple should be consistent with
1122  * keyColumnIsNull flag from the caller.
1123  */
1124  Assert(keyColumnIsNull == isnulls[spgKeyColumn]);
1125 }
1126 
1127 /*
1128  * Extract the label datums of the nodes within innerTuple
1129  *
1130  * Returns NULL if label datums are NULLs
1131  */
1132 Datum *
1134 {
1135  Datum *nodeLabels;
1136  int i;
1137  SpGistNodeTuple node;
1138 
1139  /* Either all the labels must be NULL, or none. */
1140  node = SGITNODEPTR(innerTuple);
1141  if (IndexTupleHasNulls(node))
1142  {
1143  SGITITERATE(innerTuple, i, node)
1144  {
1145  if (!IndexTupleHasNulls(node))
1146  elog(ERROR, "some but not all node labels are null in SPGiST inner tuple");
1147  }
1148  /* They're all null, so just return NULL */
1149  return NULL;
1150  }
1151  else
1152  {
1153  nodeLabels = (Datum *) palloc(sizeof(Datum) * innerTuple->nNodes);
1154  SGITITERATE(innerTuple, i, node)
1155  {
1156  if (IndexTupleHasNulls(node))
1157  elog(ERROR, "some but not all node labels are null in SPGiST inner tuple");
1158  nodeLabels[i] = SGNTDATUM(node, state);
1159  }
1160  return nodeLabels;
1161  }
1162 }
1163 
1164 /*
1165  * Add a new item to the page, replacing a PLACEHOLDER item if possible.
1166  * Return the location it's inserted at, or InvalidOffsetNumber on failure.
1167  *
1168  * If startOffset isn't NULL, we start searching for placeholders at
1169  * *startOffset, and update that to the next place to search. This is just
1170  * an optimization for repeated insertions.
1171  *
1172  * If errorOK is false, we throw error when there's not enough room,
1173  * rather than returning InvalidOffsetNumber.
1174  */
1177  OffsetNumber *startOffset, bool errorOK)
1178 {
1179  SpGistPageOpaque opaque = SpGistPageGetOpaque(page);
1180  OffsetNumber i,
1181  maxoff,
1182  offnum;
1183 
1184  if (opaque->nPlaceholder > 0 &&
1185  PageGetExactFreeSpace(page) + SGDTSIZE >= MAXALIGN(size))
1186  {
1187  /* Try to replace a placeholder */
1188  maxoff = PageGetMaxOffsetNumber(page);
1189  offnum = InvalidOffsetNumber;
1190 
1191  for (;;)
1192  {
1193  if (startOffset && *startOffset != InvalidOffsetNumber)
1194  i = *startOffset;
1195  else
1196  i = FirstOffsetNumber;
1197  for (; i <= maxoff; i++)
1198  {
1200  PageGetItemId(page, i));
1201 
1202  if (it->tupstate == SPGIST_PLACEHOLDER)
1203  {
1204  offnum = i;
1205  break;
1206  }
1207  }
1208 
1209  /* Done if we found a placeholder */
1210  if (offnum != InvalidOffsetNumber)
1211  break;
1212 
1213  if (startOffset && *startOffset != InvalidOffsetNumber)
1214  {
1215  /* Hint was no good, re-search from beginning */
1216  *startOffset = InvalidOffsetNumber;
1217  continue;
1218  }
1219 
1220  /* Hmm, no placeholder found? */
1221  opaque->nPlaceholder = 0;
1222  break;
1223  }
1224 
1225  if (offnum != InvalidOffsetNumber)
1226  {
1227  /* Replace the placeholder tuple */
1228  PageIndexTupleDelete(page, offnum);
1229 
1230  offnum = PageAddItem(page, item, size, offnum, false, false);
1231 
1232  /*
1233  * We should not have failed given the size check at the top of
1234  * the function, but test anyway. If we did fail, we must PANIC
1235  * because we've already deleted the placeholder tuple, and
1236  * there's no other way to keep the damage from getting to disk.
1237  */
1238  if (offnum != InvalidOffsetNumber)
1239  {
1240  Assert(opaque->nPlaceholder > 0);
1241  opaque->nPlaceholder--;
1242  if (startOffset)
1243  *startOffset = offnum + 1;
1244  }
1245  else
1246  elog(PANIC, "failed to add item of size %zu to SPGiST index page",
1247  size);
1248 
1249  return offnum;
1250  }
1251  }
1252 
1253  /* No luck in replacing a placeholder, so just add it to the page */
1254  offnum = PageAddItem(page, item, size,
1255  InvalidOffsetNumber, false, false);
1256 
1257  if (offnum == InvalidOffsetNumber && !errorOK)
1258  elog(ERROR, "failed to add item of size %zu to SPGiST index page",
1259  size);
1260 
1261  return offnum;
1262 }
1263 
1264 /*
1265  * spgproperty() -- Check boolean properties of indexes.
1266  *
1267  * This is optional for most AMs, but is required for SP-GiST because the core
1268  * property code doesn't support AMPROP_DISTANCE_ORDERABLE.
1269  */
1270 bool
1271 spgproperty(Oid index_oid, int attno,
1272  IndexAMProperty prop, const char *propname,
1273  bool *res, bool *isnull)
1274 {
1275  Oid opclass,
1276  opfamily,
1277  opcintype;
1278  CatCList *catlist;
1279  int i;
1280 
1281  /* Only answer column-level inquiries */
1282  if (attno == 0)
1283  return false;
1284 
1285  switch (prop)
1286  {
1288  break;
1289  default:
1290  return false;
1291  }
1292 
1293  /*
1294  * Currently, SP-GiST distance-ordered scans require that there be a
1295  * distance operator in the opclass with the default types. So we assume
1296  * that if such an operator exists, then there's a reason for it.
1297  */
1298 
1299  /* First we need to know the column's opclass. */
1300  opclass = get_index_column_opclass(index_oid, attno);
1301  if (!OidIsValid(opclass))
1302  {
1303  *isnull = true;
1304  return true;
1305  }
1306 
1307  /* Now look up the opclass family and input datatype. */
1308  if (!get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
1309  {
1310  *isnull = true;
1311  return true;
1312  }
1313 
1314  /* And now we can check whether the operator is provided. */
1316  ObjectIdGetDatum(opfamily));
1317 
1318  *res = false;
1319 
1320  for (i = 0; i < catlist->n_members; i++)
1321  {
1322  HeapTuple amoptup = &catlist->members[i]->tuple;
1323  Form_pg_amop amopform = (Form_pg_amop) GETSTRUCT(amoptup);
1324 
1325  if (amopform->amoppurpose == AMOP_ORDER &&
1326  (amopform->amoplefttype == opcintype ||
1327  amopform->amoprighttype == opcintype) &&
1328  opfamily_can_sort_type(amopform->amopsortfamily,
1329  get_op_rettype(amopform->amopopr)))
1330  {
1331  *res = true;
1332  break;
1333  }
1334  }
1335 
1336  ReleaseSysCacheList(catlist);
1337 
1338  *isnull = false;
1339 
1340  return true;
1341 }
IndexAMProperty
Definition: amapi.h:35
@ AMPROP_DISTANCE_ORDERABLE
Definition: amapi.h:42
bool opfamily_can_sort_type(Oid opfamilyoid, Oid datatypeoid)
Definition: amvalidate.c:271
int16 AttrNumber
Definition: attnum.h:21
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
int Buffer
Definition: buf.h:23
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3290
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:812
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:4741
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4480
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4497
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2111
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4715
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:708
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:157
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:158
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:350
static Size BufferGetPageSize(Buffer buffer)
Definition: bufmgr.h:339
@ EB_LOCK_FIRST
Definition: bufmgr.h:85
#define BMR_REL(p_rel)
Definition: bufmgr.h:106
void PageIndexTupleDelete(Page page, OffsetNumber offnum)
Definition: bufpage.c:1052
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:958
PageHeaderData * PageHeader
Definition: bufpage.h:170
static bool PageIsEmpty(Page page)
Definition: bufpage.h:220
Pointer Page
Definition: bufpage.h:78
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:351
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:240
static bool PageIsNew(Page page)
Definition: bufpage.h:230
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:369
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:468
unsigned short uint16
Definition: c.h:494
#define Min(x, y)
Definition: c.h:993
#define MAXALIGN(LEN)
Definition: c.h:800
uint8 bits8
Definition: c.h:502
#define lengthof(array)
Definition: c.h:777
#define OidIsValid(objectId)
Definition: c.h:764
size_t Size
Definition: c.h:594
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define PANIC
Definition: elog.h:42
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1132
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Size heap_compute_data_size(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:207
void heap_fill_tuple(TupleDesc tupleDesc, Datum *values, bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
Definition: heaptuple.c:392
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:811
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:777
BlockNumber GetFreeIndexPage(Relation rel)
Definition: indexfsm.c:38
void index_deform_tuple_internal(TupleDesc tupleDescriptor, Datum *values, bool *isnull, char *tp, bits8 *bp, int hasnulls)
Definition: indextuple.c:479
int b
Definition: isn.c:70
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Pointer Item
Definition: item.h:17
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
#define IndexTupleHasNulls(itup)
Definition: itup.h:71
#define INDEX_NULL_MASK
Definition: itup.h:68
#define IndexTupleSize(itup)
Definition: itup.h:70
#define INDEX_SIZE_MASK
Definition: itup.h:65
Assert(fmt[strlen(fmt) - 1] !='\n')
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
Definition: lsyscache.c:1239
Oid get_op_rettype(Oid opno)
Definition: lsyscache.c:1337
Oid get_index_column_opclass(Oid index_oid, int attno)
Definition: lsyscache.c:3494
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2503
Oid get_atttype(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:939
void * palloc0(Size size)
Definition: mcxt.c:1257
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1064
void * palloc(Size size)
Definition: mcxt.c:1226
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
#define makeNode(_type_)
Definition: nodes.h:176
#define InvalidOffsetNumber
Definition: off.h:26
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
bool IsBinaryCoercible(Oid srctype, Oid targettype)
FormData_pg_amop * Form_pg_amop
Definition: pg_amop.h:88
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
static char * label
#define INDEX_MAX_KEYS
#define lfirst(lc)
Definition: pg_list.h:172
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
int fillfactor
Definition: pgbench.c:187
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:530
#define RelationGetRelationName(relation)
Definition: rel.h:538
#define IndexRelationGetNumberOfAttributes(relation)
Definition: rel.h:516
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:523
List * RelationGetIndexExpressions(Relation relation)
Definition: relcache.c:4996
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
Definition: reloptions.c:1910
@ RELOPT_KIND_SPGIST
Definition: reloptions.h:50
@ RELOPT_TYPE_INT
Definition: reloptions.h:32
@ MAIN_FORKNUM
Definition: relpath.h:50
void spgcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7070
bool spginsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: spginsert.c:200
void spgbuildempty(Relation index)
Definition: spginsert.c:156
IndexBuildResult * spgbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: spginsert.c:75
#define SPGIST_OPTIONS_PROC
Definition: spgist.h:29
#define SPGIST_COMPRESS_PROC
Definition: spgist.h:28
#define SPGISTNProc
Definition: spgist.h:31
#define SPGIST_CONFIG_PROC
Definition: spgist.h:23
#define SGNTDATAPTR(x)
SpGistDeadTupleData * SpGistDeadTuple
#define SGLT_GET_HASNULLMASK(spgLeafTuple)
#define GBUF_NULLS
#define SPGIST_REDIRECT
SpGistInnerTupleData * SpGistInnerTuple
#define SGDTSIZE
#define SpGistPageStoresNulls(page)
#define SPGIST_PLACEHOLDER
#define SGITDATAPTR(x)
#define SGLT_SET_HASNULLMASK(spgLeafTuple, hasnulls)
#define SGITITERATE(x, i, nt)
#define SpGistGetTargetPageFreeSpace(relation)
#define GBUF_PARITY_MASK
#define GBUF_LEAF
#define spgFirstIncludeColumn
#define SGITMAXSIZE
#define SpGistPageGetMeta(p)
SpGistNodeTupleData * SpGistNodeTuple
#define SGITMAXPREFIXSIZE
#define SpGistPageIsLeaf(page)
#define SPGIST_METAPAGE_BLKNO
#define SpGistPageIsDeleted(page)
#define SGLTHDRSZ(hasnulls)
#define SGLT_SET_NEXTOFFSET(spgLeafTuple, offsetNumber)
#define SGITHDRSZ
#define SPGIST_META
struct SpGistLeafTupleData * SpGistLeafTuple
#define SPGIST_MAGIC_NUMBER
#define SPGIST_CACHED_PAGES
#define SPGIST_NULLS
#define GBUF_REQ_NULLS(flags)
#define SPGIST_PAGE_CAPACITY
#define SGITMAXNNODES
#define SGNTDATUM(x, s)
#define SpGistPageGetOpaque(page)
#define SPGIST_PAGE_ID
#define SGNTHDRSZ
#define GBUF_INNER_PARITY(x)
#define SGITNODEPTR(x)
#define SpGistBlockIsFixed(blkno)
#define GBUF_REQ_LEAF(flags)
struct SpGistLeafTupleData SpGistLeafTupleData
#define SPGIST_LEAF
#define spgKeyColumn
IndexScanDesc spgbeginscan(Relation rel, int keysz, int orderbysz)
Definition: spgscan.c:304
bool spgcanreturn(Relation index, int attno)
Definition: spgscan.c:1083
bool spggettuple(IndexScanDesc scan, ScanDirection dir)
Definition: spgscan.c:1026
void spgendscan(IndexScanDesc scan)
Definition: spgscan.c:429
void spgrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: spgscan.c:380
int64 spggetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: spgscan.c:942
bytea * spgoptions(Datum reloptions, bool validate)
Definition: spgutils.c:731
Datum * spgExtractNodeLabels(SpGistState *state, SpGistInnerTuple innerTuple)
Definition: spgutils.c:1133
void initSpGistState(SpGistState *state, Relation index)
Definition: spgutils.c:331
void SpGistUpdateMetaPage(Relation index)
Definition: spgutils.c:422
TupleDesc getSpGistTupleDesc(Relation index, SpGistTypeDesc *keyType)
Definition: spgutils.c:300
Buffer SpGistNewBuffer(Relation index)
Definition: spgutils.c:366
SpGistInnerTuple spgFormInnerTuple(SpGistState *state, bool hasPrefix, Datum prefix, int nNodes, SpGistNodeTuple *nodes)
Definition: spgutils.c:974
static void memcpyInnerDatum(void *target, SpGistTypeDesc *att, Datum datum)
Definition: spgutils.c:769
void spgDeformLeafTuple(SpGistLeafTuple tup, TupleDesc tupleDescriptor, Datum *datums, bool *isnulls, bool keyColumnIsNull)
Definition: spgutils.c:1088
SpGistDeadTuple spgFormDeadTuple(SpGistState *state, int tupstate, BlockNumber blkno, OffsetNumber offnum)
Definition: spgutils.c:1057
SpGistLeafTuple spgFormLeafTuple(SpGistState *state, ItemPointer heapPtr, Datum *datums, bool *isnulls)
Definition: spgutils.c:843
unsigned int SpGistGetInnerTypeSize(SpGistTypeDesc *att, Datum datum)
Definition: spgutils.c:751
static Oid GetIndexInputType(Relation index, AttrNumber indexcol)
Definition: spgutils.c:113
void SpGistInitBuffer(Buffer b, uint16 f)
Definition: spgutils.c:694
Buffer SpGistGetBuffer(Relation index, int flags, int needSpace, bool *isNew)
Definition: spgutils.c:541
static Buffer allocNewBuffer(Relation index, int flags)
Definition: spgutils.c:485
bool spgproperty(Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
Definition: spgutils.c:1271
Size SpGistGetLeafTupleSize(TupleDesc tupleDescriptor, Datum *datums, bool *isnulls)
Definition: spgutils.c:790
void SpGistSetLastUsedPage(Relation index, Buffer buffer)
Definition: spgutils.c:645
static void fillTypeDesc(SpGistTypeDesc *desc, Oid type)
Definition: spgutils.c:158
SpGistCache * spgGetCache(Relation index)
Definition: spgutils.c:180
OffsetNumber SpGistPageAddNewItem(SpGistState *state, Page page, Item item, Size size, OffsetNumber *startOffset, bool errorOK)
Definition: spgutils.c:1176
Datum spghandler(PG_FUNCTION_ARGS)
Definition: spgutils.c:44
void SpGistInitPage(Page page, uint16 f)
Definition: spgutils.c:680
#define GET_LUP(c, f)
Definition: spgutils.c:462
SpGistNodeTuple spgFormNodeTuple(SpGistState *state, Datum label, bool isnull)
Definition: spgutils.c:932
void SpGistInitMetapage(Page page)
Definition: spgutils.c:704
IndexBulkDeleteResult * spgvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: spgvacuum.c:937
IndexBulkDeleteResult * spgbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: spgvacuum.c:906
bool spgvalidate(Oid opclassoid)
Definition: spgvalidate.c:39
void spgadjustmembers(Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
Definition: spgvalidate.c:332
Definition: fmgr.h:57
ambuildphasename_function ambuildphasename
Definition: amapi.h:270
ambuildempty_function ambuildempty
Definition: amapi.h:262
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:265
bool amclusterable
Definition: amapi.h:238
amoptions_function amoptions
Definition: amapi.h:268
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:282
amrestrpos_function amrestrpos
Definition: amapi.h:279
aminsert_function aminsert
Definition: amapi.h:263
amendscan_function amendscan
Definition: amapi.h:277
uint16 amoptsprocnum
Definition: amapi.h:218
amparallelrescan_function amparallelrescan
Definition: amapi.h:284
Oid amkeytype
Definition: amapi.h:252
bool ampredlocks
Definition: amapi.h:240
uint16 amsupport
Definition: amapi.h:216
amcostestimate_function amcostestimate
Definition: amapi.h:267
bool amcanorderbyop
Definition: amapi.h:222
amadjustmembers_function amadjustmembers
Definition: amapi.h:272
ambuild_function ambuild
Definition: amapi.h:261
bool amstorage
Definition: amapi.h:236
uint16 amstrategies
Definition: amapi.h:214
bool amoptionalkey
Definition: amapi.h:230
amgettuple_function amgettuple
Definition: amapi.h:275
amcanreturn_function amcanreturn
Definition: amapi.h:266
bool amcanunique
Definition: amapi.h:226
amgetbitmap_function amgetbitmap
Definition: amapi.h:276
amproperty_function amproperty
Definition: amapi.h:269
ambulkdelete_function ambulkdelete
Definition: amapi.h:264
bool amsearcharray
Definition: amapi.h:232
bool amsummarizing
Definition: amapi.h:248
amvalidate_function amvalidate
Definition: amapi.h:271
ammarkpos_function ammarkpos
Definition: amapi.h:278
bool amcanmulticol
Definition: amapi.h:228
bool amusemaintenanceworkmem
Definition: amapi.h:246
ambeginscan_function ambeginscan
Definition: amapi.h:273
bool amcanparallel
Definition: amapi.h:242
amrescan_function amrescan
Definition: amapi.h:274
bool amcanorder
Definition: amapi.h:220
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:283
uint8 amparallelvacuumoptions
Definition: amapi.h:250
bool amcanbackward
Definition: amapi.h:224
bool amcaninclude
Definition: amapi.h:244
bool amsearchnulls
Definition: amapi.h:234
ItemPointerData t_tid
Definition: itup.h:37
unsigned short t_info
Definition: itup.h:49
Definition: pg_list.h:54
Definition: nodes.h:129
SpGistTypeDesc attPrefixType
SpGistTypeDesc attLeafType
SpGistTypeDesc attType
SpGistLUPCache lastUsedPages
spgConfigOut config
SpGistTypeDesc attLabelType
unsigned int tupstate
ItemPointerData pointer
unsigned int prefixSize
SpGistLastUsedPage cachedPage[SPGIST_CACHED_PAGES]
ItemPointerData heapPtr
SpGistLUPCache lastUsedPages
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:178
int n_members
Definition: catcache.h:176
HeapTupleData tuple
Definition: catcache.h:121
Definition: type.h:95
Oid attType
Definition: spgist.h:38
Oid leafType
Definition: spgist.h:45
Oid labelType
Definition: spgist.h:44
Oid prefixType
Definition: spgist.h:43
Definition: regguts.h:323
Definition: c.h:676
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:820
@ TYPEOID
Definition: syscache.h:114
@ AMOPSTRATEGY
Definition: syscache.h:38
#define ReleaseSysCacheList(x)
Definition: syscache.h:225
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:218
#define InvalidCompressionMethod
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdIsValid(xid)
Definition: transam.h:41
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition: vacuum.h:47
#define VACUUM_OPTION_PARALLEL_COND_CLEANUP
Definition: vacuum.h:54
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311
const char * type
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:432