PostgreSQL Source Code  git master
ts_cache.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_ts_config.h"
#include "catalog/pg_ts_config_map.h"
#include "catalog/pg_ts_dict.h"
#include "catalog/pg_ts_parser.h"
#include "catalog/pg_ts_template.h"
#include "commands/defrem.h"
#include "miscadmin.h"
#include "tsearch/ts_cache.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/regproc.h"
#include "utils/syscache.h"
Include dependency graph for ts_cache.c:

Go to the source code of this file.

Macros

#define MAXTOKENTYPE   256
 
#define MAXDICTSPERTT   100
 

Functions

static void InvalidateTSCacheCallBack (Datum arg, int cacheid, uint32 hashvalue)
 
TSParserCacheEntrylookup_ts_parser_cache (Oid prsId)
 
TSDictionaryCacheEntrylookup_ts_dictionary_cache (Oid dictId)
 
static void init_ts_config_cache (void)
 
TSConfigCacheEntrylookup_ts_config_cache (Oid cfgId)
 
Oid getTSCurrentConfig (bool emitError)
 
bool check_TSCurrentConfig (char **newval, void **extra, GucSource source)
 
void assign_TSCurrentConfig (const char *newval, void *extra)
 

Variables

static HTABTSParserCacheHash = NULL
 
static TSParserCacheEntrylastUsedParser = NULL
 
static HTABTSDictionaryCacheHash = NULL
 
static TSDictionaryCacheEntrylastUsedDictionary = NULL
 
static HTABTSConfigCacheHash = NULL
 
static TSConfigCacheEntrylastUsedConfig = NULL
 
char * TSCurrentConfig = NULL
 
static Oid TSCurrentConfigCache = InvalidOid
 

Macro Definition Documentation

◆ MAXDICTSPERTT

#define MAXDICTSPERTT   100

Definition at line 59 of file ts_cache.c.

Referenced by lookup_ts_config_cache().

◆ MAXTOKENTYPE

#define MAXTOKENTYPE   256

Definition at line 58 of file ts_cache.c.

Referenced by lookup_ts_config_cache().

Function Documentation

◆ assign_TSCurrentConfig()

void assign_TSCurrentConfig ( const char *  newval,
void *  extra 
)

Definition at line 648 of file ts_cache.c.

References InvalidOid, and TSCurrentConfigCache.

649 {
650  /* Just reset the cache to force a lookup on first use */
652 }
static Oid TSCurrentConfigCache
Definition: ts_cache.c:76
#define InvalidOid
Definition: postgres_ext.h:36

◆ check_TSCurrentConfig()

bool check_TSCurrentConfig ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 587 of file ts_cache.c.

