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: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 96.2 % 78 75 3 6 69 4
Current Date: 2026-05-05 10:23:31 +0900 Functions: 100.0 % 4 4 3 1 1
Baseline: lcov-20260505-025707-baseline Branches: 78.6 % 42 33 1 8 1 32
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 6 6 6
(360..) days: 95.8 % 72 69 3 69
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 3 3 2 1
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-2026, 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                 :                :                                          SysCacheIdentifier cacheid,
                                 53                 :                :                                          uint32 hashvalue);
                                 54                 :                : static Bitmapset *DecodeTextArrayToBitmapset(Datum array);
                                 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 *
 5037 rhaas@postgresql.org       64                 :CBC      585117 : EventCacheLookup(EventTriggerEvent event)
                                 65                 :                : {
                                 66                 :                :     EventTriggerCacheEntry *entry;
                                 67                 :                : 
 5018                            68         [ +  + ]:         585117 :     if (EventTriggerCacheState != ETCS_VALID)
 5037                            69                 :           4013 :         BuildEventTriggerCache();
                                 70                 :         585117 :     entry = hash_search(EventTriggerCache, &event, HASH_FIND, NULL);
 3255                            71         [ +  + ]:         585117 :     return entry != NULL ? entry->triggerlist : NIL;
                                 72                 :                : }
                                 73                 :                : 
                                 74                 :                : /*
                                 75                 :                :  * Rebuild the event trigger cache.
                                 76                 :                :  */
                                 77                 :                : static void
 5037                            78                 :           4013 : BuildEventTriggerCache(void)
                                 79                 :                : {
                                 80                 :                :     HASHCTL     ctl;
                                 81                 :                :     HTAB       *cache;
                                 82                 :                :     Relation    rel;
                                 83                 :                :     Relation    irel;
                                 84                 :                :     SysScanDesc scan;
                                 85                 :                : 
                                 86         [ +  + ]:           4013 :     if (EventTriggerCacheContext != NULL)
                                 87                 :                :     {
                                 88                 :                :         /*
                                 89                 :                :          * Free up any memory already allocated in EventTriggerCacheContext.
                                 90                 :                :          * This can happen either because a previous rebuild failed, or
                                 91                 :                :          * because an invalidation happened before the rebuild was complete.
                                 92                 :                :          */
  902 nathan@postgresql.or       93                 :            449 :         MemoryContextReset(EventTriggerCacheContext);
                                 94                 :                :     }
                                 95                 :                :     else
                                 96                 :                :     {
                                 97                 :                :         /*
                                 98                 :                :          * This is our first time attempting to build the cache, so we need to
                                 99                 :                :          * set up the memory context and register a syscache callback to
                                100                 :                :          * capture future invalidation events.
                                101                 :                :          */
 5037 rhaas@postgresql.org      102         [ -  + ]:           3564 :         if (CacheMemoryContext == NULL)
 5037 rhaas@postgresql.org      103                 :UBC           0 :             CreateCacheMemoryContext();
 5037 rhaas@postgresql.org      104                 :CBC        3564 :         EventTriggerCacheContext =
                                105                 :           3564 :             AllocSetContextCreate(CacheMemoryContext,
                                106                 :                :                                   "EventTriggerCache",
                                107                 :                :                                   ALLOCSET_DEFAULT_SIZES);
                                108                 :           3564 :         CacheRegisterSyscacheCallback(EVENTTRIGGEROID,
                                109                 :                :                                       InvalidateEventCacheCallback,
                                110                 :                :                                       (Datum) 0);
                                111                 :                :     }
                                112                 :                : 
                                113                 :                :     /* Prevent the memory context from being nuked while we're rebuilding. */
 5018                           114                 :           4013 :     EventTriggerCacheState = ETCS_REBUILD_STARTED;
                                115                 :                : 
                                116                 :                :     /* Create new hash table. */
 5037                           117                 :           4013 :     ctl.keysize = sizeof(EventTriggerEvent);
                                118                 :           4013 :     ctl.entrysize = sizeof(EventTriggerCacheEntry);
 5019                           119                 :           4013 :     ctl.hcxt = EventTriggerCacheContext;
 1035 dgustafsson@postgres      120                 :           4013 :     cache = hash_create("EventTriggerCacheHash", 32, &ctl,
                                121                 :                :                         HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
                                122                 :                : 
                                123                 :                :     /*
                                124                 :                :      * Prepare to scan pg_event_trigger in name order.
                                125                 :                :      */
 5037 rhaas@postgresql.org      126                 :           4013 :     rel = relation_open(EventTriggerRelationId, AccessShareLock);
                                127                 :           4013 :     irel = index_open(EventTriggerNameIndexId, AccessShareLock);
 4690                           128                 :           4013 :     scan = systable_beginscan_ordered(rel, irel, NULL, 0, NULL);
                                129                 :                : 
                                130                 :                :     /*
                                131                 :                :      * Build a cache item for each pg_event_trigger tuple, and append each one
                                132                 :                :      * to the appropriate cache entry.
                                133                 :                :      */
                                134                 :                :     for (;;)
 5037                           135                 :            418 :     {
                                136                 :                :         HeapTuple   tup;
                                137                 :                :         Form_pg_event_trigger form;
                                138                 :                :         char       *evtevent;
                                139                 :                :         EventTriggerEvent event;
                                140                 :                :         EventTriggerCacheItem *item;
                                141                 :                :         Datum       evttags;
                                142                 :                :         bool        evttags_isnull;
                                143                 :                :         EventTriggerCacheEntry *entry;
                                144                 :                :         bool        found;
                                145                 :                :         MemoryContext oldcontext;
                                146                 :                : 
                                147                 :                :         /* Get next tuple. */
                                148                 :           4431 :         tup = systable_getnext_ordered(scan, ForwardScanDirection);
                                149         [ +  + ]:           4431 :         if (!HeapTupleIsValid(tup))
                                150                 :           4013 :             break;
                                151                 :                : 
                                152                 :                :         /* Skip trigger if disabled. */
                                153                 :            418 :         form = (Form_pg_event_trigger) GETSTRUCT(tup);
                                154         [ +  + ]:            418 :         if (form->evtenabled == TRIGGER_DISABLED)
                                155                 :             22 :             continue;
                                156                 :                : 
                                157                 :                :         /* Decode event name. */
                                158                 :            396 :         evtevent = NameStr(form->evtevent);
                                159         [ +  + ]:            396 :         if (strcmp(evtevent, "ddl_command_start") == 0)
                                160                 :             57 :             event = EVT_DDLCommandStart;
 4852                           161         [ +  + ]:            339 :         else if (strcmp(evtevent, "ddl_command_end") == 0)
                                162                 :            108 :             event = EVT_DDLCommandEnd;
 4787 alvherre@alvh.no-ip.      163         [ +  + ]:            231 :         else if (strcmp(evtevent, "sql_drop") == 0)
                                164                 :             81 :             event = EVT_SQLDrop;
 4166 simon@2ndQuadrant.co      165         [ +  + ]:            150 :         else if (strcmp(evtevent, "table_rewrite") == 0)
                                166                 :             10 :             event = EVT_TableRewrite;
  932 akorotkov@postgresql      167         [ +  - ]:            140 :         else if (strcmp(evtevent, "login") == 0)
                                168                 :            140 :             event = EVT_Login;
                                169                 :                :         else
 5037 rhaas@postgresql.org      170                 :UBC           0 :             continue;
                                171                 :                : 
                                172                 :                :         /* Switch to correct memory context. */
  276 tgl@sss.pgh.pa.us         173                 :GNC         396 :         oldcontext = MemoryContextSwitchTo(EventTriggerCacheContext);
                                174                 :                : 
                                175                 :                :         /* Allocate new cache item. */
  146 michael@paquier.xyz       176                 :            396 :         item = palloc0_object(EventTriggerCacheItem);
 5037 rhaas@postgresql.org      177                 :CBC         396 :         item->fnoid = form->evtfoid;
                                178                 :            396 :         item->enabled = form->evtenabled;
                                179                 :                : 
                                180                 :                :         /* Decode and sort tags array. */
                                181                 :            396 :         evttags = heap_getattr(tup, Anum_pg_event_trigger_evttags,
                                182                 :                :                                RelationGetDescr(rel), &evttags_isnull);
                                183         [ +  + ]:            396 :         if (!evttags_isnull)
 2255 alvherre@alvh.no-ip.      184                 :            124 :             item->tagset = DecodeTextArrayToBitmapset(evttags);
                                185                 :                : 
                                186                 :                :         /* Add to cache entry. */
 5037 rhaas@postgresql.org      187                 :            396 :         entry = hash_search(cache, &event, HASH_ENTER, &found);
                                188         [ +  + ]:            396 :         if (found)
                                189                 :             28 :             entry->triggerlist = lappend(entry->triggerlist, item);
                                190                 :                :         else
                                191                 :            368 :             entry->triggerlist = list_make1(item);
                                192                 :                : 
                                193                 :                :         /* Restore previous memory context. */
  276 tgl@sss.pgh.pa.us         194                 :GNC         396 :         MemoryContextSwitchTo(oldcontext);
                                195                 :                :     }
                                196                 :                : 
                                197                 :                :     /* Done with pg_event_trigger scan. */
 5037 rhaas@postgresql.org      198                 :CBC        4013 :     systable_endscan_ordered(scan);
                                199                 :           4013 :     index_close(irel, AccessShareLock);
                                200                 :           4013 :     relation_close(rel, AccessShareLock);
                                201                 :                : 
                                202                 :                :     /* Install new cache. */
                                203                 :           4013 :     EventTriggerCache = cache;
                                204                 :                : 
                                205                 :                :     /*
                                206                 :                :      * If the cache has been invalidated since we entered this routine, we
                                207                 :                :      * still use and return the cache we just finished constructing, to avoid
                                208                 :                :      * infinite loops, but we leave the cache marked stale so that we'll
                                209                 :                :      * rebuild it again on next access.  Otherwise, we mark the cache valid.
                                210                 :                :      */
 5018                           211         [ +  - ]:           4013 :     if (EventTriggerCacheState == ETCS_REBUILD_STARTED)
                                212                 :           4013 :         EventTriggerCacheState = ETCS_VALID;
 5037                           213                 :           4013 : }
                                214                 :                : 
                                215                 :                : /*
                                216                 :                :  * Decode text[] to a Bitmapset of CommandTags.
                                217                 :                :  *
                                218                 :                :  * We could avoid a bit of overhead here if we were willing to duplicate some
                                219                 :                :  * of the logic from deconstruct_array, but it doesn't seem worth the code
                                220                 :                :  * complexity.
                                221                 :                :  */
                                222                 :                : static Bitmapset *
 2255 alvherre@alvh.no-ip.      223                 :            124 : DecodeTextArrayToBitmapset(Datum array)
                                224                 :                : {
 5037 rhaas@postgresql.org      225                 :            124 :     ArrayType  *arr = DatumGetArrayTypeP(array);
                                226                 :                :     Datum      *elems;
                                227                 :                :     Bitmapset  *bms;
                                228                 :                :     int         i;
                                229                 :                :     int         nelems;
                                230                 :                : 
                                231   [ +  -  +  -  :            124 :     if (ARR_NDIM(arr) != 1 || ARR_HASNULL(arr) || ARR_ELEMTYPE(arr) != TEXTOID)
                                              -  + ]
 5037 rhaas@postgresql.org      232         [ #  # ]:UBC           0 :         elog(ERROR, "expected 1-D text array");
 1404 peter@eisentraut.org      233                 :CBC         124 :     deconstruct_array_builtin(arr, TEXTOID, &elems, NULL, &nelems);
                                234                 :                : 
 2255 alvherre@alvh.no-ip.      235         [ +  + ]:            316 :     for (bms = NULL, i = 0; i < nelems; ++i)
                                236                 :                :     {
                                237                 :            192 :         char       *str = TextDatumGetCString(elems[i]);
                                238                 :                : 
                                239                 :            192 :         bms = bms_add_member(bms, GetCommandTagEnum(str));
                                240                 :            192 :         pfree(str);
                                241                 :                :     }
                                242                 :                : 
 5037 rhaas@postgresql.org      243                 :            124 :     pfree(elems);
  152 peter@eisentraut.org      244         [ +  - ]:GNC         124 :     if (arr != DatumGetPointer(array))
  276 tgl@sss.pgh.pa.us         245                 :            124 :         pfree(arr);
                                246                 :                : 
 2255 alvherre@alvh.no-ip.      247                 :CBC         124 :     return bms;
                                248                 :                : }
                                249                 :                : 
                                250                 :                : /*
                                251                 :                :  * Flush all cache entries when pg_event_trigger is updated.
                                252                 :                :  *
                                253                 :                :  * This should be rare enough that we don't need to be very granular about
                                254                 :                :  * it, so we just blow away everything, which also avoids the possibility of
                                255                 :                :  * memory leaks.
                                256                 :                :  */
                                257                 :                : static void
   76 michael@paquier.xyz       258                 :GNC         959 : InvalidateEventCacheCallback(Datum arg, SysCacheIdentifier cacheid,
                                259                 :                :                              uint32 hashvalue)
                                260                 :                : {
                                261                 :                :     /*
                                262                 :                :      * If the cache isn't valid, then there might be a rebuild in progress, so
                                263                 :                :      * we can't immediately blow it away.  But it's advantageous to do this
                                264                 :                :      * when possible, so as to immediately free memory.
                                265                 :                :      */
 5018 rhaas@postgresql.org      266         [ +  + ]:CBC         959 :     if (EventTriggerCacheState == ETCS_VALID)
                                267                 :                :     {
  902 nathan@postgresql.or      268                 :            475 :         MemoryContextReset(EventTriggerCacheContext);
 5018 rhaas@postgresql.org      269                 :            475 :         EventTriggerCache = NULL;
                                270                 :                :     }
                                271                 :                : 
                                272                 :                :     /* Mark cache for rebuild. */
                                273                 :            959 :     EventTriggerCacheState = ETCS_NEEDS_REBUILD;
 5037                           274                 :            959 : }
        

Generated by: LCOV version 2.5.0-beta