LCOV - differential code coverage report
Current view: top level - src/backend/executor - nodeSort.c (source / functions) Coverage Total Hit UBC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 97.7 % 131 128 3 128
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 10 10 10
Baseline: lcov-20261128-baseline Branches: 87.9 % 66 58 8 58
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: 100.0 % 1 1 1
(30,360] days: 100.0 % 1 1 1
(360..) days: 97.7 % 129 126 3 126
Function coverage date bins:
(360..) days: 100.0 % 10 10 10
Branch coverage date bins:
(7,30] days: 100.0 % 4 4 4
(30,360] days: 50.0 % 2 1 1 1
(360..) days: 88.3 % 60 53 7 53

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * nodeSort.c
                                  4                 :                :  *    Routines to handle sorting of relations.
                                  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/executor/nodeSort.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : 
                                 16                 :                : #include "postgres.h"
                                 17                 :                : 
                                 18                 :                : #include "access/parallel.h"
                                 19                 :                : #include "executor/executor.h"
                                 20                 :                : #include "executor/nodeSort.h"
                                 21                 :                : #include "miscadmin.h"
                                 22                 :                : #include "utils/tuplesort.h"
                                 23                 :                : 
                                 24                 :                : 
                                 25                 :                : /* ----------------------------------------------------------------
                                 26                 :                :  *      ExecSort
                                 27                 :                :  *
                                 28                 :                :  *      Sorts tuples from the outer subtree of the node using tuplesort,
                                 29                 :                :  *      which saves the results in a temporary file or memory. After the
                                 30                 :                :  *      initial call, returns a tuple from the file with each call.
                                 31                 :                :  *
                                 32                 :                :  *      There are two distinct ways that this sort can be performed:
                                 33                 :                :  *
                                 34                 :                :  *      1) When the result is a single column we perform a Datum sort.
                                 35                 :                :  *
                                 36                 :                :  *      2) When the result contains multiple columns we perform a tuple sort.
                                 37                 :                :  *
                                 38                 :                :  *      We could do this by always performing a tuple sort, however sorting
                                 39                 :                :  *      Datums only can be significantly faster than sorting tuples,
                                 40                 :                :  *      especially when the Datums are of a pass-by-value type.
                                 41                 :                :  *
                                 42                 :                :  *      Conditions:
                                 43                 :                :  *        -- none.
                                 44                 :                :  *
                                 45                 :                :  *      Initial States:
                                 46                 :                :  *        -- the outer child is prepared to return the first tuple.
                                 47                 :                :  * ----------------------------------------------------------------
                                 48                 :                :  */
                                 49                 :                : static TupleTableSlot *
 3421 andres@anarazel.de         50                 :CBC     7732234 : ExecSort(PlanState *pstate)
                                 51                 :                : {
                                 52                 :        7732234 :     SortState  *node = castNode(SortState, pstate);
                                 53                 :                :     EState     *estate;
                                 54                 :                :     ScanDirection dir;
                                 55                 :                :     Tuplesortstate *tuplesortstate;
                                 56                 :                :     TupleTableSlot *slot;
                                 57                 :                : 
 3413                            58         [ +  + ]:        7732234 :     CHECK_FOR_INTERRUPTS();
                                 59                 :                : 
                                 60                 :                :     /*
                                 61                 :                :      * get state info from node
                                 62                 :                :      */
 8759 tgl@sss.pgh.pa.us          63                 :        7732234 :     estate = node->ss.ps.state;
10674 bruce@momjian.us           64                 :        7732234 :     dir = estate->es_direction;
 8759 tgl@sss.pgh.pa.us          65                 :        7732234 :     tuplesortstate = (Tuplesortstate *) node->tuplesortstate;
                                 66                 :                : 
                                 67                 :                :     /*
                                 68                 :                :      * If first time through, read all tuples from outer plan and pass them to
                                 69                 :                :      * tuplesort.c. Subsequent calls just fetch tuples from tuplesort.
                                 70                 :                :      */
                                 71                 :                : 
                                 72         [ +  + ]:        7732234 :     if (!node->sort_Done)
                                 73                 :                :     {
                                 74                 :          76115 :         Sort       *plannode = (Sort *) node->ss.ps.plan;
                                 75                 :                :         PlanState  *outerNode;
                                 76                 :                :         TupleDesc   tupDesc;
 1699 drowley@postgresql.o       77                 :          76115 :         int         tuplesortopts = TUPLESORT_NONE;
                                 78                 :                : 
                                 79                 :                :         /*
                                 80                 :                :          * Want to scan subplan in the forward direction while creating the
                                 81                 :                :          * sorted data.
                                 82                 :                :          */
10674 bruce@momjian.us           83                 :          76115 :         estate->es_direction = ForwardScanDirection;
                                 84                 :                : 
                                 85                 :                :         /*
                                 86                 :                :          * Initialize tuplesort module.
                                 87                 :                :          */
 8759 tgl@sss.pgh.pa.us          88                 :          76115 :         outerNode = outerPlanState(node);
 8608                            89                 :          76115 :         tupDesc = ExecGetResultType(outerNode);
                                 90                 :                : 
 1699 drowley@postgresql.o       91         [ +  + ]:          76115 :         if (node->randomAccess)
                                 92                 :           3969 :             tuplesortopts |= TUPLESORT_RANDOMACCESS;
                                 93         [ +  + ]:          76115 :         if (node->bounded)
                                 94                 :            641 :             tuplesortopts |= TUPLESORT_ALLOWBOUNDED;
                                 95                 :                : 
 1955                            96         [ +  + ]:          76115 :         if (node->datumSort)
                                 97                 :           5896 :             tuplesortstate = tuplesort_begin_datum(TupleDescAttr(tupDesc, 0)->atttypid,
                                 98                 :           5896 :                                                    plannode->sortOperators[0],
                                 99                 :           5896 :                                                    plannode->collations[0],
                                100                 :           5896 :                                                    plannode->nullsFirst[0],
                                101                 :                :                                                    work_mem,
                                102                 :                :                                                    NULL,
                                103                 :                :                                                    tuplesortopts);
                                104                 :                :         else
                                105                 :          70219 :             tuplesortstate = tuplesort_begin_heap(tupDesc,
                                106                 :                :                                                   plannode->numCols,
                                107                 :                :                                                   plannode->sortColIdx,
                                108                 :                :                                                   plannode->sortOperators,
                                109                 :                :                                                   plannode->collations,
                                110                 :                :                                                   plannode->nullsFirst,
                                111                 :                :                                                   work_mem,
                                112                 :                :                                                   NULL,
                                113                 :                :                                                   tuplesortopts);
 7148 tgl@sss.pgh.pa.us         114         [ +  + ]:          76107 :         if (node->bounded)
                                115                 :            641 :             tuplesort_set_bound(tuplesortstate, node->bound);
  730 peter@eisentraut.org      116                 :          76107 :         node->tuplesortstate = tuplesortstate;
                                117                 :                : 
                                118                 :                :         /*
                                119                 :                :          * Scan the subplan and feed all the tuples to tuplesort using the
                                120                 :                :          * appropriate method based on the type of sort we're doing.
                                121                 :                :          */
 1955 drowley@postgresql.o      122         [ +  + ]:          76107 :         if (node->datumSort)
                                123                 :                :         {
                                124                 :                :             for (;;)
                                125                 :                :             {
                                126                 :        1038178 :                 slot = ExecProcNode(outerNode);
                                127                 :                : 
                                128   [ +  +  +  + ]:        1038168 :                 if (TupIsNull(slot))
                                129                 :                :                     break;
                                130                 :        1032282 :                 slot_getsomeattrs(slot, 1);
                                131                 :        1032282 :                 tuplesort_putdatum(tuplesortstate,
                                132                 :        1032282 :                                    slot->tts_values[0],
                                133                 :        1032282 :                                    slot->tts_isnull[0]);
                                134                 :                :             }
                                135                 :                :         }
                                136                 :                :         else
                                137                 :                :         {
                                138                 :                :             for (;;)
                                139                 :                :             {
                                140                 :        7435628 :                 slot = ExecProcNode(outerNode);
                                141                 :                : 
                                142   [ +  +  +  + ]:        7435627 :                 if (TupIsNull(slot))
                                143                 :                :                     break;
                                144                 :        7365417 :                 tuplesort_puttupleslot(tuplesortstate, slot);
                                145                 :                :             }
                                146                 :                :         }
                                147                 :                : 
                                148                 :                :         /*
                                149                 :                :          * Complete the sort.
                                150                 :                :          */
 9904 tgl@sss.pgh.pa.us         151                 :          76096 :         tuplesort_performsort(tuplesortstate);
                                152                 :                : 
                                153                 :                :         /*
                                154                 :                :          * restore to user specified direction
                                155                 :                :          */
10674 bruce@momjian.us          156                 :          76096 :         estate->es_direction = dir;
                                157                 :                : 
                                158                 :                :         /*
                                159                 :                :          * finally set the sorted flag to true
                                160                 :                :          */
 8759 tgl@sss.pgh.pa.us         161                 :          76096 :         node->sort_Done = true;
 7148                           162                 :          76096 :         node->bounded_Done = node->bounded;
                                163                 :          76096 :         node->bound_Done = node->bound;
 3378 rhaas@postgresql.org      164   [ +  +  +  + ]:          76096 :         if (node->shared_info && node->am_worker)
                                165                 :                :         {
                                166                 :                :             TuplesortInstrumentation *si;
                                167                 :                : 
                                168         [ -  + ]:             64 :             Assert(IsParallelWorker());
  259 tomas.vondra@postgre      169         [ -  + ]:             64 :             Assert(ParallelWorkerNumber < node->shared_info->num_workers);
 3378 rhaas@postgresql.org      170                 :             64 :             si = &node->shared_info->sinstrument[ParallelWorkerNumber];
                                171                 :             64 :             tuplesort_get_stats(tuplesortstate, si);
                                172                 :                :         }
                                173                 :                :     }
                                174                 :                : 
 1955 drowley@postgresql.o      175                 :        7732215 :     slot = node->ss.ps.ps_ResultTupleSlot;
                                176                 :                : 
                                177                 :                :     /*
                                178                 :                :      * Fetch the next sorted item from the appropriate tuplesort function. For
                                179                 :                :      * datum sorts we must manage the slot ourselves and leave it clear when
                                180                 :                :      * tuplesort_getdatum returns false to indicate there are no more datums.
                                181                 :                :      * For tuple sorts, tuplesort_gettupleslot manages the slot for us and
                                182                 :                :      * empties the slot when it runs out of tuples.
                                183                 :                :      */
                                184         [ +  + ]:        7732215 :     if (node->datumSort)
                                185                 :                :     {
                                186                 :         841666 :         ExecClearTuple(slot);
                                187         [ +  + ]:         841666 :         if (tuplesort_getdatum(tuplesortstate, ScanDirectionIsForward(dir),
                                188                 :                :                                false, &(slot->tts_values[0]),
                                189                 :                :                                &(slot->tts_isnull[0]), NULL))
                                190                 :         836020 :             ExecStoreVirtualTuple(slot);
                                191                 :                :     }
                                192                 :                :     else
                                193                 :        6890549 :         (void) tuplesort_gettupleslot(tuplesortstate,
                                194                 :                :                                       ScanDirectionIsForward(dir),
                                195                 :                :                                       false, slot, NULL);
                                196                 :                : 
 7459 tgl@sss.pgh.pa.us         197                 :        7732215 :     return slot;
                                198                 :                : }
                                199                 :                : 
                                200                 :                : /* ----------------------------------------------------------------
                                201                 :                :  *      ExecInitSort
                                202                 :                :  *
                                203                 :                :  *      Creates the run-time state information for the sort node
                                204                 :                :  *      produced by the planner and initializes its outer subtree.
                                205                 :                :  * ----------------------------------------------------------------
                                206                 :                :  */
                                207                 :                : SortState *
 7578                           208                 :          54187 : ExecInitSort(Sort *node, EState *estate, int eflags)
                                209                 :                : {
                                210                 :                :     SortState  *sortstate;
                                211                 :                :     TupleDesc   outerTupDesc;
                                212                 :                : 
                                213                 :                :     /*
                                214                 :                :      * create state structure
                                215                 :                :      */
10674 bruce@momjian.us          216                 :          54187 :     sortstate = makeNode(SortState);
 8759 tgl@sss.pgh.pa.us         217                 :          54187 :     sortstate->ss.ps.plan = (Plan *) node;
                                218                 :          54187 :     sortstate->ss.ps.state = estate;
 3421 andres@anarazel.de        219                 :          54187 :     sortstate->ss.ps.ExecProcNode = ExecSort;
                                220                 :                : 
                                221                 :                :     /*
                                222                 :                :      * We must have random access to the sort output to do backward scan or
                                223                 :                :      * mark/restore.  We also prefer to materialize the sort output if we
                                224                 :                :      * might be called on to rewind and replay it many times.
                                225                 :                :      */
 7578 tgl@sss.pgh.pa.us         226                 :          54187 :     sortstate->randomAccess = (eflags & (EXEC_FLAG_REWIND |
                                227                 :                :                                          EXEC_FLAG_BACKWARD |
                                228                 :          54187 :                                          EXEC_FLAG_MARK)) != 0;
                                229                 :                : 
 7148                           230                 :          54187 :     sortstate->bounded = false;
 9904                           231                 :          54187 :     sortstate->sort_Done = false;
                                232                 :          54187 :     sortstate->tuplesortstate = NULL;
                                233                 :                : 
                                234                 :                :     /*
                                235                 :                :      * Miscellaneous initialization
                                236                 :                :      *
                                237                 :                :      * Sort nodes don't initialize their ExprContexts because they never call
                                238                 :                :      * ExecQual or ExecProject.
                                239                 :                :      */
                                240                 :                : 
                                241                 :                :     /*
                                242                 :                :      * initialize child nodes
                                243                 :                :      *
                                244                 :                :      * We shield the child node from the need to support REWIND, BACKWARD, or
                                245                 :                :      * MARK/RESTORE.
                                246                 :                :      */
 7578                           247                 :          54187 :     eflags &= ~(EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK);
                                248                 :                : 
                                249                 :          54187 :     outerPlanState(sortstate) = ExecInitNode(outerPlan(node), estate, eflags);
                                250                 :                : 
                                251                 :                :     /*
                                252                 :                :      * Initialize scan slot and type.
                                253                 :                :      */
 2935 andres@anarazel.de        254                 :          54183 :     ExecCreateScanSlotFromOuterPlan(estate, &sortstate->ss, &TTSOpsVirtual);
                                255                 :                : 
                                256                 :                :     /*
                                257                 :                :      * Initialize return slot and type. No need to initialize projection info
                                258                 :                :      * because this node doesn't do projections.
                                259                 :                :      */
                                260                 :          54183 :     ExecInitResultTupleSlotTL(&sortstate->ss.ps, &TTSOpsMinimalTuple);
 8759 tgl@sss.pgh.pa.us         261                 :          54183 :     sortstate->ss.ps.ps_ProjInfo = NULL;
                                262                 :                : 
 1521 drowley@postgresql.o      263                 :          54183 :     outerTupDesc = ExecGetResultType(outerPlanState(sortstate));
                                264                 :                : 
                                265                 :                :     /*
                                266                 :                :      * We perform a Datum sort when we're sorting just a single column by a
                                267                 :                :      * single key, otherwise we perform a tuple sort.  You might think that
                                268                 :                :      * only 1 sort key would be relevant if there's only 1 output column since
                                269                 :                :      * all keys must be on that column, but the sort keys may differ in
                                270                 :                :      * collation, and if the first key's collation is nondeterministic then
                                271                 :                :      * ignoring subsequent keys could lead to an incorrect sort order.
                                272                 :                :      */
   23                           273   [ +  +  +  + ]:          54183 :     if (outerTupDesc->natts == 1 && node->numCols == 1)
 1955                           274                 :           8373 :         sortstate->datumSort = true;
                                275                 :                :     else
                                276                 :          45810 :         sortstate->datumSort = false;
                                277                 :                : 
 8759 tgl@sss.pgh.pa.us         278                 :          54183 :     return sortstate;
                                279                 :                : }
                                280                 :                : 
                                281                 :                : /* ----------------------------------------------------------------
                                282                 :                :  *      ExecEndSort(node)
                                283                 :                :  * ----------------------------------------------------------------
                                284                 :                :  */
                                285                 :                : void
                                286                 :          54065 : ExecEndSort(SortState *node)
                                287                 :                : {
                                288                 :                :     /*
                                289                 :                :      * Release tuplesort resources
                                290                 :                :      */
                                291         [ +  + ]:          54065 :     if (node->tuplesortstate != NULL)
                                292                 :          47643 :         tuplesort_end((Tuplesortstate *) node->tuplesortstate);
                                293                 :          54065 :     node->tuplesortstate = NULL;
                                294                 :                : 
                                295                 :                :     /*
                                296                 :                :      * shut down the subplan
                                297                 :                :      */
 8749                           298                 :          54065 :     ExecEndNode(outerPlanState(node));
10674 bruce@momjian.us          299                 :          54065 : }
                                300                 :                : 
                                301                 :                : /* ----------------------------------------------------------------
                                302                 :                :  *      ExecSortMarkPos
                                303                 :                :  *
                                304                 :                :  *      Calls tuplesort to save the current position in the sorted file.
                                305                 :                :  * ----------------------------------------------------------------
                                306                 :                :  */
                                307                 :                : void
 8759 tgl@sss.pgh.pa.us         308                 :         356480 : ExecSortMarkPos(SortState *node)
                                309                 :                : {
                                310                 :                :     /*
                                311                 :                :      * if we haven't sorted yet, just return
                                312                 :                :      */
                                313         [ -  + ]:         356480 :     if (!node->sort_Done)
10674 bruce@momjian.us          314                 :UBC           0 :         return;
                                315                 :                : 
 8759 tgl@sss.pgh.pa.us         316                 :CBC      356480 :     tuplesort_markpos((Tuplesortstate *) node->tuplesortstate);
                                317                 :                : }
                                318                 :                : 
                                319                 :                : /* ----------------------------------------------------------------
                                320                 :                :  *      ExecSortRestrPos
                                321                 :                :  *
                                322                 :                :  *      Calls tuplesort to restore the last saved sort file position.
                                323                 :                :  * ----------------------------------------------------------------
                                324                 :                :  */
                                325                 :                : void
                                326                 :          28324 : ExecSortRestrPos(SortState *node)
                                327                 :                : {
                                328                 :                :     /*
                                329                 :                :      * if we haven't sorted yet, just return.
                                330                 :                :      */
                                331         [ -  + ]:          28324 :     if (!node->sort_Done)
10674 bruce@momjian.us          332                 :UBC           0 :         return;
                                333                 :                : 
                                334                 :                :     /*
                                335                 :                :      * restore the scan to the previously marked position
                                336                 :                :      */
 8759 tgl@sss.pgh.pa.us         337                 :CBC       28324 :     tuplesort_restorepos((Tuplesortstate *) node->tuplesortstate);
                                338                 :                : }
                                339                 :                : 
                                340                 :                : void
 5983                           341                 :          29005 : ExecReScanSort(SortState *node)
                                342                 :                : {
 4207 bruce@momjian.us          343                 :          29005 :     PlanState  *outerPlan = outerPlanState(node);
                                344                 :                : 
                                345                 :                :     /*
                                346                 :                :      * If we haven't sorted yet, just return. If outerplan's chgParam is not
                                347                 :                :      * NULL then it will be re-scanned by ExecProcNode, else no reason to
                                348                 :                :      * re-scan it at all.
                                349                 :                :      */
 8759 tgl@sss.pgh.pa.us         350         [ +  + ]:          29005 :     if (!node->sort_Done)
10505 vadim4o@yahoo.com         351                 :            629 :         return;
                                352                 :                : 
                                353                 :                :     /* must drop pointer to sort result tuple */
 8759 tgl@sss.pgh.pa.us         354                 :          28376 :     ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
                                355                 :                : 
                                356                 :                :     /*
                                357                 :                :      * If subnode is to be rescanned then we forget previous sort results; we
                                358                 :                :      * have to re-read the subplan and re-sort.  Also must re-sort if the
                                359                 :                :      * bounded-sort parameters changed or we didn't select randomAccess.
                                360                 :                :      *
                                361                 :                :      * Otherwise we can just rewind and rescan the sorted output.
                                362                 :                :      */
 4226 rhaas@postgresql.org      363         [ +  + ]:          28376 :     if (outerPlan->chgParam != NULL ||
 7148 tgl@sss.pgh.pa.us         364         [ +  - ]:            548 :         node->bounded != node->bounded_Done ||
                                365         [ +  + ]:            548 :         node->bound != node->bound_Done ||
 7578                           366         [ +  + ]:            512 :         !node->randomAccess)
                                367                 :                :     {
 8759                           368                 :          28354 :         node->sort_Done = false;
                                369                 :          28354 :         tuplesort_end((Tuplesortstate *) node->tuplesortstate);
                                370                 :          28354 :         node->tuplesortstate = NULL;
                                371                 :                : 
                                372                 :                :         /*
                                373                 :                :          * if chgParam of subnode is not null then plan will be re-scanned by
                                374                 :                :          * first ExecProcNode.
                                375                 :                :          */
 4226 rhaas@postgresql.org      376         [ +  + ]:          28354 :         if (outerPlan->chgParam == NULL)
                                377                 :            526 :             ExecReScan(outerPlan);
                                378                 :                :     }
                                379                 :                :     else
 8759 tgl@sss.pgh.pa.us         380                 :             22 :         tuplesort_rescan((Tuplesortstate *) node->tuplesortstate);
                                381                 :                : }
                                382                 :                : 
                                383                 :                : /* ----------------------------------------------------------------
                                384                 :                :  *                      Parallel Query Support
                                385                 :                :  * ----------------------------------------------------------------
                                386                 :                :  */
                                387                 :                : 
                                388                 :                : /* ----------------------------------------------------------------
                                389                 :                :  *      ExecSortEstimate
                                390                 :                :  *
                                391                 :                :  *      Estimate space required to propagate sort statistics.
                                392                 :                :  * ----------------------------------------------------------------
                                393                 :                :  */
                                394                 :                : void
 3378 rhaas@postgresql.org      395                 :            110 : ExecSortEstimate(SortState *node, ParallelContext *pcxt)
                                396                 :                : {
                                397                 :                :     Size        size;
                                398                 :                : 
                                399                 :                :     /* don't need this if not instrumenting or no workers */
                                400   [ +  +  -  + ]:            110 :     if (!node->ss.ps.instrument || pcxt->nworkers == 0)
                                401                 :            102 :         return;
                                402                 :                : 
                                403                 :              8 :     size = mul_size(pcxt->nworkers, sizeof(TuplesortInstrumentation));
                                404                 :              8 :     size = add_size(size, offsetof(SharedSortInfo, sinstrument));
                                405                 :              8 :     shm_toc_estimate_chunk(&pcxt->estimator, size);
                                406                 :              8 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
                                407                 :                : }
                                408                 :                : 
                                409                 :                : /* ----------------------------------------------------------------
                                410                 :                :  *      ExecSortInitializeDSM
                                411                 :                :  *
                                412                 :                :  *      Initialize DSM space for sort statistics.
                                413                 :                :  * ----------------------------------------------------------------
                                414                 :                :  */
                                415                 :                : void
                                416                 :            110 : ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
                                417                 :                : {
                                418                 :                :     Size        size;
                                419                 :                : 
                                420                 :                :     /* don't need this if not instrumenting or no workers */
                                421   [ +  +  -  + ]:            110 :     if (!node->ss.ps.instrument || pcxt->nworkers == 0)
                                422                 :            102 :         return;
                                423                 :                : 
                                424                 :              8 :     size = offsetof(SharedSortInfo, sinstrument)
                                425                 :              8 :         + pcxt->nworkers * sizeof(TuplesortInstrumentation);
                                426                 :              8 :     node->shared_info = shm_toc_allocate(pcxt->toc, size);
                                427                 :                :     /* ensure any unfilled slots will contain zeroes */
                                428                 :              8 :     memset(node->shared_info, 0, size);
                                429                 :              8 :     node->shared_info->num_workers = pcxt->nworkers;
                                430                 :              8 :     shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
                                431                 :              8 :                    node->shared_info);
                                432                 :                : }
                                433                 :                : 
                                434                 :                : /* ----------------------------------------------------------------
                                435                 :                :  *      ExecSortInitializeWorker
                                436                 :                :  *
                                437                 :                :  *      Attach worker to DSM space for sort statistics.
                                438                 :                :  * ----------------------------------------------------------------
                                439                 :                :  */
                                440                 :                : void
 3299 andres@anarazel.de        441                 :            317 : ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
                                442                 :                : {
 3378 rhaas@postgresql.org      443                 :            317 :     node->shared_info =
 3299 andres@anarazel.de        444                 :            317 :         shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
 3378 rhaas@postgresql.org      445                 :            317 :     node->am_worker = true;
                                446                 :            317 : }
                                447                 :                : 
                                448                 :                : /* ----------------------------------------------------------------
                                449                 :                :  *      ExecSortRetrieveInstrumentation
                                450                 :                :  *
                                451                 :                :  *      Transfer sort statistics from DSM to private memory.
                                452                 :                :  * ----------------------------------------------------------------
                                453                 :                :  */
                                454                 :                : void
                                455                 :              8 : ExecSortRetrieveInstrumentation(SortState *node)
                                456                 :                : {
                                457                 :                :     Size        size;
                                458                 :                :     SharedSortInfo *si;
                                459                 :                : 
                                460         [ -  + ]:              8 :     if (node->shared_info == NULL)
 3378 rhaas@postgresql.org      461                 :UBC           0 :         return;
                                462                 :                : 
 3378 rhaas@postgresql.org      463                 :CBC           8 :     size = offsetof(SharedSortInfo, sinstrument)
                                464                 :              8 :         + node->shared_info->num_workers * sizeof(TuplesortInstrumentation);
                                465                 :              8 :     si = palloc(size);
                                466                 :              8 :     memcpy(si, node->shared_info, size);
                                467                 :              8 :     node->shared_info = si;
                                468                 :                : }
        

Generated by: LCOV version 2.0-1