LCOV - code coverage report
Current view: top level - src/backend/utils/cache - evtcache.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 73 76 96.1 %
Date: 2019-09-19 02:07:14 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * evtcache.c
       4             :  *    Special-purpose cache for event trigger data.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/utils/cache/evtcache.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/genam.h"
      17             : #include "access/htup_details.h"
      18             : #include "access/relation.h"
      19             : #include "catalog/pg_event_trigger.h"
      20             : #include "catalog/indexing.h"
      21             : #include "catalog/pg_type.h"
      22             : #include "commands/trigger.h"
      23             : #include "utils/array.h"
      24             : #include "utils/builtins.h"
      25             : #include "utils/catcache.h"
      26             : #include "utils/evtcache.h"
      27             : #include "utils/inval.h"
      28             : #include "utils/memutils.h"
      29             : #include "utils/hsearch.h"
      30             : #include "utils/rel.h"
      31             : #include "utils/snapmgr.h"
      32             : #include "utils/syscache.h"
      33             : 
      34             : typedef enum
      35             : {
      36             :     ETCS_NEEDS_REBUILD,
      37             :     ETCS_REBUILD_STARTED,
      38             :     ETCS_VALID
      39             : } EventTriggerCacheStateType;
      40             : 
      41             : typedef struct
      42             : {
      43             :     EventTriggerEvent event;
      44             :     List       *triggerlist;
      45             : } EventTriggerCacheEntry;
      46             : 
      47             : static HTAB *EventTriggerCache;
      48             : static MemoryContext EventTriggerCacheContext;
      49             : static EventTriggerCacheStateType EventTriggerCacheState = ETCS_NEEDS_REBUILD;
      50             : 
      51             : static void BuildEventTriggerCache(void);
      52             : static void InvalidateEventCacheCallback(Datum arg,
      53             :                                          int cacheid, uint32 hashvalue);
      54             : static int  DecodeTextArrayToCString(Datum array, char ***cstringp);
      55             : 
      56             : /*
      57             :  * Search the event cache by trigger event.
      58             :  *
      59             :  * Note that the caller had better copy any data it wants to keep around
      60             :  * across any operation that might touch a system catalog into some other
      61             :  * memory context, since a cache reset could blow the return value away.
      62             :  */
      63             : List *
      64      764442 : EventCacheLookup(EventTriggerEvent event)
      65             : {
      66             :     EventTriggerCacheEntry *entry;
      67             : 
      68      764442 :     if (EventTriggerCacheState != ETCS_VALID)
      69        2972 :         BuildEventTriggerCache();
      70      764442 :     entry = hash_search(EventTriggerCache, &event, HASH_FIND, NULL);
      71      764442 :     return entry != NULL ? entry->triggerlist : NIL;
      72             : }
      73             : 
      74             : /*
      75             :  * Rebuild the event trigger cache.
      76             :  */
      77             : static void
      78        2972 : BuildEventTriggerCache(void)
      79             : {
      80             :     HASHCTL     ctl;
      81             :     HTAB       *cache;
      82             :     MemoryContext oldcontext;
      83             :     Relation    rel;
      84             :     Relation    irel;
      85             :     SysScanDesc scan;
      86             : 
      87        2972 :     if (EventTriggerCacheContext != NULL)
      88             :     {
      89             :         /*
      90             :          * Free up any memory already allocated in EventTriggerCacheContext.
      91             :          * This can happen either because a previous rebuild failed, or
      92             :          * because an invalidation happened before the rebuild was complete.
      93             :          */
      94         420 :         MemoryContextResetAndDeleteChildren(EventTriggerCacheContext);
      95             :     }
      96             :     else
      97             :     {
      98             :         /*
      99             :          * This is our first time attempting to build the cache, so we need to
     100             :          * set up the memory context and register a syscache callback to
     101             :          * capture future invalidation events.
     102             :          */
     103        2552 :         if (CacheMemoryContext == NULL)
     104           0 :             CreateCacheMemoryContext();
     105        2552 :         EventTriggerCacheContext =
     106        2552 :             AllocSetContextCreate(CacheMemoryContext,
     107             :                                   "EventTriggerCache",
     108             :                                   ALLOCSET_DEFAULT_SIZES);
     109        2552 :         CacheRegisterSyscacheCallback(EVENTTRIGGEROID,
     110             :                                       InvalidateEventCacheCallback,
     111             :                                       (Datum) 0);
     112             :     }
     113             : 
     114             :     /* Switch to correct memory context. */
     115        2972 :     oldcontext = MemoryContextSwitchTo(EventTriggerCacheContext);
     116             : 
     117             :     /* Prevent the memory context from being nuked while we're rebuilding. */
     118        2972 :     EventTriggerCacheState = ETCS_REBUILD_STARTED;
     119             : 
     120             :     /* Create new hash table. */
     121        2972 :     MemSet(&ctl, 0, sizeof(ctl));
     122        2972 :     ctl.keysize = sizeof(EventTriggerEvent);
     123        2972 :     ctl.entrysize = sizeof(EventTriggerCacheEntry);
     124        2972 :     ctl.hcxt = EventTriggerCacheContext;
     125        2972 :     cache = hash_create("Event Trigger Cache", 32, &ctl,
     126             :                         HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     127             : 
     128             :     /*
     129             :      * Prepare to scan pg_event_trigger in name order.
     130             :      */
     131        2972 :     rel = relation_open(EventTriggerRelationId, AccessShareLock);
     132        2972 :     irel = index_open(EventTriggerNameIndexId, AccessShareLock);
     133        2972 :     scan = systable_beginscan_ordered(rel, irel, NULL, 0, NULL);
     134             : 
     135             :     /*
     136             :      * Build a cache item for each pg_event_trigger tuple, and append each one
     137             :      * to the appropriate cache entry.
     138             :      */
     139             :     for (;;)
     140         198 :     {
     141             :         HeapTuple   tup;
     142             :         Form_pg_event_trigger form;
     143             :         char       *evtevent;
     144             :         EventTriggerEvent event;
     145             :         EventTriggerCacheItem *item;
     146             :         Datum       evttags;
     147             :         bool        evttags_isnull;
     148             :         EventTriggerCacheEntry *entry;
     149             :         bool        found;
     150             : 
     151             :         /* Get next tuple. */
     152        3170 :         tup = systable_getnext_ordered(scan, ForwardScanDirection);
     153        3170 :         if (!HeapTupleIsValid(tup))
     154        2972 :             break;
     155             : 
     156             :         /* Skip trigger if disabled. */
     157         198 :         form = (Form_pg_event_trigger) GETSTRUCT(tup);
     158         198 :         if (form->evtenabled == TRIGGER_DISABLED)
     159          32 :             continue;
     160             : 
     161             :         /* Decode event name. */
     162         182 :         evtevent = NameStr(form->evtevent);
     163         182 :         if (strcmp(evtevent, "ddl_command_start") == 0)
     164          48 :             event = EVT_DDLCommandStart;
     165         134 :         else if (strcmp(evtevent, "ddl_command_end") == 0)
     166          90 :             event = EVT_DDLCommandEnd;
     167          44 :         else if (strcmp(evtevent, "sql_drop") == 0)
     168          34 :             event = EVT_SQLDrop;
     169          10 :         else if (strcmp(evtevent, "table_rewrite") == 0)
     170          10 :             event = EVT_TableRewrite;
     171             :         else
     172           0 :             continue;
     173             : 
     174             :         /* Allocate new cache item. */
     175         182 :         item = palloc0(sizeof(EventTriggerCacheItem));
     176         182 :         item->fnoid = form->evtfoid;
     177         182 :         item->enabled = form->evtenabled;
     178             : 
     179             :         /* Decode and sort tags array. */
     180         182 :         evttags = heap_getattr(tup, Anum_pg_event_trigger_evttags,
     181             :                                RelationGetDescr(rel), &evttags_isnull);
     182         182 :         if (!evttags_isnull)
     183             :         {
     184          52 :             item->ntags = DecodeTextArrayToCString(evttags, &item->tag);
     185          52 :             qsort(item->tag, item->ntags, sizeof(char *), pg_qsort_strcmp);
     186             :         }
     187             : 
     188             :         /* Add to cache entry. */
     189         182 :         entry = hash_search(cache, &event, HASH_ENTER, &found);
     190         182 :         if (found)
     191          16 :             entry->triggerlist = lappend(entry->triggerlist, item);
     192             :         else
     193         166 :             entry->triggerlist = list_make1(item);
     194             :     }
     195             : 
     196             :     /* Done with pg_event_trigger scan. */
     197        2972 :     systable_endscan_ordered(scan);
     198        2972 :     index_close(irel, AccessShareLock);
     199        2972 :     relation_close(rel, AccessShareLock);
     200             : 
     201             :     /* Restore previous memory context. */
     202        2972 :     MemoryContextSwitchTo(oldcontext);
     203             : 
     204             :     /* Install new cache. */
     205        2972 :     EventTriggerCache = cache;
     206             : 
     207             :     /*
     208             :      * If the cache has been invalidated since we entered this routine, we
     209             :      * still use and return the cache we just finished constructing, to avoid
     210             :      * infinite loops, but we leave the cache marked stale so that we'll
     211             :      * rebuild it again on next access.  Otherwise, we mark the cache valid.
     212             :      */
     213        2972 :     if (EventTriggerCacheState == ETCS_REBUILD_STARTED)
     214        2972 :         EventTriggerCacheState = ETCS_VALID;
     215        2972 : }
     216             : 
     217             : /*
     218             :  * Decode text[] to an array of C strings.
     219             :  *
     220             :  * We could avoid a bit of overhead here if we were willing to duplicate some
     221             :  * of the logic from deconstruct_array, but it doesn't seem worth the code
     222             :  * complexity.
     223             :  */
     224             : static int
     225          52 : DecodeTextArrayToCString(Datum array, char ***cstringp)
     226             : {
     227          52 :     ArrayType  *arr = DatumGetArrayTypeP(array);
     228             :     Datum      *elems;
     229             :     char      **cstring;
     230             :     int         i;
     231             :     int         nelems;
     232             : 
     233          52 :     if (ARR_NDIM(arr) != 1 || ARR_HASNULL(arr) || ARR_ELEMTYPE(arr) != TEXTOID)
     234           0 :         elog(ERROR, "expected 1-D text array");
     235          52 :     deconstruct_array(arr, TEXTOID, -1, false, 'i', &elems, NULL, &nelems);
     236             : 
     237          52 :     cstring = palloc(nelems * sizeof(char *));
     238         172 :     for (i = 0; i < nelems; ++i)
     239         120 :         cstring[i] = TextDatumGetCString(elems[i]);
     240             : 
     241          52 :     pfree(elems);
     242          52 :     *cstringp = cstring;
     243          52 :     return nelems;
     244             : }
     245             : 
     246             : /*
     247             :  * Flush all cache entries when pg_event_trigger is updated.
     248             :  *
     249             :  * This should be rare enough that we don't need to be very granular about
     250             :  * it, so we just blow away everything, which also avoids the possibility of
     251             :  * memory leaks.
     252             :  */
     253             : static void
     254         936 : InvalidateEventCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
     255             : {
     256             :     /*
     257             :      * If the cache isn't valid, then there might be a rebuild in progress, so
     258             :      * we can't immediately blow it away.  But it's advantageous to do this
     259             :      * when possible, so as to immediately free memory.
     260             :      */
     261         936 :     if (EventTriggerCacheState == ETCS_VALID)
     262             :     {
     263         452 :         MemoryContextResetAndDeleteChildren(EventTriggerCacheContext);
     264         452 :         EventTriggerCache = NULL;
     265             :     }
     266             : 
     267             :     /* Mark cache for rebuild. */
     268         936 :     EventTriggerCacheState = ETCS_NEEDS_REBUILD;
     269         936 : }

Generated by: LCOV version 1.13