Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * execAsync.c
4 : : * Support routines for asynchronous execution
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/executor/execAsync.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #include "postgres.h"
16 : :
17 : : #include "executor/execAsync.h"
18 : : #include "executor/executor.h"
19 : : #include "executor/instrument.h"
20 : : #include "executor/nodeAppend.h"
21 : : #include "executor/nodeForeignscan.h"
22 : : #include "executor/nodeMergeAppend.h"
23 : : #include "miscadmin.h"
24 : :
25 : : #define EVENT_BUFFER_SIZE 16
26 : :
27 : : /*
28 : : * Asynchronously request a tuple from a designed async-capable node.
29 : : */
30 : : void
31 : 7876 : ExecAsyncRequest(AsyncRequest *areq)
32 : : {
33 [ + + ]: 7876 : if (areq->requestee->chgParam != NULL) /* something changed? */
34 : 20 : ExecReScan(areq->requestee); /* let ReScan handle this */
35 : :
36 : : /* must provide our own instrumentation support */
37 [ + + ]: 7876 : if (areq->requestee->instrument)
38 : 810 : InstrStartNode(areq->requestee->instrument);
39 : :
40 [ + - ]: 7876 : switch (nodeTag(areq->requestee))
41 : : {
42 : 7876 : case T_ForeignScanState:
43 : 7876 : ExecAsyncForeignScanRequest(areq);
44 : 7876 : break;
45 : 0 : default:
46 : : /* If the node doesn't support async, caller messed up. */
47 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
48 : : (int) nodeTag(areq->requestee));
49 : : }
50 : :
51 : 7876 : ExecAsyncResponse(areq);
52 : :
53 : : /* must provide our own instrumentation support */
54 [ + + ]: 7876 : if (areq->requestee->instrument)
55 : 810 : InstrStopNode(areq->requestee->instrument,
56 [ + + - + ]: 810 : TupIsNull(areq->result) ? 0.0 : 1.0);
57 : 7876 : }
58 : :
59 : : /*
60 : : * Give the asynchronous node a chance to configure the file descriptor event
61 : : * for which it wishes to wait. We expect the node-type specific callback to
62 : : * make a single call of the following form:
63 : : *
64 : : * AddWaitEventToSet(set, WL_SOCKET_READABLE, fd, NULL, areq);
65 : : */
66 : : void
67 : 325 : ExecAsyncConfigureWait(AsyncRequest *areq)
68 : : {
69 : : /* must provide our own instrumentation support */
70 [ + + ]: 325 : if (areq->requestee->instrument)
71 : 20 : InstrStartNode(areq->requestee->instrument);
72 : :
73 [ + - ]: 325 : switch (nodeTag(areq->requestee))
74 : : {
75 : 325 : case T_ForeignScanState:
76 : 325 : ExecAsyncForeignScanConfigureWait(areq);
77 : 323 : break;
78 : 0 : default:
79 : : /* If the node doesn't support async, caller messed up. */
80 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
81 : : (int) nodeTag(areq->requestee));
82 : : }
83 : :
84 : : /* must provide our own instrumentation support */
85 [ + + ]: 323 : if (areq->requestee->instrument)
86 : 20 : InstrStopNode(areq->requestee->instrument, 0.0);
87 : 323 : }
88 : :
89 : : /*
90 : : * Call the asynchronous node back when a relevant event has occurred.
91 : : */
92 : : void
93 : 219 : ExecAsyncNotify(AsyncRequest *areq)
94 : : {
95 : : /* must provide our own instrumentation support */
96 [ + + ]: 219 : if (areq->requestee->instrument)
97 : 14 : InstrStartNode(areq->requestee->instrument);
98 : :
99 [ + - ]: 219 : switch (nodeTag(areq->requestee))
100 : : {
101 : 219 : case T_ForeignScanState:
102 : 219 : ExecAsyncForeignScanNotify(areq);
103 : 219 : break;
104 : 0 : default:
105 : : /* If the node doesn't support async, caller messed up. */
106 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
107 : : (int) nodeTag(areq->requestee));
108 : : }
109 : :
110 : 219 : ExecAsyncResponse(areq);
111 : :
112 : : /* must provide our own instrumentation support */
113 [ + + ]: 219 : if (areq->requestee->instrument)
114 : 14 : InstrStopNode(areq->requestee->instrument,
115 [ + + - + ]: 14 : TupIsNull(areq->result) ? 0.0 : 1.0);
116 : 219 : }
117 : :
118 : : /*
119 : : * Call the requestor back when an asynchronous node has produced a result.
120 : : */
121 : : void
122 : 8100 : ExecAsyncResponse(AsyncRequest *areq)
123 : : {
124 [ + + - ]: 8100 : switch (nodeTag(areq->requestor))
125 : : {
126 : 6345 : case T_AppendState:
127 : 6345 : ExecAsyncAppendResponse(areq);
128 : 6345 : break;
129 : 1755 : case T_MergeAppendState:
130 : 1755 : ExecAsyncMergeAppendResponse(areq);
131 : 1755 : break;
132 : 0 : default:
133 : : /* If the node doesn't support async, caller messed up. */
134 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
135 : : (int) nodeTag(areq->requestor));
136 : : }
137 : 8100 : }
138 : :
139 : : /*
140 : : * A requestee node should call this function to deliver the tuple to its
141 : : * requestor node. The requestee node can call this from its ExecAsyncRequest
142 : : * or ExecAsyncNotify callback.
143 : : */
144 : : void
145 : 7869 : ExecAsyncRequestDone(AsyncRequest *areq, TupleTableSlot *result)
146 : : {
147 : 7869 : areq->request_complete = true;
148 : 7869 : areq->result = result;
149 : 7869 : }
150 : :
151 : : /*
152 : : * A requestee node should call this function to indicate that it is pending
153 : : * for a callback. The requestee node can call this from its ExecAsyncRequest
154 : : * or ExecAsyncNotify callback.
155 : : */
156 : : void
157 : 231 : ExecAsyncRequestPending(AsyncRequest *areq)
158 : : {
159 : 231 : areq->callback_pending = true;
160 : 231 : areq->request_complete = false;
161 : 231 : areq->result = NULL;
162 : 231 : }
163 : :
164 : : /*
165 : : * Fetch the WaitEventSet associated with an async-aware plan.
166 : : */
167 : : WaitEventSet *
168 : 325 : ExecAsyncGetRequestorEventSet(PlanState *requestor)
169 : : {
170 [ + + - ]: 325 : switch (nodeTag(requestor))
171 : : {
172 : 240 : case T_AppendState:
173 : 240 : return ((AppendState *) requestor)->as_eventset;
174 : 85 : case T_MergeAppendState:
175 : 85 : return ((MergeAppendState *) requestor)->ms_eventset;
176 : 0 : default:
177 : : /* If the node doesn't support async, caller messed up. */
178 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
179 : : (int) nodeTag(requestor));
180 : : return NULL;
181 : : }
182 : : }
183 : :
184 : : /*
185 : : * Workhorse function for Exec{Merge}AppendAsyncEventWait.
186 : : */
187 : : void
188 : 201 : ExecAsyncEventWait(Bitmapset *asyncplans,
189 : : AsyncRequest **asyncrequests,
190 : : WaitEventSet *eventset,
191 : : int nevents,
192 : : long timeout,
193 : : uint32 wait_event_info)
194 : : {
195 : : WaitEvent occurred_event[EVENT_BUFFER_SIZE];
196 : : int noccurred;
197 : : int i;
198 : :
199 : : /*
200 : : * Add WL_EXIT_ON_PM_DEATH to the set, to exit immediately if the
201 : : * postmaster dies.
202 : : */
203 : 201 : AddWaitEventToSet(eventset, WL_EXIT_ON_PM_DEATH, PGINVALID_SOCKET,
204 : : NULL, NULL);
205 : :
206 : : /* Give each waiting subplan a chance to add an event. */
207 : 201 : i = -1;
208 [ + + ]: 631 : while ((i = bms_next_member(asyncplans, i)) >= 0)
209 : : {
210 : 432 : AsyncRequest *areq = asyncrequests[i];
211 : :
212 [ + + ]: 432 : if (areq->callback_pending)
213 : 325 : ExecAsyncConfigureWait(areq);
214 : : }
215 : :
216 : : /*
217 : : * No need for further processing if none of the subplans configured any
218 : : * events.
219 : : */
220 [ + + ]: 199 : if (GetNumRegisteredWaitEvents(eventset) == 1)
221 : 3 : return;
222 : :
223 : : /*
224 : : * Add the process latch to the set, so that we wake up to process the
225 : : * standard interrupts with CHECK_FOR_INTERRUPTS().
226 : : *
227 : : * NOTE: For historical reasons, it's important that this is added to the
228 : : * WaitEventSet after the ExecAsyncConfigureWait() calls. Namely,
229 : : * postgres_fdw calls "GetNumRegisteredWaitEvents(set) == 1" to check if
230 : : * any other events are in the set. That's a poor design, it's
231 : : * questionable for postgres_fdw to be doing that in the first place, but
232 : : * we cannot change it now. The pattern has possibly been copied to other
233 : : * extensions too.
234 : : */
235 : 198 : AddWaitEventToSet(eventset, WL_LATCH_SET, PGINVALID_SOCKET,
236 : : MyLatch, NULL);
237 : :
238 : : /* Return at most EVENT_BUFFER_SIZE events in one call. */
239 [ - + ]: 198 : if (nevents > EVENT_BUFFER_SIZE)
240 : 0 : nevents = EVENT_BUFFER_SIZE;
241 : :
242 : : /* Wait or poll for file descriptor events. */
243 : 198 : noccurred = WaitEventSetWait(eventset, timeout, occurred_event, nevents,
244 : : wait_event_info);
245 [ + + ]: 198 : if (noccurred == 0)
246 : 2 : return;
247 : :
248 : : /* Deliver notifications. */
249 [ + + ]: 415 : for (i = 0; i < noccurred; i++)
250 : : {
251 : 219 : WaitEvent *w = &occurred_event[i];
252 : :
253 : : /*
254 : : * Each waiting subplan should have registered its wait event with
255 : : * user_data pointing back to its AsyncRequest.
256 : : */
257 [ + - ]: 219 : if ((w->events & WL_SOCKET_READABLE) != 0)
258 : : {
259 : 219 : AsyncRequest *areq = (AsyncRequest *) w->user_data;
260 : :
261 [ + - ]: 219 : if (areq->callback_pending)
262 : : {
263 : : /*
264 : : * Mark it as no longer needing a callback. We must do this
265 : : * before dispatching the callback in case the callback resets
266 : : * the flag.
267 : : */
268 : 219 : areq->callback_pending = false;
269 : :
270 : : /* Do the actual work. */
271 : 219 : ExecAsyncNotify(areq);
272 : : }
273 : : }
274 : :
275 : : /* Handle standard interrupts */
276 [ - + ]: 219 : if ((w->events & WL_LATCH_SET) != 0)
277 : : {
278 : 0 : ResetLatch(MyLatch);
279 [ # # ]: 0 : CHECK_FOR_INTERRUPTS();
280 : : }
281 : : }
282 : : }
|