References buf, elog, ereport, errcode(), errmsg(), ERROR, free, get_namespace_name(), get_ts_config_oid(), GETSTRUCT, HeapTupleIsValid, InvalidOid, IsTransactionState(), MyDatabaseId, NameStr, NOTICE, ObjectIdGetDatum, OidIsValid, pfree(), PGC_S_TEST, quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1(), stringToQualifiedNameList(), and TSCONFIGOID.

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  */
595  {
596  Oid cfgId;
597  HeapTuple tuple;
598  Form_pg_ts_config cfg;
599  char *buf;
600 
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  if (!OidIsValid(cfgId))
608  {
609  if (source == PGC_S_TEST)
610  {
611  ereport(NOTICE,
612  (errcode(ERRCODE_UNDEFINED_OBJECT),
613  errmsg("text search configuration \"%s\" does not exist", *newval)));
614  return true;
615  }
616  else
617  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  */
625  if (!HeapTupleIsValid(tuple))
626  elog(ERROR, "cache lookup failed for text search configuration %u",
627  cfgId);
628  cfg = (Form_pg_ts_config) GETSTRUCT(tuple);
629 
630  buf = quote_qualified_identifier(get_namespace_name(cfg->cfgnamespace),
631  NameStr(cfg->cfgname));
632 
633  ReleaseSysCache(tuple);
634 
635  /* GUC wants it malloc'd not palloc'd */
636  free(*newval);
637  *newval = strdup(buf);
638  pfree(buf);
639  if (!*newval)
640  return false;
641  }
642 
643  return true;
644 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_ts_config * Form_pg_ts_config
Definition: pg_ts_config.h:48
int errcode(int sqlerrcode)
Definition: elog.c:698
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:2678
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
static char * buf
Definition: pg_test_fsync.c:68
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:11407
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
#define NOTICE
Definition: elog.h:37
#define free(a)
Definition: header.h:65
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static rewind_source * source
Definition: pg_rewind.c:79
#define newval
bool IsTransactionState(void)
Definition: xact.c:371
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1876
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681

◆ getTSCurrentConfig()

Oid getTSCurrentConfig ( bool  emitError)

Definition at line 556 of file ts_cache.c.

References elog, ERROR, get_ts_config_oid(), init_ts_config_cache(), InvalidOid, OidIsValid, stringToQualifiedNameList(), TSCurrentConfig, and TSCurrentConfigCache.

Referenced by get_current_ts_config(), json_string_to_tsvector(), json_to_tsvector(), jsonb_string_to_tsvector(), jsonb_to_tsvector(), phraseto_tsquery(), plainto_tsquery(), to_tsquery(), to_tsvector(), ts_headline(), ts_headline_json(), ts_headline_json_opt(), ts_headline_jsonb(), ts_headline_jsonb_opt(), ts_headline_opt(), and websearch_to_tsquery().

557 {
558  /* if we have a cached value, return it */
560  return TSCurrentConfigCache;
561 
562  /* fail if GUC hasn't been set up yet */
563  if (TSCurrentConfig == NULL || *TSCurrentConfig == '\0')
564  {
565  if (emitError)
566  elog(ERROR, "text search configuration isn't set");
567  else
568  return InvalidOid;
569  }
570 
571  if (TSConfigCacheHash == NULL)
572  {
573  /* First time through: initialize the tsconfig inval callback */
575  }
576 
577  /* Look up the config */
580  !emitError);
581 
582  return TSCurrentConfigCache;
583 }
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:2678
#define OidIsValid(objectId)
Definition: c.h:710
char * TSCurrentConfig
Definition: ts_cache.c:74
static Oid TSCurrentConfigCache
Definition: ts_cache.c:76
#define ERROR
Definition: elog.h:46
#define InvalidOid
Definition: postgres_ext.h:36
static void init_ts_config_cache(void)
Definition: ts_cache.c:362
static HTAB * TSConfigCacheHash
Definition: ts_cache.c:68
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1876
#define elog(elevel,...)
Definition: elog.h:232

◆ init_ts_config_cache()

static void init_ts_config_cache ( void  )
static

Definition at line 362 of file ts_cache.c.

References CacheMemoryContext, CacheRegisterSyscacheCallback(), CreateCacheMemoryContext(), HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, InvalidateTSCacheCallBack(), HASHCTL::keysize, PointerGetDatum, TSCONFIGMAP, and TSCONFIGOID.

Referenced by getTSCurrentConfig(), and lookup_ts_config_cache().

363 {
364  HASHCTL ctl;
365 
366  ctl.keysize = sizeof(Oid);
367  ctl.entrysize = sizeof(TSConfigCacheEntry);
368  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 */
375 
376  /* Also make sure CacheMemoryContext exists */
377  if (!CacheMemoryContext)
379 }
#define HASH_ELEM
Definition: hsearch.h:95
#define PointerGetDatum(X)
Definition: postgres.h:600
Size entrysize
Definition: hsearch.h:76
unsigned int Oid
Definition: postgres_ext.h:31
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
static void InvalidateTSCacheCallBack(Datum arg, int cacheid, uint32 hashvalue)
Definition: ts_cache.c:92
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1434
Size keysize
Definition: hsearch.h:75
static HTAB * TSConfigCacheHash
Definition: ts_cache.c:68
void CreateCacheMemoryContext(void)
Definition: catcache.c:620
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ InvalidateTSCacheCallBack()

static void InvalidateTSCacheCallBack ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 92 of file ts_cache.c.

References DatumGetPointer, hash(), hash_seq_init(), hash_seq_search(), InvalidOid, TSAnyCacheEntry::isvalid, status(), and TSCurrentConfigCache.

