LCOV - differential code coverage report
Current view: top level - src/backend/executor - execAsync.c (source / functions) Coverage Total Hit UNC UIC UBC GBC GNC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 86.3 % 95 82 5 8 31 51
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 8 8 3 5
Baseline: lcov-20261128-baseline Branches: 64.1 % 64 41 9 1 13 1 16 24
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 86.1 % 36 31 5 31
(360..) days: 86.4 % 59 51 8 51
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(360..) days: 100.0 % 6 6 1 5
Branch coverage date bins:
(7,30] days: 64.0 % 25 16 9 16
(360..) days: 64.1 % 39 25 1 13 1 24

 Age         Owner                    Branch data    TLA  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
 2068 efujita@postgresql.o       31                 :CBC        7876 : ExecAsyncRequest(AsyncRequest *areq)
                                 32                 :                : {
 2026                            33         [ +  + ]:           7876 :     if (areq->requestee->chgParam != NULL)    /* something changed? */
      tgl@sss.pgh.pa.us          34                 :             20 :         ExecReScan(areq->requestee); /* let ReScan handle this */
                                 35                 :                : 
                                 36                 :                :     /* must provide our own instrumentation support */
      efujita@postgresql.o       37         [ +  + ]:           7876 :     if (areq->requestee->instrument)
                                 38                 :            810 :         InstrStartNode(areq->requestee->instrument);
                                 39                 :                : 
 2068                            40         [ +  - ]:           7876 :     switch (nodeTag(areq->requestee))
                                 41                 :                :     {
                                 42                 :           7876 :         case T_ForeignScanState:
                                 43                 :           7876 :             ExecAsyncForeignScanRequest(areq);
                                 44                 :           7876 :             break;
 2068 efujita@postgresql.o       45                 :UBC           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                 :                : 
 2068 efujita@postgresql.o       51                 :CBC        7876 :     ExecAsyncResponse(areq);
                                 52                 :                : 
                                 53                 :                :     /* must provide our own instrumentation support */
 2026                            54         [ +  + ]:           7876 :     if (areq->requestee->instrument)
                                 55                 :            810 :         InstrStopNode(areq->requestee->instrument,
                                 56   [ +  +  -  + ]:            810 :                       TupIsNull(areq->result) ? 0.0 : 1.0);
 2068                            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                 :            302 : ExecAsyncConfigureWait(AsyncRequest *areq)
                                 68                 :                : {
                                 69                 :                :     /* must provide our own instrumentation support */
 2026                            70         [ +  + ]:            302 :     if (areq->requestee->instrument)
                                 71                 :             19 :         InstrStartNode(areq->requestee->instrument);
                                 72                 :                : 
 2068                            73         [ +  - ]:            302 :     switch (nodeTag(areq->requestee))
                                 74                 :                :     {
                                 75                 :            302 :         case T_ForeignScanState:
                                 76                 :            302 :             ExecAsyncForeignScanConfigureWait(areq);
                                 77                 :            300 :             break;
 2068 efujita@postgresql.o       78                 :UBC           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 */
 2026 efujita@postgresql.o       85         [ +  + ]:CBC         300 :     if (areq->requestee->instrument)
                                 86                 :             19 :         InstrStopNode(areq->requestee->instrument, 0.0);
 2068                            87                 :            300 : }
                                 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 */
 2026                            96         [ +  + ]:            219 :     if (areq->requestee->instrument)
                                 97                 :             14 :         InstrStartNode(areq->requestee->instrument);
                                 98                 :                : 
 2068                            99         [ +  - ]:            219 :     switch (nodeTag(areq->requestee))
                                100                 :                :     {
                                101                 :            219 :         case T_ForeignScanState:
                                102                 :            219 :             ExecAsyncForeignScanNotify(areq);
                                103                 :            219 :             break;
 2068 efujita@postgresql.o      104                 :UBC           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                 :                : 
 2068 efujita@postgresql.o      110                 :CBC         219 :     ExecAsyncResponse(areq);
                                111                 :                : 
                                112                 :                :     /* must provide our own instrumentation support */
 2026                           113         [ +  + ]:            219 :     if (areq->requestee->instrument)
                                114                 :             14 :         InstrStopNode(areq->requestee->instrument,
                                115   [ +  +  -  + ]:             14 :                       TupIsNull(areq->result) ? 0.0 : 1.0);
 2068                           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;
   11 efujita@postgresql.o      129                 :GNC        1755 :         case T_MergeAppendState:
                                130                 :           1755 :             ExecAsyncMergeAppendResponse(areq);
                                131                 :           1755 :             break;
 2068 efujita@postgresql.o      132                 :UBC           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                 :                :     }
 2068 efujita@postgresql.o      137                 :CBC        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 *
   11 efujita@postgresql.o      168                 :GNC         302 : ExecAsyncGetRequestorEventSet(PlanState *requestor)
                                169                 :                : {
                                170      [ +  +  - ]:            302 :     switch (nodeTag(requestor))
                                171                 :                :     {
                                172                 :            220 :         case T_AppendState:
                                173                 :            220 :             return ((AppendState *) requestor)->as_eventset;
                                174                 :             82 :         case T_MergeAppendState:
                                175                 :             82 :             return ((MergeAppendState *) requestor)->ms_eventset;
   11 efujita@postgresql.o      176                 :UNC           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
   11 efujita@postgresql.o      188                 :GNC         189 : 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                 :            189 :     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                 :            189 :     i = -1;
                                208         [ +  + ]:            589 :     while ((i = bms_next_member(asyncplans, i)) >= 0)
                                209                 :                :     {
                                210                 :            402 :         AsyncRequest *areq = asyncrequests[i];
                                211                 :                : 
                                212         [ +  + ]:            402 :         if (areq->callback_pending)
                                213                 :            302 :             ExecAsyncConfigureWait(areq);
                                214                 :                :     }
                                215                 :                : 
                                216                 :                :     /*
                                217                 :                :      * No need for further processing if none of the subplans configured any
                                218                 :                :      * events.
                                219                 :                :      */
                                220         [ +  + ]:            187 :     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                 :            186 :     AddWaitEventToSet(eventset, WL_LATCH_SET, PGINVALID_SOCKET,
                                236                 :                :                       MyLatch, NULL);
                                237                 :                : 
                                238                 :                :     /* Return at most EVENT_BUFFER_SIZE events in one call. */
                                239         [ -  + ]:            186 :     if (nevents > EVENT_BUFFER_SIZE)
   11 efujita@postgresql.o      240                 :UNC           0 :         nevents = EVENT_BUFFER_SIZE;
                                241                 :                : 
                                242                 :                :     /* Wait or poll for file descriptor events. */
   11 efujita@postgresql.o      243                 :GNC         186 :     noccurred = WaitEventSetWait(eventset, timeout, occurred_event, nevents,
                                244                 :                :                                  wait_event_info);
                                245         [ +  + ]:            186 :     if (noccurred == 0)
                                246                 :              2 :         return;
                                247                 :                : 
                                248                 :                :     /* Deliver notifications. */
                                249         [ +  + ]:            403 :     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                 :                :         {
   11 efujita@postgresql.o      278                 :UNC           0 :             ResetLatch(MyLatch);
                                279         [ #  # ]:              0 :             CHECK_FOR_INTERRUPTS();
                                280                 :                :         }
                                281                 :                :     }
                                282                 :                : }
        

Generated by: LCOV version 2.0-1