PostgreSQL Source Code  git master
sinval.h File Reference
#include <signal.h>
#include "storage/relfilelocator.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 154 of file sinval.c.

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

References catchupInterruptPending, MyLatch, and SetLatch().

Referenced by procsignal_sigusr1_handler().

◆ LocalExecuteInvalidationMessage()

void LocalExecuteInvalidationMessage ( SharedInvalidationMessage msg)

Definition at line 705 of file inval.c.

706 {
707  if (msg->id >= 0)
708  {
709  if (msg->cc.dbId == MyDatabaseId || msg->cc.dbId == InvalidOid)
710  {
712 
713  SysCacheInvalidate(msg->cc.id, msg->cc.hashValue);
714 
715  CallSyscacheCallbacks(msg->cc.id, msg->cc.hashValue);
716  }
717  }
718  else if (msg->id == SHAREDINVALCATALOG_ID)
719  {
720  if (msg->cat.dbId == MyDatabaseId || msg->cat.dbId == InvalidOid)
721  {
723 
725 
726  /* CatalogCacheFlushCatalog calls CallSyscacheCallbacks as needed */
727  }
728  }
729  else if (msg->id == SHAREDINVALRELCACHE_ID)
730  {
731  if (msg->rc.dbId == MyDatabaseId || msg->rc.dbId == InvalidOid)
732  {
733  int i;
734 
735  if (msg->rc.relId == InvalidOid)
737  else
739 
740  for (i = 0; i < relcache_callback_count; i++)
741  {
742  struct RELCACHECALLBACK *ccitem = relcache_callback_list + i;
743 
744  ccitem->function(ccitem->arg, msg->rc.relId);
745  }
746  }
747  }
748  else if (msg->id == SHAREDINVALSMGR_ID)
749  {
750  /*
751  * We could have smgr entries for relations of other databases, so no
752  * short-circuit test is possible here.
753  */
754  RelFileLocatorBackend rlocator;
755 
756  rlocator.locator = msg->sm.rlocator;
757  rlocator.backend = (msg->sm.backend_hi << 16) | (int) msg->sm.backend_lo;
758  smgrreleaserellocator(rlocator);
759  }
760  else if (msg->id == SHAREDINVALRELMAP_ID)
761  {
762  /* We only care about our own database and shared catalogs */
763  if (msg->rm.dbId == InvalidOid)
764  RelationMapInvalidate(true);
765  else if (msg->rm.dbId == MyDatabaseId)
766  RelationMapInvalidate(false);
767  }
768  else if (msg->id == SHAREDINVALSNAPSHOT_ID)
769  {
770  /* We only care about our own database and shared catalogs */
771  if (msg->sn.dbId == InvalidOid)
773  else if (msg->sn.dbId == MyDatabaseId)
775  }
776  else
777  elog(FATAL, "unrecognized SI message ID: %d", msg->id);
778 }
void CatalogCacheFlushCatalog(Oid catId)
Definition: catcache.c:783
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:224
Oid MyDatabaseId
Definition: globals.c:91
static int relcache_callback_count
Definition: inval.c:273
static struct RELCACHECALLBACK relcache_callback_list[MAX_RELCACHE_CALLBACKS]
void CallSyscacheCallbacks(int cacheid, uint32 hashvalue)
Definition: inval.c:1577
int i
Definition: isn.c:73
#define InvalidOid
Definition: postgres_ext.h:36
void RelationCacheInvalidate(bool debug_discard)
Definition: relcache.c:2973
void RelationCacheInvalidateEntry(Oid relationId)
Definition: relcache.c:2917
void RelationMapInvalidate(bool shared)
Definition: relmapper.c:468
#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 smgrreleaserellocator(RelFileLocatorBackend rlocator)
Definition: smgr.c:379
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:422
RelcacheCallbackFunction function
Definition: inval.c:269
RelFileLocator locator
uint16 backend_lo
Definition: sinval.h:92
RelFileLocator rlocator
Definition: sinval.h:93
void SysCacheInvalidate(int cacheId, uint32 hashValue)
Definition: syscache.c:577
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, RelFileLocatorBackend::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, RelFileLocatorBackend::locator, MyDatabaseId, SharedInvalidationMessage::rc, RelationCacheInvalidate(), RelationCacheInvalidateEntry(), RelationMapInvalidate(), relcache_callback_count, relcache_callback_list, SharedInvalRelcacheMsg::relId, SharedInvalSmgrMsg::rlocator, SharedInvalidationMessage::rm, SHAREDINVALCATALOG_ID, SHAREDINVALRELCACHE_ID, SHAREDINVALRELMAP_ID, SHAREDINVALSMGR_ID, SHAREDINVALSNAPSHOT_ID, SharedInvalidationMessage::sm, smgrreleaserellocator(), SharedInvalidationMessage::sn, and SysCacheInvalidate().

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

