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

PGDLLIMPORT uint64 SharedInvalidMessageCounter
 
PGDLLIMPORT volatile sig_atomic_t catchupInterruptPending
 

Macro Definition Documentation

◆ SHAREDINVALCATALOG_ID

#define SHAREDINVALCATALOG_ID   (-1)

Definition at line 67 of file sinval.h.

◆ SHAREDINVALRELCACHE_ID

#define SHAREDINVALRELCACHE_ID   (-2)

Definition at line 76 of file sinval.h.

◆ SHAREDINVALRELMAP_ID

#define SHAREDINVALRELMAP_ID   (-4)

Definition at line 96 of file sinval.h.

◆ SHAREDINVALSMGR_ID

#define SHAREDINVALSMGR_ID   (-3)

Definition at line 85 of file sinval.h.

◆ SHAREDINVALSNAPSHOT_ID

#define SHAREDINVALSNAPSHOT_ID   (-5)

Definition at line 104 of file sinval.h.

Function Documentation

◆ HandleCatchupInterrupt()

void HandleCatchupInterrupt ( void  )

Definition at line 156 of file sinval.c.

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 }
struct Latch * MyLatch
Definition: globals.c:58
void SetLatch(Latch *latch)
Definition: latch.c:566
volatile sig_atomic_t catchupInterruptPending
Definition: sinval.c:41

References catchupInterruptPending, MyLatch, and SetLatch().

Referenced by procsignal_sigusr1_handler().

◆ LocalExecuteInvalidationMessage()

void LocalExecuteInvalidationMessage ( SharedInvalidationMessage msg)

Definition at line 615 of file inval.c.

616 {
617  if (msg->id >= 0)
618  {
619  if (msg->cc.dbId == MyDatabaseId || msg->cc.dbId == InvalidOid)
620  {
622 
623  SysCacheInvalidate(msg->cc.id, msg->cc.hashValue);
624 
625  CallSyscacheCallbacks(msg->cc.id, msg->cc.hashValue);
626  }
627  }
628  else if (msg->id == SHAREDINVALCATALOG_ID)
629  {
630  if (msg->cat.dbId == MyDatabaseId || msg->cat.dbId == InvalidOid)
631  {
633 
635 
636  /* CatalogCacheFlushCatalog calls CallSyscacheCallbacks as needed */
637  }
638  }
639  else if (msg->id == SHAREDINVALRELCACHE_ID)
640  {
641  if (msg->rc.dbId == MyDatabaseId || msg->rc.dbId == InvalidOid)
642  {
643  int i;
644 
645  if (msg->rc.relId == InvalidOid)
647  else
649 
650  for (i = 0; i < relcache_callback_count; i++)
651  {
652  struct RELCACHECALLBACK *ccitem = relcache_callback_list + i;
653 
654  ccitem->function(ccitem->arg, msg->rc.relId);
655  }
656  }
657  }
658  else if (msg->id == SHAREDINVALSMGR_ID)
659  {
660  /*
661  * We could have smgr entries for relations of other databases, so no
662  * short-circuit test is possible here.
663  */
664  RelFileNodeBackend rnode;
665 
666  rnode.node = msg->sm.rnode;
667  rnode.backend = (msg->sm.backend_hi << 16) | (int) msg->sm.backend_lo;
668  smgrclosenode(rnode);
669  }
670  else if (msg->id == SHAREDINVALRELMAP_ID)
671  {
672  /* We only care about our own database and shared catalogs */
673  if (msg->rm.dbId == InvalidOid)
674  RelationMapInvalidate(true);
675  else if (msg->rm.dbId == MyDatabaseId)
676  RelationMapInvalidate(false);
677  }
678  else if (msg->id == SHAREDINVALSNAPSHOT_ID)
679  {
680  /* We only care about our own database and shared catalogs */
681  if (msg->sn.dbId == InvalidOid)
683  else if (msg->sn.dbId == MyDatabaseId)
685  }
686  else
687  elog(FATAL, "unrecognized SI message ID: %d", msg->id);
688 }
void CatalogCacheFlushCatalog(Oid catId)
Definition: catcache.c:713
#define FATAL
Definition: elog.h:35
#define elog(elevel,...)
Definition: elog.h:218
Oid MyDatabaseId
Definition: globals.c:89
static int relcache_callback_count
Definition: inval.c:274
static struct RELCACHECALLBACK relcache_callback_list[MAX_RELCACHE_CALLBACKS]
void CallSyscacheCallbacks(int cacheid, uint32 hashvalue)
Definition: inval.c:1580
int i
Definition: isn.c:73
#define InvalidOid
Definition: postgres_ext.h:36
void RelationCacheInvalidate(bool debug_discard)
Definition: relcache.c:2924
void RelationCacheInvalidateEntry(Oid relationId)
Definition: relcache.c:2868
void RelationMapInvalidate(bool shared)
Definition: relmapper.c:462
#define SHAREDINVALCATALOG_ID
Definition: sinval.h:67
#define SHAREDINVALSMGR_ID
Definition: sinval.h:85
#define SHAREDINVALSNAPSHOT_ID
Definition: sinval.h:104
#define SHAREDINVALRELCACHE_ID
Definition: sinval.h:76
#define SHAREDINVALRELMAP_ID
Definition: sinval.h:96
void smgrclosenode(RelFileNodeBackend rnode)
Definition: smgr.c:346
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:456
RelcacheCallbackFunction function
Definition: inval.c:270
BackendId backend
Definition: relfilenode.h:75
RelFileNode node
Definition: relfilenode.h:74
uint16 backend_lo
Definition: sinval.h:92
RelFileNode rnode
Definition: sinval.h:93
void SysCacheInvalidate(int cacheId, uint32 hashValue)
Definition: syscache.c:1506
SharedInvalCatcacheMsg cc
Definition: sinval.h:116
SharedInvalRelcacheMsg rc
Definition: sinval.h:118
SharedInvalCatalogMsg cat
Definition: sinval.h:117
SharedInvalSmgrMsg sm
Definition: sinval.h:119
SharedInvalSnapshotMsg sn
Definition: sinval.h:121
SharedInvalRelmapMsg rm
Definition: sinval.h:120

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, SharedInvalSnapshotMsg::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(), SharedInvalidationMessage::sn, and SysCacheInvalidate().

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

