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