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