◆ ProcessCatchupInterrupt()

void ProcessCatchupInterrupt ( void  )

Definition at line 176 of file sinval.c.

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 }
#define DEBUG4
Definition: elog.h:21
void AcceptInvalidationMessages(void)
Definition: inval.c:746
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4784
void StartTransactionCommand(void)
Definition: xact.c:2925
void CommitTransactionCommand(void)
Definition: xact.c:3022

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

Referenced by HandleAutoVacLauncherInterrupts(), and ProcessClientReadInterrupt().

◆ ProcessCommittedInvalidationMessages()

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

Definition at line 963 of file inval.c.

966 {
967  if (nmsgs <= 0)
968  return;
969 
970  elog(trace_recovery(DEBUG4), "replaying commit with %d messages%s", nmsgs,
971  (RelcacheInitFileInval ? " and relcache file invalidation" : ""));
972 
973  if (RelcacheInitFileInval)
974  {
975  elog(trace_recovery(DEBUG4), "removing relcache init files for database %u",
976  dbid);
977 
978  /*
979  * RelationCacheInitFilePreInvalidate, when the invalidation message
980  * is for a specific database, requires DatabasePath to be set, but we
981  * should not use SetDatabasePath during recovery, since it is
982  * intended to be used only once by normal backends. Hence, a quick
983  * hack: set DatabasePath directly then unset after use.
984  */
985  if (OidIsValid(dbid))
986  DatabasePath = GetDatabasePath(dbid, tsid);
987 
989 
990  if (OidIsValid(dbid))
991  {
993  DatabasePath = NULL;
994  }
995  }
996 
997  SendSharedInvalidMessages(msgs, nmsgs);
998 
999  if (RelcacheInitFileInval)
1001 }
#define OidIsValid(objectId)
Definition: c.h:710
int trace_recovery(int trace_level)
Definition: elog.c:3423
char * DatabasePath
Definition: globals.c:97
void pfree(void *pointer)
Definition: mcxt.c:1175
void RelationCacheInitFilePostInvalidate(void)
Definition: relcache.c:6646
void RelationCacheInitFilePreInvalidate(void)
Definition: relcache.c:6621
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:110
void SendSharedInvalidMessages(const SharedInvalidationMessage *msgs, int n)
Definition: sinval.c:49

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

Referenced by standby_redo(), and xact_redo_commit().

◆ ReceiveSharedInvalidMessages()

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

