LCOV - differential code coverage report
Current view: top level - src/backend/utils/cache - evtcache.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 96.2 % 78 75 3 4 71 2
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 4 4 2 2
Baseline: lcov-20250906-005545-baseline Branches: 78.6 % 42 33 1 8 1 32
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 4 4 4
(360..) days: 95.9 % 74 71 3 71
Function coverage date bins:
(360..) days: 100.0 % 4 4 2 2
Branch coverage date bins:
(30,360] days: 50.0 % 2 1 1 1
(360..) days: 80.0 % 40 32 8 32

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

Generated by: LCOV version 2.4-beta