PostgreSQL Source Code  git master
sinval.h File Reference
#include <signal.h>
#include "storage/relfilenode.h"
Include dependency graph for sinval.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SharedInvalCatcacheMsg
 
struct  SharedInvalCatalogMsg
 
struct  SharedInvalRelcacheMsg
 
struct  SharedInvalSmgrMsg
 
struct  SharedInvalRelmapMsg
 
struct  SharedInvalSnapshotMsg
 
union  SharedInvalidationMessage
 

Macros

#define SHAREDINVALCATALOG_ID   (-1)
 
#define SHAREDINVALRELCACHE_ID   (-2)
 
#define SHAREDINVALSMGR_ID   (-3)
 
#define SHAREDINVALRELMAP_ID   (-4)
 
#define SHAREDINVALSNAPSHOT_ID   (-5)
 

Functions

void SendSharedInvalidMessages (const SharedInvalidationMessage *msgs, int n)
 
void ReceiveSharedInvalidMessages (void(*invalFunction)(SharedInvalidationMessage *msg), void(*resetFunction)(void))
 
void HandleCatchupInterrupt (void)
 
void ProcessCatchupInterrupt (void)
 
int xactGetCommittedInvalidationMessages (SharedInvalidationMessage **msgs, bool *RelcacheInitFileInval)
 
void ProcessCommittedInvalidationMessages (SharedInvalidationMessage *msgs, int nmsgs, bool RelcacheInitFileInval, Oid dbid, Oid tsid)
 
void LocalExecuteInvalidationMessage (SharedInvalidationMessage *msg)
 

Variables

uint64 SharedInvalidMessageCounter
 
volatile sig_atomic_t catchupInterruptPending
 

Macro Definition Documentation

◆ SHAREDINVALCATALOG_ID

#define SHAREDINVALCATALOG_ID   (-1)

◆ SHAREDINVALRELCACHE_ID

#define SHAREDINVALRELCACHE_ID   (-2)

◆ SHAREDINVALRELMAP_ID

#define SHAREDINVALRELMAP_ID   (-4)

◆ SHAREDINVALSMGR_ID

#define SHAREDINVALSMGR_ID   (-3)

◆ SHAREDINVALSNAPSHOT_ID

#define SHAREDINVALSNAPSHOT_ID   (-5)

Function Documentation

◆ HandleCatchupInterrupt()

void HandleCatchupInterrupt ( void  )

Definition at line 156 of file sinval.c.

References catchupInterruptPending, MyLatch, and SetLatch().

Referenced by procsignal_sigusr1_handler().

157 {
158  /*
159  * Note: this is called by a SIGNAL HANDLER. You must be very wary what
160  * you do here.
161  */
162 
164 
165  /* make sure the event is processed in due course */
166  SetLatch(MyLatch);
167 }
void SetLatch(Latch *latch)
Definition: latch.c:505
struct Latch * MyLatch
Definition: globals.c:54
volatile sig_atomic_t catchupInterruptPending
Definition: sinval.c:41

◆ LocalExecuteInvalidationMessage()

void LocalExecuteInvalidationMessage ( SharedInvalidationMessage msg)

Definition at line 559 of file inval.c.

References RELCACHECALLBACK::arg, RelFileNodeBackend::backend, SharedInvalSmgrMsg::backend_hi, SharedInvalSmgrMsg::backend_lo, CallSyscacheCallbacks(), SharedInvalidationMessage::cat, CatalogCacheFlushCatalog(), SharedInvalCatalogMsg::catId, SharedInvalidationMessage::cc, SharedInvalCatcacheMsg::dbId, SharedInvalCatalogMsg::dbId, SharedInvalRelcacheMsg::dbId, SharedInvalRelmapMsg::dbId, elog, FATAL, RELCACHECALLBACK::function, SharedInvalCatcacheMsg::hashValue, i, SharedInvalCatcacheMsg::id, SharedInvalidationMessage::id, InvalidateCatalogSnapshot(), InvalidOid, MyDatabaseId, RelFileNodeBackend::node, SharedInvalidationMessage::rc, RelationCacheInvalidate(), RelationCacheInvalidateEntry(), RelationMapInvalidate(), relcache_callback_count, relcache_callback_list, SharedInvalRelcacheMsg::relId, SharedInvalidationMessage::rm, SharedInvalSmgrMsg::rnode, SHAREDINVALCATALOG_ID, SHAREDINVALRELCACHE_ID, SHAREDINVALRELMAP_ID, SHAREDINVALSMGR_ID, SHAREDINVALSNAPSHOT_ID, SharedInvalidationMessage::sm, smgrclosenode(), and SysCacheInvalidate().