Referenced by init_ts_config_cache(), lookup_ts_dictionary_cache(), and lookup_ts_parser_cache().

93 {
96  TSAnyCacheEntry *entry;
97 
98  hash_seq_init(&status, hash);
99  while ((entry = (TSAnyCacheEntry *) hash_seq_search(&status)) != NULL)
100  entry->isvalid = false;
101 
102  /* Also invalidate the current-config cache if it's pg_ts_config */
103  if (hash == TSConfigCacheHash)
105 }
static Oid TSCurrentConfigCache
Definition: ts_cache.c:76
Definition: dynahash.c:219
#define InvalidOid
Definition: postgres_ext.h:36
static HTAB * TSConfigCacheHash
Definition: ts_cache.c:68
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
#define DatumGetPointer(X)
Definition: postgres.h:593
void * arg
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:719

◆ lookup_ts_config_cache()

TSConfigCacheEntry* lookup_ts_config_cache ( Oid  cfgId)

Definition at line 385 of file ts_cache.c.

References AccessShareLock, Assert, BTEqualStrategyNumber, CacheMemoryContext, TSConfigCacheEntry::cfgId, ListDictionary::dictIds, elog, ERROR, ForwardScanDirection, GETSTRUCT, HASH_ENTER, HASH_FIND, hash_search(), HeapTupleIsValid, i, index_close(), index_open(), init_ts_config_cache(), TSConfigCacheEntry::isvalid, lastUsedConfig, ListDictionary::len, TSConfigCacheEntry::lenmap, TSConfigCacheEntry::map, MAXDICTSPERTT, MAXTOKENTYPE, MemoryContextAlloc(), MemSet, ObjectIdGetDatum, OidIsValid, pfree(), TSConfigCacheEntry::prsId, ReleaseSysCache(), ScanKeyInit(), SearchSysCache1(), systable_beginscan_ordered(), systable_endscan_ordered(), systable_getnext_ordered(), table_close(), table_open(), TSConfigMapIndexId, and TSCONFIGOID.

Referenced by hlparsetext(), parsetext(), ts_headline_byid_opt(), ts_headline_json_byid_opt(), and ts_headline_jsonb_byid_opt().

