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
 
struct  SharedInvalRelSyncMsg
 
union  SharedInvalidationMessage
 

Macros

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

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)
 
int inplaceGetInvalidationMessages (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 68 of file sinval.h.

◆ SHAREDINVALRELCACHE_ID

#define SHAREDINVALRELCACHE_ID   (-2)

Definition at line 77 of file sinval.h.

◆ SHAREDINVALRELMAP_ID

#define SHAREDINVALRELMAP_ID   (-4)

Definition at line 97 of file sinval.h.

◆ SHAREDINVALRELSYNC_ID

#define SHAREDINVALRELSYNC_ID   (-6)

Definition at line 114 of file sinval.h.

◆ SHAREDINVALSMGR_ID

#define SHAREDINVALSMGR_ID   (-3)

Definition at line 86 of file sinval.h.

◆ SHAREDINVALSNAPSHOT_ID

#define SHAREDINVALSNAPSHOT_ID   (-5)

Definition at line 105 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 */
165}
struct Latch * MyLatch
Definition: globals.c:62
void SetLatch(Latch *latch)
Definition: latch.c:288
volatile sig_atomic_t catchupInterruptPending
Definition: sinval.c:39

References catchupInterruptPending, MyLatch, and SetLatch().

Referenced by procsignal_sigusr1_handler().

◆ inplaceGetInvalidationMessages()

int inplaceGetInvalidationMessages ( SharedInvalidationMessage **  msgs,
bool *  RelcacheInitFileInval 
)

Definition at line 1081 of file inval.c.

1083{
1084 SharedInvalidationMessage *msgarray;
1085 int nummsgs;
1086 int nmsgs;
1087
1088 /* Quick exit if we haven't done anything with invalidation messages. */
1089 if (inplaceInvalInfo == NULL)
1090 {
1091 *RelcacheInitFileInval = false;
1092 *msgs = NULL;
1093 return 0;
1094 }
1095
1096 *RelcacheInitFileInval = inplaceInvalInfo->RelcacheInitFileInval;
1098 *msgs = msgarray = (SharedInvalidationMessage *)
1099 palloc(nummsgs * sizeof(SharedInvalidationMessage));
1100
1101 nmsgs = 0;
1104 (memcpy(msgarray + nmsgs,
1105 msgs,
1106 n * sizeof(SharedInvalidationMessage)),
1107 nmsgs += n));
1110 (memcpy(msgarray + nmsgs,
1111 msgs,
1112 n * sizeof(SharedInvalidationMessage)),
1113 nmsgs += n));
1114 Assert(nmsgs == nummsgs);
1115
1116 return nmsgs;
1117}
Assert(PointerIsAligned(start, uint64))
#define NumMessagesInGroup(group)
Definition: inval.c:207
#define CatCacheMsgs
Definition: inval.c:171
#define ProcessMessageSubGroupMulti(group, subgroup, codeFragment)
Definition: inval.c:402
static InvalidationInfo * inplaceInvalInfo
Definition: inval.c:257
#define RelCacheMsgs
Definition: inval.c:172
void * palloc(Size size)
Definition: mcxt.c:1317
bool RelcacheInitFileInval
Definition: inval.c:236
InvalidationMsgsGroup CurrentCmdInvalidMsgs
Definition: inval.c:233

References Assert(), CatCacheMsgs, InvalidationInfo::CurrentCmdInvalidMsgs, inplaceInvalInfo, NumMessagesInGroup, palloc(), ProcessMessageSubGroupMulti, InvalidationInfo::RelcacheInitFileInval, and RelCacheMsgs.

Referenced by heap_inplace_update_and_unlock().

◆ LocalExecuteInvalidationMessage()

void LocalExecuteInvalidationMessage ( SharedInvalidationMessage msg)

Definition at line 822 of file inval.c.