Referenced by AcceptInvalidationMessages(), AtEOSubXact_Inval(), AtEOXact_Inval(), CommandEndInvalidationMessages(), ReorderBufferExecuteInvalidations(), and ReorderBufferImmediateInvalidation().

560 {
561  if (msg->id >= 0)
562  {
563  if (msg->cc.dbId == MyDatabaseId || msg->cc.dbId == InvalidOid)
564  {
566 
567  SysCacheInvalidate(msg->cc.id, msg->cc.hashValue);
568 
569  CallSyscacheCallbacks(msg->cc.id, msg->cc.hashValue);
570  }
571  }
572  else if (msg->id == SHAREDINVALCATALOG_ID)
573  {
574  if (msg->cat.dbId == MyDatabaseId || msg->cat.dbId == InvalidOid)
575  {
577 
579 
580  /* CatalogCacheFlushCatalog calls CallSyscacheCallbacks as needed */
581  }
582  }
583  else if (msg->id == SHAREDINVALRELCACHE_ID)
584  {
585  if (msg->rc.dbId == MyDatabaseId || msg->rc.dbId == InvalidOid)
586  {
587  int i;
588 
589  if (msg->rc.relId == InvalidOid)
591  else
593 
594  for (i = 0; i < relcache_callback_count; i++)
595  {
596  struct RELCACHECALLBACK *ccitem = relcache_callback_list + i;
597 
598  ccitem->function(ccitem->arg, msg->rc.relId);
599  }
600  }
601  }
602  else if (msg->id == SHAREDINVALSMGR_ID)
603  {
604  /*
605  * We could have smgr entries for relations of other databases, so no
606  * short-circuit test is possible here.
607  */
608  RelFileNodeBackend rnode;
609 
610  rnode.node = msg->sm.rnode;
611  rnode.backend = (msg->sm.backend_hi << 16) | (int) msg->sm.backend_lo;
612  smgrclosenode(rnode);
613  }
614  else if (msg->id == SHAREDINVALRELMAP_ID)
615  {
616  /* We only care about our own database and shared catalogs */
617  if (msg->rm.dbId == InvalidOid)
618  RelationMapInvalidate(true);
619  else if (msg->rm.dbId == MyDatabaseId)
620  RelationMapInvalidate(false);
621  }
622  else if (msg->id == SHAREDINVALSNAPSHOT_ID)
623  {
624  /* We only care about our own database and shared catalogs */
625  if (msg->rm.dbId == InvalidOid)
627  else if (msg->rm.dbId == MyDatabaseId)
629  }
630  else
631  elog(FATAL, "unrecognized SI message ID: %d", msg->id);
632 }
SharedInvalRelcacheMsg rc
Definition: sinval.h:118
static struct RELCACHECALLBACK relcache_callback_list[MAX_RELCACHE_CALLBACKS]
static int relcache_callback_count
Definition: inval.c:214
RelcacheCallbackFunction function
Definition: inval.c:210
void RelationMapInvalidate(bool shared)
Definition: relmapper.c:403
#define FATAL
Definition: elog.h:52
#define SHAREDINVALRELCACHE_ID
Definition: sinval.h:76
SharedInvalRelmapMsg rm
Definition: sinval.h:120
#define SHAREDINVALRELMAP_ID
Definition: sinval.h:96
SharedInvalCatcacheMsg cc
Definition: sinval.h:116
void smgrclosenode(RelFileNodeBackend rnode)
Definition: smgr.c:311
SharedInvalCatalogMsg cat
Definition: sinval.h:117
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:507
void CallSyscacheCallbacks(int cacheid, uint32 hashvalue)
Definition: inval.c:1495
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
#define SHAREDINVALSNAPSHOT_ID
Definition: sinval.h:104
RelFileNode node
Definition: relfilenode.h:74
SharedInvalSmgrMsg sm
Definition: sinval.h:119
void SysCacheInvalidate(int cacheId, uint32 hashValue)
Definition: syscache.c:1449
void CatalogCacheFlushCatalog(Oid catId)
Definition: catcache.c:719
BackendId backend
Definition: relfilenode.h:75
void RelationCacheInvalidateEntry(Oid relationId)
Definition: relcache.c:2788
uint16 backend_lo
Definition: sinval.h:92
#define SHAREDINVALCATALOG_ID
Definition: sinval.h:67
#define elog(elevel,...)
Definition: elog.h:214
void RelationCacheInvalidate(void)
Definition: relcache.c:2831
int i
RelFileNode rnode
Definition: sinval.h:93
#define SHAREDINVALSMGR_ID
Definition: sinval.h:85

