* Rebuild the event trigger cache. */
| 75 | * Rebuild the event trigger cache. |
| 76 | */ |
| 77 | static void |
| 78 | BuildEventTriggerCache(void) |
| 79 | { |
| 80 | HASHCTL ctl; |
| 81 | HTAB *cache; |
| 82 | MemoryContext oldcontext; |
| 83 | Relation rel; |
| 84 | Relation irel; |
| 85 | SysScanDesc scan; |
| 86 | |
| 87 | 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 | 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 | if (CacheMemoryContext == NULL) |
| 104 | CreateCacheMemoryContext(); |
| 105 | EventTriggerCacheContext = |
| 106 | AllocSetContextCreate(CacheMemoryContext, |
| 107 | "EventTriggerCache", |
| 108 | ALLOCSET_DEFAULT_SIZES); |
| 109 | CacheRegisterSyscacheCallback(EVENTTRIGGEROID, |
| 110 | InvalidateEventCacheCallback, |
| 111 | (Datum) 0); |
| 112 | } |
| 113 | |
| 114 | /* Switch to correct memory context. */ |
| 115 | oldcontext = MemoryContextSwitchTo(EventTriggerCacheContext); |
| 116 | |
| 117 | /* Prevent the memory context from being nuked while we're rebuilding. */ |
| 118 | EventTriggerCacheState = ETCS_REBUILD_STARTED; |
| 119 | |
| 120 | /* Create new hash table. */ |
| 121 | ctl.keysize = sizeof(EventTriggerEvent); |
| 122 | ctl.entrysize = sizeof(EventTriggerCacheEntry); |
| 123 | ctl.hcxt = EventTriggerCacheContext; |
| 124 | cache = hash_create("Event Trigger Cache", 32, &ctl, |
| 125 | HASH_ELEM | HASH_BLOBS | HASH_CONTEXT); |
| 126 | |
| 127 | /* |
| 128 | * Prepare to scan pg_event_trigger in name order. |
| 129 | */ |
| 130 | rel = relation_open(EventTriggerRelationId, AccessShareLock); |
| 131 | irel = index_open(EventTriggerNameIndexId, AccessShareLock); |
| 132 | scan = systable_beginscan_ordered(rel, irel, NULL, 0, NULL); |
| 133 | |
| 134 | /* |
no test coverage detected