386 {
387  TSConfigCacheEntry *entry;
388 
389  if (TSConfigCacheHash == NULL)
390  {
391  /* First time through: initialize the hash table */
393  }
394 
395  /* Check single-entry cache */
396  if (lastUsedConfig && lastUsedConfig->cfgId == cfgId &&
398  return lastUsedConfig;
399 
400  /* Try to look up an existing entry */
402  (void *) &cfgId,
403  HASH_FIND, NULL);
404  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 
424  if (!HeapTupleIsValid(tp))
425  elog(ERROR, "cache lookup failed for text search configuration %u",
426  cfgId);
427  cfg = (Form_pg_ts_config) GETSTRUCT(tp);
428 
429  /*
430  * Sanity checks
431  */
432  if (!OidIsValid(cfg->cfgparser))
433  elog(ERROR, "text search configuration %u has no parser", cfgId);
434 
435  if (entry == NULL)
436  {
437  bool found;
438 
439  /* Now make the cache entry */
440  entry = (TSConfigCacheEntry *)
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  if (entry->map)
450  {
451  for (i = 0; i < entry->lenmap; i++)
452  if (entry->map[i].dictIds)
453  pfree(entry->map[i].dictIds);
454  pfree(entry->map);
455  }
456  }
457 
458  MemSet(entry, 0, sizeof(TSConfigCacheEntry));
459  entry->cfgId = cfgId;
460  entry->prsId = cfg->cfgparser;
461 
462  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  MemSet(maplists, 0, sizeof(maplists));
472  maxtokentype = 0;
473  ndicts = 0;
474 
475  ScanKeyInit(&mapskey,
476  Anum_pg_ts_config_map_mapcfg,
477  BTEqualStrategyNumber, F_OIDEQ,
478  ObjectIdGetDatum(cfgId));
479 
480  maprel = table_open(TSConfigMapRelationId, AccessShareLock);
482  mapscan = systable_beginscan_ordered(maprel, mapidx,
483  NULL, 1, &mapskey);
484 
485  while ((maptup = systable_getnext_ordered(mapscan, ForwardScanDirection)) != NULL)
486  {
488  int toktype = cfgmap->maptokentype;
489 
490  if (toktype <= 0 || toktype > MAXTOKENTYPE)
491  elog(ERROR, "maptokentype value %d is out of range", toktype);
492  if (toktype < maxtokentype)
493  elog(ERROR, "maptokentype entries are out of order");
494  if (toktype > maxtokentype)
495  {
496  /* starting a new token type, but first save the prior data */
497  if (ndicts > 0)
498  {
499  maplists[maxtokentype].len = ndicts;
500  maplists[maxtokentype].dictIds = (Oid *)
502  sizeof(Oid) * ndicts);
503  memcpy(maplists[maxtokentype].dictIds, mapdicts,
504  sizeof(Oid) * ndicts);
505  }
506  maxtokentype = toktype;
507  mapdicts[0] = cfgmap->mapdict;
508  ndicts = 1;
509  }
510  else
511  {
512  /* continuing data for current token type */
513  if (ndicts >= MAXDICTSPERTT)
514  elog(ERROR, "too many pg_ts_config_map entries for one token type");
515  mapdicts[ndicts++] = cfgmap->mapdict;
516  }
517  }
518 
519  systable_endscan_ordered(mapscan);
520  index_close(mapidx, AccessShareLock);
521  table_close(maprel, AccessShareLock);
522 
523  if (ndicts > 0)
524  {
525  /* save the last token type's dictionaries */
526  maplists[maxtokentype].len = ndicts;
527  maplists[maxtokentype].dictIds = (Oid *)
529  sizeof(Oid) * ndicts);
530  memcpy(maplists[maxtokentype].dictIds, mapdicts,
531  sizeof(Oid) * ndicts);
532  /* and save the overall map */
533  entry->lenmap = maxtokentype + 1;
534  entry->map = (ListDictionary *)
536  sizeof(ListDictionary) * entry->lenmap);
537  memcpy(entry->map, maplists,
538  sizeof(ListDictionary) * entry->lenmap);
539  }
540 
541  entry->isvalid = true;
542  }
543 
544  lastUsedConfig = entry;
545 
546  return entry;
547 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define MAXDICTSPERTT
Definition: ts_cache.c:59
FormData_pg_ts_config * Form_pg_ts_config
Definition: pg_ts_config.h:48
FormData_pg_ts_config_map * Form_pg_ts_config_map
#define AccessShareLock
Definition: lockdefs.h:36
#define MemSet(start, val, len)
Definition: c.h:1008
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
HeapTuple systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
Definition: genam.c:707
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define MAXTOKENTYPE
Definition: ts_cache.c:58
Oid * dictIds
Definition: ts_cache.h:68
ListDictionary * map
Definition: ts_cache.h:80
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
void systable_endscan_ordered(SysScanDesc sysscan)
Definition: genam.c:732
static void init_ts_config_cache(void)
Definition: ts_cache.c:362
#define TSConfigMapIndexId
static HTAB * TSConfigCacheHash
Definition: ts_cache.c:68
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
static TSConfigCacheEntry * lastUsedConfig
Definition: ts_cache.c:69
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
SysScanDesc systable_beginscan_ordered(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:642
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
#define elog(elevel,...)
Definition: elog.h:232
int i
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
#define BTEqualStrategyNumber
Definition: stratnum.h:31
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ lookup_ts_dictionary_cache()

TSDictionaryCacheEntry* lookup_ts_dictionary_cache ( Oid  dictId)

Definition at line 208 of file ts_cache.c.

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert, CacheMemoryContext, CacheRegisterSyscacheCallback(), CreateCacheMemoryContext(), DatumGetPointer, deserialize_deflist(), TSDictionaryCacheEntry::dictCtx, TSDictionaryCacheEntry::dictData, TSDictionaryCacheEntry::dictId, elog, HASHCTL::entrysize, ERROR, fmgr_info_cxt(), GETSTRUCT, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FIND, hash_search(), HeapTupleIsValid, InvalidateTSCacheCallBack(), TSDictionaryCacheEntry::isvalid, HASHCTL::keysize, lastUsedDictionary, TSDictionaryCacheEntry::lexize, TSDictionaryCacheEntry::lexizeOid, MemoryContextCopyAndSetIdentifier, MemoryContextReset(), MemoryContextSetIdentifier(), MemoryContextSwitchTo(), MemSet, NameStr, NIL, ObjectIdGetDatum, OidFunctionCall1, OidIsValid, PointerGetDatum, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), TSDICTOID, and TSTEMPLATEOID.

