LCOV - differential code coverage report
Current view: top level - src/backend/access/spgist - spgutils.c (source / functions) Coverage Total Hit UBC GNC CBC DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 89.8 % 431 387 44 6 381 57
Current Date: 2026-03-14 14:10:32 -0400 Functions: 100.0 % 26 26 6 20 2
Baseline: lcov-20260315-024220-baseline Branches: 63.7 % 300 191 109 2 189 14 8
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 6 6 6
(360..) days: 89.6 % 425 381 44 381
Function coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 100.0 % 24 24 4 20
Branch coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 63.4 % 298 189 109 189

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

Generated by: LCOV version 2.4-beta