Definition at line 71 of file sinval.c.

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 }
uint64 SharedInvalidMessageCounter
Definition: sinval.c:26
#define MAXINVALMSGS
void SICleanupQueue(bool callerHasWriteLock, int minFree)
Definition: sinvaladt.c:649
int SIGetDataEntries(SharedInvalidationMessage *data, int datasize)
Definition: sinvaladt.c:545

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

Referenced by AcceptInvalidationMessages().

◆ SendSharedInvalidMessages()

void SendSharedInvalidMessages ( const SharedInvalidationMessage msgs,
int  n 
)

Definition at line 49 of file sinval.c.

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

References SIInsertDataEntries().

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

◆ xactGetCommittedInvalidationMessages()

int xactGetCommittedInvalidationMessages ( SharedInvalidationMessage **  msgs,
bool RelcacheInitFileInval 
)

Definition at line 884 of file inval.c.

886 {
887  SharedInvalidationMessage *msgarray;
888  int nummsgs;
889  int nmsgs;
890 
891  /* Quick exit if we haven't done anything with invalidation messages. */
892  if (transInvalInfo == NULL)
893  {
894  *RelcacheInitFileInval = false;
895  *msgs = NULL;
896  return 0;
897  }
898 
899  /* Must be at top of stack */
901 
902  /*
903  * Relcache init file invalidation requires processing both before and
904  * after we send the SI messages. However, we need not do anything unless
905  * we committed.
906  */
907  *RelcacheInitFileInval = transInvalInfo->RelcacheInitFileInval;
908 
909  /*
910  * Collect all the pending messages into a single contiguous array of
911  * invalidation messages, to simplify what needs to happen while building
912  * the commit WAL message. Maintain the order that they would be
913  * processed in by AtEOXact_Inval(), to ensure emulated behaviour in redo
914  * is as similar as possible to original. We want the same bugs, if any,
915  * not new ones.
916  */
919 
920  *msgs = msgarray = (SharedInvalidationMessage *)
922  nummsgs * sizeof(SharedInvalidationMessage));
923 
924  nmsgs = 0;
926  CatCacheMsgs,
927  (memcpy(msgarray + nmsgs,
928  msgs,
929  n * sizeof(SharedInvalidationMessage)),
930  nmsgs += n));
932  CatCacheMsgs,
933  (memcpy(msgarray + nmsgs,
934  msgs,
935  n * sizeof(SharedInvalidationMessage)),
936  nmsgs += n));
938  RelCacheMsgs,
939  (memcpy(msgarray + nmsgs,
940  msgs,
941  n * sizeof(SharedInvalidationMessage)),
942  nmsgs += n));
944  RelCacheMsgs,
945  (memcpy(msgarray + nmsgs,
946  msgs,
947  n * sizeof(SharedInvalidationMessage)),
948  nmsgs += n));
949  Assert(nmsgs == nummsgs);
950 
951  return nmsgs;
952 }
#define NumMessagesInGroup(group)
Definition: inval.c:198
#define CatCacheMsgs
Definition: inval.c:162
#define ProcessMessageSubGroupMulti(group, subgroup, codeFragment)
Definition: inval.c:373
static TransInvalidationInfo * transInvalInfo
Definition: inval.c:238
#define RelCacheMsgs
Definition: inval.c:163
Assert(fmt[strlen(fmt) - 1] !='\n')
MemoryContext CurTransactionContext
Definition: mcxt.c:54
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
struct TransInvalidationInfo * parent
Definition: inval.c:223
InvalidationMsgsGroup CurrentCmdInvalidMsgs
Definition: inval.c:229
InvalidationMsgsGroup PriorCmdInvalidMsgs
Definition: inval.c:232
bool RelcacheInitFileInval
Definition: inval.c:235

References Assert(), CatCacheMsgs, TransInvalidationInfo::CurrentCmdInvalidMsgs, CurTransactionContext, MemoryContextAlloc(), TransInvalidationInfo::my_level, NumMessagesInGroup, TransInvalidationInfo::parent, TransInvalidationInfo::PriorCmdInvalidMsgs, ProcessMessageSubGroupMulti, TransInvalidationInfo::RelcacheInitFileInval, RelCacheMsgs, and transInvalInfo.

Referenced by RecordTransactionCommit(), and StartPrepare().

Variable Documentation

◆ catchupInterruptPending

PGDLLIMPORT volatile sig_atomic_t catchupInterruptPending
extern

◆ SharedInvalidMessageCounter

PGDLLIMPORT uint64 SharedInvalidMessageCounter
extern