Referenced by LexizeExec(), thesaurus_init(), thesaurus_lexize(), ts_lexize(), and unaccent_dict().

209 {
210  TSDictionaryCacheEntry *entry;
211 
212  if (TSDictionaryCacheHash == NULL)
213  {
214  /* First time through: initialize the hash table */
215  HASHCTL ctl;
216 
217  ctl.keysize = sizeof(Oid);
218  ctl.entrysize = sizeof(TSDictionaryCacheEntry);
219  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 */
226 
227  /* Also make sure CacheMemoryContext exists */
228  if (!CacheMemoryContext)
230  }
231 
232  /* Check single-entry cache */
233  if (lastUsedDictionary && lastUsedDictionary->dictId == dictId &&
235  return lastUsedDictionary;
236 
237  /* Try to look up an existing entry */
239  (void *) &dictId,
240  HASH_FIND, NULL);
241  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  tpdict = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(dictId));
254  if (!HeapTupleIsValid(tpdict))
255  elog(ERROR, "cache lookup failed for text search dictionary %u",
256  dictId);
257  dict = (Form_pg_ts_dict) GETSTRUCT(tpdict);
258 
259  /*
260  * Sanity checks
261  */
262  if (!OidIsValid(dict->dicttemplate))
263  elog(ERROR, "text search dictionary %u has no template", dictId);
264 
265  /*
266  * Retrieve dictionary's template
267  */
269  ObjectIdGetDatum(dict->dicttemplate));
270  if (!HeapTupleIsValid(tptmpl))
271  elog(ERROR, "cache lookup failed for text search template %u",
272  dict->dicttemplate);
273  template = (Form_pg_ts_template) GETSTRUCT(tptmpl);
274 
275  /*
276  * Sanity checks
277  */
278  if (!OidIsValid(template->tmpllexize))
279  elog(ERROR, "text search template %u has no lexize method",
280  template->tmpllexize);
281 
282  if (entry == NULL)
283  {
284  bool found;
285 
286  /* Now make the cache entry */
287  entry = (TSDictionaryCacheEntry *)
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 */
295  "TS dictionary",
297  MemoryContextCopyAndSetIdentifier(saveCtx, NameStr(dict->dictname));
298  }
299  else
300  {
301  /* Clear the existing entry's private context */
302  saveCtx = entry->dictCtx;
303  /* Don't let context's ident pointer dangle while we reset it */
304  MemoryContextSetIdentifier(saveCtx, NULL);
305  MemoryContextReset(saveCtx);
306  MemoryContextCopyAndSetIdentifier(saveCtx, NameStr(dict->dictname));
307  }
308 
309  MemSet(entry, 0, sizeof(TSDictionaryCacheEntry));
310  entry->dictId = dictId;
311  entry->dictCtx = saveCtx;
312 
313  entry->lexizeOid = template->tmpllexize;
314 
315  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  oldcontext = MemoryContextSwitchTo(entry->dictCtx);
327 
328  opt = SysCacheGetAttr(TSDICTOID, tpdict,
329  Anum_pg_ts_dict_dictinitoption,
330  &isnull);
331  if (isnull)
332  dictoptions = NIL;
333  else
334  dictoptions = deserialize_deflist(opt);
335 
336  entry->dictData =
337  DatumGetPointer(OidFunctionCall1(template->tmplinit,
338  PointerGetDatum(dictoptions)));
339 
340  MemoryContextSwitchTo(oldcontext);
341  }
342 
343  ReleaseSysCache(tptmpl);
344  ReleaseSysCache(tpdict);
345 
346  fmgr_info_cxt(entry->lexizeOid, &entry->lexize, entry->dictCtx);
347 
348  entry->isvalid = true;
349  }
350 
351  lastUsedDictionary = entry;
352 
353  return entry;
354 }
#define NIL
Definition: pg_list.h:65
#define AllocSetContextCreate
Definition: memutils.h:173
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define HASH_ELEM
Definition: hsearch.h:95
#define PointerGetDatum(X)
Definition: postgres.h:600
static TSDictionaryCacheEntry * lastUsedDictionary
Definition: ts_cache.c:66
List * deserialize_deflist(Datum txt)
Definition: tsearchcmds.c:1510
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:205
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:76
#define MemSet(start, val, len)
Definition: c.h:1008
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:143
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:664
FormData_pg_ts_dict * Form_pg_ts_dict
Definition: pg_ts_dict.h:52
#define MemoryContextCopyAndSetIdentifier(cxt, id)
Definition: memutils.h:98
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
static void InvalidateTSCacheCallBack(Datum arg, int cacheid, uint32 hashvalue)
Definition: ts_cache.c:92
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1434
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
Size keysize
Definition: hsearch.h:75
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
void CreateCacheMemoryContext(void)
Definition: catcache.c:620
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:336
#define DatumGetPointer(X)
Definition: postgres.h:593
FormData_pg_ts_template * Form_pg_ts_template
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
static HTAB * TSDictionaryCacheHash
Definition: ts_cache.c:65
struct TSDictionaryCacheEntry TSDictionaryCacheEntry
Definition: pg_list.h:50
MemoryContext dictCtx
Definition: ts_cache.h:61
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ lookup_ts_parser_cache()

