LCOV - code coverage report
Current view: top level - src/backend/utils/cache - inval.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 337 345 97.7 %
Date: 2023-12-11 16:10:55 Functions: 36 36 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * inval.c
       4             :  *    POSTGRES cache invalidation dispatcher code.
       5             :  *
       6             :  *  This is subtle stuff, so pay attention:
       7             :  *
       8             :  *  When a tuple is updated or deleted, our standard visibility rules
       9             :  *  consider that it is *still valid* so long as we are in the same command,
      10             :  *  ie, until the next CommandCounterIncrement() or transaction commit.
      11             :  *  (See access/heap/heapam_visibility.c, and note that system catalogs are
      12             :  *  generally scanned under the most current snapshot available, rather than
      13             :  *  the transaction snapshot.)  At the command boundary, the old tuple stops
      14             :  *  being valid and the new version, if any, becomes valid.  Therefore,
      15             :  *  we cannot simply flush a tuple from the system caches during heap_update()
      16             :  *  or heap_delete().  The tuple is still good at that point; what's more,
      17             :  *  even if we did flush it, it might be reloaded into the caches by a later
      18             :  *  request in the same command.  So the correct behavior is to keep a list
      19             :  *  of outdated (updated/deleted) tuples and then do the required cache
      20             :  *  flushes at the next command boundary.  We must also keep track of
      21             :  *  inserted tuples so that we can flush "negative" cache entries that match
      22             :  *  the new tuples; again, that mustn't happen until end of command.
      23             :  *
      24             :  *  Once we have finished the command, we still need to remember inserted
      25             :  *  tuples (including new versions of updated tuples), so that we can flush
      26             :  *  them from the caches if we abort the transaction.  Similarly, we'd better
      27             :  *  be able to flush "negative" cache entries that may have been loaded in
      28             :  *  place of deleted tuples, so we still need the deleted ones too.
      29             :  *
      30             :  *  If we successfully complete the transaction, we have to broadcast all
      31             :  *  these invalidation events to other backends (via the SI message queue)
      32             :  *  so that they can flush obsolete entries from their caches.  Note we have
      33             :  *  to record the transaction commit before sending SI messages, otherwise
      34             :  *  the other backends won't see our updated tuples as good.
      35             :  *
      36             :  *  When a subtransaction aborts, we can process and discard any events
      37             :  *  it has queued.  When a subtransaction commits, we just add its events
      38             :  *  to the pending lists of the parent transaction.
      39             :  *
      40             :  *  In short, we need to remember until xact end every insert or delete
      41             :  *  of a tuple that might be in the system caches.  Updates are treated as
      42             :  *  two events, delete + insert, for simplicity.  (If the update doesn't
      43             :  *  change the tuple hash value, catcache.c optimizes this into one event.)
      44             :  *
      45             :  *  We do not need to register EVERY tuple operation in this way, just those
      46             :  *  on tuples in relations that have associated catcaches.  We do, however,
      47             :  *  have to register every operation on every tuple that *could* be in a
      48             :  *  catcache, whether or not it currently is in our cache.  Also, if the
      49             :  *  tuple is in a relation that has multiple catcaches, we need to register
      50             :  *  an invalidation message for each such catcache.  catcache.c's
      51             :  *  PrepareToInvalidateCacheTuple() routine provides the knowledge of which
      52             :  *  catcaches may need invalidation for a given tuple.
      53             :  *
      54             :  *  Also, whenever we see an operation on a pg_class, pg_attribute, or
      55             :  *  pg_index tuple, we register a relcache flush operation for the relation
      56             :  *  described by that tuple (as specified in CacheInvalidateHeapTuple()).
      57             :  *  Likewise for pg_constraint tuples for foreign keys on relations.
      58             :  *
      59             :  *  We keep the relcache flush requests in lists separate from the catcache
      60             :  *  tuple flush requests.  This allows us to issue all the pending catcache
      61             :  *  flushes before we issue relcache flushes, which saves us from loading
      62             :  *  a catcache tuple during relcache load only to flush it again right away.
      63             :  *  Also, we avoid queuing multiple relcache flush requests for the same
      64             :  *  relation, since a relcache flush is relatively expensive to do.
      65             :  *  (XXX is it worth testing likewise for duplicate catcache flush entries?
      66             :  *  Probably not.)
      67             :  *
      68             :  *  Many subsystems own higher-level caches that depend on relcache and/or
      69             :  *  catcache, and they register callbacks here to invalidate their caches.
      70             :  *  While building a higher-level cache entry, a backend may receive a
      71             :  *  callback for the being-built entry or one of its dependencies.  This
      72             :  *  implies the new higher-level entry would be born stale, and it might
      73             :  *  remain stale for the life of the backend.  Many caches do not prevent
      74             :  *  that.  They rely on DDL for can't-miss catalog changes taking
      75             :  *  AccessExclusiveLock on suitable objects.  (For a change made with less
      76             :  *  locking, backends might never read the change.)  The relation cache,
      77             :  *  however, needs to reflect changes from CREATE INDEX CONCURRENTLY no later
      78             :  *  than the beginning of the next transaction.  Hence, when a relevant
      79             :  *  invalidation callback arrives during a build, relcache.c reattempts that
      80             :  *  build.  Caches with similar needs could do likewise.
      81             :  *
      82             :  *  If a relcache flush is issued for a system relation that we preload
      83             :  *  from the relcache init file, we must also delete the init file so that
      84             :  *  it will be rebuilt during the next backend restart.  The actual work of
      85             :  *  manipulating the init file is in relcache.c, but we keep track of the
      86             :  *  need for it here.
      87             :  *
      88             :  *  Currently, inval messages are sent without regard for the possibility
      89             :  *  that the object described by the catalog tuple might be a session-local
      90             :  *  object such as a temporary table.  This is because (1) this code has
      91             :  *  no practical way to tell the difference, and (2) it is not certain that
      92             :  *  other backends don't have catalog cache or even relcache entries for
      93             :  *  such tables, anyway; there is nothing that prevents that.  It might be
      94             :  *  worth trying to avoid sending such inval traffic in the future, if those
      95             :  *  problems can be overcome cheaply.
      96             :  *
      97             :  *  When wal_level=logical, write invalidations into WAL at each command end to
      98             :  *  support the decoding of the in-progress transactions.  See
      99             :  *  CommandEndInvalidationMessages.
     100             :  *
     101             :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
     102             :  * Portions Copyright (c) 1994, Regents of the University of California
     103             :  *
     104             :  * IDENTIFICATION
     105             :  *    src/backend/utils/cache/inval.c
     106             :  *
     107             :  *-------------------------------------------------------------------------
     108             :  */
     109             : #include "postgres.h"
     110             : 
     111             : #include <limits.h>
     112             : 
     113             : #include "access/htup_details.h"
     114             : #include "access/xact.h"
     115             : #include "access/xloginsert.h"
     116             : #include "catalog/catalog.h"
     117             : #include "catalog/pg_constraint.h"
     118             : #include "miscadmin.h"
     119             : #include "storage/sinval.h"
     120             : #include "storage/smgr.h"
     121             : #include "utils/catcache.h"
     122             : #include "utils/guc.h"
     123             : #include "utils/inval.h"
     124             : #include "utils/memdebug.h"
     125             : #include "utils/memutils.h"
     126             : #include "utils/rel.h"
     127             : #include "utils/relmapper.h"
     128             : #include "utils/snapmgr.h"
     129             : #include "utils/syscache.h"
     130             : 
     131             : 
     132             : /*
     133             :  * Pending requests are stored as ready-to-send SharedInvalidationMessages.
     134             :  * We keep the messages themselves in arrays in TopTransactionContext
     135             :  * (there are separate arrays for catcache and relcache messages).  Control
     136             :  * information is kept in a chain of TransInvalidationInfo structs, also
     137             :  * allocated in TopTransactionContext.  (We could keep a subtransaction's
     138             :  * TransInvalidationInfo in its CurTransactionContext; but that's more
     139             :  * wasteful not less so, since in very many scenarios it'd be the only
     140             :  * allocation in the subtransaction's CurTransactionContext.)
     141             :  *
     142             :  * We can store the message arrays densely, and yet avoid moving data around
     143             :  * within an array, because within any one subtransaction we need only
     144             :  * distinguish between messages emitted by prior commands and those emitted
     145             :  * by the current command.  Once a command completes and we've done local
     146             :  * processing on its messages, we can fold those into the prior-commands
     147             :  * messages just by changing array indexes in the TransInvalidationInfo
     148             :  * struct.  Similarly, we need distinguish messages of prior subtransactions
     149             :  * from those of the current subtransaction only until the subtransaction
     150             :  * completes, after which we adjust the array indexes in the parent's
     151             :  * TransInvalidationInfo to include the subtransaction's messages.
     152             :  *
     153             :  * The ordering of the individual messages within a command's or
     154             :  * subtransaction's output is not considered significant, although this
     155             :  * implementation happens to preserve the order in which they were queued.
     156             :  * (Previous versions of this code did not preserve it.)
     157             :  *
     158             :  * For notational convenience, control information is kept in two-element
     159             :  * arrays, the first for catcache messages and the second for relcache
     160             :  * messages.
     161             :  */
     162             : #define CatCacheMsgs 0
     163             : #define RelCacheMsgs 1
     164             : 
     165             : /* Pointers to main arrays in TopTransactionContext */
     166             : typedef struct InvalMessageArray
     167             : {
     168             :     SharedInvalidationMessage *msgs;    /* palloc'd array (can be expanded) */
     169             :     int         maxmsgs;        /* current allocated size of array */
     170             : } InvalMessageArray;
     171             : 
     172             : static InvalMessageArray InvalMessageArrays[2];
     173             : 
     174             : /* Control information for one logical group of messages */
     175             : typedef struct InvalidationMsgsGroup
     176             : {
     177             :     int         firstmsg[2];    /* first index in relevant array */
     178             :     int         nextmsg[2];     /* last+1 index */
     179             : } InvalidationMsgsGroup;
     180             : 
     181             : /* Macros to help preserve InvalidationMsgsGroup abstraction */
     182             : #define SetSubGroupToFollow(targetgroup, priorgroup, subgroup) \
     183             :     do { \
     184             :         (targetgroup)->firstmsg[subgroup] = \
     185             :             (targetgroup)->nextmsg[subgroup] = \
     186             :             (priorgroup)->nextmsg[subgroup]; \
     187             :     } while (0)
     188             : 
     189             : #define SetGroupToFollow(targetgroup, priorgroup) \
     190             :     do { \
     191             :         SetSubGroupToFollow(targetgroup, priorgroup, CatCacheMsgs); \
     192             :         SetSubGroupToFollow(targetgroup, priorgroup, RelCacheMsgs); \
     193             :     } while (0)
     194             : 
     195             : #define NumMessagesInSubGroup(group, subgroup) \
     196             :     ((group)->nextmsg[subgroup] - (group)->firstmsg[subgroup])
     197             : 
     198             : #define NumMessagesInGroup(group) \
     199             :     (NumMessagesInSubGroup(group, CatCacheMsgs) + \
     200             :      NumMessagesInSubGroup(group, RelCacheMsgs))
     201             : 
     202             : 
     203             : /*----------------
     204             :  * Invalidation messages are divided into two groups:
     205             :  *  1) events so far in current command, not yet reflected to caches.
     206             :  *  2) events in previous commands of current transaction; these have
     207             :  *     been reflected to local caches, and must be either broadcast to
     208             :  *     other backends or rolled back from local cache when we commit
     209             :  *     or abort the transaction.
     210             :  * Actually, we need such groups for each level of nested transaction,
     211             :  * so that we can discard events from an aborted subtransaction.  When
     212             :  * a subtransaction commits, we append its events to the parent's groups.
     213             :  *
     214             :  * The relcache-file-invalidated flag can just be a simple boolean,
     215             :  * since we only act on it at transaction commit; we don't care which
     216             :  * command of the transaction set it.
     217             :  *----------------
     218             :  */
     219             : 
     220             : typedef struct TransInvalidationInfo
     221             : {
     222             :     /* Back link to parent transaction's info */
     223             :     struct TransInvalidationInfo *parent;
     224             : 
     225             :     /* Subtransaction nesting depth */
     226             :     int         my_level;
     227             : 
     228             :     /* Events emitted by current command */
     229             :     InvalidationMsgsGroup CurrentCmdInvalidMsgs;
     230             : 
     231             :     /* Events emitted by previous commands of this (sub)transaction */
     232             :     InvalidationMsgsGroup PriorCmdInvalidMsgs;
     233             : 
     234             :     /* init file must be invalidated? */
     235             :     bool        RelcacheInitFileInval;
     236             : } TransInvalidationInfo;
     237             : 
     238             : static TransInvalidationInfo *transInvalInfo = NULL;
     239             : 
     240             : /* GUC storage */
     241             : int         debug_discard_caches = 0;
     242             : 
     243             : /*
     244             :  * Dynamically-registered callback functions.  Current implementation
     245             :  * assumes there won't be enough of these to justify a dynamically resizable
     246             :  * array; it'd be easy to improve that if needed.
     247             :  *
     248             :  * To avoid searching in CallSyscacheCallbacks, all callbacks for a given
     249             :  * syscache are linked into a list pointed to by syscache_callback_links[id].
     250             :  * The link values are syscache_callback_list[] index plus 1, or 0 for none.
     251             :  */
     252             : 
     253             : #define MAX_SYSCACHE_CALLBACKS 64
     254             : #define MAX_RELCACHE_CALLBACKS 10
     255             : 
     256             : static struct SYSCACHECALLBACK
     257             : {
     258             :     int16       id;             /* cache number */
     259             :     int16       link;           /* next callback index+1 for same cache */
     260             :     SyscacheCallbackFunction function;
     261             :     Datum       arg;
     262             : }           syscache_callback_list[MAX_SYSCACHE_CALLBACKS];
     263             : 
     264             : static int16 syscache_callback_links[SysCacheSize];
     265             : 
     266             : static int  syscache_callback_count = 0;
     267             : 
     268             : static struct RELCACHECALLBACK
     269             : {
     270             :     RelcacheCallbackFunction function;
     271             :     Datum       arg;
     272             : }           relcache_callback_list[MAX_RELCACHE_CALLBACKS];
     273             : 
     274             : static int  relcache_callback_count = 0;
     275             : 
     276             : /* ----------------------------------------------------------------
     277             :  *              Invalidation subgroup support functions
     278             :  * ----------------------------------------------------------------
     279             :  */
     280             : 
     281             : /*
     282             :  * AddInvalidationMessage
     283             :  *      Add an invalidation message to a (sub)group.
     284             :  *
     285             :  * The group must be the last active one, since we assume we can add to the
     286             :  * end of the relevant InvalMessageArray.
     287             :  *
     288             :  * subgroup must be CatCacheMsgs or RelCacheMsgs.
     289             :  */
     290             : static void
     291     5155228 : AddInvalidationMessage(InvalidationMsgsGroup *group, int subgroup,
     292             :                        const SharedInvalidationMessage *msg)
     293             : {
     294     5155228 :     InvalMessageArray *ima = &InvalMessageArrays[subgroup];
     295     5155228 :     int         nextindex = group->nextmsg[subgroup];
     296             : 
     297     5155228 :     if (nextindex >= ima->maxmsgs)
     298             :     {
     299      347794 :         if (ima->msgs == NULL)
     300             :         {
     301             :             /* Create new storage array in TopTransactionContext */
     302      301188 :             int         reqsize = 32;   /* arbitrary */
     303             : 
     304      301188 :             ima->msgs = (SharedInvalidationMessage *)
     305      301188 :                 MemoryContextAlloc(TopTransactionContext,
     306             :                                    reqsize * sizeof(SharedInvalidationMessage));
     307      301188 :             ima->maxmsgs = reqsize;
     308             :             Assert(nextindex == 0);
     309             :         }
     310             :         else
     311             :         {
     312             :             /* Enlarge storage array */
     313       46606 :             int         reqsize = 2 * ima->maxmsgs;
     314             : 
     315       46606 :             ima->msgs = (SharedInvalidationMessage *)
     316       46606 :                 repalloc(ima->msgs,
     317             :                          reqsize * sizeof(SharedInvalidationMessage));
     318       46606 :             ima->maxmsgs = reqsize;
     319             :         }
     320             :     }
     321             :     /* Okay, add message to current group */
     322     5155228 :     ima->msgs[nextindex] = *msg;
     323     5155228 :     group->nextmsg[subgroup]++;
     324     5155228 : }
     325             : 
     326             : /*
     327             :  * Append one subgroup of invalidation messages to another, resetting
     328             :  * the source subgroup to empty.
     329             :  */
     330             : static void
     331     1447160 : AppendInvalidationMessageSubGroup(InvalidationMsgsGroup *dest,
     332             :                                   InvalidationMsgsGroup *src,
     333             :                                   int subgroup)
     334             : {
     335             :     /* Messages must be adjacent in main array */
     336             :     Assert(dest->nextmsg[subgroup] == src->firstmsg[subgroup]);
     337             : 
     338             :     /* ... which makes this easy: */
     339     1447160 :     dest->nextmsg[subgroup] = src->nextmsg[subgroup];
     340             : 
     341             :     /*
     342             :      * This is handy for some callers and irrelevant for others.  But we do it
     343             :      * always, reasoning that it's bad to leave different groups pointing at
     344             :      * the same fragment of the message array.
     345             :      */
     346     1447160 :     SetSubGroupToFollow(src, dest, subgroup);
     347     1447160 : }
     348             : 
     349             : /*
     350             :  * Process a subgroup of invalidation messages.
     351             :  *
     352             :  * This is a macro that executes the given code fragment for each message in
     353             :  * a message subgroup.  The fragment should refer to the message as *msg.
     354             :  */
     355             : #define ProcessMessageSubGroup(group, subgroup, codeFragment) \
     356             :     do { \
     357             :         int     _msgindex = (group)->firstmsg[subgroup]; \
     358             :         int     _endmsg = (group)->nextmsg[subgroup]; \
     359             :         for (; _msgindex < _endmsg; _msgindex++) \
     360             :         { \
     361             :             SharedInvalidationMessage *msg = \
     362             :                 &InvalMessageArrays[subgroup].msgs[_msgindex]; \
     363             :             codeFragment; \
     364             :         } \
     365             :     } while (0)
     366             : 
     367             : /*
     368             :  * Process a subgroup of invalidation messages as an array.
     369             :  *
     370             :  * As above, but the code fragment can handle an array of messages.
     371             :  * The fragment should refer to the messages as msgs[], with n entries.
     372             :  */
     373             : #define ProcessMessageSubGroupMulti(group, subgroup, codeFragment) \
     374             :     do { \
     375             :         int     n = NumMessagesInSubGroup(group, subgroup); \
     376             :         if (n > 0) { \
     377             :             SharedInvalidationMessage *msgs = \
     378             :                 &InvalMessageArrays[subgroup].msgs[(group)->firstmsg[subgroup]]; \
     379             :             codeFragment; \
     380             :         } \
     381             :     } while (0)
     382             : 
     383             : 
     384             : /* ----------------------------------------------------------------
     385             :  *              Invalidation group support functions
     386             :  *
     387             :  * These routines understand about the division of a logical invalidation
     388             :  * group into separate physical arrays for catcache and relcache entries.
     389             :  * ----------------------------------------------------------------
     390             :  */
     391             : 
     392             : /*
     393             :  * Add a catcache inval entry
     394             :  */
     395             : static void
     396     4264430 : AddCatcacheInvalidationMessage(InvalidationMsgsGroup *group,
     397             :                                int id, uint32 hashValue, Oid dbId)
     398             : {
     399             :     SharedInvalidationMessage msg;
     400             : 
     401             :     Assert(id < CHAR_MAX);
     402     4264430 :     msg.cc.id = (int8) id;
     403     4264430 :     msg.cc.dbId = dbId;
     404     4264430 :     msg.cc.hashValue = hashValue;
     405             : 
     406             :     /*
     407             :      * Define padding bytes in SharedInvalidationMessage structs to be
     408             :      * defined. Otherwise the sinvaladt.c ringbuffer, which is accessed by
     409             :      * multiple processes, will cause spurious valgrind warnings about
     410             :      * undefined memory being used. That's because valgrind remembers the
     411             :      * undefined bytes from the last local process's store, not realizing that
     412             :      * another process has written since, filling the previously uninitialized
     413             :      * bytes
     414             :      */
     415             :     VALGRIND_MAKE_MEM_DEFINED(&msg, sizeof(msg));
     416             : 
     417     4264430 :     AddInvalidationMessage(group, CatCacheMsgs, &msg);
     418     4264430 : }
     419             : 
     420             : /*
     421             :  * Add a whole-catalog inval entry
     422             :  */
     423             : static void
     424         202 : AddCatalogInvalidationMessage(InvalidationMsgsGroup *group,
     425             :                               Oid dbId, Oid catId)
     426             : {
     427             :     SharedInvalidationMessage msg;
     428             : 
     429         202 :     msg.cat.id = SHAREDINVALCATALOG_ID;
     430         202 :     msg.cat.dbId = dbId;
     431         202 :     msg.cat.catId = catId;
     432             :     /* check AddCatcacheInvalidationMessage() for an explanation */
     433             :     VALGRIND_MAKE_MEM_DEFINED(&msg, sizeof(msg));
     434             : 
     435         202 :     AddInvalidationMessage(group, CatCacheMsgs, &msg);
     436         202 : }
     437             : 
     438             : /*
     439             :  * Add a relcache inval entry
     440             :  */
     441             : static void
     442     1462626 : AddRelcacheInvalidationMessage(InvalidationMsgsGroup *group,
     443             :                                Oid dbId, Oid relId)
     444             : {
     445             :     SharedInvalidationMessage msg;
     446             : 
     447             :     /*
     448             :      * Don't add a duplicate item. We assume dbId need not be checked because
     449             :      * it will never change. InvalidOid for relId means all relations so we
     450             :      * don't need to add individual ones when it is present.
     451             :      */
     452     4876186 :     ProcessMessageSubGroup(group, RelCacheMsgs,
     453             :                            if (msg->rc.id == SHAREDINVALRELCACHE_ID &&
     454             :                                (msg->rc.relId == relId ||
     455             :                                 msg->rc.relId == InvalidOid))
     456             :                            return);
     457             : 
     458             :     /* OK, add the item */
     459      483792 :     msg.rc.id = SHAREDINVALRELCACHE_ID;
     460      483792 :     msg.rc.dbId = dbId;
     461      483792 :     msg.rc.relId = relId;
     462             :     /* check AddCatcacheInvalidationMessage() for an explanation */
     463             :     VALGRIND_MAKE_MEM_DEFINED(&msg, sizeof(msg));
     464             : 
     465      483792 :     AddInvalidationMessage(group, RelCacheMsgs, &msg);
     466             : }
     467             : 
     468             : /*
     469             :  * Add a snapshot inval entry
     470             :  *
     471             :  * We put these into the relcache subgroup for simplicity.
     472             :  */
     473             : static void
     474      801092 : AddSnapshotInvalidationMessage(InvalidationMsgsGroup *group,
     475             :                                Oid dbId, Oid relId)
     476             : {
     477             :     SharedInvalidationMessage msg;
     478             : 
     479             :     /* Don't add a duplicate item */
     480             :     /* We assume dbId need not be checked because it will never change */
     481     1203946 :     ProcessMessageSubGroup(group, RelCacheMsgs,
     482             :                            if (msg->sn.id == SHAREDINVALSNAPSHOT_ID &&
     483             :                                msg->sn.relId == relId)
     484             :                            return);
     485             : 
     486             :     /* OK, add the item */
     487      406804 :     msg.sn.id = SHAREDINVALSNAPSHOT_ID;
     488      406804 :     msg.sn.dbId = dbId;
     489      406804 :     msg.sn.relId = relId;
     490             :     /* check AddCatcacheInvalidationMessage() for an explanation */
     491             :     VALGRIND_MAKE_MEM_DEFINED(&msg, sizeof(msg));
     492             : 
     493      406804 :     AddInvalidationMessage(group, RelCacheMsgs, &msg);
     494             : }
     495             : 
     496             : /*
     497             :  * Append one group of invalidation messages to another, resetting
     498             :  * the source group to empty.
     499             :  */
     500             : static void
     501      723580 : AppendInvalidationMessages(InvalidationMsgsGroup *dest,
     502             :                            InvalidationMsgsGroup *src)
     503             : {
     504      723580 :     AppendInvalidationMessageSubGroup(dest, src, CatCacheMsgs);
     505      723580 :     AppendInvalidationMessageSubGroup(dest, src, RelCacheMsgs);
     506      723580 : }
     507             : 
     508             : /*
     509             :  * Execute the given function for all the messages in an invalidation group.
     510             :  * The group is not altered.
     511             :  *
     512             :  * catcache entries are processed first, for reasons mentioned above.
     513             :  */
     514             : static void
     515      562080 : ProcessInvalidationMessages(InvalidationMsgsGroup *group,
     516             :                             void (*func) (SharedInvalidationMessage *msg))
     517             : {
     518     4801240 :     ProcessMessageSubGroup(group, CatCacheMsgs, func(msg));
     519     1435902 :     ProcessMessageSubGroup(group, RelCacheMsgs, func(msg));
     520      562074 : }
     521             : 
     522             : /*
     523             :  * As above, but the function is able to process an array of messages
     524             :  * rather than just one at a time.
     525             :  */
     526             : static void
     527      165320 : ProcessInvalidationMessagesMulti(InvalidationMsgsGroup *group,
     528             :                                  void (*func) (const SharedInvalidationMessage *msgs, int n))
     529             : {
     530      165320 :     ProcessMessageSubGroupMulti(group, CatCacheMsgs, func(msgs, n));
     531      165320 :     ProcessMessageSubGroupMulti(group, RelCacheMsgs, func(msgs, n));
     532      165320 : }
     533             : 
     534             : /* ----------------------------------------------------------------
     535             :  *                    private support functions
     536             :  * ----------------------------------------------------------------
     537             :  */
     538             : 
     539             : /*
     540             :  * RegisterCatcacheInvalidation
     541             :  *
     542             :  * Register an invalidation event for a catcache tuple entry.
     543             :  */
     544             : static void
     545     4264430 : RegisterCatcacheInvalidation(int cacheId,
     546             :                              uint32 hashValue,
     547             :                              Oid dbId)
     548             : {
     549     4264430 :     AddCatcacheInvalidationMessage(&transInvalInfo->CurrentCmdInvalidMsgs,
     550             :                                    cacheId, hashValue, dbId);
     551     4264430 : }
     552             : 
     553             : /*
     554             :  * RegisterCatalogInvalidation
     555             :  *
     556             :  * Register an invalidation event for all catcache entries from a catalog.
     557             :  */
     558             : static void
     559         202 : RegisterCatalogInvalidation(Oid dbId, Oid catId)
     560             : {
     561         202 :     AddCatalogInvalidationMessage(&transInvalInfo->CurrentCmdInvalidMsgs,
     562             :                                   dbId, catId);
     563         202 : }
     564             : 
     565             : /*
     566             :  * RegisterRelcacheInvalidation
     567             :  *
     568             :  * As above, but register a relcache invalidation event.
     569             :  */
     570             : static void
     571     1462626 : RegisterRelcacheInvalidation(Oid dbId, Oid relId)
     572             : {
     573     1462626 :     AddRelcacheInvalidationMessage(&transInvalInfo->CurrentCmdInvalidMsgs,
     574             :                                    dbId, relId);
     575             : 
     576             :     /*
     577             :      * Most of the time, relcache invalidation is associated with system
     578             :      * catalog updates, but there are a few cases where it isn't.  Quick hack
     579             :      * to ensure that the next CommandCounterIncrement() will think that we
     580             :      * need to do CommandEndInvalidationMessages().
     581             :      */
     582     1462626 :     (void) GetCurrentCommandId(true);
     583             : 
     584             :     /*
     585             :      * If the relation being invalidated is one of those cached in a relcache
     586             :      * init file, mark that we need to zap that file at commit. For simplicity
     587             :      * invalidations for a specific database always invalidate the shared file
     588             :      * as well.  Also zap when we are invalidating whole relcache.
     589             :      */
     590     1462626 :     if (relId == InvalidOid || RelationIdIsInInitFile(relId))
     591       33434 :         transInvalInfo->RelcacheInitFileInval = true;
     592     1462626 : }
     593             : 
     594             : /*
     595             :  * RegisterSnapshotInvalidation
     596             :  *
     597             :  * Register an invalidation event for MVCC scans against a given catalog.
     598             :  * Only needed for catalogs that don't have catcaches.
     599             :  */
     600             : static void
     601      801092 : RegisterSnapshotInvalidation(Oid dbId, Oid relId)
     602             : {
     603      801092 :     AddSnapshotInvalidationMessage(&transInvalInfo->CurrentCmdInvalidMsgs,
     604             :                                    dbId, relId);
     605      801092 : }
     606             : 
     607             : /*
     608             :  * PrepareInvalidationState
     609             :  *      Initialize inval data for the current (sub)transaction.
     610             :  */
     611             : static void
     612     3321894 : PrepareInvalidationState(void)
     613             : {
     614             :     TransInvalidationInfo *myInfo;
     615             : 
     616     6474748 :     if (transInvalInfo != NULL &&
     617     3152854 :         transInvalInfo->my_level == GetCurrentTransactionNestLevel())
     618     3152712 :         return;
     619             : 
     620             :     myInfo = (TransInvalidationInfo *)
     621      169182 :         MemoryContextAllocZero(TopTransactionContext,
     622             :                                sizeof(TransInvalidationInfo));
     623      169182 :     myInfo->parent = transInvalInfo;
     624      169182 :     myInfo->my_level = GetCurrentTransactionNestLevel();
     625             : 
     626             :     /* Now, do we have a previous stack entry? */
     627      169182 :     if (transInvalInfo != NULL)
     628             :     {
     629             :         /* Yes; this one should be for a deeper nesting level. */
     630             :         Assert(myInfo->my_level > transInvalInfo->my_level);
     631             : 
     632             :         /*
     633             :          * The parent (sub)transaction must not have any current (i.e.,
     634             :          * not-yet-locally-processed) messages.  If it did, we'd have a
     635             :          * semantic problem: the new subtransaction presumably ought not be
     636             :          * able to see those events yet, but since the CommandCounter is
     637             :          * linear, that can't work once the subtransaction advances the
     638             :          * counter.  This is a convenient place to check for that, as well as
     639             :          * being important to keep management of the message arrays simple.
     640             :          */
     641         142 :         if (NumMessagesInGroup(&transInvalInfo->CurrentCmdInvalidMsgs) != 0)
     642           0 :             elog(ERROR, "cannot start a subtransaction when there are unprocessed inval messages");
     643             : 
     644             :         /*
     645             :          * MemoryContextAllocZero set firstmsg = nextmsg = 0 in each group,
     646             :          * which is fine for the first (sub)transaction, but otherwise we need
     647             :          * to update them to follow whatever is already in the arrays.
     648             :          */
     649         142 :         SetGroupToFollow(&myInfo->PriorCmdInvalidMsgs,
     650             :                          &transInvalInfo->CurrentCmdInvalidMsgs);
     651         142 :         SetGroupToFollow(&myInfo->CurrentCmdInvalidMsgs,
     652             :                          &myInfo->PriorCmdInvalidMsgs);
     653             :     }
     654             :     else
     655             :     {
     656             :         /*
     657             :          * Here, we need only clear any array pointers left over from a prior
     658             :          * transaction.
     659             :          */
     660      169040 :         InvalMessageArrays[CatCacheMsgs].msgs = NULL;
     661      169040 :         InvalMessageArrays[CatCacheMsgs].maxmsgs = 0;
     662      169040 :         InvalMessageArrays[RelCacheMsgs].msgs = NULL;
     663      169040 :         InvalMessageArrays[RelCacheMsgs].maxmsgs = 0;
     664             :     }
     665             : 
     666      169182 :     transInvalInfo = myInfo;
     667             : }
     668             : 
     669             : /* ----------------------------------------------------------------
     670             :  *                    public functions
     671             :  * ----------------------------------------------------------------
     672             :  */
     673             : 
     674             : void
     675        3862 : InvalidateSystemCachesExtended(bool debug_discard)
     676             : {
     677             :     int         i;
     678             : 
     679        3862 :     InvalidateCatalogSnapshot();
     680        3862 :     ResetCatalogCaches();
     681        3862 :     RelationCacheInvalidate(debug_discard); /* gets smgr and relmap too */
     682             : 
     683       58254 :     for (i = 0; i < syscache_callback_count; i++)
     684             :     {
     685       54392 :         struct SYSCACHECALLBACK *ccitem = syscache_callback_list + i;
     686             : 
     687       54392 :         ccitem->function(ccitem->arg, ccitem->id, 0);
     688             :     }
     689             : 
     690        8804 :     for (i = 0; i < relcache_callback_count; i++)
     691             :     {
     692        4942 :         struct RELCACHECALLBACK *ccitem = relcache_callback_list + i;
     693             : 
     694        4942 :         ccitem->function(ccitem->arg, InvalidOid);
     695             :     }
     696        3862 : }
     697             : 
     698             : /*
     699             :  * LocalExecuteInvalidationMessage
     700             :  *
     701             :  * Process a single invalidation message (which could be of any type).
     702             :  * Only the local caches are flushed; this does not transmit the message
     703             :  * to other backends.
     704             :  */
     705             : void
     706    28440218 : LocalExecuteInvalidationMessage(SharedInvalidationMessage *msg)
     707             : {
     708    28440218 :     if (msg->id >= 0)
     709             :     {
     710    23235984 :         if (msg->cc.dbId == MyDatabaseId || msg->cc.dbId == InvalidOid)
     711             :         {
     712    16450260 :             InvalidateCatalogSnapshot();
     713             : 
     714    16450260 :             SysCacheInvalidate(msg->cc.id, msg->cc.hashValue);
     715             : 
     716    16450260 :             CallSyscacheCallbacks(msg->cc.id, msg->cc.hashValue);
     717             :         }
     718             :     }
     719     5204234 :     else if (msg->id == SHAREDINVALCATALOG_ID)
     720             :     {
     721         772 :         if (msg->cat.dbId == MyDatabaseId || msg->cat.dbId == InvalidOid)
     722             :         {
     723         664 :             InvalidateCatalogSnapshot();
     724             : 
     725         664 :             CatalogCacheFlushCatalog(msg->cat.catId);
     726             : 
     727             :             /* CatalogCacheFlushCatalog calls CallSyscacheCallbacks as needed */
     728             :         }
     729             :     }
     730     5203462 :     else if (msg->id == SHAREDINVALRELCACHE_ID)
     731             :     {
     732     2621028 :         if (msg->rc.dbId == MyDatabaseId || msg->rc.dbId == InvalidOid)
     733             :         {
     734             :             int         i;
     735             : 
     736     1891714 :             if (msg->rc.relId == InvalidOid)
     737         234 :                 RelationCacheInvalidate(false);
     738             :             else
     739     1891480 :                 RelationCacheInvalidateEntry(msg->rc.relId);
     740             : 
     741     5231882 :             for (i = 0; i < relcache_callback_count; i++)
     742             :             {
     743     3340174 :                 struct RELCACHECALLBACK *ccitem = relcache_callback_list + i;
     744             : 
     745     3340174 :                 ccitem->function(ccitem->arg, msg->rc.relId);
     746             :             }
     747             :         }
     748             :     }
     749     2582434 :     else if (msg->id == SHAREDINVALSMGR_ID)
     750             :     {
     751             :         /*
     752             :          * We could have smgr entries for relations of other databases, so no
     753             :          * short-circuit test is possible here.
     754             :          */
     755             :         RelFileLocatorBackend rlocator;
     756             : 
     757      360984 :         rlocator.locator = msg->sm.rlocator;
     758      360984 :         rlocator.backend = (msg->sm.backend_hi << 16) | (int) msg->sm.backend_lo;
     759      360984 :         smgrcloserellocator(rlocator);
     760             :     }
     761     2221450 :     else if (msg->id == SHAREDINVALRELMAP_ID)
     762             :     {
     763             :         /* We only care about our own database and shared catalogs */
     764         330 :         if (msg->rm.dbId == InvalidOid)
     765         164 :             RelationMapInvalidate(true);
     766         166 :         else if (msg->rm.dbId == MyDatabaseId)
     767         100 :             RelationMapInvalidate(false);
     768             :     }
     769     2221120 :     else if (msg->id == SHAREDINVALSNAPSHOT_ID)
     770             :     {
     771             :         /* We only care about our own database and shared catalogs */
     772     2221120 :         if (msg->sn.dbId == InvalidOid)
     773       74690 :             InvalidateCatalogSnapshot();
     774     2146430 :         else if (msg->sn.dbId == MyDatabaseId)
     775     1572326 :             InvalidateCatalogSnapshot();
     776             :     }
     777             :     else
     778           0 :         elog(FATAL, "unrecognized SI message ID: %d", msg->id);
     779    28440212 : }
     780             : 
     781             : /*
     782             :  *      InvalidateSystemCaches
     783             :  *
     784             :  *      This blows away all tuples in the system catalog caches and
     785             :  *      all the cached relation descriptors and smgr cache entries.
     786             :  *      Relation descriptors that have positive refcounts are then rebuilt.
     787             :  *
     788             :  *      We call this when we see a shared-inval-queue overflow signal,
     789             :  *      since that tells us we've lost some shared-inval messages and hence
     790             :  *      don't know what needs to be invalidated.
     791             :  */
     792             : void
     793        3862 : InvalidateSystemCaches(void)
     794             : {
     795        3862 :     InvalidateSystemCachesExtended(false);
     796        3862 : }
     797             : 
     798             : /*
     799             :  * AcceptInvalidationMessages
     800             :  *      Read and process invalidation messages from the shared invalidation
     801             :  *      message queue.
     802             :  *
     803             :  * Note:
     804             :  *      This should be called as the first step in processing a transaction.
     805             :  */
     806             : void
     807    24905844 : AcceptInvalidationMessages(void)
     808             : {
     809    24905844 :     ReceiveSharedInvalidMessages(LocalExecuteInvalidationMessage,
     810             :                                  InvalidateSystemCaches);
     811             : 
     812             :     /*----------
     813             :      * Test code to force cache flushes anytime a flush could happen.
     814             :      *
     815             :      * This helps detect intermittent faults caused by code that reads a cache
     816             :      * entry and then performs an action that could invalidate the entry, but
     817             :      * rarely actually does so.  This can spot issues that would otherwise
     818             :      * only arise with badly timed concurrent DDL, for example.
     819             :      *
     820             :      * The default debug_discard_caches = 0 does no forced cache flushes.
     821             :      *
     822             :      * If used with CLOBBER_FREED_MEMORY,
     823             :      * debug_discard_caches = 1 (formerly known as CLOBBER_CACHE_ALWAYS)
     824             :      * provides a fairly thorough test that the system contains no cache-flush
     825             :      * hazards.  However, it also makes the system unbelievably slow --- the
     826             :      * regression tests take about 100 times longer than normal.
     827             :      *
     828             :      * If you're a glutton for punishment, try
     829             :      * debug_discard_caches = 3 (formerly known as CLOBBER_CACHE_RECURSIVELY).
     830             :      * This slows things by at least a factor of 10000, so I wouldn't suggest
     831             :      * trying to run the entire regression tests that way.  It's useful to try
     832             :      * a few simple tests, to make sure that cache reload isn't subject to
     833             :      * internal cache-flush hazards, but after you've done a few thousand
     834             :      * recursive reloads it's unlikely you'll learn more.
     835             :      *----------
     836             :      */
     837             : #ifdef DISCARD_CACHES_ENABLED
     838             :     {
     839             :         static int  recursion_depth = 0;
     840             : 
     841             :         if (recursion_depth < debug_discard_caches)
     842             :         {
     843             :             recursion_depth++;
     844             :             InvalidateSystemCachesExtended(true);
     845             :             recursion_depth--;
     846             :         }
     847             :     }
     848             : #endif
     849    24905844 : }
     850             : 
     851             : /*
     852             :  * PostPrepare_Inval
     853             :  *      Clean up after successful PREPARE.
     854             :  *
     855             :  * Here, we want to act as though the transaction aborted, so that we will
     856             :  * undo any syscache changes it made, thereby bringing us into sync with the
     857             :  * outside world, which doesn't believe the transaction committed yet.
     858             :  *
     859             :  * If the prepared transaction is later aborted, there is nothing more to
     860             :  * do; if it commits, we will receive the consequent inval messages just
     861             :  * like everyone else.
     862             :  */
     863             : void
     864         748 : PostPrepare_Inval(void)
     865             : {
     866         748 :     AtEOXact_Inval(false);
     867         748 : }
     868             : 
     869             : /*
     870             :  * xactGetCommittedInvalidationMessages() is called by
     871             :  * RecordTransactionCommit() to collect invalidation messages to add to the
     872             :  * commit record. This applies only to commit message types, never to
     873             :  * abort records. Must always run before AtEOXact_Inval(), since that
     874             :  * removes the data we need to see.
     875             :  *
     876             :  * Remember that this runs before we have officially committed, so we
     877             :  * must not do anything here to change what might occur *if* we should
     878             :  * fail between here and the actual commit.
     879             :  *
     880             :  * see also xact_redo_commit() and xact_desc_commit()
     881             :  */
     882             : int
     883      314816 : xactGetCommittedInvalidationMessages(SharedInvalidationMessage **msgs,
     884             :                                      bool *RelcacheInitFileInval)
     885             : {
     886             :     SharedInvalidationMessage *msgarray;
     887             :     int         nummsgs;
     888             :     int         nmsgs;
     889             : 
     890             :     /* Quick exit if we haven't done anything with invalidation messages. */
     891      314816 :     if (transInvalInfo == NULL)
     892             :     {
     893      191518 :         *RelcacheInitFileInval = false;
     894      191518 :         *msgs = NULL;
     895      191518 :         return 0;
     896             :     }
     897             : 
     898             :     /* Must be at top of stack */
     899             :     Assert(transInvalInfo->my_level == 1 && transInvalInfo->parent == NULL);
     900             : 
     901             :     /*
     902             :      * Relcache init file invalidation requires processing both before and
     903             :      * after we send the SI messages.  However, we need not do anything unless
     904             :      * we committed.
     905             :      */
     906      123298 :     *RelcacheInitFileInval = transInvalInfo->RelcacheInitFileInval;
     907             : 
     908             :     /*
     909             :      * Collect all the pending messages into a single contiguous array of
     910             :      * invalidation messages, to simplify what needs to happen while building
     911             :      * the commit WAL message.  Maintain the order that they would be
     912             :      * processed in by AtEOXact_Inval(), to ensure emulated behaviour in redo
     913             :      * is as similar as possible to original.  We want the same bugs, if any,
     914             :      * not new ones.
     915             :      */
     916      123298 :     nummsgs = NumMessagesInGroup(&transInvalInfo->PriorCmdInvalidMsgs) +
     917      123298 :         NumMessagesInGroup(&transInvalInfo->CurrentCmdInvalidMsgs);
     918             : 
     919      123298 :     *msgs = msgarray = (SharedInvalidationMessage *)
     920      123298 :         MemoryContextAlloc(CurTransactionContext,
     921             :                            nummsgs * sizeof(SharedInvalidationMessage));
     922             : 
     923      123298 :     nmsgs = 0;
     924      123298 :     ProcessMessageSubGroupMulti(&transInvalInfo->PriorCmdInvalidMsgs,
     925             :                                 CatCacheMsgs,
     926             :                                 (memcpy(msgarray + nmsgs,
     927             :                                         msgs,
     928             :                                         n * sizeof(SharedInvalidationMessage)),
     929             :                                  nmsgs += n));
     930      123298 :     ProcessMessageSubGroupMulti(&transInvalInfo->CurrentCmdInvalidMsgs,
     931             :                                 CatCacheMsgs,
     932             :                                 (memcpy(msgarray + nmsgs,
     933             :                                         msgs,
     934             :                                         n * sizeof(SharedInvalidationMessage)),
     935             :                                  nmsgs += n));
     936      123298 :     ProcessMessageSubGroupMulti(&transInvalInfo->PriorCmdInvalidMsgs,
     937             :                                 RelCacheMsgs,
     938             :                                 (memcpy(msgarray + nmsgs,
     939             :                                         msgs,
     940             :                                         n * sizeof(SharedInvalidationMessage)),
     941             :                                  nmsgs += n));
     942      123298 :     ProcessMessageSubGroupMulti(&transInvalInfo->CurrentCmdInvalidMsgs,
     943             :                                 RelCacheMsgs,
     944             :                                 (memcpy(msgarray + nmsgs,
     945             :                                         msgs,
     946             :                                         n * sizeof(SharedInvalidationMessage)),
     947             :                                  nmsgs += n));
     948             :     Assert(nmsgs == nummsgs);
     949             : 
     950      123298 :     return nmsgs;
     951             : }
     952             : 
     953             : /*
     954             :  * ProcessCommittedInvalidationMessages is executed by xact_redo_commit() or
     955             :  * standby_redo() to process invalidation messages. Currently that happens
     956             :  * only at end-of-xact.
     957             :  *
     958             :  * Relcache init file invalidation requires processing both
     959             :  * before and after we send the SI messages. See AtEOXact_Inval()
     960             :  */
     961             : void
     962       35056 : ProcessCommittedInvalidationMessages(SharedInvalidationMessage *msgs,
     963             :                                      int nmsgs, bool RelcacheInitFileInval,
     964             :                                      Oid dbid, Oid tsid)
     965             : {
     966       35056 :     if (nmsgs <= 0)
     967        8902 :         return;
     968             : 
     969       26154 :     elog(DEBUG4, "replaying commit with %d messages%s", nmsgs,
     970             :          (RelcacheInitFileInval ? " and relcache file invalidation" : ""));
     971             : 
     972       26154 :     if (RelcacheInitFileInval)
     973             :     {
     974         240 :         elog(DEBUG4, "removing relcache init files for database %u", dbid);
     975             : 
     976             :         /*
     977             :          * RelationCacheInitFilePreInvalidate, when the invalidation message
     978             :          * is for a specific database, requires DatabasePath to be set, but we
     979             :          * should not use SetDatabasePath during recovery, since it is
     980             :          * intended to be used only once by normal backends.  Hence, a quick
     981             :          * hack: set DatabasePath directly then unset after use.
     982             :          */
     983         240 :         if (OidIsValid(dbid))
     984         240 :             DatabasePath = GetDatabasePath(dbid, tsid);
     985             : 
     986         240 :         RelationCacheInitFilePreInvalidate();
     987             : 
     988         240 :         if (OidIsValid(dbid))
     989             :         {
     990         240 :             pfree(DatabasePath);
     991         240 :             DatabasePath = NULL;
     992             :         }
     993             :     }
     994             : 
     995       26154 :     SendSharedInvalidMessages(msgs, nmsgs);
     996             : 
     997       26154 :     if (RelcacheInitFileInval)
     998         240 :         RelationCacheInitFilePostInvalidate();
     999             : }
    1000             : 
    1001             : /*
    1002             :  * AtEOXact_Inval
    1003             :  *      Process queued-up invalidation messages at end of main transaction.
    1004             :  *
    1005             :  * If isCommit, we must send out the messages in our PriorCmdInvalidMsgs list
    1006             :  * to the shared invalidation message queue.  Note that these will be read
    1007             :  * not only by other backends, but also by our own backend at the next
    1008             :  * transaction start (via AcceptInvalidationMessages).  This means that
    1009             :  * we can skip immediate local processing of anything that's still in
    1010             :  * CurrentCmdInvalidMsgs, and just send that list out too.
    1011             :  *
    1012             :  * If not isCommit, we are aborting, and must locally process the messages
    1013             :  * in PriorCmdInvalidMsgs.  No messages need be sent to other backends,
    1014             :  * since they'll not have seen our changed tuples anyway.  We can forget
    1015             :  * about CurrentCmdInvalidMsgs too, since those changes haven't touched
    1016             :  * the caches yet.
    1017             :  *
    1018             :  * In any case, reset our state to empty.  We need not physically
    1019             :  * free memory here, since TopTransactionContext is about to be emptied
    1020             :  * anyway.
    1021             :  *
    1022             :  * Note:
    1023             :  *      This should be called as the last step in processing a transaction.
    1024             :  */
    1025             : void
    1026      516616 : AtEOXact_Inval(bool isCommit)
    1027             : {
    1028             :     /* Quick exit if no messages */
    1029      516616 :     if (transInvalInfo == NULL)
    1030      347640 :         return;
    1031             : 
    1032             :     /* Must be at top of stack */
    1033             :     Assert(transInvalInfo->my_level == 1 && transInvalInfo->parent == NULL);
    1034             : 
    1035      168976 :     if (isCommit)
    1036             :     {
    1037             :         /*
    1038             :          * Relcache init file invalidation requires processing both before and
    1039             :          * after we send the SI messages.  However, we need not do anything
    1040             :          * unless we committed.
    1041             :          */
    1042      165320 :         if (transInvalInfo->RelcacheInitFileInval)
    1043       11796 :             RelationCacheInitFilePreInvalidate();
    1044             : 
    1045      165320 :         AppendInvalidationMessages(&transInvalInfo->PriorCmdInvalidMsgs,
    1046      165320 :                                    &transInvalInfo->CurrentCmdInvalidMsgs);
    1047             : 
    1048      165320 :         ProcessInvalidationMessagesMulti(&transInvalInfo->PriorCmdInvalidMsgs,
    1049             :                                          SendSharedInvalidMessages);
    1050             : 
    1051      165320 :         if (transInvalInfo->RelcacheInitFileInval)
    1052       11796 :             RelationCacheInitFilePostInvalidate();
    1053             :     }
    1054             :     else
    1055             :     {
    1056        3656 :         ProcessInvalidationMessages(&transInvalInfo->PriorCmdInvalidMsgs,
    1057             :                                     LocalExecuteInvalidationMessage);
    1058             :     }
    1059             : 
    1060             :     /* Need not free anything explicitly */
    1061      168976 :     transInvalInfo = NULL;
    1062             : }
    1063             : 
    1064             : /*
    1065             :  * AtEOSubXact_Inval
    1066             :  *      Process queued-up invalidation messages at end of subtransaction.
    1067             :  *
    1068             :  * If isCommit, process CurrentCmdInvalidMsgs if any (there probably aren't),
    1069             :  * and then attach both CurrentCmdInvalidMsgs and PriorCmdInvalidMsgs to the
    1070             :  * parent's PriorCmdInvalidMsgs list.
    1071             :  *
    1072             :  * If not isCommit, we are aborting, and must locally process the messages
    1073             :  * in PriorCmdInvalidMsgs.  No messages need be sent to other backends.
    1074             :  * We can forget about CurrentCmdInvalidMsgs too, since those changes haven't
    1075             :  * touched the caches yet.
    1076             :  *
    1077             :  * In any case, pop the transaction stack.  We need not physically free memory
    1078             :  * here, since CurTransactionContext is about to be emptied anyway
    1079             :  * (if aborting).  Beware of the possibility of aborting the same nesting
    1080             :  * level twice, though.
    1081             :  */
    1082             : void
    1083       18048 : AtEOSubXact_Inval(bool isCommit)
    1084             : {
    1085             :     int         my_level;
    1086       18048 :     TransInvalidationInfo *myInfo = transInvalInfo;
    1087             : 
    1088             :     /* Quick exit if no messages. */
    1089       18048 :     if (myInfo == NULL)
    1090       16480 :         return;
    1091             : 
    1092             :     /* Also bail out quickly if messages are not for this level. */
    1093        1568 :     my_level = GetCurrentTransactionNestLevel();
    1094        1568 :     if (myInfo->my_level != my_level)
    1095             :     {
    1096             :         Assert(myInfo->my_level < my_level);
    1097        1294 :         return;
    1098             :     }
    1099             : 
    1100         274 :     if (isCommit)
    1101             :     {
    1102             :         /* If CurrentCmdInvalidMsgs still has anything, fix it */
    1103          92 :         CommandEndInvalidationMessages();
    1104             : 
    1105             :         /*
    1106             :          * We create invalidation stack entries lazily, so the parent might
    1107             :          * not have one.  Instead of creating one, moving all the data over,
    1108             :          * and then freeing our own, we can just adjust the level of our own
    1109             :          * entry.
    1110             :          */
    1111          92 :         if (myInfo->parent == NULL || myInfo->parent->my_level < my_level - 1)
    1112             :         {
    1113          68 :             myInfo->my_level--;
    1114          68 :             return;
    1115             :         }
    1116             : 
    1117             :         /*
    1118             :          * Pass up my inval messages to parent.  Notice that we stick them in
    1119             :          * PriorCmdInvalidMsgs, not CurrentCmdInvalidMsgs, since they've
    1120             :          * already been locally processed.  (This would trigger the Assert in
    1121             :          * AppendInvalidationMessageSubGroup if the parent's
    1122             :          * CurrentCmdInvalidMsgs isn't empty; but we already checked that in
    1123             :          * PrepareInvalidationState.)
    1124             :          */
    1125          24 :         AppendInvalidationMessages(&myInfo->parent->PriorCmdInvalidMsgs,
    1126             :                                    &myInfo->PriorCmdInvalidMsgs);
    1127             : 
    1128             :         /* Must readjust parent's CurrentCmdInvalidMsgs indexes now */
    1129          24 :         SetGroupToFollow(&myInfo->parent->CurrentCmdInvalidMsgs,
    1130             :                          &myInfo->parent->PriorCmdInvalidMsgs);
    1131             : 
    1132             :         /* Pending relcache inval becomes parent's problem too */
    1133          24 :         if (myInfo->RelcacheInitFileInval)
    1134           0 :             myInfo->parent->RelcacheInitFileInval = true;
    1135             : 
    1136             :         /* Pop the transaction state stack */
    1137          24 :         transInvalInfo = myInfo->parent;
    1138             : 
    1139             :         /* Need not free anything else explicitly */
    1140          24 :         pfree(myInfo);
    1141             :     }
    1142             :     else
    1143             :     {
    1144         182 :         ProcessInvalidationMessages(&myInfo->PriorCmdInvalidMsgs,
    1145             :                                     LocalExecuteInvalidationMessage);
    1146             : 
    1147             :         /* Pop the transaction state stack */
    1148         182 :         transInvalInfo = myInfo->parent;
    1149             : 
    1150             :         /* Need not free anything else explicitly */
    1151         182 :         pfree(myInfo);
    1152             :     }
    1153             : }
    1154             : 
    1155             : /*
    1156             :  * CommandEndInvalidationMessages
    1157             :  *      Process queued-up invalidation messages at end of one command
    1158             :  *      in a transaction.
    1159             :  *
    1160             :  * Here, we send no messages to the shared queue, since we don't know yet if
    1161             :  * we will commit.  We do need to locally process the CurrentCmdInvalidMsgs
    1162             :  * list, so as to flush our caches of any entries we have outdated in the
    1163             :  * current command.  We then move the current-cmd list over to become part
    1164             :  * of the prior-cmds list.
    1165             :  *
    1166             :  * Note:
    1167             :  *      This should be called during CommandCounterIncrement(),
    1168             :  *      after we have advanced the command ID.
    1169             :  */
    1170             : void
    1171      917932 : CommandEndInvalidationMessages(void)
    1172             : {
    1173             :     /*
    1174             :      * You might think this shouldn't be called outside any transaction, but
    1175             :      * bootstrap does it, and also ABORT issued when not in a transaction. So
    1176             :      * just quietly return if no state to work on.
    1177             :      */
    1178      917932 :     if (transInvalInfo == NULL)
    1179      359690 :         return;
    1180             : 
    1181      558242 :     ProcessInvalidationMessages(&transInvalInfo->CurrentCmdInvalidMsgs,
    1182             :                                 LocalExecuteInvalidationMessage);
    1183             : 
    1184             :     /* WAL Log per-command invalidation messages for wal_level=logical */
    1185      558236 :     if (XLogLogicalInfoActive())
    1186        7114 :         LogLogicalInvalidations();
    1187             : 
    1188      558236 :     AppendInvalidationMessages(&transInvalInfo->PriorCmdInvalidMsgs,
    1189      558236 :                                &transInvalInfo->CurrentCmdInvalidMsgs);
    1190             : }
    1191             : 
    1192             : 
    1193             : /*
    1194             :  * CacheInvalidateHeapTuple
    1195             :  *      Register the given tuple for invalidation at end of command
    1196             :  *      (ie, current command is creating or outdating this tuple).
    1197             :  *      Also, detect whether a relcache invalidation is implied.
    1198             :  *
    1199             :  * For an insert or delete, tuple is the target tuple and newtuple is NULL.
    1200             :  * For an update, we are called just once, with tuple being the old tuple
    1201             :  * version and newtuple the new version.  This allows avoidance of duplicate
    1202             :  * effort during an update.
    1203             :  */
    1204             : void
    1205    19775050 : CacheInvalidateHeapTuple(Relation relation,
    1206             :                          HeapTuple tuple,
    1207             :                          HeapTuple newtuple)
    1208             : {
    1209             :     Oid         tupleRelId;
    1210             :     Oid         databaseId;
    1211             :     Oid         relationId;
    1212             : 
    1213             :     /* Do nothing during bootstrap */
    1214    19775050 :     if (IsBootstrapProcessingMode())
    1215      801856 :         return;
    1216             : 
    1217             :     /*
    1218             :      * We only need to worry about invalidation for tuples that are in system
    1219             :      * catalogs; user-relation tuples are never in catcaches and can't affect
    1220             :      * the relcache either.
    1221             :      */
    1222    18973194 :     if (!IsCatalogRelation(relation))
    1223    15814760 :         return;
    1224             : 
    1225             :     /*
    1226             :      * IsCatalogRelation() will return true for TOAST tables of system
    1227             :      * catalogs, but we don't care about those, either.
    1228             :      */
    1229     3158434 :     if (IsToastRelation(relation))
    1230       21140 :         return;
    1231             : 
    1232             :     /*
    1233             :      * If we're not prepared to queue invalidation messages for this
    1234             :      * subtransaction level, get ready now.
    1235             :      */
    1236     3137294 :     PrepareInvalidationState();
    1237             : 
    1238             :     /*
    1239             :      * First let the catcache do its thing
    1240             :      */
    1241     3137294 :     tupleRelId = RelationGetRelid(relation);
    1242     3137294 :     if (RelationInvalidatesSnapshotsOnly(tupleRelId))
    1243             :     {
    1244      801092 :         databaseId = IsSharedRelation(tupleRelId) ? InvalidOid : MyDatabaseId;
    1245      801092 :         RegisterSnapshotInvalidation(databaseId, tupleRelId);
    1246             :     }
    1247             :     else
    1248     2336202 :         PrepareToInvalidateCacheTuple(relation, tuple, newtuple,
    1249             :                                       RegisterCatcacheInvalidation);
    1250             : 
    1251             :     /*
    1252             :      * Now, is this tuple one of the primary definers of a relcache entry? See
    1253             :      * comments in file header for deeper explanation.
    1254             :      *
    1255             :      * Note we ignore newtuple here; we assume an update cannot move a tuple
    1256             :      * from being part of one relcache entry to being part of another.
    1257             :      */
    1258     3137294 :     if (tupleRelId == RelationRelationId)
    1259             :     {
    1260      311860 :         Form_pg_class classtup = (Form_pg_class) GETSTRUCT(tuple);
    1261             : 
    1262      311860 :         relationId = classtup->oid;
    1263      311860 :         if (classtup->relisshared)
    1264        4424 :             databaseId = InvalidOid;
    1265             :         else
    1266      307436 :             databaseId = MyDatabaseId;
    1267             :     }
    1268     2825434 :     else if (tupleRelId == AttributeRelationId)
    1269             :     {
    1270      931362 :         Form_pg_attribute atttup = (Form_pg_attribute) GETSTRUCT(tuple);
    1271             : 
    1272      931362 :         relationId = atttup->attrelid;
    1273             : 
    1274             :         /*
    1275             :          * KLUGE ALERT: we always send the relcache event with MyDatabaseId,
    1276             :          * even if the rel in question is shared (which we can't easily tell).
    1277             :          * This essentially means that only backends in this same database
    1278             :          * will react to the relcache flush request.  This is in fact
    1279             :          * appropriate, since only those backends could see our pg_attribute
    1280             :          * change anyway.  It looks a bit ugly though.  (In practice, shared
    1281             :          * relations can't have schema changes after bootstrap, so we should
    1282             :          * never come here for a shared rel anyway.)
    1283             :          */
    1284      931362 :         databaseId = MyDatabaseId;
    1285             :     }
    1286     1894072 :     else if (tupleRelId == IndexRelationId)
    1287             :     {
    1288       55566 :         Form_pg_index indextup = (Form_pg_index) GETSTRUCT(tuple);
    1289             : 
    1290             :         /*
    1291             :          * When a pg_index row is updated, we should send out a relcache inval
    1292             :          * for the index relation.  As above, we don't know the shared status
    1293             :          * of the index, but in practice it doesn't matter since indexes of
    1294             :          * shared catalogs can't have such updates.
    1295             :          */
    1296       55566 :         relationId = indextup->indexrelid;
    1297       55566 :         databaseId = MyDatabaseId;
    1298             :     }
    1299     1838506 :     else if (tupleRelId == ConstraintRelationId)
    1300             :     {
    1301       49592 :         Form_pg_constraint constrtup = (Form_pg_constraint) GETSTRUCT(tuple);
    1302             : 
    1303             :         /*
    1304             :          * Foreign keys are part of relcache entries, too, so send out an
    1305             :          * inval for the table that the FK applies to.
    1306             :          */
    1307       49592 :         if (constrtup->contype == CONSTRAINT_FOREIGN &&
    1308        6292 :             OidIsValid(constrtup->conrelid))
    1309             :         {
    1310        6292 :             relationId = constrtup->conrelid;
    1311        6292 :             databaseId = MyDatabaseId;
    1312             :         }
    1313             :         else
    1314       43300 :             return;
    1315             :     }
    1316             :     else
    1317     1788914 :         return;
    1318             : 
    1319             :     /*
    1320             :      * Yes.  We need to register a relcache invalidation event.
    1321             :      */
    1322     1305080 :     RegisterRelcacheInvalidation(databaseId, relationId);
    1323             : }
    1324             : 
    1325             : /*
    1326             :  * CacheInvalidateCatalog
    1327             :  *      Register invalidation of the whole content of a system catalog.
    1328             :  *
    1329             :  * This is normally used in VACUUM FULL/CLUSTER, where we haven't so much
    1330             :  * changed any tuples as moved them around.  Some uses of catcache entries
    1331             :  * expect their TIDs to be correct, so we have to blow away the entries.
    1332             :  *
    1333             :  * Note: we expect caller to verify that the rel actually is a system
    1334             :  * catalog.  If it isn't, no great harm is done, just a wasted sinval message.
    1335             :  */
    1336             : void
    1337         202 : CacheInvalidateCatalog(Oid catalogId)
    1338             : {
    1339             :     Oid         databaseId;
    1340             : 
    1341         202 :     PrepareInvalidationState();
    1342             : 
    1343         202 :     if (IsSharedRelation(catalogId))
    1344          36 :         databaseId = InvalidOid;
    1345             :     else
    1346         166 :         databaseId = MyDatabaseId;
    1347             : 
    1348         202 :     RegisterCatalogInvalidation(databaseId, catalogId);
    1349         202 : }
    1350             : 
    1351             : /*
    1352             :  * CacheInvalidateRelcache
    1353             :  *      Register invalidation of the specified relation's relcache entry
    1354             :  *      at end of command.
    1355             :  *
    1356             :  * This is used in places that need to force relcache rebuild but aren't
    1357             :  * changing any of the tuples recognized as contributors to the relcache
    1358             :  * entry by CacheInvalidateHeapTuple.  (An example is dropping an index.)
    1359             :  */
    1360             : void
    1361       98478 : CacheInvalidateRelcache(Relation relation)
    1362             : {
    1363             :     Oid         databaseId;
    1364             :     Oid         relationId;
    1365             : 
    1366       98478 :     PrepareInvalidationState();
    1367             : 
    1368       98478 :     relationId = RelationGetRelid(relation);
    1369       98478 :     if (relation->rd_rel->relisshared)
    1370        2772 :         databaseId = InvalidOid;
    1371             :     else
    1372       95706 :         databaseId = MyDatabaseId;
    1373             : 
    1374       98478 :     RegisterRelcacheInvalidation(databaseId, relationId);
    1375       98478 : }
    1376             : 
    1377             : /*
    1378             :  * CacheInvalidateRelcacheAll
    1379             :  *      Register invalidation of the whole relcache at the end of command.
    1380             :  *
    1381             :  * This is used by alter publication as changes in publications may affect
    1382             :  * large number of tables.
    1383             :  */
    1384             : void
    1385          82 : CacheInvalidateRelcacheAll(void)
    1386             : {
    1387          82 :     PrepareInvalidationState();
    1388             : 
    1389          82 :     RegisterRelcacheInvalidation(InvalidOid, InvalidOid);
    1390          82 : }
    1391             : 
    1392             : /*
    1393             :  * CacheInvalidateRelcacheByTuple
    1394             :  *      As above, but relation is identified by passing its pg_class tuple.
    1395             :  */
    1396             : void
    1397       58986 : CacheInvalidateRelcacheByTuple(HeapTuple classTuple)
    1398             : {
    1399       58986 :     Form_pg_class classtup = (Form_pg_class) GETSTRUCT(classTuple);
    1400             :     Oid         databaseId;
    1401             :     Oid         relationId;
    1402             : 
    1403       58986 :     PrepareInvalidationState();
    1404             : 
    1405       58986 :     relationId = classtup->oid;
    1406       58986 :     if (classtup->relisshared)
    1407        1082 :         databaseId = InvalidOid;
    1408             :     else
    1409       57904 :         databaseId = MyDatabaseId;
    1410       58986 :     RegisterRelcacheInvalidation(databaseId, relationId);
    1411       58986 : }
    1412             : 
    1413             : /*
    1414             :  * CacheInvalidateRelcacheByRelid
    1415             :  *      As above, but relation is identified by passing its OID.
    1416             :  *      This is the least efficient of the three options; use one of
    1417             :  *      the above routines if you have a Relation or pg_class tuple.
    1418             :  */
    1419             : void
    1420       26852 : CacheInvalidateRelcacheByRelid(Oid relid)
    1421             : {
    1422             :     HeapTuple   tup;
    1423             : 
    1424       26852 :     PrepareInvalidationState();
    1425             : 
    1426       26852 :     tup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    1427       26852 :     if (!HeapTupleIsValid(tup))
    1428           0 :         elog(ERROR, "cache lookup failed for relation %u", relid);
    1429       26852 :     CacheInvalidateRelcacheByTuple(tup);
    1430       26852 :     ReleaseSysCache(tup);
    1431       26852 : }
    1432             : 
    1433             : 
    1434             : /*
    1435             :  * CacheInvalidateSmgr
    1436             :  *      Register invalidation of smgr references to a physical relation.
    1437             :  *
    1438             :  * Sending this type of invalidation msg forces other backends to close open
    1439             :  * smgr entries for the rel.  This should be done to flush dangling open-file
    1440             :  * references when the physical rel is being dropped or truncated.  Because
    1441             :  * these are nontransactional (i.e., not-rollback-able) operations, we just
    1442             :  * send the inval message immediately without any queuing.
    1443             :  *
    1444             :  * Note: in most cases there will have been a relcache flush issued against
    1445             :  * the rel at the logical level.  We need a separate smgr-level flush because
    1446             :  * it is possible for backends to have open smgr entries for rels they don't
    1447             :  * have a relcache entry for, e.g. because the only thing they ever did with
    1448             :  * the rel is write out dirty shared buffers.
    1449             :  *
    1450             :  * Note: because these messages are nontransactional, they won't be captured
    1451             :  * in commit/abort WAL entries.  Instead, calls to CacheInvalidateSmgr()
    1452             :  * should happen in low-level smgr.c routines, which are executed while
    1453             :  * replaying WAL as well as when creating it.
    1454             :  *
    1455             :  * Note: In order to avoid bloating SharedInvalidationMessage, we store only
    1456             :  * three bytes of the backend ID using what would otherwise be padding space.
    1457             :  * Thus, the maximum possible backend ID is 2^23-1.
    1458             :  */
    1459             : void
    1460       80842 : CacheInvalidateSmgr(RelFileLocatorBackend rlocator)
    1461             : {
    1462             :     SharedInvalidationMessage msg;
    1463             : 
    1464       80842 :     msg.sm.id = SHAREDINVALSMGR_ID;
    1465       80842 :     msg.sm.backend_hi = rlocator.backend >> 16;
    1466       80842 :     msg.sm.backend_lo = rlocator.backend & 0xffff;
    1467       80842 :     msg.sm.rlocator = rlocator.locator;
    1468             :     /* check AddCatcacheInvalidationMessage() for an explanation */
    1469             :     VALGRIND_MAKE_MEM_DEFINED(&msg, sizeof(msg));
    1470             : 
    1471       80842 :     SendSharedInvalidMessages(&msg, 1);
    1472       80842 : }
    1473             : 
    1474             : /*
    1475             :  * CacheInvalidateRelmap
    1476             :  *      Register invalidation of the relation mapping for a database,
    1477             :  *      or for the shared catalogs if databaseId is zero.
    1478             :  *
    1479             :  * Sending this type of invalidation msg forces other backends to re-read
    1480             :  * the indicated relation mapping file.  It is also necessary to send a
    1481             :  * relcache inval for the specific relations whose mapping has been altered,
    1482             :  * else the relcache won't get updated with the new filenode data.
    1483             :  *
    1484             :  * Note: because these messages are nontransactional, they won't be captured
    1485             :  * in commit/abort WAL entries.  Instead, calls to CacheInvalidateRelmap()
    1486             :  * should happen in low-level relmapper.c routines, which are executed while
    1487             :  * replaying WAL as well as when creating it.
    1488             :  */
    1489             : void
    1490         208 : CacheInvalidateRelmap(Oid databaseId)
    1491             : {
    1492             :     SharedInvalidationMessage msg;
    1493             : 
    1494         208 :     msg.rm.id = SHAREDINVALRELMAP_ID;
    1495         208 :     msg.rm.dbId = databaseId;
    1496             :     /* check AddCatcacheInvalidationMessage() for an explanation */
    1497             :     VALGRIND_MAKE_MEM_DEFINED(&msg, sizeof(msg));
    1498             : 
    1499         208 :     SendSharedInvalidMessages(&msg, 1);
    1500         208 : }
    1501             : 
    1502             : 
    1503             : /*
    1504             :  * CacheRegisterSyscacheCallback
    1505             :  *      Register the specified function to be called for all future
    1506             :  *      invalidation events in the specified cache.  The cache ID and the
    1507             :  *      hash value of the tuple being invalidated will be passed to the
    1508             :  *      function.
    1509             :  *
    1510             :  * NOTE: Hash value zero will be passed if a cache reset request is received.
    1511             :  * In this case the called routines should flush all cached state.
    1512             :  * Yes, there's a possibility of a false match to zero, but it doesn't seem
    1513             :  * worth troubling over, especially since most of the current callees just
    1514             :  * flush all cached state anyway.
    1515             :  */
    1516             : void
    1517      355860 : CacheRegisterSyscacheCallback(int cacheid,
    1518             :                               SyscacheCallbackFunction func,
    1519             :                               Datum arg)
    1520             : {
    1521      355860 :     if (cacheid < 0 || cacheid >= SysCacheSize)
    1522           0 :         elog(FATAL, "invalid cache ID: %d", cacheid);
    1523      355860 :     if (syscache_callback_count >= MAX_SYSCACHE_CALLBACKS)
    1524           0 :         elog(FATAL, "out of syscache_callback_list slots");
    1525             : 
    1526      355860 :     if (syscache_callback_links[cacheid] == 0)
    1527             :     {
    1528             :         /* first callback for this cache */
    1529      282038 :         syscache_callback_links[cacheid] = syscache_callback_count + 1;
    1530             :     }
    1531             :     else
    1532             :     {
    1533             :         /* add to end of chain, so that older callbacks are called first */
    1534       73822 :         int         i = syscache_callback_links[cacheid] - 1;
    1535             : 
    1536       97830 :         while (syscache_callback_list[i].link > 0)
    1537       24008 :             i = syscache_callback_list[i].link - 1;
    1538       73822 :         syscache_callback_list[i].link = syscache_callback_count + 1;
    1539             :     }
    1540             : 
    1541      355860 :     syscache_callback_list[syscache_callback_count].id = cacheid;
    1542      355860 :     syscache_callback_list[syscache_callback_count].link = 0;
    1543      355860 :     syscache_callback_list[syscache_callback_count].function = func;
    1544      355860 :     syscache_callback_list[syscache_callback_count].arg = arg;
    1545             : 
    1546      355860 :     ++syscache_callback_count;
    1547      355860 : }
    1548             : 
    1549             : /*
    1550             :  * CacheRegisterRelcacheCallback
    1551             :  *      Register the specified function to be called for all future
    1552             :  *      relcache invalidation events.  The OID of the relation being
    1553             :  *      invalidated will be passed to the function.
    1554             :  *
    1555             :  * NOTE: InvalidOid will be passed if a cache reset request is received.
    1556             :  * In this case the called routines should flush all cached state.
    1557             :  */
    1558             : void
    1559       31692 : CacheRegisterRelcacheCallback(RelcacheCallbackFunction func,
    1560             :                               Datum arg)
    1561             : {
    1562       31692 :     if (relcache_callback_count >= MAX_RELCACHE_CALLBACKS)
    1563           0 :         elog(FATAL, "out of relcache_callback_list slots");
    1564             : 
    1565       31692 :     relcache_callback_list[relcache_callback_count].function = func;
    1566       31692 :     relcache_callback_list[relcache_callback_count].arg = arg;
    1567             : 
    1568       31692 :     ++relcache_callback_count;
    1569       31692 : }
    1570             : 
    1571             : /*
    1572             :  * CallSyscacheCallbacks
    1573             :  *
    1574             :  * This is exported so that CatalogCacheFlushCatalog can call it, saving
    1575             :  * this module from knowing which catcache IDs correspond to which catalogs.
    1576             :  */
    1577             : void
    1578    16451100 : CallSyscacheCallbacks(int cacheid, uint32 hashvalue)
    1579             : {
    1580             :     int         i;
    1581             : 
    1582    16451100 :     if (cacheid < 0 || cacheid >= SysCacheSize)
    1583           0 :         elog(ERROR, "invalid cache ID: %d", cacheid);
    1584             : 
    1585    16451100 :     i = syscache_callback_links[cacheid] - 1;
    1586    18738356 :     while (i >= 0)
    1587             :     {
    1588     2287256 :         struct SYSCACHECALLBACK *ccitem = syscache_callback_list + i;
    1589             : 
    1590             :         Assert(ccitem->id == cacheid);
    1591     2287256 :         ccitem->function(ccitem->arg, cacheid, hashvalue);
    1592     2287256 :         i = ccitem->link - 1;
    1593             :     }
    1594    16451100 : }
    1595             : 
    1596             : /*
    1597             :  * LogLogicalInvalidations
    1598             :  *
    1599             :  * Emit WAL for invalidations caused by the current command.
    1600             :  *
    1601             :  * This is currently only used for logging invalidations at the command end
    1602             :  * or at commit time if any invalidations are pending.
    1603             :  */
    1604             : void
    1605       26918 : LogLogicalInvalidations(void)
    1606             : {
    1607             :     xl_xact_invals xlrec;
    1608             :     InvalidationMsgsGroup *group;
    1609             :     int         nmsgs;
    1610             : 
    1611             :     /* Quick exit if we haven't done anything with invalidation messages. */
    1612       26918 :     if (transInvalInfo == NULL)
    1613       16766 :         return;
    1614             : 
    1615       10152 :     group = &transInvalInfo->CurrentCmdInvalidMsgs;
    1616       10152 :     nmsgs = NumMessagesInGroup(group);
    1617             : 
    1618       10152 :     if (nmsgs > 0)
    1619             :     {
    1620             :         /* prepare record */
    1621        8268 :         memset(&xlrec, 0, MinSizeOfXactInvals);
    1622        8268 :         xlrec.nmsgs = nmsgs;
    1623             : 
    1624             :         /* perform insertion */
    1625        8268 :         XLogBeginInsert();
    1626        8268 :         XLogRegisterData((char *) (&xlrec), MinSizeOfXactInvals);
    1627        8268 :         ProcessMessageSubGroupMulti(group, CatCacheMsgs,
    1628             :                                     XLogRegisterData((char *) msgs,
    1629             :                                                      n * sizeof(SharedInvalidationMessage)));
    1630        8268 :         ProcessMessageSubGroupMulti(group, RelCacheMsgs,
    1631             :                                     XLogRegisterData((char *) msgs,
    1632             :                                                      n * sizeof(SharedInvalidationMessage)));
    1633        8268 :         XLogInsert(RM_XACT_ID, XLOG_XACT_INVALIDATIONS);
    1634             :     }
    1635             : }

Generated by: LCOV version 1.14