◆ ProcessCatchupInterrupt()

void ProcessCatchupInterrupt ( void  )

Definition at line 176 of file sinval.c.

References AcceptInvalidationMessages(), catchupInterruptPending, CommitTransactionCommand(), DEBUG4, elog, IsTransactionOrTransactionBlock(), and StartTransactionCommand().

Referenced by HandleAutoVacLauncherInterrupts(), and ProcessClientReadInterrupt().

177 {
179  {
180  /*
181  * What we need to do here is cause ReceiveSharedInvalidMessages() to
182  * run, which will do the necessary work and also reset the
183  * catchupInterruptPending flag. If we are inside a transaction we
184  * can just call AcceptInvalidationMessages() to do this. If we
185  * aren't, we start and immediately end a transaction; the call to
186  * AcceptInvalidationMessages() happens down inside transaction start.
187  *
188  * It is awfully tempting to just call AcceptInvalidationMessages()
189  * without the rest of the xact start/stop overhead, and I think that
190  * would actually work in the normal case; but I am not sure that
191  * things would clean up nicely if we got an error partway through.
192  */
194  {
195  elog(DEBUG4, "ProcessCatchupEvent inside transaction");
197  }
198  else
199  {
200  elog(DEBUG4, "ProcessCatchupEvent outside transaction");
203  }
204  }
205 }
void AcceptInvalidationMessages(void)
Definition: inval.c:684
void CommitTransactionCommand(void)
Definition: xact.c:2947
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4702
#define DEBUG4
Definition: elog.h:22
void StartTransactionCommand(void)
Definition: xact.c:2846
#define elog(elevel,...)
Definition: elog.h:214
volatile sig_atomic_t catchupInterruptPending
Definition: sinval.c:41

◆ ProcessCommittedInvalidationMessages()

void ProcessCommittedInvalidationMessages ( SharedInvalidationMessage msgs,
int  nmsgs,
bool  RelcacheInitFileInval,
Oid  dbid,
Oid  tsid 
)

Definition at line 887 of file inval.c.

References DatabasePath, DEBUG4, elog, GetDatabasePath(), OidIsValid, pfree(), RelationCacheInitFilePostInvalidate(), RelationCacheInitFilePreInvalidate(), SendSharedInvalidMessages(), and trace_recovery().

Referenced by standby_redo(), and xact_redo_commit().