◆ ProcessCatchupInterrupt()

void ProcessCatchupInterrupt ( void  )

Definition at line 174 of file sinval.c.

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

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 961 of file inval.c.

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

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

Referenced by standby_redo(), and xact_redo_commit().

◆ ReceiveSharedInvalidMessages()

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

Definition at line 69 of file sinval.c.

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

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

Referenced by AcceptInvalidationMessages().

◆ SendSharedInvalidMessages()

void SendSharedInvalidMessages ( const SharedInvalidationMessage msgs,
int  n 
)

Definition at line 47 of file sinval.c.

48 {
49  SIInsertDataEntries(msgs, n);
50 }
void SIInsertDataEntries(const SharedInvalidationMessage *data, int n)
Definition: sinvaladt.c:370

References SIInsertDataEntries().

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

◆ xactGetCommittedInvalidationMessages()

int xactGetCommittedInvalidationMessages ( SharedInvalidationMessage **  msgs,
bool RelcacheInitFileInval 
)

Definition at line 882 of file inval.c.

884 {
885  SharedInvalidationMessage *msgarray;
886  int nummsgs;
887  int nmsgs;
888 
889  /* Quick exit if we haven't done anything with invalidation messages. */
890  if (transInvalInfo == NULL)
891  {
892  *RelcacheInitFileInval = false;
893  *msgs = NULL;
894  return 0;
895  }
896 
897  /* Must be at top of stack */
899 
900  /*
901  * Relcache init file invalidation requires processing both before and
902  * after we send the SI messages. However, we need not do anything unless
903  * we committed.
904  */
905  *RelcacheInitFileInval = transInvalInfo->RelcacheInitFileInval;
906 
907  /*
908  * Collect all the pending messages into a single contiguous array of
909  * invalidation messages, to simplify what needs to happen while building
910  * the commit WAL message. Maintain the order that they would be
911  * processed in by AtEOXact_Inval(), to ensure emulated behaviour in redo
912  * is as similar as possible to original. We want the same bugs, if any,
913  * not new ones.
914  */
917 
918  *msgs = msgarray = (SharedInvalidationMessage *)
920  nummsgs * sizeof(SharedInvalidationMessage));
921 
922  nmsgs = 0;
924  CatCacheMsgs,
925  (memcpy(msgarray + nmsgs,
926  msgs,
927  n * sizeof(SharedInvalidationMessage)),
928  nmsgs += n));
930  CatCacheMsgs,
931  (memcpy(msgarray + nmsgs,
932  msgs,
933  n * sizeof(SharedInvalidationMessage)),
934  nmsgs += n));
936  RelCacheMsgs,
937  (memcpy(msgarray + nmsgs,
938  msgs,
939  n * sizeof(SharedInvalidationMessage)),
940  nmsgs += n));
942  RelCacheMsgs,
943  (memcpy(msgarray + nmsgs,
944  msgs,
945  n * sizeof(SharedInvalidationMessage)),
946  nmsgs += n));
947  Assert(nmsgs == nummsgs);
948 
949  return nmsgs;
950 }
#define Assert(condition)
Definition: c.h:858
#define NumMessagesInGroup(group)
Definition: inval.c:197
#define CatCacheMsgs
Definition: inval.c:161
#define ProcessMessageSubGroupMulti(group, subgroup, codeFragment)
Definition: inval.c:372
static TransInvalidationInfo * transInvalInfo
Definition: inval.c:237
#define RelCacheMsgs
Definition: inval.c:162
MemoryContext CurTransactionContext
Definition: mcxt.c:155
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1180
struct TransInvalidationInfo * parent
Definition: inval.c:222
InvalidationMsgsGroup CurrentCmdInvalidMsgs
Definition: inval.c:228
InvalidationMsgsGroup PriorCmdInvalidMsgs
Definition: inval.c:231
bool RelcacheInitFileInval
Definition: inval.c:234

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