823{
824 if (msg->id >= 0)
825 {
826 if (msg->cc.dbId == MyDatabaseId || msg->cc.dbId == InvalidOid)
827 {
829
830 SysCacheInvalidate(msg->cc.id, msg->cc.hashValue);
831
833 }
834 }
835 else if (msg->id == SHAREDINVALCATALOG_ID)
836 {
837 if (msg->cat.dbId == MyDatabaseId || msg->cat.dbId == InvalidOid)
838 {
840
842
843 /* CatalogCacheFlushCatalog calls CallSyscacheCallbacks as needed */
844 }
845 }
846 else if (msg->id == SHAREDINVALRELCACHE_ID)
847 {
848 if (msg->rc.dbId == MyDatabaseId || msg->rc.dbId == InvalidOid)
849 {
850 int i;
851
852 if (msg->rc.relId == InvalidOid)
854 else
856
857 for (i = 0; i < relcache_callback_count; i++)
858 {
859 struct RELCACHECALLBACK *ccitem = relcache_callback_list + i;
860
861 ccitem->function(ccitem->arg, msg->rc.relId);
862 }
863 }
864 }
865 else if (msg->id == SHAREDINVALSMGR_ID)
866 {
867 /*
868 * We could have smgr entries for relations of other databases, so no
869 * short-circuit test is possible here.
870 */
871 RelFileLocatorBackend rlocator;
872
873 rlocator.locator = msg->sm.rlocator;
874 rlocator.backend = (msg->sm.backend_hi << 16) | (int) msg->sm.backend_lo;
875 smgrreleaserellocator(rlocator);
876 }
877 else if (msg->id == SHAREDINVALRELMAP_ID)
878 {
879 /* We only care about our own database and shared catalogs */
880 if (msg->rm.dbId == InvalidOid)
882 else if (msg->rm.dbId == MyDatabaseId)
884 }
885 else if (msg->id == SHAREDINVALSNAPSHOT_ID)
886 {
887 /* We only care about our own database and shared catalogs */
888 if (msg->sn.dbId == InvalidOid)
890 else if (msg->sn.dbId == MyDatabaseId)
892 }
893 else if (msg->id == SHAREDINVALRELSYNC_ID)
894 {
895 /* We only care about our own database */
896 if (msg->rs.dbId == MyDatabaseId)
898 }
899 else
900 elog(FATAL, "unrecognized SI message ID: %d", msg->id);
901}
void CatalogCacheFlushCatalog(Oid catId)
Definition: catcache.c:834
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:225
Oid MyDatabaseId
Definition: globals.c:93
void CallRelSyncCallbacks(Oid relid)
Definition: inval.c:1906
static int relcache_callback_count
Definition: inval.c:294
static struct RELCACHECALLBACK relcache_callback_list[MAX_RELCACHE_CALLBACKS]
void CallSyscacheCallbacks(int cacheid, uint32 hashvalue)
Definition: inval.c:1884
int i
Definition: isn.c:74
#define InvalidOid
Definition: postgres_ext.h:37
void RelationCacheInvalidate(bool debug_discard)
Definition: relcache.c:2954
void RelationCacheInvalidateEntry(Oid relationId)
Definition: relcache.c:2898
void RelationMapInvalidate(bool shared)
Definition: relmapper.c:468
#define SHAREDINVALCATALOG_ID
Definition: sinval.h:68
#define SHAREDINVALRELSYNC_ID
Definition: sinval.h:114
#define SHAREDINVALSMGR_ID
Definition: sinval.h:86
#define SHAREDINVALSNAPSHOT_ID
Definition: sinval.h:105
#define SHAREDINVALRELCACHE_ID
Definition: sinval.h:77
#define SHAREDINVALRELMAP_ID
Definition: sinval.h:97
void smgrreleaserellocator(RelFileLocatorBackend rlocator)
Definition: smgr.c:382
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:443
RelcacheCallbackFunction function
Definition: inval.c:290
RelFileLocator locator
uint16 backend_lo
Definition: sinval.h:93
RelFileLocator rlocator
Definition: sinval.h:94
void SysCacheInvalidate(int cacheId, uint32 hashValue)
Definition: syscache.c:698
SharedInvalCatcacheMsg cc
Definition: sinval.h:127
SharedInvalRelcacheMsg rc
Definition: sinval.h:129
SharedInvalCatalogMsg cat
Definition: sinval.h:128
SharedInvalRelSyncMsg rs
Definition: sinval.h:133
SharedInvalSmgrMsg sm
Definition: sinval.h:130
SharedInvalSnapshotMsg sn
Definition: sinval.h:132
SharedInvalRelmapMsg rm
Definition: sinval.h:131

References RELCACHECALLBACK::arg, RelFileLocatorBackend::backend, SharedInvalSmgrMsg::backend_hi, SharedInvalSmgrMsg::backend_lo, CallRelSyncCallbacks(), CallSyscacheCallbacks(), SharedInvalidationMessage::cat, CatalogCacheFlushCatalog(), SharedInvalCatalogMsg::catId, SharedInvalidationMessage::cc, SharedInvalCatcacheMsg::dbId, SharedInvalCatalogMsg::dbId, SharedInvalRelcacheMsg::dbId, SharedInvalRelmapMsg::dbId, SharedInvalSnapshotMsg::dbId, SharedInvalRelSyncMsg::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, SharedInvalRelSyncMsg::relid, SharedInvalSmgrMsg::rlocator, SharedInvalidationMessage::rm, SharedInvalidationMessage::rs, SHAREDINVALCATALOG_ID, SHAREDINVALRELCACHE_ID, SHAREDINVALRELMAP_ID, SHAREDINVALRELSYNC_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:929
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4989
void StartTransactionCommand(void)
Definition: xact.c:3059
void CommitTransactionCommand(void)
Definition: xact.c:3157

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