890 {
891  if (nmsgs <= 0)
892  return;
893 
894  elog(trace_recovery(DEBUG4), "replaying commit with %d messages%s", nmsgs,
895  (RelcacheInitFileInval ? " and relcache file invalidation" : ""));
896 
897  if (RelcacheInitFileInval)
898  {
899  elog(trace_recovery(DEBUG4), "removing relcache init files for database %u",
900  dbid);
901 
902  /*
903  * RelationCacheInitFilePreInvalidate, when the invalidation message
904  * is for a specific database, requires DatabasePath to be set, but we
905  * should not use SetDatabasePath during recovery, since it is
906  * intended to be used only once by normal backends. Hence, a quick
907  * hack: set DatabasePath directly then unset after use.
908  */
909  if (OidIsValid(dbid))
910  DatabasePath = GetDatabasePath(dbid, tsid);
911 
913 
914  if (OidIsValid(dbid))
915  {
917  DatabasePath = NULL;
918  }
919  }
920 
921  SendSharedInvalidMessages(msgs, nmsgs);
922 
923  if (RelcacheInitFileInval)
925 }
#define DEBUG4
Definition: elog.h:22
#define OidIsValid(objectId)
Definition: c.h:651
int trace_recovery(int trace_level)
Definition: elog.c:3543
void pfree(void *pointer)
Definition: mcxt.c:1057
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:110
void RelationCacheInitFilePostInvalidate(void)
Definition: relcache.c:6323
char * DatabasePath
Definition: globals.c:93
void RelationCacheInitFilePreInvalidate(void)
Definition: relcache.c:6298
void SendSharedInvalidMessages(const SharedInvalidationMessage *msgs, int n)
Definition: sinval.c:49
#define elog(elevel,...)
Definition: elog.h:214

◆ ReceiveSharedInvalidMessages()

void ReceiveSharedInvalidMessages ( void(*)(SharedInvalidationMessage *msg)  invalFunction,
void(*)(void)  resetFunction 
)

Definition at line 71 of file sinval.c.

References catchupInterruptPending, DEBUG4, elog, MAXINVALMSGS, SharedInvalidMessageCounter, SICleanupQueue(), and SIGetDataEntries().

Referenced by AcceptInvalidationMessages().

73 {
74 #define MAXINVALMSGS 32
75  static SharedInvalidationMessage messages[MAXINVALMSGS];
76 
77  /*
78  * We use volatile here to prevent bugs if a compiler doesn't realize that
79  * recursion is a possibility ...
80  */
81  static volatile int nextmsg = 0;
82  static volatile int nummsgs = 0;
83 
84  /* Deal with any messages still pending from an outer recursion */
85  while (nextmsg < nummsgs)
86  {
87  SharedInvalidationMessage msg = messages[nextmsg++];
88 
90  invalFunction(&msg);
91  }
92 
93  do
94  {
95  int getResult;
96 
97  nextmsg = nummsgs = 0;
98 
99  /* Try to get some more messages */
100  getResult = SIGetDataEntries(messages, MAXINVALMSGS);
101 
102  if (getResult < 0)
103  {
104  /* got a reset message */
105  elog(DEBUG4, "cache state reset");
107  resetFunction();
108  break; /* nothing more to do */
109  }
110 
111  /* Process them, being wary that a recursive call might eat some */
112  nextmsg = 0;
113  nummsgs = getResult;
114 
115  while (nextmsg < nummsgs)
116  {
117  SharedInvalidationMessage msg = messages[nextmsg++];
118 
120  invalFunction(&msg);
121  }
122 
123  /*
124  * We only need to loop if the last SIGetDataEntries call (which might
125  * have been within a recursive call) returned a full buffer.
126  */
127  } while (nummsgs == MAXINVALMSGS);
128 
129  /*
130  * We are now caught up. If we received a catchup signal, reset that
131  * flag, and call SICleanupQueue(). This is not so much because we need
132  * to flush dead messages right now, as that we want to pass on the
133  * catchup signal to the next slowest backend. "Daisy chaining" the
134  * catchup signal this way avoids creating spikes in system load for what
135  * should be just a background maintenance activity.
136  */
138  {
139  catchupInterruptPending = false;
140  elog(DEBUG4, "sinval catchup complete, cleaning queue");
141  SICleanupQueue(false, 0);
142  }
143 }
#define DEBUG4
Definition: elog.h:22
int SIGetDataEntries(SharedInvalidationMessage *data, int datasize)
Definition: sinvaladt.c:536
#define MAXINVALMSGS
void SICleanupQueue(bool callerHasWriteLock, int minFree)
Definition: sinvaladt.c:640
uint64 SharedInvalidMessageCounter
Definition: sinval.c:26
#define elog(elevel,...)
Definition: elog.h:214
volatile sig_atomic_t catchupInterruptPending
Definition: sinval.c:41