TSParserCacheEntry* lookup_ts_parser_cache ( Oid  prsId)

Definition at line 111 of file ts_cache.c.

References Assert, CacheMemoryContext, CacheRegisterSyscacheCallback(), CreateCacheMemoryContext(), elog, TSParserCacheEntry::endOid, HASHCTL::entrysize, ERROR, fmgr_info_cxt(), GETSTRUCT, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FIND, hash_search(), TSParserCacheEntry::headlineOid, HeapTupleIsValid, InvalidateTSCacheCallBack(), TSParserCacheEntry::isvalid, HASHCTL::keysize, lastUsedParser, TSParserCacheEntry::lextypeOid, MemSet, ObjectIdGetDatum, OidIsValid, PointerGetDatum, TSParserCacheEntry::prsend, TSParserCacheEntry::prsheadline, TSParserCacheEntry::prsId, TSParserCacheEntry::prsstart, TSParserCacheEntry::prstoken, ReleaseSysCache(), SearchSysCache1(), TSParserCacheEntry::startOid, TSParserCacheEntry::tokenOid, and TSPARSEROID.

Referenced by getTokenTypes(), hlparsetext(), parsetext(), prs_setup_firstcall(), ts_headline_byid_opt(), ts_headline_json_byid_opt(), ts_headline_jsonb_byid_opt(), and tt_setup_firstcall().