Referenced by ProcessAutoVacLauncherInterrupts(), and ProcessClientReadInterrupt().

◆ ProcessCommittedInvalidationMessages()

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

Definition at line 1128 of file inval.c.

1131{
1132 if (nmsgs <= 0)
1133 return;
1134
1135 elog(DEBUG4, "replaying commit with %d messages%s", nmsgs,
1136 (RelcacheInitFileInval ? " and relcache file invalidation" : ""));
1137
1138 if (RelcacheInitFileInval)
1139 {
1140 elog(DEBUG4, "removing relcache init files for database %u", dbid);
1141
1142 /*
1143 * RelationCacheInitFilePreInvalidate, when the invalidation message
1144 * is for a specific database, requires DatabasePath to be set, but we
1145 * should not use SetDatabasePath during recovery, since it is
1146 * intended to be used only once by normal backends. Hence, a quick
1147 * hack: set DatabasePath directly then unset after use.
1148 */
1149 if (OidIsValid(dbid))
1150 DatabasePath = GetDatabasePath(dbid, tsid);
1151
1153
1154 if (OidIsValid(dbid))
1155 {
1157 DatabasePath = NULL;
1158 }
1159 }
1160
1161 SendSharedInvalidMessages(msgs, nmsgs);
1162
1163 if (RelcacheInitFileInval)
1165}
#define OidIsValid(objectId)
Definition: c.h:746
char * DatabasePath
Definition: globals.c:103
void pfree(void *pointer)
Definition: mcxt.c:1524
void RelationCacheInitFilePostInvalidate(void)
Definition: relcache.c:6813
void RelationCacheInitFilePreInvalidate(void)
Definition: relcache.c:6788
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 heap_xlog_inplace(), 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
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 {
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:576
int SIGetDataEntries(SharedInvalidationMessage *data, int datasize)
Definition: sinvaladt.c:472

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:369

References SIInsertDataEntries().

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

◆ xactGetCommittedInvalidationMessages()

int xactGetCommittedInvalidationMessages ( SharedInvalidationMessage **  msgs,
bool *  RelcacheInitFileInval 
)

Definition at line 1005 of file inval.c.

1007{
1008 SharedInvalidationMessage *msgarray;
1009 int nummsgs;
1010 int nmsgs;
1011
1012 /* Quick exit if we haven't done anything with invalidation messages. */
1013 if (transInvalInfo == NULL)
1014 {
1015 *RelcacheInitFileInval = false;
1016 *msgs = NULL;
1017 return 0;
1018 }
1019
1020 /* Must be at top of stack */
1022
1023 /*
1024 * Relcache init file invalidation requires processing both before and
1025 * after we send the SI messages. However, we need not do anything unless
1026 * we committed.
1027 */
1028 *RelcacheInitFileInval = transInvalInfo->ii.RelcacheInitFileInval;
1029
1030 /*
1031 * Collect all the pending messages into a single contiguous array of
1032 * invalidation messages, to simplify what needs to happen while building
1033 * the commit WAL message. Maintain the order that they would be
1034 * processed in by AtEOXact_Inval(), to ensure emulated behaviour in redo
1035 * is as similar as possible to original. We want the same bugs, if any,
1036 * not new ones.
1037 */
1040
1041 *msgs = msgarray = (SharedInvalidationMessage *)
1043 nummsgs * sizeof(SharedInvalidationMessage));
1044
1045 nmsgs = 0;
1048 (memcpy(msgarray + nmsgs,
1049 msgs,
1050 n * sizeof(SharedInvalidationMessage)),
1051 nmsgs += n));
1054 (memcpy(msgarray + nmsgs,
1055 msgs,
1056 n * sizeof(SharedInvalidationMessage)),
1057 nmsgs += n));
1060 (memcpy(msgarray + nmsgs,
1061 msgs,
1062 n * sizeof(SharedInvalidationMessage)),
1063 nmsgs += n));
1066 (memcpy(msgarray + nmsgs,
1067 msgs,
1068 n * sizeof(SharedInvalidationMessage)),
1069 nmsgs += n));
1070 Assert(nmsgs == nummsgs);
1071
1072 return nmsgs;
1073}
static TransInvalidationInfo * transInvalInfo
Definition: inval.c:255
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
MemoryContext CurTransactionContext
Definition: mcxt.c:155
struct TransInvalidationInfo * parent
Definition: inval.c:249
struct InvalidationInfo ii
Definition: inval.c:243
InvalidationMsgsGroup PriorCmdInvalidMsgs
Definition: inval.c:246

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

Referenced by RecordTransactionCommit(), and StartPrepare().

Variable Documentation

◆ catchupInterruptPending

PGDLLIMPORT volatile sig_atomic_t catchupInterruptPending
extern

◆ SharedInvalidMessageCounter

PGDLLIMPORT uint64 SharedInvalidMessageCounter
extern