LCOV - code coverage report
Current view: top level - src/backend/utils/cache - ts_cache.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 196 217 90.3 %
Date: 2021-12-09 04:09:06 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * ts_cache.c
       4             :  *    Tsearch related object caches.
       5             :  *
       6             :  * Tsearch performance is very sensitive to performance of parsers,
       7             :  * dictionaries and mapping, so lookups should be cached as much
       8             :  * as possible.
       9             :  *
      10             :  * Once a backend has created a cache entry for a particular TS object OID,
      11             :  * the cache entry will exist for the life of the backend; hence it is
      12             :  * safe to hold onto a pointer to the cache entry while doing things that
      13             :  * might result in recognizing a cache invalidation.  Beware however that
      14             :  * subsidiary information might be deleted and reallocated somewhere else
      15             :  * if a cache inval and reval happens!  This does not look like it will be
      16             :  * a big problem as long as parser and dictionary methods do not attempt
      17             :  * any database access.
      18             :  *
      19             :  *
      20             :  * Copyright (c) 2006-2021, PostgreSQL Global Development Group
      21             :  *
      22             :  * IDENTIFICATION
      23             :  *    src/backend/utils/cache/ts_cache.c
      24             :  *
      25             :  *-------------------------------------------------------------------------
      26             :  */
      27             : #include "postgres.h"
      28             : 
      29             : #include "access/genam.h"
      30             : #include "access/htup_details.h"
      31             : #include "access/table.h"
      32             : #include "access/xact.h"
      33             : #include "catalog/namespace.h"
      34             : #include "catalog/pg_ts_config.h"
      35             : #include "catalog/pg_ts_config_map.h"
      36             : #include "catalog/pg_ts_dict.h"
      37             : #include "catalog/pg_ts_parser.h"
      38             : #include "catalog/pg_ts_template.h"
      39             : #include "commands/defrem.h"
      40             : #include "miscadmin.h"
      41             : #include "tsearch/ts_cache.h"
      42             : #include "utils/builtins.h"
      43             : #include "utils/catcache.h"
      44             : #include "utils/fmgroids.h"
      45             : #include "utils/inval.h"
      46             : #include "utils/lsyscache.h"
      47             : #include "utils/memutils.h"
      48             : #include "utils/regproc.h"
      49             : #include "utils/syscache.h"
      50             : 
      51             : 
      52             : /*
      53             :  * MAXTOKENTYPE/MAXDICTSPERTT are arbitrary limits on the workspace size
      54             :  * used in lookup_ts_config_cache().  We could avoid hardwiring a limit
      55             :  * by making the workspace dynamically enlargeable, but it seems unlikely
      56             :  * to be worth the trouble.
      57             :  */
      58             : #define MAXTOKENTYPE    256
      59             : #define MAXDICTSPERTT   100
      60             : 
      61             : 
      62             : static HTAB *TSParserCacheHash = NULL;
      63             : static TSParserCacheEntry *lastUsedParser = NULL;
      64             : 
      65             : static HTAB *TSDictionaryCacheHash = NULL;
      66             : static TSDictionaryCacheEntry *lastUsedDictionary = NULL;
      67             : 
      68             : static HTAB *TSConfigCacheHash = NULL;
      69             : static TSConfigCacheEntry *lastUsedConfig = NULL;
      70             : 
      71             : /*
      72             :  * GUC default_text_search_config, and a cache of the current config's OID
      73             :  */
      74             : char       *TSCurrentConfig = NULL;
      75             : 
      76             : static Oid  TSCurrentConfigCache = InvalidOid;
      77             : 
      78             : 
      79             : /*
      80             :  * We use this syscache callback to detect when a visible change to a TS
      81             :  * catalog entry has been made, by either our own backend or another one.
      82             :  *
      83             :  * In principle we could just flush the specific cache entry that changed,
      84             :  * but given that TS configuration changes are probably infrequent, it
      85             :  * doesn't seem worth the trouble to determine that; we just flush all the
      86             :  * entries of the related hash table.
      87             :  *
      88             :  * We can use the same function for all TS caches by passing the hash
      89             :  * table address as the "arg".
      90             :  */
      91             : static void
      92        1110 : InvalidateTSCacheCallBack(Datum arg, int cacheid, uint32 hashvalue)
      93             : {
      94        1110 :     HTAB       *hash = (HTAB *) DatumGetPointer(arg);
      95             :     HASH_SEQ_STATUS status;
      96             :     TSAnyCacheEntry *entry;
      97             : 
      98        1110 :     hash_seq_init(&status, hash);
      99        3428 :     while ((entry = (TSAnyCacheEntry *) hash_seq_search(&status)) != NULL)
     100        2318 :         entry->isvalid = false;
     101             : 
     102             :     /* Also invalidate the current-config cache if it's pg_ts_config */
     103        1110 :     if (hash == TSConfigCacheHash)
     104         984 :         TSCurrentConfigCache = InvalidOid;
     105        1110 : }
     106             : 
     107             : /*
     108             :  * Fetch parser cache entry
     109             :  */
     110             : TSParserCacheEntry *
     111       44066 : lookup_ts_parser_cache(Oid prsId)
     112             : {
     113             :     TSParserCacheEntry *entry;
     114             : 
     115       44066 :     if (TSParserCacheHash == NULL)
     116             :     {
     117             :         /* First time through: initialize the hash table */
     118             :         HASHCTL     ctl;
     119             : 
     120         560 :         ctl.keysize = sizeof(Oid);
     121         560 :         ctl.entrysize = sizeof(TSParserCacheEntry);
     122         560 :         TSParserCacheHash = hash_create("Tsearch parser cache", 4,
     123             :                                         &ctl, HASH_ELEM | HASH_BLOBS);
     124             :         /* Flush cache on pg_ts_parser changes */
     125         560 :         CacheRegisterSyscacheCallback(TSPARSEROID, InvalidateTSCacheCallBack,
     126             :                                       PointerGetDatum(TSParserCacheHash));
     127             : 
     128             :         /* Also make sure CacheMemoryContext exists */
     129         560 :         if (!CacheMemoryContext)
     130           0 :             CreateCacheMemoryContext();
     131             :     }
     132             : 
     133             :     /* Check single-entry cache */
     134       44066 :     if (lastUsedParser && lastUsedParser->prsId == prsId &&
     135       43506 :         lastUsedParser->isvalid)
     136       43502 :         return lastUsedParser;
     137             : 
     138             :     /* Try to look up an existing entry */
     139         564 :     entry = (TSParserCacheEntry *) hash_search(TSParserCacheHash,
     140             :                                                (void *) &prsId,
     141             :                                                HASH_FIND, NULL);
     142         564 :     if (entry == NULL || !entry->isvalid)
     143             :     {
     144             :         /*
     145             :          * If we didn't find one, we want to make one. But first look up the
     146             :          * object to be sure the OID is real.
     147             :          */
     148             :         HeapTuple   tp;
     149             :         Form_pg_ts_parser prs;
     150             : 
     151         564 :         tp = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId));
     152         564 :         if (!HeapTupleIsValid(tp))
     153           0 :             elog(ERROR, "cache lookup failed for text search parser %u",
     154             :                  prsId);
     155         564 :         prs = (Form_pg_ts_parser) GETSTRUCT(tp);
     156             : 
     157             :         /*
     158             :          * Sanity checks
     159             :          */
     160         564 :         if (!OidIsValid(prs->prsstart))
     161           0 :             elog(ERROR, "text search parser %u has no prsstart method", prsId);
     162         564 :         if (!OidIsValid(prs->prstoken))
     163           0 :             elog(ERROR, "text search parser %u has no prstoken method", prsId);
     164         564 :         if (!OidIsValid(prs->prsend))
     165           0 :             elog(ERROR, "text search parser %u has no prsend method", prsId);
     166             : 
     167         564 :         if (entry == NULL)
     168             :         {
     169             :             bool        found;
     170             : 
     171             :             /* Now make the cache entry */
     172             :             entry = (TSParserCacheEntry *)
     173         560 :                 hash_search(TSParserCacheHash,
     174             :                             (void *) &prsId,
     175             :                             HASH_ENTER, &found);
     176             :             Assert(!found);     /* it wasn't there a moment ago */
     177             :         }
     178             : 
     179       16356 :         MemSet(entry, 0, sizeof(TSParserCacheEntry));
     180         564 :         entry->prsId = prsId;
     181         564 :         entry->startOid = prs->prsstart;
     182         564 :         entry->tokenOid = prs->prstoken;
     183         564 :         entry->endOid = prs->prsend;
     184         564 :         entry->headlineOid = prs->prsheadline;
     185         564 :         entry->lextypeOid = prs->prslextype;
     186             : 
     187         564 :         ReleaseSysCache(tp);
     188             : 
     189         564 :         fmgr_info_cxt(entry->startOid, &entry->prsstart, CacheMemoryContext);
     190         564 :         fmgr_info_cxt(entry->tokenOid, &entry->prstoken, CacheMemoryContext);
     191         564 :         fmgr_info_cxt(entry->endOid, &entry->prsend, CacheMemoryContext);
     192         564 :         if (OidIsValid(entry->headlineOid))
     193         564 :             fmgr_info_cxt(entry->headlineOid, &entry->prsheadline,
     194             :                           CacheMemoryContext);
     195             : 
     196         564 :         entry->isvalid = true;
     197             :     }
     198             : 
     199         564 :     lastUsedParser = entry;
     200             : 
     201         564 :     return entry;
     202             : }
     203             : 
     204             : /*
     205             :  * Fetch dictionary cache entry
     206             :  */
     207             : TSDictionaryCacheEntry *
     208        8922 : lookup_ts_dictionary_cache(Oid dictId)
     209             : {
     210             :     TSDictionaryCacheEntry *entry;
     211             : 
     212        8922 :     if (TSDictionaryCacheHash == NULL)
     213             :     {
     214             :         /* First time through: initialize the hash table */
     215             :         HASHCTL     ctl;
     216             : 
     217          30 :         ctl.keysize = sizeof(Oid);
     218          30 :         ctl.entrysize = sizeof(TSDictionaryCacheEntry);
     219          30 :         TSDictionaryCacheHash = hash_create("Tsearch dictionary cache", 8,
     220             :                                             &ctl, HASH_ELEM | HASH_BLOBS);
     221             :         /* Flush cache on pg_ts_dict and pg_ts_template changes */
     222          30 :         CacheRegisterSyscacheCallback(TSDICTOID, InvalidateTSCacheCallBack,
     223             :                                       PointerGetDatum(TSDictionaryCacheHash));
     224          30 :         CacheRegisterSyscacheCallback(TSTEMPLATEOID, InvalidateTSCacheCallBack,
     225             :                                       PointerGetDatum(TSDictionaryCacheHash));
     226             : 
     227             :         /* Also make sure CacheMemoryContext exists */
     228          30 :         if (!CacheMemoryContext)
     229           0 :             CreateCacheMemoryContext();
     230             :     }
     231             : 
     232             :     /* Check single-entry cache */
     233        8922 :     if (lastUsedDictionary && lastUsedDictionary->dictId == dictId &&
     234        7284 :         lastUsedDictionary->isvalid)
     235        7254 :         return lastUsedDictionary;
     236             : 
     237             :     /* Try to look up an existing entry */
     238        1668 :     entry = (TSDictionaryCacheEntry *) hash_search(TSDictionaryCacheHash,
     239             :                                                    (void *) &dictId,
     240             :                                                    HASH_FIND, NULL);
     241        1668 :     if (entry == NULL || !entry->isvalid)
     242             :     {
     243             :         /*
     244             :          * If we didn't find one, we want to make one. But first look up the
     245             :          * object to be sure the OID is real.
     246             :          */
     247             :         HeapTuple   tpdict,
     248             :                     tptmpl;
     249             :         Form_pg_ts_dict dict;
     250             :         Form_pg_ts_template template;
     251             :         MemoryContext saveCtx;
     252             : 
     253         120 :         tpdict = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(dictId));
     254         120 :         if (!HeapTupleIsValid(tpdict))
     255           0 :             elog(ERROR, "cache lookup failed for text search dictionary %u",
     256             :                  dictId);
     257         120 :         dict = (Form_pg_ts_dict) GETSTRUCT(tpdict);
     258             : 
     259             :         /*
     260             :          * Sanity checks
     261             :          */
     262         120 :         if (!OidIsValid(dict->dicttemplate))
     263           0 :             elog(ERROR, "text search dictionary %u has no template", dictId);
     264             : 
     265             :         /*
     266             :          * Retrieve dictionary's template
     267             :          */
     268         120 :         tptmpl = SearchSysCache1(TSTEMPLATEOID,
     269         120 :                                  ObjectIdGetDatum(dict->dicttemplate));
     270         120 :         if (!HeapTupleIsValid(tptmpl))
     271           0 :             elog(ERROR, "cache lookup failed for text search template %u",
     272             :                  dict->dicttemplate);
     273         120 :         template = (Form_pg_ts_template) GETSTRUCT(tptmpl);
     274             : 
     275             :         /*
     276             :          * Sanity checks
     277             :          */
     278         120 :         if (!OidIsValid(template->tmpllexize))
     279           0 :             elog(ERROR, "text search template %u has no lexize method",
     280             :                  template->tmpllexize);
     281             : 
     282         120 :         if (entry == NULL)
     283             :         {
     284             :             bool        found;
     285             : 
     286             :             /* Now make the cache entry */
     287             :             entry = (TSDictionaryCacheEntry *)
     288          70 :                 hash_search(TSDictionaryCacheHash,
     289             :                             (void *) &dictId,
     290             :                             HASH_ENTER, &found);
     291             :             Assert(!found);     /* it wasn't there a moment ago */
     292             : 
     293             :             /* Create private memory context the first time through */
     294          70 :             saveCtx = AllocSetContextCreate(CacheMemoryContext,
     295             :                                             "TS dictionary",
     296             :                                             ALLOCSET_SMALL_SIZES);
     297          70 :             MemoryContextCopyAndSetIdentifier(saveCtx, NameStr(dict->dictname));
     298             :         }
     299             :         else
     300             :         {
     301             :             /* Clear the existing entry's private context */
     302          50 :             saveCtx = entry->dictCtx;
     303             :             /* Don't let context's ident pointer dangle while we reset it */
     304          50 :             MemoryContextSetIdentifier(saveCtx, NULL);
     305          50 :             MemoryContextReset(saveCtx);
     306          50 :             MemoryContextCopyAndSetIdentifier(saveCtx, NameStr(dict->dictname));
     307             :         }
     308             : 
     309        1320 :         MemSet(entry, 0, sizeof(TSDictionaryCacheEntry));
     310         120 :         entry->dictId = dictId;
     311         120 :         entry->dictCtx = saveCtx;
     312             : 
     313         120 :         entry->lexizeOid = template->tmpllexize;
     314             : 
     315         120 :         if (OidIsValid(template->tmplinit))
     316             :         {
     317             :             List       *dictoptions;
     318             :             Datum       opt;
     319             :             bool        isnull;
     320             :             MemoryContext oldcontext;
     321             : 
     322             :             /*
     323             :              * Init method runs in dictionary's private memory context, and we
     324             :              * make sure the options are stored there too
     325             :              */
     326         120 :             oldcontext = MemoryContextSwitchTo(entry->dictCtx);
     327             : 
     328         120 :             opt = SysCacheGetAttr(TSDICTOID, tpdict,
     329             :                                   Anum_pg_ts_dict_dictinitoption,
     330             :                                   &isnull);
     331         120 :             if (isnull)
     332          20 :                 dictoptions = NIL;
     333             :             else
     334         100 :                 dictoptions = deserialize_deflist(opt);
     335             : 
     336         120 :             entry->dictData =
     337         120 :                 DatumGetPointer(OidFunctionCall1(template->tmplinit,
     338             :                                                  PointerGetDatum(dictoptions)));
     339             : 
     340         120 :             MemoryContextSwitchTo(oldcontext);
     341             :         }
     342             : 
     343         120 :         ReleaseSysCache(tptmpl);
     344         120 :         ReleaseSysCache(tpdict);
     345             : 
     346         120 :         fmgr_info_cxt(entry->lexizeOid, &entry->lexize, entry->dictCtx);
     347             : 
     348         120 :         entry->isvalid = true;
     349             :     }
     350             : 
     351        1668 :     lastUsedDictionary = entry;
     352             : 
     353        1668 :     return entry;
     354             : }
     355             : 
     356             : /*
     357             :  * Initialize config cache and prepare callbacks.  This is split out of
     358             :  * lookup_ts_config_cache because we need to activate the callback before
     359             :  * caching TSCurrentConfigCache, too.
     360             :  */
     361             : static void
     362          22 : init_ts_config_cache(void)
     363             : {
     364             :     HASHCTL     ctl;
     365             : 
     366          22 :     ctl.keysize = sizeof(Oid);
     367          22 :     ctl.entrysize = sizeof(TSConfigCacheEntry);
     368          22 :     TSConfigCacheHash = hash_create("Tsearch configuration cache", 16,
     369             :                                     &ctl, HASH_ELEM | HASH_BLOBS);
     370             :     /* Flush cache on pg_ts_config and pg_ts_config_map changes */
     371          22 :     CacheRegisterSyscacheCallback(TSCONFIGOID, InvalidateTSCacheCallBack,
     372             :                                   PointerGetDatum(TSConfigCacheHash));
     373          22 :     CacheRegisterSyscacheCallback(TSCONFIGMAP, InvalidateTSCacheCallBack,
     374             :                                   PointerGetDatum(TSConfigCacheHash));
     375             : 
     376             :     /* Also make sure CacheMemoryContext exists */
     377          22 :     if (!CacheMemoryContext)
     378           0 :         CreateCacheMemoryContext();
     379          22 : }
     380             : 
     381             : /*
     382             :  * Fetch configuration cache entry
     383             :  */
     384             : TSConfigCacheEntry *
     385        3146 : lookup_ts_config_cache(Oid cfgId)
     386             : {
     387             :     TSConfigCacheEntry *entry;
     388             : 
     389        3146 :     if (TSConfigCacheHash == NULL)
     390             :     {
     391             :         /* First time through: initialize the hash table */
     392          14 :         init_ts_config_cache();
     393             :     }
     394             : 
     395             :     /* Check single-entry cache */
     396        3146 :     if (lastUsedConfig && lastUsedConfig->cfgId == cfgId &&
     397        3052 :         lastUsedConfig->isvalid)
     398        3044 :         return lastUsedConfig;
     399             : 
     400             :     /* Try to look up an existing entry */
     401         102 :     entry = (TSConfigCacheEntry *) hash_search(TSConfigCacheHash,
     402             :                                                (void *) &cfgId,
     403             :                                                HASH_FIND, NULL);
     404         102 :     if (entry == NULL || !entry->isvalid)
     405             :     {
     406             :         /*
     407             :          * If we didn't find one, we want to make one. But first look up the
     408             :          * object to be sure the OID is real.
     409             :          */
     410             :         HeapTuple   tp;
     411             :         Form_pg_ts_config cfg;
     412             :         Relation    maprel;
     413             :         Relation    mapidx;
     414             :         ScanKeyData mapskey;
     415             :         SysScanDesc mapscan;
     416             :         HeapTuple   maptup;
     417             :         ListDictionary maplists[MAXTOKENTYPE + 1];
     418             :         Oid         mapdicts[MAXDICTSPERTT];
     419             :         int         maxtokentype;
     420             :         int         ndicts;
     421             :         int         i;
     422             : 
     423          58 :         tp = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(cfgId));
     424          58 :         if (!HeapTupleIsValid(tp))
     425           0 :             elog(ERROR, "cache lookup failed for text search configuration %u",
     426             :                  cfgId);
     427          58 :         cfg = (Form_pg_ts_config) GETSTRUCT(tp);
     428             : 
     429             :         /*
     430             :          * Sanity checks
     431             :          */
     432          58 :         if (!OidIsValid(cfg->cfgparser))
     433           0 :             elog(ERROR, "text search configuration %u has no parser", cfgId);
     434             : 
     435          58 :         if (entry == NULL)
     436             :         {
     437             :             bool        found;
     438             : 
     439             :             /* Now make the cache entry */
     440             :             entry = (TSConfigCacheEntry *)
     441          50 :                 hash_search(TSConfigCacheHash,
     442             :                             (void *) &cfgId,
     443             :                             HASH_ENTER, &found);
     444             :             Assert(!found);     /* it wasn't there a moment ago */
     445             :         }
     446             :         else
     447             :         {
     448             :             /* Cleanup old contents */
     449           8 :             if (entry->map)
     450             :             {
     451         192 :                 for (i = 0; i < entry->lenmap; i++)
     452         184 :                     if (entry->map[i].dictIds)
     453         152 :                         pfree(entry->map[i].dictIds);
     454           8 :                 pfree(entry->map);
     455             :             }
     456             :         }
     457             : 
     458         232 :         MemSet(entry, 0, sizeof(TSConfigCacheEntry));
     459          58 :         entry->cfgId = cfgId;
     460          58 :         entry->prsId = cfg->cfgparser;
     461             : 
     462          58 :         ReleaseSysCache(tp);
     463             : 
     464             :         /*
     465             :          * Scan pg_ts_config_map to gather dictionary list for each token type
     466             :          *
     467             :          * Because the index is on (mapcfg, maptokentype, mapseqno), we will
     468             :          * see the entries in maptokentype order, and in mapseqno order for
     469             :          * each token type, even though we didn't explicitly ask for that.
     470             :          */
     471          58 :         MemSet(maplists, 0, sizeof(maplists));
     472          58 :         maxtokentype = 0;
     473          58 :         ndicts = 0;
     474             : 
     475          58 :         ScanKeyInit(&mapskey,
     476             :                     Anum_pg_ts_config_map_mapcfg,
     477             :                     BTEqualStrategyNumber, F_OIDEQ,
     478             :                     ObjectIdGetDatum(cfgId));
     479             : 
     480          58 :         maprel = table_open(TSConfigMapRelationId, AccessShareLock);
     481          58 :         mapidx = index_open(TSConfigMapIndexId, AccessShareLock);
     482          58 :         mapscan = systable_beginscan_ordered(maprel, mapidx,
     483             :                                              NULL, 1, &mapskey);
     484             : 
     485        1304 :         while ((maptup = systable_getnext_ordered(mapscan, ForwardScanDirection)) != NULL)
     486             :         {
     487        1246 :             Form_pg_ts_config_map cfgmap = (Form_pg_ts_config_map) GETSTRUCT(maptup);
     488        1246 :             int         toktype = cfgmap->maptokentype;
     489             : 
     490        1246 :             if (toktype <= 0 || toktype > MAXTOKENTYPE)
     491           0 :                 elog(ERROR, "maptokentype value %d is out of range", toktype);
     492        1246 :             if (toktype < maxtokentype)
     493           0 :                 elog(ERROR, "maptokentype entries are out of order");
     494        1246 :             if (toktype > maxtokentype)
     495             :             {
     496             :                 /* starting a new token type, but first save the prior data */
     497        1066 :                 if (ndicts > 0)
     498             :                 {
     499        1008 :                     maplists[maxtokentype].len = ndicts;
     500        1008 :                     maplists[maxtokentype].dictIds = (Oid *)
     501        1008 :                         MemoryContextAlloc(CacheMemoryContext,
     502             :                                            sizeof(Oid) * ndicts);
     503        1008 :                     memcpy(maplists[maxtokentype].dictIds, mapdicts,
     504             :                            sizeof(Oid) * ndicts);
     505             :                 }
     506        1066 :                 maxtokentype = toktype;
     507        1066 :                 mapdicts[0] = cfgmap->mapdict;
     508        1066 :                 ndicts = 1;
     509             :             }
     510             :             else
     511             :             {
     512             :                 /* continuing data for current token type */
     513         180 :                 if (ndicts >= MAXDICTSPERTT)
     514           0 :                     elog(ERROR, "too many pg_ts_config_map entries for one token type");
     515         180 :                 mapdicts[ndicts++] = cfgmap->mapdict;
     516             :             }
     517             :         }
     518             : 
     519          58 :         systable_endscan_ordered(mapscan);
     520          58 :         index_close(mapidx, AccessShareLock);
     521          58 :         table_close(maprel, AccessShareLock);
     522             : 
     523          58 :         if (ndicts > 0)
     524             :         {
     525             :             /* save the last token type's dictionaries */
     526          58 :             maplists[maxtokentype].len = ndicts;
     527          58 :             maplists[maxtokentype].dictIds = (Oid *)
     528          58 :                 MemoryContextAlloc(CacheMemoryContext,
     529             :                                    sizeof(Oid) * ndicts);
     530          58 :             memcpy(maplists[maxtokentype].dictIds, mapdicts,
     531             :                    sizeof(Oid) * ndicts);
     532             :             /* and save the overall map */
     533          58 :             entry->lenmap = maxtokentype + 1;
     534          58 :             entry->map = (ListDictionary *)
     535          58 :                 MemoryContextAlloc(CacheMemoryContext,
     536          58 :                                    sizeof(ListDictionary) * entry->lenmap);
     537          58 :             memcpy(entry->map, maplists,
     538          58 :                    sizeof(ListDictionary) * entry->lenmap);
     539             :         }
     540             : 
     541          58 :         entry->isvalid = true;
     542             :     }
     543             : 
     544         102 :     lastUsedConfig = entry;
     545             : 
     546         102 :     return entry;
     547             : }
     548             : 
     549             : 
     550             : /*---------------------------------------------------
     551             :  * GUC variable "default_text_search_config"
     552             :  *---------------------------------------------------
     553             :  */
     554             : 
     555             : Oid
     556         260 : getTSCurrentConfig(bool emitError)
     557             : {
     558             :     /* if we have a cached value, return it */
     559         260 :     if (OidIsValid(TSCurrentConfigCache))
     560         240 :         return TSCurrentConfigCache;
     561             : 
     562             :     /* fail if GUC hasn't been set up yet */
     563          20 :     if (TSCurrentConfig == NULL || *TSCurrentConfig == '\0')
     564             :     {
     565           0 :         if (emitError)
     566           0 :             elog(ERROR, "text search configuration isn't set");
     567             :         else
     568           0 :             return InvalidOid;
     569             :     }
     570             : 
     571          20 :     if (TSConfigCacheHash == NULL)
     572             :     {
     573             :         /* First time through: initialize the tsconfig inval callback */
     574           8 :         init_ts_config_cache();
     575             :     }
     576             : 
     577             :     /* Look up the config */
     578          20 :     TSCurrentConfigCache =
     579          20 :         get_ts_config_oid(stringToQualifiedNameList(TSCurrentConfig),
     580          20 :                           !emitError);
     581             : 
     582          20 :     return TSCurrentConfigCache;
     583             : }
     584             : 
     585             : /* GUC check_hook for default_text_search_config */
     586             : bool
     587        8706 : check_TSCurrentConfig(char **newval, void **extra, GucSource source)
     588             : {
     589             :     /*
     590             :      * If we aren't inside a transaction, or connected to a database, we
     591             :      * cannot do the catalog accesses necessary to verify the config name.
     592             :      * Must accept it on faith.
     593             :      */
     594        8706 :     if (IsTransactionState() && MyDatabaseId != InvalidOid)
     595             :     {
     596             :         Oid         cfgId;
     597             :         HeapTuple   tuple;
     598             :         Form_pg_ts_config cfg;
     599             :         char       *buf;
     600             : 
     601        3410 :         cfgId = get_ts_config_oid(stringToQualifiedNameList(*newval), true);
     602             : 
     603             :         /*
     604             :          * When source == PGC_S_TEST, don't throw a hard error for a
     605             :          * nonexistent configuration, only a NOTICE.  See comments in guc.h.
     606             :          */
     607        3410 :         if (!OidIsValid(cfgId))
     608             :         {
     609          18 :             if (source == PGC_S_TEST)
     610             :             {
     611          10 :                 ereport(NOTICE,
     612             :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
     613             :                          errmsg("text search configuration \"%s\" does not exist", *newval)));
     614          10 :                 return true;
     615             :             }
     616             :             else
     617           8 :                 return false;
     618             :         }
     619             : 
     620             :         /*
     621             :          * Modify the actually stored value to be fully qualified, to ensure
     622             :          * later changes of search_path don't affect it.
     623             :          */
     624        3392 :         tuple = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(cfgId));
     625        3392 :         if (!HeapTupleIsValid(tuple))
     626           0 :             elog(ERROR, "cache lookup failed for text search configuration %u",
     627             :                  cfgId);
     628        3392 :         cfg = (Form_pg_ts_config) GETSTRUCT(tuple);
     629             : 
     630        3392 :         buf = quote_qualified_identifier(get_namespace_name(cfg->cfgnamespace),
     631        3392 :                                          NameStr(cfg->cfgname));
     632             : 
     633        3392 :         ReleaseSysCache(tuple);
     634             : 
     635             :         /* GUC wants it malloc'd not palloc'd */
     636        3392 :         free(*newval);
     637        3392 :         *newval = strdup(buf);
     638        3392 :         pfree(buf);
     639        3392 :         if (!*newval)
     640           0 :             return false;
     641             :     }
     642             : 
     643        8688 :     return true;
     644             : }
     645             : 
     646             : /* GUC assign_hook for default_text_search_config */
     647             : void
     648        8684 : assign_TSCurrentConfig(const char *newval, void *extra)
     649             : {
     650             :     /* Just reset the cache to force a lookup on first use */
     651        8684 :     TSCurrentConfigCache = InvalidOid;
     652        8684 : }

Generated by: LCOV version 1.14