LCOV - code coverage report
Current view: top level - src/backend/access/gin - ginutil.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 88.1 % 219 193
Test Date: 2026-02-17 17:20:33 Functions: 92.9 % 14 13
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * ginutil.c
       4              :  *    Utility routines for the Postgres inverted index access method.
       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/gin/ginutil.c
      12              :  *-------------------------------------------------------------------------
      13              :  */
      14              : 
      15              : #include "postgres.h"
      16              : 
      17              : #include "access/gin_private.h"
      18              : #include "access/ginxlog.h"
      19              : #include "access/reloptions.h"
      20              : #include "access/xloginsert.h"
      21              : #include "catalog/pg_collation.h"
      22              : #include "catalog/pg_type.h"
      23              : #include "commands/progress.h"
      24              : #include "commands/vacuum.h"
      25              : #include "miscadmin.h"
      26              : #include "storage/indexfsm.h"
      27              : #include "utils/builtins.h"
      28              : #include "utils/index_selfuncs.h"
      29              : #include "utils/rel.h"
      30              : #include "utils/typcache.h"
      31              : 
      32              : 
      33              : /*
      34              :  * GIN handler function: return IndexAmRoutine with access method parameters
      35              :  * and callbacks.
      36              :  */
      37              : Datum
      38         2030 : ginhandler(PG_FUNCTION_ARGS)
      39              : {
      40              :     static const IndexAmRoutine amroutine = {
      41              :         .type = T_IndexAmRoutine,
      42              :         .amstrategies = 0,
      43              :         .amsupport = GINNProcs,
      44              :         .amoptsprocnum = GIN_OPTIONS_PROC,
      45              :         .amcanorder = false,
      46              :         .amcanorderbyop = false,
      47              :         .amcanhash = false,
      48              :         .amconsistentequality = false,
      49              :         .amconsistentordering = false,
      50              :         .amcanbackward = false,
      51              :         .amcanunique = false,
      52              :         .amcanmulticol = true,
      53              :         .amoptionalkey = true,
      54              :         .amsearcharray = false,
      55              :         .amsearchnulls = false,
      56              :         .amstorage = true,
      57              :         .amclusterable = false,
      58              :         .ampredlocks = true,
      59              :         .amcanparallel = false,
      60              :         .amcanbuildparallel = true,
      61              :         .amcaninclude = false,
      62              :         .amusemaintenanceworkmem = true,
      63              :         .amsummarizing = false,
      64              :         .amparallelvacuumoptions =
      65              :         VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_CLEANUP,
      66              :         .amkeytype = InvalidOid,
      67              : 
      68              :         .ambuild = ginbuild,
      69              :         .ambuildempty = ginbuildempty,
      70              :         .aminsert = gininsert,
      71              :         .aminsertcleanup = NULL,
      72              :         .ambulkdelete = ginbulkdelete,
      73              :         .amvacuumcleanup = ginvacuumcleanup,
      74              :         .amcanreturn = NULL,
      75              :         .amcostestimate = gincostestimate,
      76              :         .amgettreeheight = NULL,
      77              :         .amoptions = ginoptions,
      78              :         .amproperty = NULL,
      79              :         .ambuildphasename = ginbuildphasename,
      80              :         .amvalidate = ginvalidate,
      81              :         .amadjustmembers = ginadjustmembers,
      82              :         .ambeginscan = ginbeginscan,
      83              :         .amrescan = ginrescan,
      84              :         .amgettuple = NULL,
      85              :         .amgetbitmap = gingetbitmap,
      86              :         .amendscan = ginendscan,
      87              :         .ammarkpos = NULL,
      88              :         .amrestrpos = NULL,
      89              :         .amestimateparallelscan = NULL,
      90              :         .aminitparallelscan = NULL,
      91              :         .amparallelrescan = NULL,
      92              :     };
      93              : 
      94         2030 :     PG_RETURN_POINTER(&amroutine);
      95              : }
      96              : 
      97              : /*
      98              :  * initGinState: fill in an empty GinState struct to describe the index
      99              :  *
     100              :  * Note: assorted subsidiary data is allocated in the CurrentMemoryContext.
     101              :  */
     102              : void
     103         2754 : initGinState(GinState *state, Relation index)
     104              : {
     105         2754 :     TupleDesc   origTupdesc = RelationGetDescr(index);
     106              :     int         i;
     107              : 
     108         2754 :     MemSet(state, 0, sizeof(GinState));
     109              : 
     110         2754 :     state->index = index;
     111         2754 :     state->oneCol = (origTupdesc->natts == 1);
     112         2754 :     state->origTupdesc = origTupdesc;
     113              : 
     114         5653 :     for (i = 0; i < origTupdesc->natts; i++)
     115              :     {
     116         2899 :         Form_pg_attribute attr = TupleDescAttr(origTupdesc, i);
     117              : 
     118         2899 :         if (state->oneCol)
     119         2609 :             state->tupdesc[i] = state->origTupdesc;
     120              :         else
     121              :         {
     122          290 :             state->tupdesc[i] = CreateTemplateTupleDesc(2);
     123              : 
     124          290 :             TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 1, NULL,
     125              :                                INT2OID, -1, 0);
     126          290 :             TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 2, NULL,
     127              :                                attr->atttypid,
     128              :                                attr->atttypmod,
     129          290 :                                attr->attndims);
     130          290 :             TupleDescInitEntryCollation(state->tupdesc[i], (AttrNumber) 2,
     131              :                                         attr->attcollation);
     132              :         }
     133              : 
     134              :         /*
     135              :          * If the compare proc isn't specified in the opclass definition, look
     136              :          * up the index key type's default btree comparator.
     137              :          */
     138         2899 :         if (index_getprocid(index, i + 1, GIN_COMPARE_PROC) != InvalidOid)
     139              :         {
     140         1379 :             fmgr_info_copy(&(state->compareFn[i]),
     141         1379 :                            index_getprocinfo(index, i + 1, GIN_COMPARE_PROC),
     142              :                            CurrentMemoryContext);
     143              :         }
     144              :         else
     145              :         {
     146              :             TypeCacheEntry *typentry;
     147              : 
     148         1520 :             typentry = lookup_type_cache(attr->atttypid,
     149              :                                          TYPECACHE_CMP_PROC_FINFO);
     150         1520 :             if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid))
     151            0 :                 ereport(ERROR,
     152              :                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
     153              :                          errmsg("could not identify a comparison function for type %s",
     154              :                                 format_type_be(attr->atttypid))));
     155         1520 :             fmgr_info_copy(&(state->compareFn[i]),
     156              :                            &(typentry->cmp_proc_finfo),
     157              :                            CurrentMemoryContext);
     158              :         }
     159              : 
     160              :         /* Opclass must always provide extract procs */
     161         2899 :         fmgr_info_copy(&(state->extractValueFn[i]),
     162         2899 :                        index_getprocinfo(index, i + 1, GIN_EXTRACTVALUE_PROC),
     163              :                        CurrentMemoryContext);
     164         2899 :         fmgr_info_copy(&(state->extractQueryFn[i]),
     165         2899 :                        index_getprocinfo(index, i + 1, GIN_EXTRACTQUERY_PROC),
     166              :                        CurrentMemoryContext);
     167              : 
     168              :         /*
     169              :          * Check opclass capability to do tri-state or binary logic consistent
     170              :          * check.
     171              :          */
     172         2899 :         if (index_getprocid(index, i + 1, GIN_TRICONSISTENT_PROC) != InvalidOid)
     173              :         {
     174         2512 :             fmgr_info_copy(&(state->triConsistentFn[i]),
     175         2512 :                            index_getprocinfo(index, i + 1, GIN_TRICONSISTENT_PROC),
     176              :                            CurrentMemoryContext);
     177              :         }
     178              : 
     179         2899 :         if (index_getprocid(index, i + 1, GIN_CONSISTENT_PROC) != InvalidOid)
     180              :         {
     181         2899 :             fmgr_info_copy(&(state->consistentFn[i]),
     182         2899 :                            index_getprocinfo(index, i + 1, GIN_CONSISTENT_PROC),
     183              :                            CurrentMemoryContext);
     184              :         }
     185              : 
     186         2899 :         if (state->consistentFn[i].fn_oid == InvalidOid &&
     187            0 :             state->triConsistentFn[i].fn_oid == InvalidOid)
     188              :         {
     189            0 :             elog(ERROR, "missing GIN support function (%d or %d) for attribute %d of index \"%s\"",
     190              :                  GIN_CONSISTENT_PROC, GIN_TRICONSISTENT_PROC,
     191              :                  i + 1, RelationGetRelationName(index));
     192              :         }
     193              : 
     194              :         /*
     195              :          * Check opclass capability to do partial match.
     196              :          */
     197         2899 :         if (index_getprocid(index, i + 1, GIN_COMPARE_PARTIAL_PROC) != InvalidOid)
     198              :         {
     199          495 :             fmgr_info_copy(&(state->comparePartialFn[i]),
     200          495 :                            index_getprocinfo(index, i + 1, GIN_COMPARE_PARTIAL_PROC),
     201              :                            CurrentMemoryContext);
     202          495 :             state->canPartialMatch[i] = true;
     203              :         }
     204              :         else
     205              :         {
     206         2404 :             state->canPartialMatch[i] = false;
     207              :         }
     208              : 
     209              :         /*
     210              :          * If the index column has a specified collation, we should honor that
     211              :          * while doing comparisons.  However, we may have a collatable storage
     212              :          * type for a noncollatable indexed data type (for instance, hstore
     213              :          * uses text index entries).  If there's no index collation then
     214              :          * specify default collation in case the support functions need
     215              :          * collation.  This is harmless if the support functions don't care
     216              :          * about collation, so we just do it unconditionally.  (We could
     217              :          * alternatively call get_typcollation, but that seems like expensive
     218              :          * overkill --- there aren't going to be any cases where a GIN storage
     219              :          * type has a nondefault collation.)
     220              :          */
     221         2899 :         if (OidIsValid(index->rd_indcollation[i]))
     222          203 :             state->supportCollation[i] = index->rd_indcollation[i];
     223              :         else
     224         2696 :             state->supportCollation[i] = DEFAULT_COLLATION_OID;
     225              :     }
     226         2754 : }
     227              : 
     228              : /*
     229              :  * Extract attribute (column) number of stored entry from GIN tuple
     230              :  */
     231              : OffsetNumber
     232      8613108 : gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple)
     233              : {
     234              :     OffsetNumber colN;
     235              : 
     236      8613108 :     if (ginstate->oneCol)
     237              :     {
     238              :         /* column number is not stored explicitly */
     239      4283603 :         colN = FirstOffsetNumber;
     240              :     }
     241              :     else
     242              :     {
     243              :         Datum       res;
     244              :         bool        isnull;
     245              : 
     246              :         /*
     247              :          * First attribute is always int16, so we can safely use any tuple
     248              :          * descriptor to obtain first attribute of tuple
     249              :          */
     250      4329505 :         res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0],
     251              :                             &isnull);
     252              :         Assert(!isnull);
     253              : 
     254      4329505 :         colN = DatumGetUInt16(res);
     255              :         Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts);
     256              :     }
     257              : 
     258      8613108 :     return colN;
     259              : }
     260              : 
     261              : /*
     262              :  * Extract stored datum (and possible null category) from GIN tuple
     263              :  */
     264              : Datum
     265      6449432 : gintuple_get_key(GinState *ginstate, IndexTuple tuple,
     266              :                  GinNullCategory *category)
     267              : {
     268              :     Datum       res;
     269              :     bool        isnull;
     270              : 
     271      6449432 :     if (ginstate->oneCol)
     272              :     {
     273              :         /*
     274              :          * Single column index doesn't store attribute numbers in tuples
     275              :          */
     276      4285041 :         res = index_getattr(tuple, FirstOffsetNumber, ginstate->origTupdesc,
     277              :                             &isnull);
     278              :     }
     279              :     else
     280              :     {
     281              :         /*
     282              :          * Since the datum type depends on which index column it's from, we
     283              :          * must be careful to use the right tuple descriptor here.
     284              :          */
     285      2164391 :         OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple);
     286              : 
     287      2164391 :         res = index_getattr(tuple, OffsetNumberNext(FirstOffsetNumber),
     288      2164391 :                             ginstate->tupdesc[colN - 1],
     289              :                             &isnull);
     290              :     }
     291              : 
     292      6449432 :     if (isnull)
     293          900 :         *category = GinGetNullCategory(tuple, ginstate);
     294              :     else
     295      6448532 :         *category = GIN_CAT_NORM_KEY;
     296              : 
     297      6449432 :     return res;
     298              : }
     299              : 
     300              : /*
     301              :  * Allocate a new page (either by recycling, or by extending the index file)
     302              :  * The returned buffer is already pinned and exclusive-locked
     303              :  * Caller is responsible for initializing the page by calling GinInitBuffer
     304              :  */
     305              : Buffer
     306         4637 : GinNewBuffer(Relation index)
     307              : {
     308              :     Buffer      buffer;
     309              : 
     310              :     /* First, try to get a page from FSM */
     311              :     for (;;)
     312            0 :     {
     313         4637 :         BlockNumber blkno = GetFreeIndexPage(index);
     314              : 
     315         4637 :         if (blkno == InvalidBlockNumber)
     316         4584 :             break;
     317              : 
     318           53 :         buffer = ReadBuffer(index, blkno);
     319              : 
     320              :         /*
     321              :          * We have to guard against the possibility that someone else already
     322              :          * recycled this page; the buffer may be locked if so.
     323              :          */
     324           53 :         if (ConditionalLockBuffer(buffer))
     325              :         {
     326           53 :             if (GinPageIsRecyclable(BufferGetPage(buffer)))
     327           53 :                 return buffer;  /* OK to use */
     328              : 
     329            0 :             LockBuffer(buffer, GIN_UNLOCK);
     330              :         }
     331              : 
     332              :         /* Can't use it, so release buffer and try again */
     333            0 :         ReleaseBuffer(buffer);
     334              :     }
     335              : 
     336              :     /* Must extend the file */
     337         4584 :     buffer = ExtendBufferedRel(BMR_REL(index), MAIN_FORKNUM, NULL,
     338              :                                EB_LOCK_FIRST);
     339              : 
     340         4584 :     return buffer;
     341              : }
     342              : 
     343              : void
     344        31541 : GinInitPage(Page page, uint32 f, Size pageSize)
     345              : {
     346              :     GinPageOpaque opaque;
     347              : 
     348        31541 :     PageInit(page, pageSize, sizeof(GinPageOpaqueData));
     349              : 
     350        31541 :     opaque = GinPageGetOpaque(page);
     351        31541 :     opaque->flags = f;
     352        31541 :     opaque->rightlink = InvalidBlockNumber;
     353        31541 : }
     354              : 
     355              : void
     356         2036 : GinInitBuffer(Buffer b, uint32 f)
     357              : {
     358         2036 :     GinInitPage(BufferGetPage(b), f, BufferGetPageSize(b));
     359         2036 : }
     360              : 
     361              : void
     362        24157 : GinInitMetabuffer(Buffer b)
     363              : {
     364              :     GinMetaPageData *metadata;
     365        24157 :     Page        page = BufferGetPage(b);
     366              : 
     367        24157 :     GinInitPage(page, GIN_META, BufferGetPageSize(b));
     368              : 
     369        24157 :     metadata = GinPageGetMeta(page);
     370              : 
     371        24157 :     metadata->head = metadata->tail = InvalidBlockNumber;
     372        24157 :     metadata->tailFreeSize = 0;
     373        24157 :     metadata->nPendingPages = 0;
     374        24157 :     metadata->nPendingHeapTuples = 0;
     375        24157 :     metadata->nTotalPages = 0;
     376        24157 :     metadata->nEntryPages = 0;
     377        24157 :     metadata->nDataPages = 0;
     378        24157 :     metadata->nEntries = 0;
     379        24157 :     metadata->ginVersion = GIN_CURRENT_VERSION;
     380              : 
     381              :     /*
     382              :      * Set pd_lower just past the end of the metadata.  This is essential,
     383              :      * because without doing so, metadata will be lost if xlog.c compresses
     384              :      * the page.
     385              :      */
     386        24157 :     ((PageHeader) page)->pd_lower =
     387        24157 :         ((char *) metadata + sizeof(GinMetaPageData)) - (char *) page;
     388        24157 : }
     389              : 
     390              : /*
     391              :  * Support for sorting key datums in ginExtractEntries
     392              :  *
     393              :  * Note: we only have to worry about null and not-null keys here;
     394              :  * ginExtractEntries never generates more than one placeholder null,
     395              :  * so it doesn't have to sort those.
     396              :  */
     397              : typedef struct
     398              : {
     399              :     Datum       datum;
     400              :     bool        isnull;
     401              : } keyEntryData;
     402              : 
     403              : typedef struct
     404              : {
     405              :     FmgrInfo   *cmpDatumFunc;
     406              :     Oid         collation;
     407              :     bool        haveDups;
     408              : } cmpEntriesArg;
     409              : 
     410              : static int
     411      2075394 : cmpEntries(const void *a, const void *b, void *arg)
     412              : {
     413      2075394 :     const keyEntryData *aa = (const keyEntryData *) a;
     414      2075394 :     const keyEntryData *bb = (const keyEntryData *) b;
     415      2075394 :     cmpEntriesArg *data = (cmpEntriesArg *) arg;
     416              :     int         res;
     417              : 
     418      2075394 :     if (aa->isnull)
     419              :     {
     420            0 :         if (bb->isnull)
     421            0 :             res = 0;            /* NULL "=" NULL */
     422              :         else
     423            0 :             res = 1;            /* NULL ">" not-NULL */
     424              :     }
     425      2075394 :     else if (bb->isnull)
     426            0 :         res = -1;               /* not-NULL "<" NULL */
     427              :     else
     428      2075394 :         res = DatumGetInt32(FunctionCall2Coll(data->cmpDatumFunc,
     429              :                                               data->collation,
     430      2075394 :                                               aa->datum, bb->datum));
     431              : 
     432              :     /*
     433              :      * Detect if we have any duplicates.  If there are equal keys, qsort must
     434              :      * compare them at some point, else it wouldn't know whether one should go
     435              :      * before or after the other.
     436              :      */
     437      2075394 :     if (res == 0)
     438        36173 :         data->haveDups = true;
     439              : 
     440      2075394 :     return res;
     441              : }
     442              : 
     443              : 
     444              : /*
     445              :  * Extract the index key values from an indexable item
     446              :  *
     447              :  * The resulting key values are sorted, and any duplicates are removed.
     448              :  * This avoids generating redundant index entries.
     449              :  */
     450              : Datum *
     451       690484 : ginExtractEntries(GinState *ginstate, OffsetNumber attnum,
     452              :                   Datum value, bool isNull,
     453              :                   int32 *nentries, GinNullCategory **categories)
     454              : {
     455              :     Datum      *entries;
     456              :     bool       *nullFlags;
     457              :     int32       i;
     458              : 
     459              :     /*
     460              :      * We don't call the extractValueFn on a null item.  Instead generate a
     461              :      * placeholder.
     462              :      */
     463       690484 :     if (isNull)
     464              :     {
     465         3311 :         *nentries = 1;
     466         3311 :         entries = palloc_object(Datum);
     467         3311 :         entries[0] = (Datum) 0;
     468         3311 :         *categories = palloc_object(GinNullCategory);
     469         3311 :         (*categories)[0] = GIN_CAT_NULL_ITEM;
     470         3311 :         return entries;
     471              :     }
     472              : 
     473              :     /* OK, call the opclass's extractValueFn */
     474       687173 :     nullFlags = NULL;           /* in case extractValue doesn't set it */
     475              :     entries = (Datum *)
     476      1374346 :         DatumGetPointer(FunctionCall3Coll(&ginstate->extractValueFn[attnum - 1],
     477       687173 :                                           ginstate->supportCollation[attnum - 1],
     478              :                                           value,
     479              :                                           PointerGetDatum(nentries),
     480              :                                           PointerGetDatum(&nullFlags)));
     481              : 
     482              :     /*
     483              :      * Generate a placeholder if the item contained no keys.
     484              :      */
     485       687173 :     if (entries == NULL || *nentries <= 0)
     486              :     {
     487          888 :         *nentries = 1;
     488          888 :         entries = palloc_object(Datum);
     489          888 :         entries[0] = (Datum) 0;
     490          888 :         *categories = palloc_object(GinNullCategory);
     491          888 :         (*categories)[0] = GIN_CAT_EMPTY_ITEM;
     492          888 :         return entries;
     493              :     }
     494              : 
     495              :     /*
     496              :      * If the extractValueFn didn't create a nullFlags array, create one,
     497              :      * assuming that everything's non-null.
     498              :      */
     499       686285 :     if (nullFlags == NULL)
     500       116997 :         nullFlags = (bool *) palloc0(*nentries * sizeof(bool));
     501              : 
     502              :     /*
     503              :      * If there's more than one key, sort and unique-ify.
     504              :      *
     505              :      * XXX Using qsort here is notationally painful, and the overhead is
     506              :      * pretty bad too.  For small numbers of keys it'd likely be better to use
     507              :      * a simple insertion sort.
     508              :      */
     509       686285 :     if (*nentries > 1)
     510              :     {
     511              :         keyEntryData *keydata;
     512              :         cmpEntriesArg arg;
     513              : 
     514       295403 :         keydata = palloc_array(keyEntryData, *nentries);
     515      1646440 :         for (i = 0; i < *nentries; i++)
     516              :         {
     517      1351037 :             keydata[i].datum = entries[i];
     518      1351037 :             keydata[i].isnull = nullFlags[i];
     519              :         }
     520              : 
     521       295403 :         arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1];
     522       295403 :         arg.collation = ginstate->supportCollation[attnum - 1];
     523       295403 :         arg.haveDups = false;
     524       295403 :         qsort_arg(keydata, *nentries, sizeof(keyEntryData),
     525              :                   cmpEntries, &arg);
     526              : 
     527       295403 :         if (arg.haveDups)
     528              :         {
     529              :             /* there are duplicates, must get rid of 'em */
     530              :             int32       j;
     531              : 
     532        13961 :             entries[0] = keydata[0].datum;
     533        13961 :             nullFlags[0] = keydata[0].isnull;
     534        13961 :             j = 1;
     535       105761 :             for (i = 1; i < *nentries; i++)
     536              :             {
     537        91800 :                 if (cmpEntries(&keydata[i - 1], &keydata[i], &arg) != 0)
     538              :                 {
     539        74215 :                     entries[j] = keydata[i].datum;
     540        74215 :                     nullFlags[j] = keydata[i].isnull;
     541        74215 :                     j++;
     542              :                 }
     543              :             }
     544        13961 :             *nentries = j;
     545              :         }
     546              :         else
     547              :         {
     548              :             /* easy, no duplicates */
     549      1526718 :             for (i = 0; i < *nentries; i++)
     550              :             {
     551      1245276 :                 entries[i] = keydata[i].datum;
     552      1245276 :                 nullFlags[i] = keydata[i].isnull;
     553              :             }
     554              :         }
     555              : 
     556       295403 :         pfree(keydata);
     557              :     }
     558              : 
     559              :     /*
     560              :      * Create GinNullCategory representation from nullFlags.
     561              :      */
     562       686285 :     *categories = (GinNullCategory *) palloc0(*nentries * sizeof(GinNullCategory));
     563      2410619 :     for (i = 0; i < *nentries; i++)
     564      1724334 :         (*categories)[i] = (nullFlags[i] ? GIN_CAT_NULL_KEY : GIN_CAT_NORM_KEY);
     565              : 
     566       686285 :     return entries;
     567              : }
     568              : 
     569              : bytea *
     570          308 : ginoptions(Datum reloptions, bool validate)
     571              : {
     572              :     static const relopt_parse_elt tab[] = {
     573              :         {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)},
     574              :         {"gin_pending_list_limit", RELOPT_TYPE_INT, offsetof(GinOptions,
     575              :                                                              pendingListCleanupSize)}
     576              :     };
     577              : 
     578          308 :     return (bytea *) build_reloptions(reloptions, validate,
     579              :                                       RELOPT_KIND_GIN,
     580              :                                       sizeof(GinOptions),
     581              :                                       tab, lengthof(tab));
     582              : }
     583              : 
     584              : /*
     585              :  * Fetch index's statistical data into *stats
     586              :  *
     587              :  * Note: in the result, nPendingPages can be trusted to be up-to-date,
     588              :  * as can ginVersion; but the other fields are as of the last VACUUM.
     589              :  */
     590              : void
     591         1296 : ginGetStats(Relation index, GinStatsData *stats)
     592              : {
     593              :     Buffer      metabuffer;
     594              :     Page        metapage;
     595              :     GinMetaPageData *metadata;
     596              : 
     597         1296 :     metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
     598         1296 :     LockBuffer(metabuffer, GIN_SHARE);
     599         1296 :     metapage = BufferGetPage(metabuffer);
     600         1296 :     metadata = GinPageGetMeta(metapage);
     601              : 
     602         1296 :     stats->nPendingPages = metadata->nPendingPages;
     603         1296 :     stats->nTotalPages = metadata->nTotalPages;
     604         1296 :     stats->nEntryPages = metadata->nEntryPages;
     605         1296 :     stats->nDataPages = metadata->nDataPages;
     606         1296 :     stats->nEntries = metadata->nEntries;
     607         1296 :     stats->ginVersion = metadata->ginVersion;
     608              : 
     609         1296 :     UnlockReleaseBuffer(metabuffer);
     610         1296 : }
     611              : 
     612              : /*
     613              :  * Write the given statistics to the index's metapage
     614              :  *
     615              :  * Note: nPendingPages and ginVersion are *not* copied over
     616              :  */
     617              : void
     618          226 : ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build)
     619              : {
     620              :     Buffer      metabuffer;
     621              :     Page        metapage;
     622              :     GinMetaPageData *metadata;
     623              : 
     624          226 :     metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
     625          226 :     LockBuffer(metabuffer, GIN_EXCLUSIVE);
     626          226 :     metapage = BufferGetPage(metabuffer);
     627          226 :     metadata = GinPageGetMeta(metapage);
     628              : 
     629          226 :     START_CRIT_SECTION();
     630              : 
     631          226 :     metadata->nTotalPages = stats->nTotalPages;
     632          226 :     metadata->nEntryPages = stats->nEntryPages;
     633          226 :     metadata->nDataPages = stats->nDataPages;
     634          226 :     metadata->nEntries = stats->nEntries;
     635              : 
     636              :     /*
     637              :      * Set pd_lower just past the end of the metadata.  This is essential,
     638              :      * because without doing so, metadata will be lost if xlog.c compresses
     639              :      * the page.  (We must do this here because pre-v11 versions of PG did not
     640              :      * set the metapage's pd_lower correctly, so a pg_upgraded index might
     641              :      * contain the wrong value.)
     642              :      */
     643          226 :     ((PageHeader) metapage)->pd_lower =
     644          226 :         ((char *) metadata + sizeof(GinMetaPageData)) - (char *) metapage;
     645              : 
     646          226 :     MarkBufferDirty(metabuffer);
     647              : 
     648          226 :     if (RelationNeedsWAL(index) && !is_build)
     649              :     {
     650              :         XLogRecPtr  recptr;
     651              :         ginxlogUpdateMeta data;
     652              : 
     653           25 :         data.locator = index->rd_locator;
     654           25 :         data.ntuples = 0;
     655           25 :         data.newRightlink = data.prevTail = InvalidBlockNumber;
     656           25 :         memcpy(&data.metadata, metadata, sizeof(GinMetaPageData));
     657              : 
     658           25 :         XLogBeginInsert();
     659           25 :         XLogRegisterData(&data, sizeof(ginxlogUpdateMeta));
     660           25 :         XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT | REGBUF_STANDARD);
     661              : 
     662           25 :         recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE);
     663           25 :         PageSetLSN(metapage, recptr);
     664              :     }
     665              : 
     666          226 :     UnlockReleaseBuffer(metabuffer);
     667              : 
     668          226 :     END_CRIT_SECTION();
     669          226 : }
     670              : 
     671              : /*
     672              :  *  ginbuildphasename() -- Return name of index build phase.
     673              :  */
     674              : char *
     675            0 : ginbuildphasename(int64 phasenum)
     676              : {
     677            0 :     switch (phasenum)
     678              :     {
     679            0 :         case PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE:
     680            0 :             return "initializing";
     681            0 :         case PROGRESS_GIN_PHASE_INDEXBUILD_TABLESCAN:
     682            0 :             return "scanning table";
     683            0 :         case PROGRESS_GIN_PHASE_PERFORMSORT_1:
     684            0 :             return "sorting tuples (workers)";
     685            0 :         case PROGRESS_GIN_PHASE_MERGE_1:
     686            0 :             return "merging tuples (workers)";
     687            0 :         case PROGRESS_GIN_PHASE_PERFORMSORT_2:
     688            0 :             return "sorting tuples";
     689            0 :         case PROGRESS_GIN_PHASE_MERGE_2:
     690            0 :             return "merging tuples";
     691            0 :         default:
     692            0 :             return NULL;
     693              :     }
     694              : }
        

Generated by: LCOV version 2.0-1