◆ SendSharedInvalidMessages()

void SendSharedInvalidMessages ( const SharedInvalidationMessage msgs,
int  n 
)

Definition at line 49 of file sinval.c.

References SIInsertDataEntries().

Referenced by AtEOXact_Inval(), CacheInvalidateRelmap(), CacheInvalidateSmgr(), FinishPreparedTransaction(), and ProcessCommittedInvalidationMessages().

50 {
51  SIInsertDataEntries(msgs, n);
52 }
void SIInsertDataEntries(const SharedInvalidationMessage *data, int n)
Definition: sinvaladt.c:433

◆ xactGetCommittedInvalidationMessages()

int xactGetCommittedInvalidationMessages ( SharedInvalidationMessage **  msgs,
bool RelcacheInitFileInval 
)

Definition at line 831 of file inval.c.

References Assert, TransInvalidationInfo::CurrentCmdInvalidMsgs, CurTransactionContext, MakeSharedInvalidMessagesArray(), MemoryContextSwitchTo(), TransInvalidationInfo::my_level, numSharedInvalidMessagesArray, TransInvalidationInfo::parent, TransInvalidationInfo::PriorCmdInvalidMsgs, ProcessInvalidationMessagesMulti(), TransInvalidationInfo::RelcacheInitFileInval, and SharedInvalidMessagesArray.

Referenced by RecordTransactionCommit(), and StartPrepare().

833 {
834  MemoryContext oldcontext;
835 
836  /* Quick exit if we haven't done anything with invalidation messages. */
837  if (transInvalInfo == NULL)
838  {
839  *RelcacheInitFileInval = false;
840  *msgs = NULL;
841  return 0;
842  }
843 
844  /* Must be at top of stack */
846 
847  /*
848  * Relcache init file invalidation requires processing both before and
849  * after we send the SI messages. However, we need not do anything unless
850  * we committed.
851  */
852  *RelcacheInitFileInval = transInvalInfo->RelcacheInitFileInval;
853 
854  /*
855  * Walk through TransInvalidationInfo to collect all the messages into a
856  * single contiguous array of invalidation messages. It must be contiguous
857  * so we can copy directly into WAL message. Maintain the order that they
858  * would be processed in by AtEOXact_Inval(), to ensure emulated behaviour
859  * in redo is as similar as possible to original. We want the same bugs,
860  * if any, not new ones.
861  */
863 
868  MemoryContextSwitchTo(oldcontext);
869 
871  SharedInvalidMessagesArray == NULL));
872 
874 
876 }
static SharedInvalidationMessage * SharedInvalidMessagesArray
Definition: inval.c:178
InvalidationListHeader PriorCmdInvalidMsgs
Definition: inval.c:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
struct TransInvalidationInfo * parent
Definition: inval.c:161
static TransInvalidationInfo * transInvalInfo
Definition: inval.c:176
InvalidationListHeader CurrentCmdInvalidMsgs
Definition: inval.c:167
static void MakeSharedInvalidMessagesArray(const SharedInvalidationMessage *msgs, int n)
Definition: inval.c:781
static int numSharedInvalidMessagesArray
Definition: inval.c:179
static void ProcessInvalidationMessagesMulti(InvalidationListHeader *hdr, void(*func)(const SharedInvalidationMessage *msgs, int n))
Definition: inval.c:471
bool RelcacheInitFileInval
Definition: inval.c:173
#define Assert(condition)
Definition: c.h:745

Variable Documentation

◆ catchupInterruptPending

volatile sig_atomic_t catchupInterruptPending

◆ SharedInvalidMessageCounter

uint64 SharedInvalidMessageCounter