Debian Codegraph @

src/backend/utils/cache/ts_cache.c

raw
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-2025, 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 "nodes/miscnodes.h"
42 #include "tsearch/ts_cache.h"
43 #include "utils/builtins.h"
44 #include "utils/catcache.h"
45 #include "utils/fmgroids.h"
46 #include "utils/guc_hooks.h"
47 #include "utils/inval.h"
48 #include "utils/lsyscache.h"
49 #include "utils/memutils.h"
50 #include "utils/regproc.h"
51 #include "utils/syscache.h"
52
53
54 /*
55 * MAXTOKENTYPE/MAXDICTSPERTT are arbitrary limits on the workspace size
56 * used in lookup_ts_config_cache(). We could avoid hardwiring a limit
57 * by making the workspace dynamically enlargeable, but it seems unlikely
58 * to be worth the trouble.
59 */
60 #define MAXTOKENTYPE 256
61 #define MAXDICTSPERTT 100
62
63
64 static HTAB *TSParserCacheHash = NULL;
65 static TSParserCacheEntry *lastUsedParser = NULL;
66
67 static HTAB *TSDictionaryCacheHash = NULL;
68 static TSDictionaryCacheEntry *lastUsedDictionary = NULL;
69
70 static HTAB *TSConfigCacheHash = NULL;
71 static TSConfigCacheEntry *lastUsedConfig = NULL;
72
73 /*
74 * GUC default_text_search_config, and a cache of the current config's OID
75 */
76 char *TSCurrentConfig = NULL;
77
78 static Oid TSCurrentConfigCache = InvalidOid;
79
80
81 /*
82 * We use this syscache callback to detect when a visible change to a TS
83 * catalog entry has been made, by either our own backend or another one.
84 *
85 * In principle we could just flush the specific cache entry that changed,
86 * but given that TS configuration changes are probably infrequent, it
87 * doesn't seem worth the trouble to determine that; we just flush all the
88 * entries of the related hash table.
89 *
90 * We can use the same function for all TS caches by passing the hash
91 * table address as the "arg".
92 */
93 static void
94 InvalidateTSCacheCallBack(Datum arg, int cacheid, uint32 hashvalue)
95 {
96 HTAB *hash = (HTAB *) DatumGetPointer(arg);
97 HASH_SEQ_STATUS status;
98 TSAnyCacheEntry *entry;
99
100 hash_seq_init(&status, hash);
101 while ((entry = (TSAnyCacheEntry *) hash_seq_search(&status)) != NULL)
102 entry->isvalid = false;
103
104 /* Also invalidate the current-config cache if it's pg_ts_config */
105 if (hash == TSConfigCacheHash)
106 TSCurrentConfigCache = InvalidOid;
107 }
108
109 /*
110 * Fetch parser cache entry
111 */
112 TSParserCacheEntry *
113 lookup_ts_parser_cache(Oid prsId)
114 {
115 TSParserCacheEntry *entry;
116
117 if (TSParserCacheHash == NULL)
118 {
119 /* First time through: initialize the hash table */
120 HASHCTL ctl;
121
122 ctl.keysize = sizeof(Oid);
123 ctl.entrysize = sizeof(TSParserCacheEntry);
124 TSParserCacheHash = hash_create("Tsearch parser cache", 4,
125 &ctl, HASH_ELEM | HASH_BLOBS);
126 /* Flush cache on pg_ts_parser changes */
127 CacheRegisterSyscacheCallback(TSPARSEROID, InvalidateTSCacheCallBack,
128 PointerGetDatum(TSParserCacheHash));
129
130 /* Also make sure CacheMemoryContext exists */
131 if (!CacheMemoryContext)
132 CreateCacheMemoryContext();
133 }
134
135 /* Check single-entry cache */
136 if (lastUsedParser && lastUsedParser->prsId == prsId &&
137 lastUsedParser->isvalid)
138 return lastUsedParser;
139
140 /* Try to look up an existing entry */
141 entry = (TSParserCacheEntry *) hash_search(TSParserCacheHash,
142 &prsId,
143 HASH_FIND, NULL);
144 if (entry == NULL || !entry->isvalid)
145 {
146 /*
147 * If we didn't find one, we want to make one. But first look up the
148 * object to be sure the OID is real.
149 */
150 HeapTuple tp;
151 Form_pg_ts_parser prs;
152
153 tp = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId));
154 if (!HeapTupleIsValid(tp))
155 elog(ERROR, "cache lookup failed for text search parser %u",
156 prsId);
157 prs = (Form_pg_ts_parser) GETSTRUCT(tp);
158
159 /*
160 * Sanity checks
161 */
162 if (!OidIsValid(prs->prsstart))
163 elog(ERROR, "text search parser %u has no prsstart method", prsId);
164 if (!OidIsValid(prs->prstoken))
165 elog(ERROR, "text search parser %u has no prstoken method", prsId);
166 if (!OidIsValid(prs->prsend))
167 elog(ERROR, "text search parser %u has no prsend method", prsId);
168
169 if (entry == NULL)
170 {
171 bool found;
172
173 /* Now make the cache entry */
174 entry = (TSParserCacheEntry *)
175 hash_search(TSParserCacheHash, &prsId, 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
189 fmgr_info_cxt(entry->startOid, &entry->prsstart, CacheMemoryContext);
190 fmgr_info_cxt(entry->tokenOid, &entry->prstoken, CacheMemoryContext);
191 fmgr_info_cxt(entry->endOid, &entry->prsend, CacheMemoryContext);
192 if (OidIsValid(entry->headlineOid))
193 fmgr_info_cxt(entry->headlineOid, &entry->prsheadline,
194 CacheMemoryContext);
195
196 entry->isvalid = true;
197 }
198
199 lastUsedParser = entry;
200
201 return entry;
202 }
203
204 /*
205 * Fetch dictionary cache entry
206 */
207 TSDictionaryCacheEntry *
208 lookup_ts_dictionary_cache(Oid dictId)
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 */
222 CacheRegisterSyscacheCallback(TSDICTOID, InvalidateTSCacheCallBack,
223 PointerGetDatum(TSDictionaryCacheHash));
224 CacheRegisterSyscacheCallback(TSTEMPLATEOID, InvalidateTSCacheCallBack,
225 PointerGetDatum(TSDictionaryCacheHash));
226
227 /* Also make sure CacheMemoryContext exists */
228 if (!CacheMemoryContext)
229 CreateCacheMemoryContext();
230 }
231
232 /* Check single-entry cache */
233 if (lastUsedDictionary && lastUsedDictionary->dictId == dictId &&
234 lastUsedDictionary->isvalid)
235 return lastUsedDictionary;
236
237 /* Try to look up an existing entry */
238 entry = (TSDictionaryCacheEntry *) hash_search(TSDictionaryCacheHash,
239 &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 */
268 tptmpl = SearchSysCache1(TSTEMPLATEOID,
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 /*
283 * OK, create or clear out the hashtable entry
284 */
285 if (entry == NULL)
286 {
287 bool found;
288
289 entry = (TSDictionaryCacheEntry *)
290 hash_search(TSDictionaryCacheHash,
291 &dictId,
292 HASH_ENTER, &found);
293 Assert(!found); /* it wasn't there a moment ago */
294
295 memset(entry, 0, sizeof(TSDictionaryCacheEntry));
296 entry->dictId = dictId;
297 saveCtx = NULL;
298 }
299 else
300 {
301 saveCtx = entry->dictCtx; /* could be NULL if we failed before */
302 memset(entry, 0, sizeof(TSDictionaryCacheEntry));
303 entry->dictId = dictId;
304 entry->dictCtx = saveCtx;
305 }
306
307 /*
308 * Create or clear the entry's private memory context
309 */
310 if (saveCtx == NULL)
311 {
312 saveCtx = AllocSetContextCreate(CacheMemoryContext,
313 "TS dictionary",
314 ALLOCSET_SMALL_SIZES);
315 entry->dictCtx = saveCtx;
316 MemoryContextCopyAndSetIdentifier(saveCtx, NameStr(dict->dictname));
317 }
318 else
319 {
320 /* Don't let context's ident pointer dangle while we reset it */
321 MemoryContextSetIdentifier(saveCtx, NULL);
322 MemoryContextReset(saveCtx);
323 MemoryContextCopyAndSetIdentifier(saveCtx, NameStr(dict->dictname));
324 }
325
326 entry->lexizeOid = template->tmpllexize;
327
328 if (OidIsValid(template->tmplinit))
329 {
330 List *dictoptions;
331 Datum opt;
332 bool isnull;
333 MemoryContext oldcontext;
334
335 /*
336 * Init method runs in dictionary's private memory context, and we
337 * make sure the options are stored there too
338 */
339 oldcontext = MemoryContextSwitchTo(entry->dictCtx);
340
341 opt = SysCacheGetAttr(TSDICTOID, tpdict,
342 Anum_pg_ts_dict_dictinitoption,
343 &isnull);
344 if (isnull)
345 dictoptions = NIL;
346 else
347 dictoptions = deserialize_deflist(opt);
348
349 entry->dictData =
350 DatumGetPointer(OidFunctionCall1(template->tmplinit,
351 PointerGetDatum(dictoptions)));
352
353 MemoryContextSwitchTo(oldcontext);
354 }
355
356 ReleaseSysCache(tptmpl);
357 ReleaseSysCache(tpdict);
358
359 fmgr_info_cxt(entry->lexizeOid, &entry->lexize, entry->dictCtx);
360
361 entry->isvalid = true;
362 }
363
364 lastUsedDictionary = entry;
365
366 return entry;
367 }
368
369 /*
370 * Initialize config cache and prepare callbacks. This is split out of
371 * lookup_ts_config_cache because we need to activate the callback before
372 * caching TSCurrentConfigCache, too.
373 */
374 static void
375 init_ts_config_cache(void)
376 {
377 HASHCTL ctl;
378
379 ctl.keysize = sizeof(Oid);
380 ctl.entrysize = sizeof(TSConfigCacheEntry);
381 TSConfigCacheHash = hash_create("Tsearch configuration cache", 16,
382 &ctl, HASH_ELEM | HASH_BLOBS);
383 /* Flush cache on pg_ts_config and pg_ts_config_map changes */
384 CacheRegisterSyscacheCallback(TSCONFIGOID, InvalidateTSCacheCallBack,
385 PointerGetDatum(TSConfigCacheHash));
386 CacheRegisterSyscacheCallback(TSCONFIGMAP, InvalidateTSCacheCallBack,
387 PointerGetDatum(TSConfigCacheHash));
388
389 /* Also make sure CacheMemoryContext exists */
390 if (!CacheMemoryContext)
391 CreateCacheMemoryContext();
392 }
393
394 /*
395 * Fetch configuration cache entry
396 */
397 TSConfigCacheEntry *
398 lookup_ts_config_cache(Oid cfgId)
399 {
400 TSConfigCacheEntry *entry;
401
402 if (TSConfigCacheHash == NULL)
403 {
404 /* First time through: initialize the hash table */
405 init_ts_config_cache();
406 }
407
408 /* Check single-entry cache */
409 if (lastUsedConfig && lastUsedConfig->cfgId == cfgId &&
410 lastUsedConfig->isvalid)
411 return lastUsedConfig;
412
413 /* Try to look up an existing entry */
414 entry = (TSConfigCacheEntry *) hash_search(TSConfigCacheHash,
415 &cfgId,
416 HASH_FIND, NULL);
417 if (entry == NULL || !entry->isvalid)
418 {
419 /*
420 * If we didn't find one, we want to make one. But first look up the
421 * object to be sure the OID is real.
422 */
423 HeapTuple tp;
424 Form_pg_ts_config cfg;
425 Relation maprel;
426 Relation mapidx;
427 ScanKeyData mapskey;
428 SysScanDesc mapscan;
429 HeapTuple maptup;
430 ListDictionary maplists[MAXTOKENTYPE + 1];
431 Oid mapdicts[MAXDICTSPERTT];
432 int maxtokentype;
433 int ndicts;
434 int i;
435
436 tp = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(cfgId));
437 if (!HeapTupleIsValid(tp))
438 elog(ERROR, "cache lookup failed for text search configuration %u",
439 cfgId);
440 cfg = (Form_pg_ts_config) GETSTRUCT(tp);
441
442 /*
443 * Sanity checks
444 */
445 if (!OidIsValid(cfg->cfgparser))
446 elog(ERROR, "text search configuration %u has no parser", cfgId);
447
448 if (entry == NULL)
449 {
450 bool found;
451
452 /* Now make the cache entry */
453 entry = (TSConfigCacheEntry *)
454 hash_search(TSConfigCacheHash,
455 &cfgId,
456 HASH_ENTER, &found);
457 Assert(!found); /* it wasn't there a moment ago */
458 }
459 else
460 {
461 /* Cleanup old contents */
462 if (entry->map)
463 {
464 for (i = 0; i < entry->lenmap; i++)
465 if (entry->map[i].dictIds)
466 pfree(entry->map[i].dictIds);
467 pfree(entry->map);
468 }
469 }
470
471 MemSet(entry, 0, sizeof(TSConfigCacheEntry));
472 entry->cfgId = cfgId;
473 entry->prsId = cfg->cfgparser;
474
475 ReleaseSysCache(tp);
476
477 /*
478 * Scan pg_ts_config_map to gather dictionary list for each token type
479 *
480 * Because the index is on (mapcfg, maptokentype, mapseqno), we will
481 * see the entries in maptokentype order, and in mapseqno order for
482 * each token type, even though we didn't explicitly ask for that.
483 */
484 MemSet(maplists, 0, sizeof(maplists));
485 maxtokentype = 0;
486 ndicts = 0;
487
488 ScanKeyInit(&mapskey,
489 Anum_pg_ts_config_map_mapcfg,
490 BTEqualStrategyNumber, F_OIDEQ,
491 ObjectIdGetDatum(cfgId));
492
493 maprel = table_open(TSConfigMapRelationId, AccessShareLock);
494 mapidx = index_open(TSConfigMapIndexId, AccessShareLock);
495 mapscan = systable_beginscan_ordered(maprel, mapidx,
496 NULL, 1, &mapskey);
497
498 while ((maptup = systable_getnext_ordered(mapscan, ForwardScanDirection)) != NULL)
499 {
500 Form_pg_ts_config_map cfgmap = (Form_pg_ts_config_map) GETSTRUCT(maptup);
501 int toktype = cfgmap->maptokentype;
502
503 if (toktype <= 0 || toktype > MAXTOKENTYPE)
504 elog(ERROR, "maptokentype value %d is out of range", toktype);
505 if (toktype < maxtokentype)
506 elog(ERROR, "maptokentype entries are out of order");
507 if (toktype > maxtokentype)
508 {
509 /* starting a new token type, but first save the prior data */
510 if (ndicts > 0)
511 {
512 maplists[maxtokentype].len = ndicts;
513 maplists[maxtokentype].dictIds = (Oid *)
514 MemoryContextAlloc(CacheMemoryContext,
515 sizeof(Oid) * ndicts);
516 memcpy(maplists[maxtokentype].dictIds, mapdicts,
517 sizeof(Oid) * ndicts);
518 }
519 maxtokentype = toktype;
520 mapdicts[0] = cfgmap->mapdict;
521 ndicts = 1;
522 }
523 else
524 {
525 /* continuing data for current token type */
526 if (ndicts >= MAXDICTSPERTT)
527 elog(ERROR, "too many pg_ts_config_map entries for one token type");
528 mapdicts[ndicts++] = cfgmap->mapdict;
529 }
530 }
531
532 systable_endscan_ordered(mapscan);
533 index_close(mapidx, AccessShareLock);
534 table_close(maprel, AccessShareLock);
535
536 if (ndicts > 0)
537 {
538 /* save the last token type's dictionaries */
539 maplists[maxtokentype].len = ndicts;
540 maplists[maxtokentype].dictIds = (Oid *)
541 MemoryContextAlloc(CacheMemoryContext,
542 sizeof(Oid) * ndicts);
543 memcpy(maplists[maxtokentype].dictIds, mapdicts,
544 sizeof(Oid) * ndicts);
545 /* and save the overall map */
546 entry->lenmap = maxtokentype + 1;
547 entry->map = (ListDictionary *)
548 MemoryContextAlloc(CacheMemoryContext,
549 sizeof(ListDictionary) * entry->lenmap);
550 memcpy(entry->map, maplists,
551 sizeof(ListDictionary) * entry->lenmap);
552 }
553
554 entry->isvalid = true;
555 }
556
557 lastUsedConfig = entry;
558
559 return entry;
560 }
561
562
563 /*---------------------------------------------------
564 * GUC variable "default_text_search_config"
565 *---------------------------------------------------
566 */
567
568 Oid
569 getTSCurrentConfig(bool emitError)
570 {
571 List *namelist;
572
573 /* if we have a cached value, return it */
574 if (OidIsValid(TSCurrentConfigCache))
575 return TSCurrentConfigCache;
576
577 /* fail if GUC hasn't been set up yet */
578 if (TSCurrentConfig == NULL || *TSCurrentConfig == '\0')
579 {
580 if (emitError)
581 elog(ERROR, "text search configuration isn't set");
582 else
583 return InvalidOid;
584 }
585
586 if (TSConfigCacheHash == NULL)
587 {
588 /* First time through: initialize the tsconfig inval callback */
589 init_ts_config_cache();
590 }
591
592 /* Look up the config */
593 if (emitError)
594 {
595 namelist = stringToQualifiedNameList(TSCurrentConfig, NULL);
596 TSCurrentConfigCache = get_ts_config_oid(namelist, false);
597 }
598 else
599 {
600 ErrorSaveContext escontext = {T_ErrorSaveContext};
601
602 namelist = stringToQualifiedNameList(TSCurrentConfig,
603 (Node *) &escontext);
604 if (namelist != NIL)
605 TSCurrentConfigCache = get_ts_config_oid(namelist, true);
606 else
607 TSCurrentConfigCache = InvalidOid; /* bad name list syntax */
608 }
609
610 return TSCurrentConfigCache;
611 }
612
613 /* GUC check_hook for default_text_search_config */
614 bool
615 check_default_text_search_config(char **newval, void **extra, GucSource source)
616 {
617 /*
618 * If we aren't inside a transaction, or connected to a database, we
619 * cannot do the catalog accesses necessary to verify the config name.
620 * Must accept it on faith.
621 */
622 if (IsTransactionState() && MyDatabaseId != InvalidOid)
623 {
624 ErrorSaveContext escontext = {T_ErrorSaveContext};
625 List *namelist;
626 Oid cfgId;
627 HeapTuple tuple;
628 Form_pg_ts_config cfg;
629 char *buf;
630
631 namelist = stringToQualifiedNameList(*newval,
632 (Node *) &escontext);
633 if (namelist != NIL)
634 cfgId = get_ts_config_oid(namelist, true);
635 else
636 cfgId = InvalidOid; /* bad name list syntax */
637
638 /*
639 * When source == PGC_S_TEST, don't throw a hard error for a
640 * nonexistent configuration, only a NOTICE. See comments in guc.h.
641 */
642 if (!OidIsValid(cfgId))
643 {
644 if (source == PGC_S_TEST)
645 {
646 ereport(NOTICE,
647 (errcode(ERRCODE_UNDEFINED_OBJECT),
648 errmsg("text search configuration \"%s\" does not exist", *newval)));
649 return true;
650 }
651 else
652 return false;
653 }
654
655 /*
656 * Modify the actually stored value to be fully qualified, to ensure
657 * later changes of search_path don't affect it.
658 */
659 tuple = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(cfgId));
660 if (!HeapTupleIsValid(tuple))
661 elog(ERROR, "cache lookup failed for text search configuration %u",
662 cfgId);
663 cfg = (Form_pg_ts_config) GETSTRUCT(tuple);
664
665 buf = quote_qualified_identifier(get_namespace_name(cfg->cfgnamespace),
666 NameStr(cfg->cfgname));
667
668 ReleaseSysCache(tuple);
669
670 /* GUC wants it guc_malloc'd not palloc'd */
671 guc_free(*newval);
672 *newval = guc_strdup(LOG, buf);
673 pfree(buf);
674 if (!*newval)
675 return false;
676 }
677
678 return true;
679 }
680
681 /* GUC assign_hook for default_text_search_config */
682 void
683 assign_default_text_search_config(const char *newval, void *extra)
684 {
685 /* Just reset the cache to force a lookup on first use */
686 TSCurrentConfigCache = InvalidOid;
687 }