Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * sinval.c
4 : : * POSTGRES shared cache invalidation communication code.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/storage/ipc/sinval.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/xact.h"
18 : : #include "miscadmin.h"
19 : : #include "storage/latch.h"
20 : : #include "storage/sinvaladt.h"
21 : : #include "utils/inval.h"
22 : :
23 : :
24 : : uint64 SharedInvalidMessageCounter;
25 : :
26 : :
27 : : /*
28 : : * Because backends sitting idle will not be reading sinval events, we
29 : : * need a way to give an idle backend a swift kick in the rear and make
30 : : * it catch up before the sinval queue overflows and forces it to go
31 : : * through a cache reset exercise. This is done by sending
32 : : * PROCSIG_CATCHUP_INTERRUPT to any backend that gets too far behind.
33 : : *
34 : : * The signal handler will set an interrupt pending flag and will set the
35 : : * processes latch. Whenever starting to read from the client, or when
36 : : * interrupted while doing so, ProcessClientReadInterrupt() will call
37 : : * ProcessCatchupEvent().
38 : : */
39 : : volatile sig_atomic_t catchupInterruptPending = false;
40 : :
41 : :
42 : : /*
43 : : * SendSharedInvalidMessages
44 : : * Add shared-cache-invalidation message(s) to the global SI message queue.
45 : : */
46 : : void
6736 tgl@sss.pgh.pa.us 47 :CBC 478738 : SendSharedInvalidMessages(const SharedInvalidationMessage *msgs, int n)
48 : : {
49 : 478738 : SIInsertDataEntries(msgs, n);
11099 scrappy@hub.org 50 : 478738 : }
51 : :
52 : : /*
53 : : * ReceiveSharedInvalidMessages
54 : : * Process shared-cache-invalidation messages waiting for this backend
55 : : *
56 : : * We guarantee to process all messages that had been queued before the
57 : : * routine was entered. It is of course possible for more messages to get
58 : : * queued right after our last SIGetDataEntries call.
59 : : *
60 : : * NOTE: it is entirely possible for this routine to be invoked recursively
61 : : * as a consequence of processing inside the invalFunction or resetFunction.
62 : : * Furthermore, such a recursive call must guarantee that all outstanding
63 : : * inval messages have been processed before it exits. This is the reason
64 : : * for the strange-looking choice to use a statically allocated buffer array
65 : : * and counters; it's so that a recursive call can process messages already
66 : : * sucked out of sinvaladt.c.
67 : : */
68 : : void
2677 andres@anarazel.de 69 : 23054881 : ReceiveSharedInvalidMessages(void (*invalFunction) (SharedInvalidationMessage *msg),
70 : : void (*resetFunction) (void))
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 */
6736 tgl@sss.pgh.pa.us 83 [ + + ]: 23056787 : while (nextmsg < nummsgs)
84 : : {
4590 85 : 1906 : SharedInvalidationMessage msg = messages[nextmsg++];
86 : :
5622 rhaas@postgresql.org 87 : 1906 : SharedInvalidMessageCounter++;
4590 tgl@sss.pgh.pa.us 88 : 1906 : invalFunction(&msg);
89 : : }
90 : :
91 : : do
92 : : {
93 : : int getResult;
94 : :
6736 95 : 23588617 : nextmsg = nummsgs = 0;
96 : :
97 : : /* Try to get some more messages */
98 : 23588617 : getResult = SIGetDataEntries(messages, MAXINVALMSGS);
99 : :
9947 100 [ + + ]: 23588617 : if (getResult < 0)
101 : : {
102 : : /* got a reset message */
8528 103 [ - + ]: 260 : elog(DEBUG4, "cache state reset");
5622 rhaas@postgresql.org 104 : 260 : SharedInvalidMessageCounter++;
9947 tgl@sss.pgh.pa.us 105 : 260 : resetFunction();
6736 106 : 260 : break; /* nothing more to do */
107 : : }
108 : :
109 : : /* Process them, being wary that a recursive call might eat some */
110 : 23588357 : nextmsg = 0;
111 : 23588357 : nummsgs = getResult;
112 : :
113 [ + + ]: 44050351 : while (nextmsg < nummsgs)
114 : : {
4590 115 : 20461994 : SharedInvalidationMessage msg = messages[nextmsg++];
116 : :
5622 rhaas@postgresql.org 117 : 20461994 : SharedInvalidMessageCounter++;
4590 tgl@sss.pgh.pa.us 118 : 20461994 : 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 : : */
6736 125 [ + + ]: 23588357 : } while (nummsgs == MAXINVALMSGS);
126 : :
127 : : /*
128 : : * We are now caught up. If we received a catchup signal, call
129 : : * SICleanupQueue(). This is not so much because we need to flush dead
130 : : * messages right now, as that we want to pass on the catchup signal to
131 : : * the next slowest backend. "Daisy chaining" the catchup signal this way
132 : : * avoids creating spikes in system load for what should be just a
133 : : * background maintenance activity.
134 : : *
135 : : * SICleanupQueue() resets catchupInterruptPending, unless we got signaled
136 : : * again in the meanwhile.
137 : : */
4316 andres@anarazel.de 138 [ + + ]: 23054881 : if (catchupInterruptPending)
139 : : {
6736 tgl@sss.pgh.pa.us 140 [ - + ]: 3021 : elog(DEBUG4, "sinval catchup complete, cleaning queue");
141 : 3021 : SICleanupQueue(false, 0);
142 : : }
11099 scrappy@hub.org 143 : 23054881 : }
144 : :
145 : :
146 : : /*
147 : : * HandleCatchupInterrupt
148 : : *
149 : : * This is called when PROCSIG_CATCHUP_INTERRUPT is received.
150 : : *
151 : : * We used to directly call ProcessCatchupEvent directly when idle. These days
152 : : * we just set a flag to do it later and notify the process of that fact by
153 : : * setting the process's latch.
154 : : */
155 : : void
6329 tgl@sss.pgh.pa.us 156 : 3024 : HandleCatchupInterrupt(void)
157 : : {
158 : : /*
159 : : * Note: this is called by a SIGNAL HANDLER. You must be very wary what
160 : : * you do here.
161 : : */
162 : :
4316 andres@anarazel.de 163 : 3024 : catchupInterruptPending = true;
164 : :
165 : : /* latch will be set by procsignal_sigusr1_handler */
8224 tgl@sss.pgh.pa.us 166 : 3024 : }
167 : :
168 : : /*
169 : : * ProcessCatchupInterrupt
170 : : *
171 : : * The portion of catchup interrupt handling that runs outside of the signal
172 : : * handler, which allows it to actually process pending invalidations.
173 : : */
174 : : void
4316 andres@anarazel.de 175 : 2288 : ProcessCatchupInterrupt(void)
176 : : {
177 [ + + ]: 3873 : while (catchupInterruptPending)
178 : : {
179 : : /*
180 : : * What we need to do here is cause ReceiveSharedInvalidMessages() to
181 : : * run, which will do the necessary work and also reset the
182 : : * catchupInterruptPending flag. If we are inside a transaction we
183 : : * can just call AcceptInvalidationMessages() to do this. If we
184 : : * aren't, we start and immediately end a transaction; the call to
185 : : * AcceptInvalidationMessages() happens down inside transaction start.
186 : : *
187 : : * It is awfully tempting to just call AcceptInvalidationMessages()
188 : : * without the rest of the xact start/stop overhead, and I think that
189 : : * would actually work in the normal case; but I am not sure that
190 : : * things would clean up nicely if we got an error partway through.
191 : : */
192 [ + + ]: 1585 : if (IsTransactionOrTransactionBlock())
193 : : {
194 [ - + ]: 29 : elog(DEBUG4, "ProcessCatchupEvent inside transaction");
195 : 29 : AcceptInvalidationMessages();
196 : : }
197 : : else
198 : : {
199 [ - + ]: 1556 : elog(DEBUG4, "ProcessCatchupEvent outside transaction");
200 : 1556 : StartTransactionCommand();
201 : 1556 : CommitTransactionCommand();
202 : : }
203 : : }
8224 tgl@sss.pgh.pa.us 204 : 2288 : }
|