112 {
113  TSParserCacheEntry *entry;
114 
115  if (TSParserCacheHash == NULL)
116  {
117  /* First time through: initialize the hash table */
118  HASHCTL ctl;
119 
120  ctl.keysize = sizeof(Oid);
121  ctl.entrysize = sizeof(TSParserCacheEntry);
122  TSParserCacheHash = hash_create("Tsearch parser cache", 4,
123  &ctl, HASH_ELEM | HASH_BLOBS);
124  /* Flush cache on pg_ts_parser changes */
127 
128  /* Also make sure CacheMemoryContext exists */
129  if (!CacheMemoryContext)
131  }
132 
133  /* Check single-entry cache */
134  if (lastUsedParser && lastUsedParser->prsId == prsId &&
136  return lastUsedParser;
137 
138  /* Try to look up an existing entry */
140  (void *) &prsId,
141  HASH_FIND, NULL);
142  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 
152  if (!HeapTupleIsValid(tp))
153  elog(ERROR, "cache lookup failed for text search parser %u",
154  prsId);
155  prs = (Form_pg_ts_parser) GETSTRUCT(tp);
156 
157  /*
158  * Sanity checks
159  */
160  if (!OidIsValid(prs->prsstart))
161  elog(ERROR, "text search parser %u has no prsstart method", prsId);
162  if (!OidIsValid(prs->prstoken))
163  elog(ERROR, "text search parser %u has no prstoken method", prsId);
164  if (!OidIsValid(prs->prsend))
165  elog(ERROR, "text search parser %u has no prsend method", prsId);
166 
167  if (entry == NULL)
168  {
169  bool found;
170 
171  /* Now make the cache entry */
172  entry = (TSParserCacheEntry *)
174  (void *) &prsId,
175  HASH_ENTER, &found);
176  Assert(!found); /* it wasn't there a moment ago */
177  }
178 
179  MemSet(entry, 0, sizeof(TSParserCacheEntry));
180  entry->prsId = prsId;
181  entry->startOid = prs->prsstart;
182  entry->tokenOid = prs->prstoken;
183  entry->endOid = prs->prsend;
184  entry->headlineOid = prs->prsheadline;
185  entry->lextypeOid = prs->prslextype;
186 
187  ReleaseSysCache(tp);
188 
191  fmgr_info_cxt(entry->endOid, &entry->prsend, CacheMemoryContext);
192  if (OidIsValid(entry->headlineOid))
193  fmgr_info_cxt(entry->headlineOid, &entry->prsheadline,
195 
196  entry->isvalid = true;
197  }
198 
199  lastUsedParser = entry;
200 
201  return entry;
202 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define HASH_ELEM
Definition: hsearch.h:95
#define PointerGetDatum(X)
Definition: postgres.h:600
Size entrysize
Definition: hsearch.h:76
#define MemSet(start, val, len)
Definition: c.h:1008
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
FmgrInfo prsend
Definition: ts_cache.h:47
static HTAB * TSParserCacheHash
Definition: ts_cache.c:62
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
static void InvalidateTSCacheCallBack(Datum arg, int cacheid, uint32 hashvalue)
Definition: ts_cache.c:92
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1434
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
FormData_pg_ts_parser * Form_pg_ts_parser
Definition: pg_ts_parser.h:55
Size keysize
Definition: hsearch.h:75
static TSParserCacheEntry * lastUsedParser
Definition: ts_cache.c:63
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
FmgrInfo prstoken
Definition: ts_cache.h:46
void CreateCacheMemoryContext(void)
Definition: catcache.c:620
FmgrInfo prsstart
Definition: ts_cache.h:45
#define elog(elevel,...)
Definition: elog.h:232
struct TSParserCacheEntry TSParserCacheEntry
MemoryContext CacheMemoryContext
Definition: mcxt.c:51
FmgrInfo prsheadline
Definition: ts_cache.h:48

Variable Documentation

◆ lastUsedConfig

TSConfigCacheEntry* lastUsedConfig = NULL
static

Definition at line 69 of file ts_cache.c.

Referenced by lookup_ts_config_cache().

◆ lastUsedDictionary

TSDictionaryCacheEntry* lastUsedDictionary = NULL
static

Definition at line 66 of file ts_cache.c.

Referenced by lookup_ts_dictionary_cache().

◆ lastUsedParser

TSParserCacheEntry* lastUsedParser = NULL
static

Definition at line 63 of file ts_cache.c.

Referenced by lookup_ts_parser_cache().

◆ TSConfigCacheHash

HTAB* TSConfigCacheHash = NULL
static

Definition at line 68 of file ts_cache.c.

◆ TSCurrentConfig

char* TSCurrentConfig = NULL

Definition at line 74 of file ts_cache.c.

Referenced by getTSCurrentConfig().

◆ TSCurrentConfigCache

Oid TSCurrentConfigCache = InvalidOid
static

◆ TSDictionaryCacheHash

HTAB* TSDictionaryCacheHash = NULL
static

Definition at line 65 of file ts_cache.c.

◆ TSParserCacheHash

HTAB* TSParserCacheHash = NULL
static

Definition at line 62 of file ts_cache.c.