LCOV - differential code coverage report
Current view: top level - src/backend/executor - nodeWindowAgg.c (source / functions) Coverage Total Hit LBC UBC GBC GNC CBC DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 92.1 % 1268 1168 100 1 5 1162 17
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 29 29 4 25
Baseline: 16@8cea358b128 Branches: 74.9 % 992 743 4 245 1 742
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 100.0 % 1 1 1
(120,180] days: 100.0 % 5 5 5
(240..) days: 92.1 % 1262 1162 100 1 1161
Function coverage date bins:
(240..) days: 100.0 % 29 29 4 25
Branch coverage date bins:
(240..) days: 74.9 % 992 743 4 245 1 742

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * nodeWindowAgg.c
                                  4                 :                :  *    routines to handle WindowAgg nodes.
                                  5                 :                :  *
                                  6                 :                :  * A WindowAgg node evaluates "window functions" across suitable partitions
                                  7                 :                :  * of the input tuple set.  Any one WindowAgg works for just a single window
                                  8                 :                :  * specification, though it can evaluate multiple window functions sharing
                                  9                 :                :  * identical window specifications.  The input tuples are required to be
                                 10                 :                :  * delivered in sorted order, with the PARTITION BY columns (if any) as
                                 11                 :                :  * major sort keys and the ORDER BY columns (if any) as minor sort keys.
                                 12                 :                :  * (The planner generates a stack of WindowAggs with intervening Sort nodes
                                 13                 :                :  * as needed, if a query involves more than one window specification.)
                                 14                 :                :  *
                                 15                 :                :  * Since window functions can require access to any or all of the rows in
                                 16                 :                :  * the current partition, we accumulate rows of the partition into a
                                 17                 :                :  * tuplestore.  The window functions are called using the WindowObject API
                                 18                 :                :  * so that they can access those rows as needed.
                                 19                 :                :  *
                                 20                 :                :  * We also support using plain aggregate functions as window functions.
                                 21                 :                :  * For these, the regular Agg-node environment is emulated for each partition.
                                 22                 :                :  * As required by the SQL spec, the output represents the value of the
                                 23                 :                :  * aggregate function over all rows in the current row's window frame.
                                 24                 :                :  *
                                 25                 :                :  *
                                 26                 :                :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
                                 27                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 28                 :                :  *
                                 29                 :                :  * IDENTIFICATION
                                 30                 :                :  *    src/backend/executor/nodeWindowAgg.c
                                 31                 :                :  *
                                 32                 :                :  *-------------------------------------------------------------------------
                                 33                 :                :  */
                                 34                 :                : #include "postgres.h"
                                 35                 :                : 
                                 36                 :                : #include "access/htup_details.h"
                                 37                 :                : #include "catalog/objectaccess.h"
                                 38                 :                : #include "catalog/pg_aggregate.h"
                                 39                 :                : #include "catalog/pg_proc.h"
                                 40                 :                : #include "executor/executor.h"
                                 41                 :                : #include "executor/nodeWindowAgg.h"
                                 42                 :                : #include "miscadmin.h"
                                 43                 :                : #include "nodes/nodeFuncs.h"
                                 44                 :                : #include "optimizer/clauses.h"
                                 45                 :                : #include "optimizer/optimizer.h"
                                 46                 :                : #include "parser/parse_agg.h"
                                 47                 :                : #include "parser/parse_coerce.h"
                                 48                 :                : #include "utils/acl.h"
                                 49                 :                : #include "utils/builtins.h"
                                 50                 :                : #include "utils/datum.h"
                                 51                 :                : #include "utils/expandeddatum.h"
                                 52                 :                : #include "utils/lsyscache.h"
                                 53                 :                : #include "utils/memutils.h"
                                 54                 :                : #include "utils/regproc.h"
                                 55                 :                : #include "utils/syscache.h"
                                 56                 :                : #include "windowapi.h"
                                 57                 :                : 
                                 58                 :                : /*
                                 59                 :                :  * All the window function APIs are called with this object, which is passed
                                 60                 :                :  * to window functions as fcinfo->context.
                                 61                 :                :  */
                                 62                 :                : typedef struct WindowObjectData
                                 63                 :                : {
                                 64                 :                :     NodeTag     type;
                                 65                 :                :     WindowAggState *winstate;   /* parent WindowAggState */
                                 66                 :                :     List       *argstates;      /* ExprState trees for fn's arguments */
                                 67                 :                :     void       *localmem;       /* WinGetPartitionLocalMemory's chunk */
                                 68                 :                :     int         markptr;        /* tuplestore mark pointer for this fn */
                                 69                 :                :     int         readptr;        /* tuplestore read pointer for this fn */
                                 70                 :                :     int64       markpos;        /* row that markptr is positioned on */
                                 71                 :                :     int64       seekpos;        /* row that readptr is positioned on */
                                 72                 :                : } WindowObjectData;
                                 73                 :                : 
                                 74                 :                : /*
                                 75                 :                :  * We have one WindowStatePerFunc struct for each window function and
                                 76                 :                :  * window aggregate handled by this node.
                                 77                 :                :  */
                                 78                 :                : typedef struct WindowStatePerFuncData
                                 79                 :                : {
                                 80                 :                :     /* Links to WindowFunc expr and state nodes this working state is for */
                                 81                 :                :     WindowFuncExprState *wfuncstate;
                                 82                 :                :     WindowFunc *wfunc;
                                 83                 :                : 
                                 84                 :                :     int         numArguments;   /* number of arguments */
                                 85                 :                : 
                                 86                 :                :     FmgrInfo    flinfo;         /* fmgr lookup data for window function */
                                 87                 :                : 
                                 88                 :                :     Oid         winCollation;   /* collation derived for window function */
                                 89                 :                : 
                                 90                 :                :     /*
                                 91                 :                :      * We need the len and byval info for the result of each function in order
                                 92                 :                :      * to know how to copy/delete values.
                                 93                 :                :      */
                                 94                 :                :     int16       resulttypeLen;
                                 95                 :                :     bool        resulttypeByVal;
                                 96                 :                : 
                                 97                 :                :     bool        plain_agg;      /* is it just a plain aggregate function? */
                                 98                 :                :     int         aggno;          /* if so, index of its WindowStatePerAggData */
                                 99                 :                : 
                                100                 :                :     WindowObject winobj;        /* object used in window function API */
                                101                 :                : }           WindowStatePerFuncData;
                                102                 :                : 
                                103                 :                : /*
                                104                 :                :  * For plain aggregate window functions, we also have one of these.
                                105                 :                :  */
                                106                 :                : typedef struct WindowStatePerAggData
                                107                 :                : {
                                108                 :                :     /* Oids of transition functions */
                                109                 :                :     Oid         transfn_oid;
                                110                 :                :     Oid         invtransfn_oid; /* may be InvalidOid */
                                111                 :                :     Oid         finalfn_oid;    /* may be InvalidOid */
                                112                 :                : 
                                113                 :                :     /*
                                114                 :                :      * fmgr lookup data for transition functions --- only valid when
                                115                 :                :      * corresponding oid is not InvalidOid.  Note in particular that fn_strict
                                116                 :                :      * flags are kept here.
                                117                 :                :      */
                                118                 :                :     FmgrInfo    transfn;
                                119                 :                :     FmgrInfo    invtransfn;
                                120                 :                :     FmgrInfo    finalfn;
                                121                 :                : 
                                122                 :                :     int         numFinalArgs;   /* number of arguments to pass to finalfn */
                                123                 :                : 
                                124                 :                :     /*
                                125                 :                :      * initial value from pg_aggregate entry
                                126                 :                :      */
                                127                 :                :     Datum       initValue;
                                128                 :                :     bool        initValueIsNull;
                                129                 :                : 
                                130                 :                :     /*
                                131                 :                :      * cached value for current frame boundaries
                                132                 :                :      */
                                133                 :                :     Datum       resultValue;
                                134                 :                :     bool        resultValueIsNull;
                                135                 :                : 
                                136                 :                :     /*
                                137                 :                :      * We need the len and byval info for the agg's input, result, and
                                138                 :                :      * transition data types in order to know how to copy/delete values.
                                139                 :                :      */
                                140                 :                :     int16       inputtypeLen,
                                141                 :                :                 resulttypeLen,
                                142                 :                :                 transtypeLen;
                                143                 :                :     bool        inputtypeByVal,
                                144                 :                :                 resulttypeByVal,
                                145                 :                :                 transtypeByVal;
                                146                 :                : 
                                147                 :                :     int         wfuncno;        /* index of associated WindowStatePerFuncData */
                                148                 :                : 
                                149                 :                :     /* Context holding transition value and possibly other subsidiary data */
                                150                 :                :     MemoryContext aggcontext;   /* may be private, or winstate->aggcontext */
                                151                 :                : 
                                152                 :                :     /* Current transition value */
                                153                 :                :     Datum       transValue;     /* current transition value */
                                154                 :                :     bool        transValueIsNull;
                                155                 :                : 
                                156                 :                :     int64       transValueCount;    /* number of currently-aggregated rows */
                                157                 :                : 
                                158                 :                :     /* Data local to eval_windowaggregates() */
                                159                 :                :     bool        restart;        /* need to restart this agg in this cycle? */
                                160                 :                : } WindowStatePerAggData;
                                161                 :                : 
                                162                 :                : static void initialize_windowaggregate(WindowAggState *winstate,
                                163                 :                :                                        WindowStatePerFunc perfuncstate,
                                164                 :                :                                        WindowStatePerAgg peraggstate);
                                165                 :                : static void advance_windowaggregate(WindowAggState *winstate,
                                166                 :                :                                     WindowStatePerFunc perfuncstate,
                                167                 :                :                                     WindowStatePerAgg peraggstate);
                                168                 :                : static bool advance_windowaggregate_base(WindowAggState *winstate,
                                169                 :                :                                          WindowStatePerFunc perfuncstate,
                                170                 :                :                                          WindowStatePerAgg peraggstate);
                                171                 :                : static void finalize_windowaggregate(WindowAggState *winstate,
                                172                 :                :                                      WindowStatePerFunc perfuncstate,
                                173                 :                :                                      WindowStatePerAgg peraggstate,
                                174                 :                :                                      Datum *result, bool *isnull);
                                175                 :                : 
                                176                 :                : static void eval_windowaggregates(WindowAggState *winstate);
                                177                 :                : static void eval_windowfunction(WindowAggState *winstate,
                                178                 :                :                                 WindowStatePerFunc perfuncstate,
                                179                 :                :                                 Datum *result, bool *isnull);
                                180                 :                : 
                                181                 :                : static void begin_partition(WindowAggState *winstate);
                                182                 :                : static void spool_tuples(WindowAggState *winstate, int64 pos);
                                183                 :                : static void release_partition(WindowAggState *winstate);
                                184                 :                : 
                                185                 :                : static int  row_is_in_frame(WindowAggState *winstate, int64 pos,
                                186                 :                :                             TupleTableSlot *slot);
                                187                 :                : static void update_frameheadpos(WindowAggState *winstate);
                                188                 :                : static void update_frametailpos(WindowAggState *winstate);
                                189                 :                : static void update_grouptailpos(WindowAggState *winstate);
                                190                 :                : 
                                191                 :                : static WindowStatePerAggData *initialize_peragg(WindowAggState *winstate,
                                192                 :                :                                                 WindowFunc *wfunc,
                                193                 :                :                                                 WindowStatePerAgg peraggstate);
                                194                 :                : static Datum GetAggInitVal(Datum textInitVal, Oid transtype);
                                195                 :                : 
                                196                 :                : static bool are_peers(WindowAggState *winstate, TupleTableSlot *slot1,
                                197                 :                :                       TupleTableSlot *slot2);
                                198                 :                : static bool window_gettupleslot(WindowObject winobj, int64 pos,
                                199                 :                :                                 TupleTableSlot *slot);
                                200                 :                : 
                                201                 :                : 
                                202                 :                : /*
                                203                 :                :  * initialize_windowaggregate
                                204                 :                :  * parallel to initialize_aggregates in nodeAgg.c
                                205                 :                :  */
                                206                 :                : static void
 5586 tgl@sss.pgh.pa.us         207                 :CBC        1989 : initialize_windowaggregate(WindowAggState *winstate,
                                208                 :                :                            WindowStatePerFunc perfuncstate,
                                209                 :                :                            WindowStatePerAgg peraggstate)
                                210                 :                : {
                                211                 :                :     MemoryContext oldContext;
                                212                 :                : 
                                213                 :                :     /*
                                214                 :                :      * If we're using a private aggcontext, we may reset it here.  But if the
                                215                 :                :      * context is shared, we don't know which other aggregates may still need
                                216                 :                :      * it, so we must leave it to the caller to reset at an appropriate time.
                                217                 :                :      */
 3655                           218         [ +  + ]:           1989 :     if (peraggstate->aggcontext != winstate->aggcontext)
  151 nathan@postgresql.or      219                 :GNC        1454 :         MemoryContextReset(peraggstate->aggcontext);
                                220                 :                : 
 5586 tgl@sss.pgh.pa.us         221         [ +  + ]:CBC        1989 :     if (peraggstate->initValueIsNull)
                                222                 :            742 :         peraggstate->transValue = peraggstate->initValue;
                                223                 :                :     else
                                224                 :                :     {
 3655                           225                 :           1247 :         oldContext = MemoryContextSwitchTo(peraggstate->aggcontext);
 5586                           226                 :           2494 :         peraggstate->transValue = datumCopy(peraggstate->initValue,
                                227                 :           1247 :                                             peraggstate->transtypeByVal,
                                228                 :           1247 :                                             peraggstate->transtypeLen);
                                229                 :           1247 :         MemoryContextSwitchTo(oldContext);
                                230                 :                :     }
                                231                 :           1989 :     peraggstate->transValueIsNull = peraggstate->initValueIsNull;
 3655                           232                 :           1989 :     peraggstate->transValueCount = 0;
                                233                 :           1989 :     peraggstate->resultValue = (Datum) 0;
 5583                           234                 :           1989 :     peraggstate->resultValueIsNull = true;
 5586                           235                 :           1989 : }
                                236                 :                : 
                                237                 :                : /*
                                238                 :                :  * advance_windowaggregate
                                239                 :                :  * parallel to advance_aggregates in nodeAgg.c
                                240                 :                :  */
                                241                 :                : static void
                                242                 :          73643 : advance_windowaggregate(WindowAggState *winstate,
                                243                 :                :                         WindowStatePerFunc perfuncstate,
                                244                 :                :                         WindowStatePerAgg peraggstate)
                                245                 :                : {
 1905 andres@anarazel.de        246                 :          73643 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
 5421 bruce@momjian.us          247                 :          73643 :     WindowFuncExprState *wfuncstate = perfuncstate->wfuncstate;
                                248                 :          73643 :     int         numArguments = perfuncstate->numArguments;
                                249                 :                :     Datum       newVal;
                                250                 :                :     ListCell   *arg;
                                251                 :                :     int         i;
                                252                 :                :     MemoryContext oldContext;
 5586 tgl@sss.pgh.pa.us         253                 :          73643 :     ExprContext *econtext = winstate->tmpcontext;
 3925 noah@leadboat.com         254                 :          73643 :     ExprState  *filter = wfuncstate->aggfilter;
                                255                 :                : 
 5586 tgl@sss.pgh.pa.us         256                 :          73643 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
                                257                 :                : 
                                258                 :                :     /* Skip anything FILTERed out */
 3925 noah@leadboat.com         259         [ +  + ]:          73643 :     if (filter)
                                260                 :                :     {
                                261                 :                :         bool        isnull;
 2642 andres@anarazel.de        262                 :            171 :         Datum       res = ExecEvalExpr(filter, econtext, &isnull);
                                263                 :                : 
 3925 noah@leadboat.com         264   [ +  +  +  + ]:            171 :         if (isnull || !DatumGetBool(res))
                                265                 :                :         {
                                266                 :             81 :             MemoryContextSwitchTo(oldContext);
                                267                 :             81 :             return;
                                268                 :                :         }
                                269                 :                :     }
                                270                 :                : 
                                271                 :                :     /* We start from 1, since the 0th arg will be the transition value */
 5586 tgl@sss.pgh.pa.us         272                 :          73562 :     i = 1;
                                273   [ +  +  +  +  :         116899 :     foreach(arg, wfuncstate->args)
                                              +  + ]
                                274                 :                :     {
 5421 bruce@momjian.us          275                 :          43337 :         ExprState  *argstate = (ExprState *) lfirst(arg);
                                276                 :                : 
 1905 andres@anarazel.de        277                 :          43337 :         fcinfo->args[i].value = ExecEvalExpr(argstate, econtext,
                                278                 :                :                                              &fcinfo->args[i].isnull);
 5586 tgl@sss.pgh.pa.us         279                 :          43337 :         i++;
                                280                 :                :     }
                                281                 :                : 
                                282         [ +  + ]:          73562 :     if (peraggstate->transfn.fn_strict)
                                283                 :                :     {
                                284                 :                :         /*
                                285                 :                :          * For a strict transfn, nothing happens when there's a NULL input; we
                                286                 :                :          * just keep the prior transValue.  Note transValueCount doesn't
                                287                 :                :          * change either.
                                288                 :                :          */
                                289         [ +  + ]:          51062 :         for (i = 1; i <= numArguments; i++)
                                290                 :                :         {
 1905 andres@anarazel.de        291         [ +  + ]:          10450 :             if (fcinfo->args[i].isnull)
                                292                 :                :             {
 5586 tgl@sss.pgh.pa.us         293                 :             99 :                 MemoryContextSwitchTo(oldContext);
                                294                 :             99 :                 return;
                                295                 :                :             }
                                296                 :                :         }
                                297                 :                : 
                                298                 :                :         /*
                                299                 :                :          * For strict transition functions with initial value NULL we use the
                                300                 :                :          * first non-NULL input as the initial state.  (We already checked
                                301                 :                :          * that the agg's input type is binary-compatible with its transtype,
                                302                 :                :          * so straight copy here is OK.)
                                303                 :                :          *
                                304                 :                :          * We must copy the datum into aggcontext if it is pass-by-ref.  We do
                                305                 :                :          * not need to pfree the old transValue, since it's NULL.
                                306                 :                :          */
 3655                           307   [ +  +  +  + ]:          40612 :         if (peraggstate->transValueCount == 0 && peraggstate->transValueIsNull)
                                308                 :                :         {
                                309                 :            223 :             MemoryContextSwitchTo(peraggstate->aggcontext);
 1905 andres@anarazel.de        310                 :            446 :             peraggstate->transValue = datumCopy(fcinfo->args[1].value,
 5421 bruce@momjian.us          311                 :            223 :                                                 peraggstate->transtypeByVal,
                                312                 :            223 :                                                 peraggstate->transtypeLen);
 5586 tgl@sss.pgh.pa.us         313                 :            223 :             peraggstate->transValueIsNull = false;
 3655                           314                 :            223 :             peraggstate->transValueCount = 1;
 5586                           315                 :            223 :             MemoryContextSwitchTo(oldContext);
                                316                 :            223 :             return;
                                317                 :                :         }
                                318                 :                : 
                                319         [ -  + ]:          40389 :         if (peraggstate->transValueIsNull)
                                320                 :                :         {
                                321                 :                :             /*
                                322                 :                :              * Don't call a strict function with NULL inputs.  Note it is
                                323                 :                :              * possible to get here despite the above tests, if the transfn is
                                324                 :                :              * strict *and* returned a NULL on a prior cycle.  If that happens
                                325                 :                :              * we will propagate the NULL all the way to the end.  That can
                                326                 :                :              * only happen if there's no inverse transition function, though,
                                327                 :                :              * since we disallow transitions back to NULL when there is one.
                                328                 :                :              */
 5586 tgl@sss.pgh.pa.us         329                 :UBC           0 :             MemoryContextSwitchTo(oldContext);
 3655                           330         [ #  # ]:              0 :             Assert(!OidIsValid(peraggstate->invtransfn_oid));
 5586                           331                 :              0 :             return;
                                332                 :                :         }
                                333                 :                :     }
                                334                 :                : 
                                335                 :                :     /*
                                336                 :                :      * OK to call the transition function.  Set winstate->curaggcontext while
                                337                 :                :      * calling it, for possible use by AggCheckCallContext.
                                338                 :                :      */
 5586 tgl@sss.pgh.pa.us         339                 :CBC       73240 :     InitFunctionCallInfoData(*fcinfo, &(peraggstate->transfn),
                                340                 :                :                              numArguments + 1,
                                341                 :                :                              perfuncstate->winCollation,
                                342                 :                :                              (void *) winstate, NULL);
 1905 andres@anarazel.de        343                 :          73240 :     fcinfo->args[0].value = peraggstate->transValue;
                                344                 :          73240 :     fcinfo->args[0].isnull = peraggstate->transValueIsNull;
 3655 tgl@sss.pgh.pa.us         345                 :          73240 :     winstate->curaggcontext = peraggstate->aggcontext;
 5586                           346                 :          73240 :     newVal = FunctionCallInvoke(fcinfo);
 3655                           347                 :          73234 :     winstate->curaggcontext = NULL;
                                348                 :                : 
                                349                 :                :     /*
                                350                 :                :      * Moving-aggregate transition functions must not return null, see
                                351                 :                :      * advance_windowaggregate_base().
                                352                 :                :      */
                                353   [ -  +  -  - ]:          73234 :     if (fcinfo->isnull && OidIsValid(peraggstate->invtransfn_oid))
 3655 tgl@sss.pgh.pa.us         354         [ #  # ]:UBC           0 :         ereport(ERROR,
                                355                 :                :                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                                356                 :                :                  errmsg("moving-aggregate transition function must not return null")));
                                357                 :                : 
                                358                 :                :     /*
                                359                 :                :      * We must track the number of rows included in transValue, since to
                                360                 :                :      * remove the last input, advance_windowaggregate_base() mustn't call the
                                361                 :                :      * inverse transition function, but simply reset transValue back to its
                                362                 :                :      * initial value.
                                363                 :                :      */
 3655 tgl@sss.pgh.pa.us         364                 :CBC       73234 :     peraggstate->transValueCount++;
                                365                 :                : 
                                366                 :                :     /*
                                367                 :                :      * If pass-by-ref datatype, must copy the new value into aggcontext and
                                368                 :                :      * free the prior transValue.  But if transfn returned a pointer to its
                                369                 :                :      * first input, we don't need to do anything.  Also, if transfn returned a
                                370                 :                :      * pointer to a R/W expanded object that is already a child of the
                                371                 :                :      * aggcontext, assume we can adopt that value without copying it.  (See
                                372                 :                :      * comments for ExecAggCopyTransValue, which this code duplicates.)
                                373                 :                :      */
 5586                           374   [ +  +  +  + ]:          77413 :     if (!peraggstate->transtypeByVal &&
                                375                 :           4179 :         DatumGetPointer(newVal) != DatumGetPointer(peraggstate->transValue))
                                376                 :                :     {
                                377         [ +  - ]:            480 :         if (!fcinfo->isnull)
                                378                 :                :         {
 3655                           379                 :            480 :             MemoryContextSwitchTo(peraggstate->aggcontext);
 2723                           380   [ +  -  +  - ]:            483 :             if (DatumIsReadWriteExpandedObject(newVal,
                                381                 :                :                                                false,
                                382         [ +  + ]:            480 :                                                peraggstate->transtypeLen) &&
                                383         [ +  - ]:              3 :                 MemoryContextGetParent(DatumGetEOHP(newVal)->eoh_context) == CurrentMemoryContext)
                                384                 :                :                  /* do nothing */ ;
                                385                 :                :             else
                                386                 :            477 :                 newVal = datumCopy(newVal,
                                387                 :            477 :                                    peraggstate->transtypeByVal,
                                388                 :            477 :                                    peraggstate->transtypeLen);
                                389                 :                :         }
 5586                           390         [ +  + ]:            480 :         if (!peraggstate->transValueIsNull)
                                391                 :                :         {
 2723                           392   [ +  -  -  +  :            450 :             if (DatumIsReadWriteExpandedObject(peraggstate->transValue,
                                              -  - ]
                                393                 :                :                                                false,
                                394                 :                :                                                peraggstate->transtypeLen))
 2723 tgl@sss.pgh.pa.us         395                 :UBC           0 :                 DeleteExpandedObject(peraggstate->transValue);
                                396                 :                :             else
 2723 tgl@sss.pgh.pa.us         397                 :CBC         450 :                 pfree(DatumGetPointer(peraggstate->transValue));
                                398                 :                :         }
                                399                 :                :     }
                                400                 :                : 
 5586                           401                 :          73234 :     MemoryContextSwitchTo(oldContext);
                                402                 :          73234 :     peraggstate->transValue = newVal;
                                403                 :          73234 :     peraggstate->transValueIsNull = fcinfo->isnull;
                                404                 :                : }
                                405                 :                : 
                                406                 :                : /*
                                407                 :                :  * advance_windowaggregate_base
                                408                 :                :  * Remove the oldest tuple from an aggregation.
                                409                 :                :  *
                                410                 :                :  * This is very much like advance_windowaggregate, except that we will call
                                411                 :                :  * the inverse transition function (which caller must have checked is
                                412                 :                :  * available).
                                413                 :                :  *
                                414                 :                :  * Returns true if we successfully removed the current row from this
                                415                 :                :  * aggregate, false if not (in the latter case, caller is responsible
                                416                 :                :  * for cleaning up by restarting the aggregation).
                                417                 :                :  */
                                418                 :                : static bool
 3655                           419                 :           2313 : advance_windowaggregate_base(WindowAggState *winstate,
                                420                 :                :                              WindowStatePerFunc perfuncstate,
                                421                 :                :                              WindowStatePerAgg peraggstate)
                                422                 :                : {
 1905 andres@anarazel.de        423                 :           2313 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
 3655 tgl@sss.pgh.pa.us         424                 :           2313 :     WindowFuncExprState *wfuncstate = perfuncstate->wfuncstate;
                                425                 :           2313 :     int         numArguments = perfuncstate->numArguments;
                                426                 :                :     Datum       newVal;
                                427                 :                :     ListCell   *arg;
                                428                 :                :     int         i;
                                429                 :                :     MemoryContext oldContext;
                                430                 :           2313 :     ExprContext *econtext = winstate->tmpcontext;
                                431                 :           2313 :     ExprState  *filter = wfuncstate->aggfilter;
                                432                 :                : 
                                433                 :           2313 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
                                434                 :                : 
                                435                 :                :     /* Skip anything FILTERed out */
                                436         [ +  + ]:           2313 :     if (filter)
                                437                 :                :     {
                                438                 :                :         bool        isnull;
 2642 andres@anarazel.de        439                 :             51 :         Datum       res = ExecEvalExpr(filter, econtext, &isnull);
                                440                 :                : 
 3655 tgl@sss.pgh.pa.us         441   [ +  -  +  + ]:             51 :         if (isnull || !DatumGetBool(res))
                                442                 :                :         {
                                443                 :             24 :             MemoryContextSwitchTo(oldContext);
                                444                 :             24 :             return true;
                                445                 :                :         }
                                446                 :                :     }
                                447                 :                : 
                                448                 :                :     /* We start from 1, since the 0th arg will be the transition value */
                                449                 :           2289 :     i = 1;
                                450   [ +  +  +  +  :           4569 :     foreach(arg, wfuncstate->args)
                                              +  + ]
                                451                 :                :     {
                                452                 :           2280 :         ExprState  *argstate = (ExprState *) lfirst(arg);
                                453                 :                : 
 1905 andres@anarazel.de        454                 :           2280 :         fcinfo->args[i].value = ExecEvalExpr(argstate, econtext,
                                455                 :                :                                              &fcinfo->args[i].isnull);
 3655 tgl@sss.pgh.pa.us         456                 :           2280 :         i++;
                                457                 :                :     }
                                458                 :                : 
                                459         [ +  + ]:           2289 :     if (peraggstate->invtransfn.fn_strict)
                                460                 :                :     {
                                461                 :                :         /*
                                462                 :                :          * For a strict (inv)transfn, nothing happens when there's a NULL
                                463                 :                :          * input; we just keep the prior transValue.  Note transValueCount
                                464                 :                :          * doesn't change either.
                                465                 :                :          */
                                466         [ +  + ]:           2802 :         for (i = 1; i <= numArguments; i++)
                                467                 :                :         {
 1905 andres@anarazel.de        468         [ +  + ]:           1416 :             if (fcinfo->args[i].isnull)
                                469                 :                :             {
 3655 tgl@sss.pgh.pa.us         470                 :             39 :                 MemoryContextSwitchTo(oldContext);
                                471                 :             39 :                 return true;
                                472                 :                :             }
                                473                 :                :         }
                                474                 :                :     }
                                475                 :                : 
                                476                 :                :     /* There should still be an added but not yet removed value */
                                477         [ -  + ]:           2250 :     Assert(peraggstate->transValueCount > 0);
                                478                 :                : 
                                479                 :                :     /*
                                480                 :                :      * In moving-aggregate mode, the state must never be NULL, except possibly
                                481                 :                :      * before any rows have been aggregated (which is surely not the case at
                                482                 :                :      * this point).  This restriction allows us to interpret a NULL result
                                483                 :                :      * from the inverse function as meaning "sorry, can't do an inverse
                                484                 :                :      * transition in this case".  We already checked this in
                                485                 :                :      * advance_windowaggregate, but just for safety, check again.
                                486                 :                :      */
                                487         [ -  + ]:           2250 :     if (peraggstate->transValueIsNull)
 3655 tgl@sss.pgh.pa.us         488         [ #  # ]:UBC           0 :         elog(ERROR, "aggregate transition value is NULL before inverse transition");
                                489                 :                : 
                                490                 :                :     /*
                                491                 :                :      * We mustn't use the inverse transition function to remove the last
                                492                 :                :      * input.  Doing so would yield a non-NULL state, whereas we should be in
                                493                 :                :      * the initial state afterwards which may very well be NULL.  So instead,
                                494                 :                :      * we simply re-initialize the aggregate in this case.
                                495                 :                :      */
 3655 tgl@sss.pgh.pa.us         496         [ +  + ]:CBC        2250 :     if (peraggstate->transValueCount == 1)
                                497                 :                :     {
                                498                 :             45 :         MemoryContextSwitchTo(oldContext);
                                499                 :             45 :         initialize_windowaggregate(winstate,
                                500                 :             45 :                                    &winstate->perfunc[peraggstate->wfuncno],
                                501                 :                :                                    peraggstate);
                                502                 :             45 :         return true;
                                503                 :                :     }
                                504                 :                : 
                                505                 :                :     /*
                                506                 :                :      * OK to call the inverse transition function.  Set
                                507                 :                :      * winstate->curaggcontext while calling it, for possible use by
                                508                 :                :      * AggCheckCallContext.
                                509                 :                :      */
                                510                 :           2205 :     InitFunctionCallInfoData(*fcinfo, &(peraggstate->invtransfn),
                                511                 :                :                              numArguments + 1,
                                512                 :                :                              perfuncstate->winCollation,
                                513                 :                :                              (void *) winstate, NULL);
 1905 andres@anarazel.de        514                 :           2205 :     fcinfo->args[0].value = peraggstate->transValue;
                                515                 :           2205 :     fcinfo->args[0].isnull = peraggstate->transValueIsNull;
 3655 tgl@sss.pgh.pa.us         516                 :           2205 :     winstate->curaggcontext = peraggstate->aggcontext;
                                517                 :           2205 :     newVal = FunctionCallInvoke(fcinfo);
                                518                 :           2205 :     winstate->curaggcontext = NULL;
                                519                 :                : 
                                520                 :                :     /*
                                521                 :                :      * If the function returns NULL, report failure, forcing a restart.
                                522                 :                :      */
                                523         [ +  + ]:           2205 :     if (fcinfo->isnull)
                                524                 :                :     {
                                525                 :            119 :         MemoryContextSwitchTo(oldContext);
                                526                 :            119 :         return false;
                                527                 :                :     }
                                528                 :                : 
                                529                 :                :     /* Update number of rows included in transValue */
                                530                 :           2086 :     peraggstate->transValueCount--;
                                531                 :                : 
                                532                 :                :     /*
                                533                 :                :      * If pass-by-ref datatype, must copy the new value into aggcontext and
                                534                 :                :      * free the prior transValue.  But if invtransfn returned a pointer to its
                                535                 :                :      * first input, we don't need to do anything.  Also, if invtransfn
                                536                 :                :      * returned a pointer to a R/W expanded object that is already a child of
                                537                 :                :      * the aggcontext, assume we can adopt that value without copying it. (See
                                538                 :                :      * comments for ExecAggCopyTransValue, which this code duplicates.)
                                539                 :                :      *
                                540                 :                :      * Note: the checks for null values here will never fire, but it seems
                                541                 :                :      * best to have this stanza look just like advance_windowaggregate.
                                542                 :                :      */
                                543   [ +  +  +  + ]:           3151 :     if (!peraggstate->transtypeByVal &&
                                544                 :           1065 :         DatumGetPointer(newVal) != DatumGetPointer(peraggstate->transValue))
                                545                 :                :     {
                                546         [ +  - ]:            333 :         if (!fcinfo->isnull)
                                547                 :                :         {
                                548                 :            333 :             MemoryContextSwitchTo(peraggstate->aggcontext);
 2723                           549   [ +  -  -  - ]:            333 :             if (DatumIsReadWriteExpandedObject(newVal,
                                550                 :                :                                                false,
                                551         [ -  + ]:            333 :                                                peraggstate->transtypeLen) &&
 2723 tgl@sss.pgh.pa.us         552         [ #  # ]:UBC           0 :                 MemoryContextGetParent(DatumGetEOHP(newVal)->eoh_context) == CurrentMemoryContext)
                                553                 :                :                  /* do nothing */ ;
                                554                 :                :             else
 2723 tgl@sss.pgh.pa.us         555                 :CBC         333 :                 newVal = datumCopy(newVal,
                                556                 :            333 :                                    peraggstate->transtypeByVal,
                                557                 :            333 :                                    peraggstate->transtypeLen);
                                558                 :                :         }
 3655                           559         [ +  - ]:            333 :         if (!peraggstate->transValueIsNull)
                                560                 :                :         {
 2723                           561   [ +  -  -  +  :            333 :             if (DatumIsReadWriteExpandedObject(peraggstate->transValue,
                                              -  - ]
                                562                 :                :                                                false,
                                563                 :                :                                                peraggstate->transtypeLen))
 2723 tgl@sss.pgh.pa.us         564                 :UBC           0 :                 DeleteExpandedObject(peraggstate->transValue);
                                565                 :                :             else
 2723 tgl@sss.pgh.pa.us         566                 :CBC         333 :                 pfree(DatumGetPointer(peraggstate->transValue));
                                567                 :                :         }
                                568                 :                :     }
                                569                 :                : 
 3655                           570                 :           2086 :     MemoryContextSwitchTo(oldContext);
                                571                 :           2086 :     peraggstate->transValue = newVal;
                                572                 :           2086 :     peraggstate->transValueIsNull = fcinfo->isnull;
                                573                 :                : 
                                574                 :           2086 :     return true;
                                575                 :                : }
                                576                 :                : 
                                577                 :                : /*
                                578                 :                :  * finalize_windowaggregate
                                579                 :                :  * parallel to finalize_aggregate in nodeAgg.c
                                580                 :                :  */
                                581                 :                : static void
 5586                           582                 :           5283 : finalize_windowaggregate(WindowAggState *winstate,
                                583                 :                :                          WindowStatePerFunc perfuncstate,
                                584                 :                :                          WindowStatePerAgg peraggstate,
                                585                 :                :                          Datum *result, bool *isnull)
                                586                 :                : {
                                587                 :                :     MemoryContext oldContext;
                                588                 :                : 
                                589                 :           5283 :     oldContext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
                                590                 :                : 
                                591                 :                :     /*
                                592                 :                :      * Apply the agg's finalfn if one is provided, else return transValue.
                                593                 :                :      */
                                594         [ +  + ]:           5283 :     if (OidIsValid(peraggstate->finalfn_oid))
                                595                 :                :     {
 1905 andres@anarazel.de        596                 :           2942 :         LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
 3644 tgl@sss.pgh.pa.us         597                 :           2942 :         int         numFinalArgs = peraggstate->numFinalArgs;
                                598                 :                :         bool        anynull;
                                599                 :                :         int         i;
                                600                 :                : 
 1905 andres@anarazel.de        601                 :           2942 :         InitFunctionCallInfoData(fcinfodata.fcinfo, &(peraggstate->finalfn),
                                602                 :                :                                  numFinalArgs,
                                603                 :                :                                  perfuncstate->winCollation,
                                604                 :                :                                  (void *) winstate, NULL);
                                605                 :           2942 :         fcinfo->args[0].value =
                                606   [ +  +  +  + ]:           2942 :             MakeExpandedObjectReadOnly(peraggstate->transValue,
                                607                 :                :                                        peraggstate->transValueIsNull,
                                608                 :                :                                        peraggstate->transtypeLen);
                                609                 :           2942 :         fcinfo->args[0].isnull = peraggstate->transValueIsNull;
 3644 tgl@sss.pgh.pa.us         610                 :           2942 :         anynull = peraggstate->transValueIsNull;
                                611                 :                : 
                                612                 :                :         /* Fill any remaining argument positions with nulls */
                                613         [ +  + ]:           2992 :         for (i = 1; i < numFinalArgs; i++)
                                614                 :                :         {
 1905 andres@anarazel.de        615                 :             50 :             fcinfo->args[i].value = (Datum) 0;
                                616                 :             50 :             fcinfo->args[i].isnull = true;
 3644 tgl@sss.pgh.pa.us         617                 :             50 :             anynull = true;
                                618                 :                :         }
                                619                 :                : 
 1905 andres@anarazel.de        620   [ +  +  -  + ]:           2942 :         if (fcinfo->flinfo->fn_strict && anynull)
                                621                 :                :         {
                                622                 :                :             /* don't call a strict function with NULL inputs */
 5586 tgl@sss.pgh.pa.us         623                 :UBC           0 :             *result = (Datum) 0;
                                624                 :              0 :             *isnull = true;
                                625                 :                :         }
                                626                 :                :         else
                                627                 :                :         {
                                628                 :                :             Datum       res;
                                629                 :                : 
 3655 tgl@sss.pgh.pa.us         630                 :CBC        2942 :             winstate->curaggcontext = peraggstate->aggcontext;
  364                           631                 :           2942 :             res = FunctionCallInvoke(fcinfo);
 3655                           632                 :           2936 :             winstate->curaggcontext = NULL;
 1905 andres@anarazel.de        633                 :           2936 :             *isnull = fcinfo->isnull;
  364 tgl@sss.pgh.pa.us         634   [ +  +  +  + ]:           2936 :             *result = MakeExpandedObjectReadOnly(res,
                                635                 :                :                                                  fcinfo->isnull,
                                636                 :                :                                                  peraggstate->resulttypeLen);
                                637                 :                :         }
                                638                 :                :     }
                                639                 :                :     else
                                640                 :                :     {
  556                           641                 :           2341 :         *result =
                                642   [ +  +  +  + ]:           2341 :             MakeExpandedObjectReadOnly(peraggstate->transValue,
                                643                 :                :                                        peraggstate->transValueIsNull,
                                644                 :                :                                        peraggstate->transtypeLen);
 5586                           645                 :           2341 :         *isnull = peraggstate->transValueIsNull;
                                646                 :                :     }
                                647                 :                : 
                                648                 :           5277 :     MemoryContextSwitchTo(oldContext);
                                649                 :           5277 : }
                                650                 :                : 
                                651                 :                : /*
                                652                 :                :  * eval_windowaggregates
                                653                 :                :  * evaluate plain aggregates being used as window functions
                                654                 :                :  *
                                655                 :                :  * This differs from nodeAgg.c in two ways.  First, if the window's frame
                                656                 :                :  * start position moves, we use the inverse transition function (if it exists)
                                657                 :                :  * to remove rows from the transition value.  And second, we expect to be
                                658                 :                :  * able to call aggregate final functions repeatedly after aggregating more
                                659                 :                :  * data onto the same transition value.  This is not a behavior required by
                                660                 :                :  * nodeAgg.c.
                                661                 :                :  */
                                662                 :                : static void
                                663                 :          65054 : eval_windowaggregates(WindowAggState *winstate)
                                664                 :                : {
                                665                 :                :     WindowStatePerAgg peraggstate;
                                666                 :                :     int         wfuncno,
                                667                 :                :                 numaggs,
                                668                 :                :                 numaggs_restart,
                                669                 :                :                 i;
                                670                 :                :     int64       aggregatedupto_nonrestarted;
                                671                 :                :     MemoryContext oldContext;
                                672                 :                :     ExprContext *econtext;
                                673                 :                :     WindowObject agg_winobj;
                                674                 :                :     TupleTableSlot *agg_row_slot;
                                675                 :                :     TupleTableSlot *temp_slot;
                                676                 :                : 
                                677                 :          65054 :     numaggs = winstate->numaggs;
                                678         [ -  + ]:          65054 :     if (numaggs == 0)
 5586 tgl@sss.pgh.pa.us         679                 :UBC           0 :         return;                 /* nothing to do */
                                680                 :                : 
                                681                 :                :     /* final output execution is in ps_ExprContext */
 5586 tgl@sss.pgh.pa.us         682                 :CBC       65054 :     econtext = winstate->ss.ps.ps_ExprContext;
 5175                           683                 :          65054 :     agg_winobj = winstate->agg_winobj;
                                684                 :          65054 :     agg_row_slot = winstate->agg_row_slot;
 3655                           685                 :          65054 :     temp_slot = winstate->temp_slot_1;
                                686                 :                : 
                                687                 :                :     /*
                                688                 :                :      * If the window's frame start clause is UNBOUNDED_PRECEDING and no
                                689                 :                :      * exclusion clause is specified, then the window frame consists of a
                                690                 :                :      * contiguous group of rows extending forward from the start of the
                                691                 :                :      * partition, and rows only enter the frame, never exit it, as the current
                                692                 :                :      * row advances forward.  This makes it possible to use an incremental
                                693                 :                :      * strategy for evaluating aggregates: we run the transition function for
                                694                 :                :      * each row added to the frame, and run the final function whenever we
                                695                 :                :      * need the current aggregate value.  This is considerably more efficient
                                696                 :                :      * than the naive approach of re-running the entire aggregate calculation
                                697                 :                :      * for each current row.  It does assume that the final function doesn't
                                698                 :                :      * damage the running transition value, but we have the same assumption in
                                699                 :                :      * nodeAgg.c too (when it rescans an existing hash table).
                                700                 :                :      *
                                701                 :                :      * If the frame start does sometimes move, we can still optimize as above
                                702                 :                :      * whenever successive rows share the same frame head, but if the frame
                                703                 :                :      * head moves beyond the previous head we try to remove those rows using
                                704                 :                :      * the aggregate's inverse transition function.  This function restores
                                705                 :                :      * the aggregate's current state to what it would be if the removed row
                                706                 :                :      * had never been aggregated in the first place.  Inverse transition
                                707                 :                :      * functions may optionally return NULL, indicating that the function was
                                708                 :                :      * unable to remove the tuple from aggregation.  If this happens, or if
                                709                 :                :      * the aggregate doesn't have an inverse transition function at all, we
                                710                 :                :      * must perform the aggregation all over again for all tuples within the
                                711                 :                :      * new frame boundaries.
                                712                 :                :      *
                                713                 :                :      * If there's any exclusion clause, then we may have to aggregate over a
                                714                 :                :      * non-contiguous set of rows, so we punt and recalculate for every row.
                                715                 :                :      * (For some frame end choices, it might be that the frame is always
                                716                 :                :      * contiguous anyway, but that's an optimization to investigate later.)
                                717                 :                :      *
                                718                 :                :      * In many common cases, multiple rows share the same frame and hence the
                                719                 :                :      * same aggregate value. (In particular, if there's no ORDER BY in a RANGE
                                720                 :                :      * window, then all rows are peers and so they all have window frame equal
                                721                 :                :      * to the whole partition.)  We optimize such cases by calculating the
                                722                 :                :      * aggregate value once when we reach the first row of a peer group, and
                                723                 :                :      * then returning the saved value for all subsequent rows.
                                724                 :                :      *
                                725                 :                :      * 'aggregatedupto' keeps track of the first row that has not yet been
                                726                 :                :      * accumulated into the aggregate transition values.  Whenever we start a
                                727                 :                :      * new peer group, we accumulate forward to the end of the peer group.
                                728                 :                :      */
                                729                 :                : 
                                730                 :                :     /*
                                731                 :                :      * First, update the frame head position.
                                732                 :                :      *
                                733                 :                :      * The frame head should never move backwards, and the code below wouldn't
                                734                 :                :      * cope if it did, so for safety we complain if it does.
                                735                 :                :      */
 2258                           736                 :          65054 :     update_frameheadpos(winstate);
 3655                           737         [ -  + ]:          65051 :     if (winstate->frameheadpos < winstate->aggregatedbase)
 3655 tgl@sss.pgh.pa.us         738         [ #  # ]:UBC           0 :         elog(ERROR, "window frame head moved backward");
                                739                 :                : 
                                740                 :                :     /*
                                741                 :                :      * If the frame didn't change compared to the previous row, we can re-use
                                742                 :                :      * the result values that were previously saved at the bottom of this
                                743                 :                :      * function.  Since we don't know the current frame's end yet, this is not
                                744                 :                :      * possible to check for fully.  But if the frame end mode is UNBOUNDED
                                745                 :                :      * FOLLOWING or CURRENT ROW, no exclusion clause is specified, and the
                                746                 :                :      * current row lies within the previous row's frame, then the two frames'
                                747                 :                :      * ends must coincide.  Note that on the first row aggregatedbase ==
                                748                 :                :      * aggregatedupto, meaning this test must fail, so we don't need to check
                                749                 :                :      * the "there was no previous row" case explicitly here.
                                750                 :                :      */
 3655 tgl@sss.pgh.pa.us         751         [ +  + ]:CBC       65051 :     if (winstate->aggregatedbase == winstate->frameheadpos &&
                                752         [ +  + ]:          63162 :         (winstate->frameOptions & (FRAMEOPTION_END_UNBOUNDED_FOLLOWING |
                                753                 :          62202 :                                    FRAMEOPTION_END_CURRENT_ROW)) &&
 2258                           754         [ +  + ]:          62202 :         !(winstate->frameOptions & FRAMEOPTION_EXCLUSION) &&
 3655                           755         [ +  + ]:          62112 :         winstate->aggregatedbase <= winstate->currentpos &&
                                756         [ +  + ]:          62094 :         winstate->aggregatedupto > winstate->currentpos)
                                757                 :                :     {
 5586                           758         [ +  + ]:         121276 :         for (i = 0; i < numaggs; i++)
                                759                 :                :         {
                                760                 :          60641 :             peraggstate = &winstate->peragg[i];
                                761                 :          60641 :             wfuncno = peraggstate->wfuncno;
 3655                           762                 :          60641 :             econtext->ecxt_aggvalues[wfuncno] = peraggstate->resultValue;
                                763                 :          60641 :             econtext->ecxt_aggnulls[wfuncno] = peraggstate->resultValueIsNull;
                                764                 :                :         }
                                765                 :          60635 :         return;
                                766                 :                :     }
                                767                 :                : 
                                768                 :                :     /*----------
                                769                 :                :      * Initialize restart flags.
                                770                 :                :      *
                                771                 :                :      * We restart the aggregation:
                                772                 :                :      *   - if we're processing the first row in the partition, or
                                773                 :                :      *   - if the frame's head moved and we cannot use an inverse
                                774                 :                :      *     transition function, or
                                775                 :                :      *   - we have an EXCLUSION clause, or
                                776                 :                :      *   - if the new frame doesn't overlap the old one
                                777                 :                :      *
                                778                 :                :      * Note that we don't strictly need to restart in the last case, but if
                                779                 :                :      * we're going to remove all rows from the aggregation anyway, a restart
                                780                 :                :      * surely is faster.
                                781                 :                :      *----------
                                782                 :                :      */
                                783                 :           4416 :     numaggs_restart = 0;
                                784         [ +  + ]:           9711 :     for (i = 0; i < numaggs; i++)
                                785                 :                :     {
                                786                 :           5295 :         peraggstate = &winstate->peragg[i];
                                787         [ +  + ]:           5295 :         if (winstate->currentpos == 0 ||
                                788         [ +  + ]:           4300 :             (winstate->aggregatedbase != winstate->frameheadpos &&
                                789         [ +  + ]:           2603 :              !OidIsValid(peraggstate->invtransfn_oid)) ||
 2258                           790         [ +  + ]:           4262 :             (winstate->frameOptions & FRAMEOPTION_EXCLUSION) ||
 3655                           791         [ +  + ]:           3704 :             winstate->aggregatedupto <= winstate->frameheadpos)
                                792                 :                :         {
                                793                 :           1825 :             peraggstate->restart = true;
                                794                 :           1825 :             numaggs_restart++;
                                795                 :                :         }
                                796                 :                :         else
                                797                 :           3470 :             peraggstate->restart = false;
                                798                 :                :     }
                                799                 :                : 
                                800                 :                :     /*
                                801                 :                :      * If we have any possibly-moving aggregates, attempt to advance
                                802                 :                :      * aggregatedbase to match the frame's head by removing input rows that
                                803                 :                :      * fell off the top of the frame from the aggregations.  This can fail,
                                804                 :                :      * i.e. advance_windowaggregate_base() can return false, in which case
                                805                 :                :      * we'll restart that aggregate below.
                                806                 :                :      */
                                807         [ +  + ]:           6000 :     while (numaggs_restart < numaggs &&
                                808         [ +  + ]:           4265 :            winstate->aggregatedbase < winstate->frameheadpos)
                                809                 :                :     {
                                810                 :                :         /*
                                811                 :                :          * Fetch the next tuple of those being removed. This should never fail
                                812                 :                :          * as we should have been here before.
                                813                 :                :          */
                                814         [ -  + ]:           1584 :         if (!window_gettupleslot(agg_winobj, winstate->aggregatedbase,
                                815                 :                :                                  temp_slot))
 3655 tgl@sss.pgh.pa.us         816         [ #  # ]:UBC           0 :             elog(ERROR, "could not re-fetch previously fetched frame row");
                                817                 :                : 
                                818                 :                :         /* Set tuple context for evaluation of aggregate arguments */
 3655 tgl@sss.pgh.pa.us         819                 :CBC        1584 :         winstate->tmpcontext->ecxt_outertuple = temp_slot;
                                820                 :                : 
                                821                 :                :         /*
                                822                 :                :          * Perform the inverse transition for each aggregate function in the
                                823                 :                :          * window, unless it has already been marked as needing a restart.
                                824                 :                :          */
                                825         [ +  + ]:           3903 :         for (i = 0; i < numaggs; i++)
                                826                 :                :         {
                                827                 :                :             bool        ok;
                                828                 :                : 
                                829                 :           2319 :             peraggstate = &winstate->peragg[i];
                                830         [ +  + ]:           2319 :             if (peraggstate->restart)
                                831                 :              6 :                 continue;
                                832                 :                : 
                                833                 :           2313 :             wfuncno = peraggstate->wfuncno;
                                834                 :           2313 :             ok = advance_windowaggregate_base(winstate,
                                835                 :           2313 :                                               &winstate->perfunc[wfuncno],
                                836                 :                :                                               peraggstate);
                                837         [ +  + ]:           2313 :             if (!ok)
                                838                 :                :             {
                                839                 :                :                 /* Inverse transition function has failed, must restart */
                                840                 :            119 :                 peraggstate->restart = true;
                                841                 :            119 :                 numaggs_restart++;
                                842                 :                :             }
                                843                 :                :         }
                                844                 :                : 
                                845                 :                :         /* Reset per-input-tuple context after each tuple */
                                846                 :           1584 :         ResetExprContext(winstate->tmpcontext);
                                847                 :                : 
                                848                 :                :         /* And advance the aggregated-row state */
                                849                 :           1584 :         winstate->aggregatedbase++;
                                850                 :           1584 :         ExecClearTuple(temp_slot);
                                851                 :                :     }
                                852                 :                : 
                                853                 :                :     /*
                                854                 :                :      * If we successfully advanced the base rows of all the aggregates,
                                855                 :                :      * aggregatedbase now equals frameheadpos; but if we failed for any, we
                                856                 :                :      * must forcibly update aggregatedbase.
                                857                 :                :      */
                                858                 :           4416 :     winstate->aggregatedbase = winstate->frameheadpos;
                                859                 :                : 
                                860                 :                :     /*
                                861                 :                :      * If we created a mark pointer for aggregates, keep it pushed up to frame
                                862                 :                :      * head, so that tuplestore can discard unnecessary rows.
                                863                 :                :      */
                                864         [ +  + ]:           4416 :     if (agg_winobj->markptr >= 0)
                                865                 :           3112 :         WinSetMarkPosition(agg_winobj, winstate->frameheadpos);
                                866                 :                : 
                                867                 :                :     /*
                                868                 :                :      * Now restart the aggregates that require it.
                                869                 :                :      *
                                870                 :                :      * We assume that aggregates using the shared context always restart if
                                871                 :                :      * *any* aggregate restarts, and we may thus clean up the shared
                                872                 :                :      * aggcontext if that is the case.  Private aggcontexts are reset by
                                873                 :                :      * initialize_windowaggregate() if their owning aggregate restarts. If we
                                874                 :                :      * aren't restarting an aggregate, we need to free any previously saved
                                875                 :                :      * result for it, else we'll leak memory.
                                876                 :                :      */
                                877         [ +  + ]:           4416 :     if (numaggs_restart > 0)
  151 nathan@postgresql.or      878                 :GNC        1847 :         MemoryContextReset(winstate->aggcontext);
 3655 tgl@sss.pgh.pa.us         879         [ +  + ]:CBC        9711 :     for (i = 0; i < numaggs; i++)
                                880                 :                :     {
                                881                 :           5295 :         peraggstate = &winstate->peragg[i];
                                882                 :                : 
                                883                 :                :         /* Aggregates using the shared ctx must restart if *any* agg does */
                                884   [ +  +  +  +  :           5295 :         Assert(peraggstate->aggcontext != winstate->aggcontext ||
                                              -  + ]
                                885                 :                :                numaggs_restart == 0 ||
                                886                 :                :                peraggstate->restart);
                                887                 :                : 
                                888         [ +  + ]:           5295 :         if (peraggstate->restart)
                                889                 :                :         {
 5583                           890                 :           1944 :             wfuncno = peraggstate->wfuncno;
 3655                           891                 :           1944 :             initialize_windowaggregate(winstate,
                                892                 :           1944 :                                        &winstate->perfunc[wfuncno],
                                893                 :                :                                        peraggstate);
                                894                 :                :         }
                                895         [ +  + ]:           3351 :         else if (!peraggstate->resultValueIsNull)
                                896                 :                :         {
                                897         [ +  + ]:           3234 :             if (!peraggstate->resulttypeByVal)
                                898                 :           1076 :                 pfree(DatumGetPointer(peraggstate->resultValue));
                                899                 :           3234 :             peraggstate->resultValue = (Datum) 0;
                                900                 :           3234 :             peraggstate->resultValueIsNull = true;
                                901                 :                :         }
                                902                 :                :     }
                                903                 :                : 
                                904                 :                :     /*
                                905                 :                :      * Non-restarted aggregates now contain the rows between aggregatedbase
                                906                 :                :      * (i.e., frameheadpos) and aggregatedupto, while restarted aggregates
                                907                 :                :      * contain no rows.  If there are any restarted aggregates, we must thus
                                908                 :                :      * begin aggregating anew at frameheadpos, otherwise we may simply
                                909                 :                :      * continue at aggregatedupto.  We must remember the old value of
                                910                 :                :      * aggregatedupto to know how long to skip advancing non-restarted
                                911                 :                :      * aggregates.  If we modify aggregatedupto, we must also clear
                                912                 :                :      * agg_row_slot, per the loop invariant below.
                                913                 :                :      */
                                914                 :           4416 :     aggregatedupto_nonrestarted = winstate->aggregatedupto;
                                915         [ +  + ]:           4416 :     if (numaggs_restart > 0 &&
                                916         [ +  + ]:           1847 :         winstate->aggregatedupto != winstate->frameheadpos)
                                917                 :                :     {
                                918                 :            705 :         winstate->aggregatedupto = winstate->frameheadpos;
                                919                 :            705 :         ExecClearTuple(agg_row_slot);
                                920                 :                :     }
                                921                 :                : 
                                922                 :                :     /*
                                923                 :                :      * Advance until we reach a row not in frame (or end of partition).
                                924                 :                :      *
                                925                 :                :      * Note the loop invariant: agg_row_slot is either empty or holds the row
                                926                 :                :      * at position aggregatedupto.  We advance aggregatedupto after processing
                                927                 :                :      * a row.
                                928                 :                :      */
                                929                 :                :     for (;;)
 5586                           930                 :          73149 :     {
                                931                 :                :         int         ret;
                                932                 :                : 
                                933                 :                :         /* Fetch next row if we didn't already */
 5583                           934   [ +  -  +  + ]:          77565 :         if (TupIsNull(agg_row_slot))
                                935                 :                :         {
 5175                           936         [ +  + ]:          75634 :             if (!window_gettupleslot(agg_winobj, winstate->aggregatedupto,
                                937                 :                :                                      agg_row_slot))
 5583                           938                 :           2053 :                 break;          /* must be end of partition */
                                939                 :                :         }
                                940                 :                : 
                                941                 :                :         /*
                                942                 :                :          * Exit loop if no more rows can be in frame.  Skip aggregation if
                                943                 :                :          * current row is not in frame but there might be more in the frame.
                                944                 :                :          */
 2258                           945                 :          75512 :         ret = row_is_in_frame(winstate, winstate->aggregatedupto, agg_row_slot);
                                946         [ +  + ]:          75506 :         if (ret < 0)
 5583                           947                 :           2351 :             break;
 2258                           948         [ +  + ]:          73155 :         if (ret == 0)
                                949                 :            948 :             goto next_tuple;
                                950                 :                : 
                                951                 :                :         /* Set tuple context for evaluation of aggregate arguments */
 5583                           952                 :          72207 :         winstate->tmpcontext->ecxt_outertuple = agg_row_slot;
                                953                 :                : 
                                954                 :                :         /* Accumulate row into the aggregates */
 5586                           955         [ +  + ]:         155629 :         for (i = 0; i < numaggs; i++)
                                956                 :                :         {
 5583                           957                 :          83428 :             peraggstate = &winstate->peragg[i];
                                958                 :                : 
                                959                 :                :             /* Non-restarted aggs skip until aggregatedupto_nonrestarted */
 3655                           960         [ +  + ]:          83428 :             if (!peraggstate->restart &&
                                961         [ +  + ]:          59611 :                 winstate->aggregatedupto < aggregatedupto_nonrestarted)
                                962                 :           9785 :                 continue;
                                963                 :                : 
 5583                           964                 :          73643 :             wfuncno = peraggstate->wfuncno;
 5586                           965                 :          73643 :             advance_windowaggregate(winstate,
                                966                 :          73643 :                                     &winstate->perfunc[wfuncno],
                                967                 :                :                                     peraggstate);
                                968                 :                :         }
                                969                 :                : 
 2258                           970                 :          72201 : next_tuple:
                                971                 :                :         /* Reset per-input-tuple context after each tuple */
 5586                           972                 :          73149 :         ResetExprContext(winstate->tmpcontext);
                                973                 :                : 
                                974                 :                :         /* And advance the aggregated-row state */
                                975                 :          73149 :         winstate->aggregatedupto++;
 5583                           976                 :          73149 :         ExecClearTuple(agg_row_slot);
                                977                 :                :     }
                                978                 :                : 
                                979                 :                :     /* The frame's end is not supposed to move backwards, ever */
 3655                           980         [ -  + ]:           4404 :     Assert(aggregatedupto_nonrestarted <= winstate->aggregatedupto);
                                981                 :                : 
                                982                 :                :     /*
                                983                 :                :      * finalize aggregates and fill result/isnull fields.
                                984                 :                :      */
 5586                           985         [ +  + ]:           9681 :     for (i = 0; i < numaggs; i++)
                                986                 :                :     {
                                987                 :                :         Datum      *result;
                                988                 :                :         bool       *isnull;
                                989                 :                : 
                                990                 :           5283 :         peraggstate = &winstate->peragg[i];
                                991                 :           5283 :         wfuncno = peraggstate->wfuncno;
                                992                 :           5283 :         result = &econtext->ecxt_aggvalues[wfuncno];
                                993                 :           5283 :         isnull = &econtext->ecxt_aggnulls[wfuncno];
                                994                 :           5283 :         finalize_windowaggregate(winstate,
                                995                 :           5283 :                                  &winstate->perfunc[wfuncno],
                                996                 :                :                                  peraggstate,
                                997                 :                :                                  result, isnull);
                                998                 :                : 
                                999                 :                :         /*
                               1000                 :                :          * save the result in case next row shares the same frame.
                               1001                 :                :          *
                               1002                 :                :          * XXX in some framing modes, eg ROWS/END_CURRENT_ROW, we can know in
                               1003                 :                :          * advance that the next row can't possibly share the same frame. Is
                               1004                 :                :          * it worth detecting that and skipping this code?
                               1005                 :                :          */
 3655                          1006   [ +  +  +  + ]:           5277 :         if (!peraggstate->resulttypeByVal && !*isnull)
                               1007                 :                :         {
                               1008                 :           1376 :             oldContext = MemoryContextSwitchTo(peraggstate->aggcontext);
                               1009                 :           1376 :             peraggstate->resultValue =
                               1010                 :           1376 :                 datumCopy(*result,
                               1011                 :           1376 :                           peraggstate->resulttypeByVal,
                               1012                 :           1376 :                           peraggstate->resulttypeLen);
                               1013                 :           1376 :             MemoryContextSwitchTo(oldContext);
                               1014                 :                :         }
                               1015                 :                :         else
                               1016                 :                :         {
 5586                          1017                 :           3901 :             peraggstate->resultValue = *result;
                               1018                 :                :         }
                               1019                 :           5277 :         peraggstate->resultValueIsNull = *isnull;
                               1020                 :                :     }
                               1021                 :                : }
                               1022                 :                : 
                               1023                 :                : /*
                               1024                 :                :  * eval_windowfunction
                               1025                 :                :  *
                               1026                 :                :  * Arguments of window functions are not evaluated here, because a window
                               1027                 :                :  * function can need random access to arbitrary rows in the partition.
                               1028                 :                :  * The window function uses the special WinGetFuncArgInPartition and
                               1029                 :                :  * WinGetFuncArgInFrame functions to evaluate the arguments for the rows
                               1030                 :                :  * it wants.
                               1031                 :                :  */
                               1032                 :                : static void
                               1033                 :         407430 : eval_windowfunction(WindowAggState *winstate, WindowStatePerFunc perfuncstate,
                               1034                 :                :                     Datum *result, bool *isnull)
                               1035                 :                : {
 1905 andres@anarazel.de       1036                 :         407430 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
                               1037                 :                :     MemoryContext oldContext;
                               1038                 :                : 
 5586 tgl@sss.pgh.pa.us        1039                 :         407430 :     oldContext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
                               1040                 :                : 
                               1041                 :                :     /*
                               1042                 :                :      * We don't pass any normal arguments to a window function, but we do pass
                               1043                 :                :      * it the number of arguments, in order to permit window function
                               1044                 :                :      * implementations to support varying numbers of arguments.  The real info
                               1045                 :                :      * goes through the WindowObject, which is passed via fcinfo->context.
                               1046                 :                :      */
 1905 andres@anarazel.de       1047                 :         407430 :     InitFunctionCallInfoData(*fcinfo, &(perfuncstate->flinfo),
                               1048                 :                :                              perfuncstate->numArguments,
                               1049                 :                :                              perfuncstate->winCollation,
                               1050                 :                :                              (void *) perfuncstate->winobj, NULL);
                               1051                 :                :     /* Just in case, make all the regular argument slots be null */
                               1052         [ +  + ]:         503826 :     for (int argno = 0; argno < perfuncstate->numArguments; argno++)
                               1053                 :          96396 :         fcinfo->args[argno].isnull = true;
                               1054                 :                :     /* Window functions don't have a current aggregate context, either */
 3655 tgl@sss.pgh.pa.us        1055                 :         407430 :     winstate->curaggcontext = NULL;
                               1056                 :                : 
 1905 andres@anarazel.de       1057                 :         407430 :     *result = FunctionCallInvoke(fcinfo);
                               1058                 :         407385 :     *isnull = fcinfo->isnull;
                               1059                 :                : 
                               1060                 :                :     /*
                               1061                 :                :      * The window function might have returned a pass-by-ref result that's
                               1062                 :                :      * just a pointer into one of the WindowObject's temporary slots.  That's
                               1063                 :                :      * not a problem if it's the only window function using the WindowObject;
                               1064                 :                :      * but if there's more than one function, we'd better copy the result to
                               1065                 :                :      * ensure it's not clobbered by later window functions.
                               1066                 :                :      */
                               1067   [ +  +  +  + ]:         407385 :     if (!perfuncstate->resulttypeByVal && !fcinfo->isnull &&
  556 tgl@sss.pgh.pa.us        1068         [ +  + ]:            504 :         winstate->numfuncs > 1)
 5586                          1069                 :             48 :         *result = datumCopy(*result,
                               1070                 :             48 :                             perfuncstate->resulttypeByVal,
                               1071                 :             48 :                             perfuncstate->resulttypeLen);
                               1072                 :                : 
                               1073                 :         407385 :     MemoryContextSwitchTo(oldContext);
                               1074                 :         407385 : }
                               1075                 :                : 
                               1076                 :                : /*
                               1077                 :                :  * begin_partition
                               1078                 :                :  * Start buffering rows of the next partition.
                               1079                 :                :  */
                               1080                 :                : static void
                               1081                 :           1670 : begin_partition(WindowAggState *winstate)
                               1082                 :                : {
 2258                          1083                 :           1670 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
 5421 bruce@momjian.us         1084                 :           1670 :     PlanState  *outerPlan = outerPlanState(winstate);
 2104 tgl@sss.pgh.pa.us        1085                 :           1670 :     int         frameOptions = winstate->frameOptions;
 5421 bruce@momjian.us         1086                 :           1670 :     int         numfuncs = winstate->numfuncs;
                               1087                 :                :     int         i;
                               1088                 :                : 
 5586 tgl@sss.pgh.pa.us        1089                 :           1670 :     winstate->partition_spooled = false;
 5175                          1090                 :           1670 :     winstate->framehead_valid = false;
 5583                          1091                 :           1670 :     winstate->frametail_valid = false;
 2258                          1092                 :           1670 :     winstate->grouptail_valid = false;
 5586                          1093                 :           1670 :     winstate->spooled_rows = 0;
                               1094                 :           1670 :     winstate->currentpos = 0;
 5175                          1095                 :           1670 :     winstate->frameheadpos = 0;
 2258                          1096                 :           1670 :     winstate->frametailpos = 0;
                               1097                 :           1670 :     winstate->currentgroup = 0;
                               1098                 :           1670 :     winstate->frameheadgroup = 0;
                               1099                 :           1670 :     winstate->frametailgroup = 0;
                               1100                 :           1670 :     winstate->groupheadpos = 0;
                               1101                 :           1670 :     winstate->grouptailpos = -1; /* see update_grouptailpos */
 5583                          1102                 :           1670 :     ExecClearTuple(winstate->agg_row_slot);
 2258                          1103         [ +  + ]:           1670 :     if (winstate->framehead_slot)
                               1104                 :            512 :         ExecClearTuple(winstate->framehead_slot);
                               1105         [ +  + ]:           1670 :     if (winstate->frametail_slot)
                               1106                 :            848 :         ExecClearTuple(winstate->frametail_slot);
                               1107                 :                : 
                               1108                 :                :     /*
                               1109                 :                :      * If this is the very first partition, we need to fetch the first input
                               1110                 :                :      * row to store in first_part_slot.
                               1111                 :                :      */
 5586                          1112   [ +  -  +  + ]:           1670 :     if (TupIsNull(winstate->first_part_slot))
                               1113                 :                :     {
                               1114                 :           1066 :         TupleTableSlot *outerslot = ExecProcNode(outerPlan);
                               1115                 :                : 
                               1116   [ +  +  +  + ]:           1066 :         if (!TupIsNull(outerslot))
 5421 bruce@momjian.us         1117                 :           1057 :             ExecCopySlot(winstate->first_part_slot, outerslot);
                               1118                 :                :         else
                               1119                 :                :         {
                               1120                 :                :             /* outer plan is empty, so we have nothing to do */
 5586 tgl@sss.pgh.pa.us        1121                 :              9 :             winstate->partition_spooled = true;
                               1122                 :              9 :             winstate->more_partitions = false;
                               1123                 :              9 :             return;
                               1124                 :                :         }
                               1125                 :                :     }
                               1126                 :                : 
                               1127                 :                :     /* Create new tuplestore for this partition */
                               1128                 :           1661 :     winstate->buffer = tuplestore_begin_heap(false, false, work_mem);
                               1129                 :                : 
                               1130                 :                :     /*
                               1131                 :                :      * Set up read pointers for the tuplestore.  The current pointer doesn't
                               1132                 :                :      * need BACKWARD capability, but the per-window-function read pointers do,
                               1133                 :                :      * and the aggregate pointer does if we might need to restart aggregation.
                               1134                 :                :      */
                               1135                 :           1661 :     winstate->current_ptr = 0;   /* read pointer 0 is pre-allocated */
                               1136                 :                : 
                               1137                 :                :     /* reset default REWIND capability bit for current ptr */
                               1138                 :           1661 :     tuplestore_set_eflags(winstate->buffer, 0);
                               1139                 :                : 
                               1140                 :                :     /* create read pointers for aggregates, if needed */
                               1141         [ +  + ]:           1661 :     if (winstate->numaggs > 0)
                               1142                 :                :     {
 5175                          1143                 :            911 :         WindowObject agg_winobj = winstate->agg_winobj;
                               1144                 :            911 :         int         readptr_flags = 0;
                               1145                 :                : 
                               1146                 :                :         /*
                               1147                 :                :          * If the frame head is potentially movable, or we have an EXCLUSION
                               1148                 :                :          * clause, we might need to restart aggregation ...
                               1149                 :                :          */
 2104                          1150         [ +  + ]:            911 :         if (!(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) ||
                               1151         [ +  + ]:            381 :             (frameOptions & FRAMEOPTION_EXCLUSION))
                               1152                 :                :         {
                               1153                 :                :             /* ... so create a mark pointer to track the frame head */
 5175                          1154                 :            539 :             agg_winobj->markptr = tuplestore_alloc_read_pointer(winstate->buffer, 0);
                               1155                 :                :             /* and the read pointer will need BACKWARD capability */
                               1156                 :            539 :             readptr_flags |= EXEC_FLAG_BACKWARD;
                               1157                 :                :         }
                               1158                 :                : 
                               1159                 :            911 :         agg_winobj->readptr = tuplestore_alloc_read_pointer(winstate->buffer,
                               1160                 :                :                                                             readptr_flags);
                               1161                 :            911 :         agg_winobj->markpos = -1;
                               1162                 :            911 :         agg_winobj->seekpos = -1;
                               1163                 :                : 
                               1164                 :                :         /* Also reset the row counters for aggregates */
                               1165                 :            911 :         winstate->aggregatedbase = 0;
                               1166                 :            911 :         winstate->aggregatedupto = 0;
                               1167                 :                :     }
                               1168                 :                : 
                               1169                 :                :     /* create mark and read pointers for each real window function */
 5586                          1170         [ +  + ]:           3742 :     for (i = 0; i < numfuncs; i++)
                               1171                 :                :     {
 5421 bruce@momjian.us         1172                 :           2081 :         WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
                               1173                 :                : 
 5586 tgl@sss.pgh.pa.us        1174         [ +  + ]:           2081 :         if (!perfuncstate->plain_agg)
                               1175                 :                :         {
 5421 bruce@momjian.us         1176                 :           1083 :             WindowObject winobj = perfuncstate->winobj;
                               1177                 :                : 
 5586 tgl@sss.pgh.pa.us        1178                 :           1083 :             winobj->markptr = tuplestore_alloc_read_pointer(winstate->buffer,
                               1179                 :                :                                                             0);
                               1180                 :           1083 :             winobj->readptr = tuplestore_alloc_read_pointer(winstate->buffer,
                               1181                 :                :                                                             EXEC_FLAG_BACKWARD);
                               1182                 :           1083 :             winobj->markpos = -1;
                               1183                 :           1083 :             winobj->seekpos = -1;
                               1184                 :                :         }
                               1185                 :                :     }
                               1186                 :                : 
                               1187                 :                :     /*
                               1188                 :                :      * If we are in RANGE or GROUPS mode, then determining frame boundaries
                               1189                 :                :      * requires physical access to the frame endpoint rows, except in certain
                               1190                 :                :      * degenerate cases.  We create read pointers to point to those rows, to
                               1191                 :                :      * simplify access and ensure that the tuplestore doesn't discard the
                               1192                 :                :      * endpoint rows prematurely.  (Must create pointers in exactly the same
                               1193                 :                :      * cases that update_frameheadpos and update_frametailpos need them.)
                               1194                 :                :      */
 2258                          1195                 :           1661 :     winstate->framehead_ptr = winstate->frametail_ptr = -1; /* if not used */
                               1196                 :                : 
 2104                          1197         [ +  + ]:           1661 :     if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1198                 :                :     {
                               1199         [ +  + ]:           1073 :         if (((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
                               1200         [ -  + ]:             41 :              node->ordNumCols != 0) ||
                               1201         [ +  + ]:           1032 :             (frameOptions & FRAMEOPTION_START_OFFSET))
 2258                          1202                 :            512 :             winstate->framehead_ptr =
                               1203                 :            512 :                 tuplestore_alloc_read_pointer(winstate->buffer, 0);
 2104                          1204         [ +  + ]:           1073 :         if (((frameOptions & FRAMEOPTION_END_CURRENT_ROW) &&
                               1205         [ +  + ]:            534 :              node->ordNumCols != 0) ||
                               1206         [ +  + ]:            726 :             (frameOptions & FRAMEOPTION_END_OFFSET))
 2258                          1207                 :            848 :             winstate->frametail_ptr =
                               1208                 :            848 :                 tuplestore_alloc_read_pointer(winstate->buffer, 0);
                               1209                 :                :     }
                               1210                 :                : 
                               1211                 :                :     /*
                               1212                 :                :      * If we have an exclusion clause that requires knowing the boundaries of
                               1213                 :                :      * the current row's peer group, we create a read pointer to track the
                               1214                 :                :      * tail position of the peer group (i.e., first row of the next peer
                               1215                 :                :      * group).  The head position does not require its own pointer because we
                               1216                 :                :      * maintain that as a side effect of advancing the current row.
                               1217                 :                :      */
                               1218                 :           1661 :     winstate->grouptail_ptr = -1;
                               1219                 :                : 
 2104                          1220         [ +  + ]:           1661 :     if ((frameOptions & (FRAMEOPTION_EXCLUDE_GROUP |
                               1221                 :            144 :                          FRAMEOPTION_EXCLUDE_TIES)) &&
 2258                          1222         [ +  + ]:            144 :         node->ordNumCols != 0)
                               1223                 :                :     {
                               1224                 :            138 :         winstate->grouptail_ptr =
                               1225                 :            138 :             tuplestore_alloc_read_pointer(winstate->buffer, 0);
                               1226                 :                :     }
                               1227                 :                : 
                               1228                 :                :     /*
                               1229                 :                :      * Store the first tuple into the tuplestore (it's always available now;
                               1230                 :                :      * we either read it above, or saved it at the end of previous partition)
                               1231                 :                :      */
 5586                          1232                 :           1661 :     tuplestore_puttupleslot(winstate->buffer, winstate->first_part_slot);
                               1233                 :           1661 :     winstate->spooled_rows++;
                               1234                 :                : }
                               1235                 :                : 
                               1236                 :                : /*
                               1237                 :                :  * Read tuples from the outer node, up to and including position 'pos', and
                               1238                 :                :  * store them into the tuplestore. If pos is -1, reads the whole partition.
                               1239                 :                :  */
                               1240                 :                : static void
                               1241                 :         817282 : spool_tuples(WindowAggState *winstate, int64 pos)
                               1242                 :                : {
 5421 bruce@momjian.us         1243                 :         817282 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
                               1244                 :                :     PlanState  *outerPlan;
                               1245                 :                :     TupleTableSlot *outerslot;
                               1246                 :                :     MemoryContext oldcontext;
                               1247                 :                : 
 5586 tgl@sss.pgh.pa.us        1248         [ +  + ]:         817282 :     if (!winstate->buffer)
                               1249                 :              9 :         return;                 /* just a safety check */
                               1250         [ +  + ]:         817273 :     if (winstate->partition_spooled)
                               1251                 :          40573 :         return;                 /* whole partition done already */
                               1252                 :                : 
                               1253                 :                :     /*
                               1254                 :                :      * When in pass-through mode we can just exhaust all tuples in the current
                               1255                 :                :      * partition.  We don't need these tuples for any further window function
                               1256                 :                :      * evaluation, however, we do need to keep them around if we're not the
                               1257                 :                :      * top-level window as another WindowAgg node above must see these.
                               1258                 :                :      */
  737 drowley@postgresql.o     1259         [ +  + ]:         776700 :     if (winstate->status != WINDOWAGG_RUN)
                               1260                 :                :     {
                               1261   [ +  -  -  + ]:              9 :         Assert(winstate->status == WINDOWAGG_PASSTHROUGH ||
                               1262                 :                :                winstate->status == WINDOWAGG_PASSTHROUGH_STRICT);
                               1263                 :                : 
                               1264                 :              9 :         pos = -1;
                               1265                 :                :     }
                               1266                 :                : 
                               1267                 :                :     /*
                               1268                 :                :      * If the tuplestore has spilled to disk, alternate reading and writing
                               1269                 :                :      * becomes quite expensive due to frequent buffer flushes.  It's cheaper
                               1270                 :                :      * to force the entire partition to get spooled in one go.
                               1271                 :                :      *
                               1272                 :                :      * XXX this is a horrid kluge --- it'd be better to fix the performance
                               1273                 :                :      * problem inside tuplestore.  FIXME
                               1274                 :                :      */
                               1275         [ -  + ]:         776691 :     else if (!tuplestore_in_memory(winstate->buffer))
 5586 tgl@sss.pgh.pa.us        1276                 :UBC           0 :         pos = -1;
                               1277                 :                : 
 5586 tgl@sss.pgh.pa.us        1278                 :CBC      776700 :     outerPlan = outerPlanState(winstate);
                               1279                 :                : 
                               1280                 :                :     /* Must be in query context to call outerplan */
                               1281                 :         776700 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               1282                 :                : 
                               1283   [ +  +  +  + ]:        2021946 :     while (winstate->spooled_rows <= pos || pos == -1)
                               1284                 :                :     {
                               1285                 :         470132 :         outerslot = ExecProcNode(outerPlan);
                               1286   [ +  +  +  + ]:         470132 :         if (TupIsNull(outerslot))
                               1287                 :                :         {
                               1288                 :                :             /* reached the end of the last partition */
                               1289                 :            982 :             winstate->partition_spooled = true;
                               1290                 :            982 :             winstate->more_partitions = false;
                               1291                 :            982 :             break;
                               1292                 :                :         }
                               1293                 :                : 
                               1294         [ +  + ]:         469150 :         if (node->partNumCols > 0)
                               1295                 :                :         {
 2250 andres@anarazel.de       1296                 :          61813 :             ExprContext *econtext = winstate->tmpcontext;
                               1297                 :                : 
                               1298                 :          61813 :             econtext->ecxt_innertuple = winstate->first_part_slot;
                               1299                 :          61813 :             econtext->ecxt_outertuple = outerslot;
                               1300                 :                : 
                               1301                 :                :             /* Check if this tuple still belongs to the current partition */
                               1302         [ +  + ]:          61813 :             if (!ExecQualAndReset(winstate->partEqfunction, econtext))
                               1303                 :                :             {
                               1304                 :                :                 /*
                               1305                 :                :                  * end of partition; copy the tuple for the next cycle.
                               1306                 :                :                  */
 5586 tgl@sss.pgh.pa.us        1307                 :            604 :                 ExecCopySlot(winstate->first_part_slot, outerslot);
                               1308                 :            604 :                 winstate->partition_spooled = true;
                               1309                 :            604 :                 winstate->more_partitions = true;
                               1310                 :            604 :                 break;
                               1311                 :                :             }
                               1312                 :                :         }
                               1313                 :                : 
                               1314                 :                :         /*
                               1315                 :                :          * Remember the tuple unless we're the top-level window and we're in
                               1316                 :                :          * pass-through mode.
                               1317                 :                :          */
  737 drowley@postgresql.o     1318         [ +  + ]:         468546 :         if (winstate->status != WINDOWAGG_PASSTHROUGH_STRICT)
                               1319                 :                :         {
                               1320                 :                :             /* Still in partition, so save it into the tuplestore */
                               1321                 :         468540 :             tuplestore_puttupleslot(winstate->buffer, outerslot);
                               1322                 :         468540 :             winstate->spooled_rows++;
                               1323                 :                :         }
                               1324                 :                :     }
                               1325                 :                : 
 5586 tgl@sss.pgh.pa.us        1326                 :         776700 :     MemoryContextSwitchTo(oldcontext);
                               1327                 :                : }
                               1328                 :                : 
                               1329                 :                : /*
                               1330                 :                :  * release_partition
                               1331                 :                :  * clear information kept within a partition, including
                               1332                 :                :  * tuplestore and aggregate results.
                               1333                 :                :  */
                               1334                 :                : static void
                               1335                 :           2775 : release_partition(WindowAggState *winstate)
                               1336                 :                : {
                               1337                 :                :     int         i;
                               1338                 :                : 
                               1339         [ +  + ]:           6225 :     for (i = 0; i < winstate->numfuncs; i++)
                               1340                 :                :     {
 5421 bruce@momjian.us         1341                 :           3450 :         WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
                               1342                 :                : 
                               1343                 :                :         /* Release any partition-local state of this window function */
 5586 tgl@sss.pgh.pa.us        1344         [ +  + ]:           3450 :         if (perfuncstate->winobj)
                               1345                 :           1740 :             perfuncstate->winobj->localmem = NULL;
                               1346                 :                :     }
                               1347                 :                : 
                               1348                 :                :     /*
                               1349                 :                :      * Release all partition-local memory (in particular, any partition-local
                               1350                 :                :      * state that we might have trashed our pointers to in the above loop, and
                               1351                 :                :      * any aggregate temp data).  We don't rely on retail pfree because some
                               1352                 :                :      * aggregates might have allocated data we don't have direct pointers to.
                               1353                 :                :      */
  151 nathan@postgresql.or     1354                 :GNC        2775 :     MemoryContextReset(winstate->partcontext);
                               1355                 :           2775 :     MemoryContextReset(winstate->aggcontext);
 3655 tgl@sss.pgh.pa.us        1356         [ +  + ]:CBC        4485 :     for (i = 0; i < winstate->numaggs; i++)
                               1357                 :                :     {
                               1358         [ +  + ]:           1710 :         if (winstate->peragg[i].aggcontext != winstate->aggcontext)
  151 nathan@postgresql.or     1359                 :GNC         963 :             MemoryContextReset(winstate->peragg[i].aggcontext);
                               1360                 :                :     }
                               1361                 :                : 
 5586 tgl@sss.pgh.pa.us        1362         [ +  + ]:CBC        2775 :     if (winstate->buffer)
                               1363                 :           1595 :         tuplestore_end(winstate->buffer);
                               1364                 :           2775 :     winstate->buffer = NULL;
                               1365                 :           2775 :     winstate->partition_spooled = false;
                               1366                 :           2775 : }
                               1367                 :                : 
                               1368                 :                : /*
                               1369                 :                :  * row_is_in_frame
                               1370                 :                :  * Determine whether a row is in the current row's window frame according
                               1371                 :                :  * to our window framing rule
                               1372                 :                :  *
                               1373                 :                :  * The caller must have already determined that the row is in the partition
                               1374                 :                :  * and fetched it into a slot.  This function just encapsulates the framing
                               1375                 :                :  * rules.
                               1376                 :                :  *
                               1377                 :                :  * Returns:
                               1378                 :                :  * -1, if the row is out of frame and no succeeding rows can be in frame
                               1379                 :                :  * 0, if the row is out of frame but succeeding rows might be in frame
                               1380                 :                :  * 1, if the row is in frame
                               1381                 :                :  *
                               1382                 :                :  * May clobber winstate->temp_slot_2.
                               1383                 :                :  */
                               1384                 :                : static int
 5583                          1385                 :          79574 : row_is_in_frame(WindowAggState *winstate, int64 pos, TupleTableSlot *slot)
                               1386                 :                : {
 5175                          1387                 :          79574 :     int         frameOptions = winstate->frameOptions;
                               1388                 :                : 
 5583                          1389         [ -  + ]:          79574 :     Assert(pos >= 0);            /* else caller error */
                               1390                 :                : 
                               1391                 :                :     /*
                               1392                 :                :      * First, check frame starting conditions.  We might as well delegate this
                               1393                 :                :      * to update_frameheadpos always; it doesn't add any notable cost.
                               1394                 :                :      */
 2258                          1395                 :          79574 :     update_frameheadpos(winstate);
                               1396         [ +  + ]:          79574 :     if (pos < winstate->frameheadpos)
 2258 tgl@sss.pgh.pa.us        1397                 :GBC          72 :         return 0;
                               1398                 :                : 
                               1399                 :                :     /*
                               1400                 :                :      * Okay so far, now check frame ending conditions.  Here, we avoid calling
                               1401                 :                :      * update_frametailpos in simple cases, so as not to spool tuples further
                               1402                 :                :      * ahead than necessary.
                               1403                 :                :      */
 5175 tgl@sss.pgh.pa.us        1404         [ +  + ]:CBC       79502 :     if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
                               1405                 :                :     {
                               1406         [ +  + ]:          63993 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1407                 :                :         {
                               1408                 :                :             /* rows after current row are out of frame */
                               1409         [ +  + ]:           1104 :             if (pos > winstate->currentpos)
 2258                          1410                 :            486 :                 return -1;
                               1411                 :                :         }
                               1412         [ +  - ]:          62889 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1413                 :                :         {
                               1414                 :                :             /* following row that is not peer is out of frame */
 5175                          1415         [ +  + ]:          62889 :             if (pos > winstate->currentpos &&
                               1416         [ +  + ]:          61225 :                 !are_peers(winstate, slot, winstate->ss.ss_ScanTupleSlot))
 2258                          1417                 :            632 :                 return -1;
                               1418                 :                :         }
                               1419                 :                :         else
 5175 tgl@sss.pgh.pa.us        1420                 :UBC           0 :             Assert(false);
                               1421                 :                :     }
 2258 tgl@sss.pgh.pa.us        1422         [ +  + ]:CBC       15509 :     else if (frameOptions & FRAMEOPTION_END_OFFSET)
                               1423                 :                :     {
 5175                          1424         [ +  + ]:           8967 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1425                 :                :         {
 5161 bruce@momjian.us         1426                 :           1974 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
                               1427                 :                : 
                               1428                 :                :             /* rows after current row + offset are out of frame */
 2258 tgl@sss.pgh.pa.us        1429         [ +  + ]:           1974 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
 5175                          1430                 :             57 :                 offset = -offset;
                               1431                 :                : 
                               1432         [ +  + ]:           1974 :             if (pos > winstate->currentpos + offset)
 2258                          1433                 :            576 :                 return -1;
                               1434                 :                :         }
                               1435         [ +  - ]:           6993 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1436                 :                :         {
                               1437                 :                :             /* hard cases, so delegate to update_frametailpos */
                               1438                 :           6993 :             update_frametailpos(winstate);
                               1439         [ +  + ]:           6972 :             if (pos >= winstate->frametailpos)
                               1440                 :            735 :                 return -1;
                               1441                 :                :         }
                               1442                 :                :         else
 5175 tgl@sss.pgh.pa.us        1443                 :UBC           0 :             Assert(false);
                               1444                 :                :     }
                               1445                 :                : 
                               1446                 :                :     /* Check exclusion clause */
 2258 tgl@sss.pgh.pa.us        1447         [ +  + ]:CBC       77052 :     if (frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
                               1448                 :                :     {
                               1449         [ +  + ]:           1233 :         if (pos == winstate->currentpos)
                               1450                 :            210 :             return 0;
                               1451                 :                :     }
                               1452         [ +  + ]:          75819 :     else if ((frameOptions & FRAMEOPTION_EXCLUDE_GROUP) ||
                               1453         [ +  + ]:          74388 :              ((frameOptions & FRAMEOPTION_EXCLUDE_TIES) &&
                               1454         [ +  + ]:           1485 :               pos != winstate->currentpos))
                               1455                 :                :     {
                               1456                 :           2646 :         WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
                               1457                 :                : 
                               1458                 :                :         /* If no ORDER BY, all rows are peers with each other */
                               1459         [ +  + ]:           2646 :         if (node->ordNumCols == 0)
                               1460                 :            234 :             return 0;
                               1461                 :                :         /* Otherwise, check the group boundaries */
                               1462         [ +  + ]:           2412 :         if (pos >= winstate->groupheadpos)
                               1463                 :                :         {
                               1464                 :           1296 :             update_grouptailpos(winstate);
                               1465         [ +  + ]:           1296 :             if (pos < winstate->grouptailpos)
                               1466                 :            504 :                 return 0;
                               1467                 :                :         }
                               1468                 :                :     }
                               1469                 :                : 
                               1470                 :                :     /* If we get here, it's in frame */
                               1471                 :          76104 :     return 1;
                               1472                 :                : }
                               1473                 :                : 
                               1474                 :                : /*
                               1475                 :                :  * update_frameheadpos
                               1476                 :                :  * make frameheadpos valid for the current row
                               1477                 :                :  *
                               1478                 :                :  * Note that frameheadpos is computed without regard for any window exclusion
                               1479                 :                :  * clause; the current row and/or its peers are considered part of the frame
                               1480                 :                :  * for this purpose even if they must be excluded later.
                               1481                 :                :  *
                               1482                 :                :  * May clobber winstate->temp_slot_2.
                               1483                 :                :  */
                               1484                 :                : static void
                               1485                 :         150089 : update_frameheadpos(WindowAggState *winstate)
                               1486                 :                : {
 5583                          1487                 :         150089 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
 5175                          1488                 :         150089 :     int         frameOptions = winstate->frameOptions;
                               1489                 :                :     MemoryContext oldcontext;
                               1490                 :                : 
                               1491         [ +  + ]:         150089 :     if (winstate->framehead_valid)
 5583                          1492                 :          82668 :         return;                 /* already known for current row */
                               1493                 :                : 
                               1494                 :                :     /* We may be called in a short-lived context */
 2258                          1495                 :          67421 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               1496                 :                : 
 5175                          1497         [ +  + ]:          67421 :     if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
                               1498                 :                :     {
                               1499                 :                :         /* In UNBOUNDED PRECEDING mode, frame head is always row 0 */
                               1500                 :          62281 :         winstate->frameheadpos = 0;
                               1501                 :          62281 :         winstate->framehead_valid = true;
                               1502                 :                :     }
                               1503         [ +  + ]:           5140 :     else if (frameOptions & FRAMEOPTION_START_CURRENT_ROW)
                               1504                 :                :     {
                               1505         [ +  + ]:           1402 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1506                 :                :         {
                               1507                 :                :             /* In ROWS mode, frame head is the same as current */
                               1508                 :           1188 :             winstate->frameheadpos = winstate->currentpos;
                               1509                 :           1188 :             winstate->framehead_valid = true;
                               1510                 :                :         }
 2258                          1511         [ +  - ]:            214 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1512                 :                :         {
                               1513                 :                :             /* If no ORDER BY, all rows are peers with each other */
 5175                          1514         [ -  + ]:            214 :             if (node->ordNumCols == 0)
                               1515                 :                :             {
 5175 tgl@sss.pgh.pa.us        1516                 :UBC           0 :                 winstate->frameheadpos = 0;
                               1517                 :              0 :                 winstate->framehead_valid = true;
 2258                          1518                 :              0 :                 MemoryContextSwitchTo(oldcontext);
 5175                          1519                 :              0 :                 return;
                               1520                 :                :             }
                               1521                 :                : 
                               1522                 :                :             /*
                               1523                 :                :              * In RANGE or GROUPS START_CURRENT_ROW mode, frame head is the
                               1524                 :                :              * first row that is a peer of current row.  We keep a copy of the
                               1525                 :                :              * last-known frame head row in framehead_slot, and advance as
                               1526                 :                :              * necessary.  Note that if we reach end of partition, we will
                               1527                 :                :              * leave frameheadpos = end+1 and framehead_slot empty.
                               1528                 :                :              */
 2258 tgl@sss.pgh.pa.us        1529                 :CBC         214 :             tuplestore_select_read_pointer(winstate->buffer,
                               1530                 :                :                                            winstate->framehead_ptr);
                               1531         [ +  + ]:            214 :             if (winstate->frameheadpos == 0 &&
                               1532   [ +  -  +  + ]:            106 :                 TupIsNull(winstate->framehead_slot))
                               1533                 :                :             {
                               1534                 :                :                 /* fetch first row into framehead_slot, if we didn't already */
                               1535         [ -  + ]:             41 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1536                 :                :                                              winstate->framehead_slot))
 2258 tgl@sss.pgh.pa.us        1537         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1538                 :                :             }
                               1539                 :                : 
 2258 tgl@sss.pgh.pa.us        1540   [ +  -  +  - ]:CBC         372 :             while (!TupIsNull(winstate->framehead_slot))
                               1541                 :                :             {
                               1542         [ +  + ]:            372 :                 if (are_peers(winstate, winstate->framehead_slot,
                               1543                 :                :                               winstate->ss.ss_ScanTupleSlot))
                               1544                 :            214 :                     break;      /* this row is the correct frame head */
                               1545                 :                :                 /* Note we advance frameheadpos even if the fetch fails */
                               1546                 :            158 :                 winstate->frameheadpos++;
                               1547                 :            158 :                 spool_tuples(winstate, winstate->frameheadpos);
                               1548         [ -  + ]:            158 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1549                 :                :                                              winstate->framehead_slot))
 2258 tgl@sss.pgh.pa.us        1550                 :UBC           0 :                     break;      /* end of partition */
                               1551                 :                :             }
 5175 tgl@sss.pgh.pa.us        1552                 :CBC         214 :             winstate->framehead_valid = true;
                               1553                 :                :         }
                               1554                 :                :         else
 5175 tgl@sss.pgh.pa.us        1555                 :UBC           0 :             Assert(false);
                               1556                 :                :     }
 2258 tgl@sss.pgh.pa.us        1557         [ +  - ]:CBC        3738 :     else if (frameOptions & FRAMEOPTION_START_OFFSET)
                               1558                 :                :     {
 5175                          1559         [ +  + ]:           3738 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1560                 :                :         {
                               1561                 :                :             /* In ROWS mode, bound is physically n before/after current */
 5161 bruce@momjian.us         1562                 :            786 :             int64       offset = DatumGetInt64(winstate->startOffsetValue);
                               1563                 :                : 
 2258 tgl@sss.pgh.pa.us        1564         [ +  + ]:            786 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
 5175                          1565                 :            756 :                 offset = -offset;
                               1566                 :                : 
                               1567                 :            786 :             winstate->frameheadpos = winstate->currentpos + offset;
                               1568                 :                :             /* frame head can't go before first row */
                               1569         [ +  + ]:            786 :             if (winstate->frameheadpos < 0)
                               1570                 :            114 :                 winstate->frameheadpos = 0;
 2258                          1571         [ -  + ]:            672 :             else if (winstate->frameheadpos > winstate->currentpos + 1)
                               1572                 :                :             {
                               1573                 :                :                 /* make sure frameheadpos is not past end of partition */
 5175 tgl@sss.pgh.pa.us        1574                 :UBC           0 :                 spool_tuples(winstate, winstate->frameheadpos - 1);
                               1575         [ #  # ]:              0 :                 if (winstate->frameheadpos > winstate->spooled_rows)
                               1576                 :              0 :                     winstate->frameheadpos = winstate->spooled_rows;
                               1577                 :                :             }
 5175 tgl@sss.pgh.pa.us        1578                 :CBC         786 :             winstate->framehead_valid = true;
                               1579                 :                :         }
                               1580         [ +  + ]:           2952 :         else if (frameOptions & FRAMEOPTION_RANGE)
                               1581                 :                :         {
                               1582                 :                :             /*
                               1583                 :                :              * In RANGE START_OFFSET mode, frame head is the first row that
                               1584                 :                :              * satisfies the in_range constraint relative to the current row.
                               1585                 :                :              * We keep a copy of the last-known frame head row in
                               1586                 :                :              * framehead_slot, and advance as necessary.  Note that if we
                               1587                 :                :              * reach end of partition, we will leave frameheadpos = end+1 and
                               1588                 :                :              * framehead_slot empty.
                               1589                 :                :              */
 2242                          1590                 :           2262 :             int         sortCol = node->ordColIdx[0];
                               1591                 :                :             bool        sub,
                               1592                 :                :                         less;
                               1593                 :                : 
                               1594                 :                :             /* We must have an ordering column */
 2104                          1595         [ -  + ]:           2262 :             Assert(node->ordNumCols == 1);
                               1596                 :                : 
                               1597                 :                :             /* Precompute flags for in_range checks */
 2258                          1598         [ +  + ]:           2262 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
                               1599                 :           1851 :                 sub = true;     /* subtract startOffset from current row */
                               1600                 :                :             else
                               1601                 :            411 :                 sub = false;    /* add it */
                               1602                 :           2262 :             less = false;       /* normally, we want frame head >= sum */
                               1603                 :                :             /* If sort order is descending, flip both flags */
                               1604         [ +  + ]:           2262 :             if (!winstate->inRangeAsc)
                               1605                 :                :             {
                               1606                 :            327 :                 sub = !sub;
                               1607                 :            327 :                 less = true;
                               1608                 :                :             }
                               1609                 :                : 
                               1610                 :           2262 :             tuplestore_select_read_pointer(winstate->buffer,
                               1611                 :                :                                            winstate->framehead_ptr);
                               1612         [ +  + ]:           2262 :             if (winstate->frameheadpos == 0 &&
                               1613   [ +  -  +  + ]:           1251 :                 TupIsNull(winstate->framehead_slot))
                               1614                 :                :             {
                               1615                 :                :                 /* fetch first row into framehead_slot, if we didn't already */
                               1616         [ -  + ]:            285 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1617                 :                :                                              winstate->framehead_slot))
 2258 tgl@sss.pgh.pa.us        1618         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1619                 :                :             }
                               1620                 :                : 
 2258 tgl@sss.pgh.pa.us        1621   [ +  -  +  + ]:CBC        3633 :             while (!TupIsNull(winstate->framehead_slot))
                               1622                 :                :             {
                               1623                 :                :                 Datum       headval,
                               1624                 :                :                             currval;
                               1625                 :                :                 bool        headisnull,
                               1626                 :                :                             currisnull;
                               1627                 :                : 
 2242                          1628                 :           3531 :                 headval = slot_getattr(winstate->framehead_slot, sortCol,
                               1629                 :                :                                        &headisnull);
                               1630                 :           3531 :                 currval = slot_getattr(winstate->ss.ss_ScanTupleSlot, sortCol,
                               1631                 :                :                                        &currisnull);
 2258                          1632   [ +  +  +  + ]:           3531 :                 if (headisnull || currisnull)
                               1633                 :                :                 {
                               1634                 :                :                     /* order of the rows depends only on nulls_first */
                               1635         [ +  + ]:             54 :                     if (winstate->inRangeNullsFirst)
                               1636                 :                :                     {
                               1637                 :                :                         /* advance head if head is null and curr is not */
                               1638   [ +  -  +  + ]:             24 :                         if (!headisnull || currisnull)
                               1639                 :                :                             break;
                               1640                 :                :                     }
                               1641                 :                :                     else
                               1642                 :                :                     {
                               1643                 :                :                         /* advance head if head is not null and curr is null */
                               1644   [ +  +  +  - ]:             30 :                         if (headisnull || !currisnull)
                               1645                 :                :                             break;
                               1646                 :                :                     }
                               1647                 :                :                 }
                               1648                 :                :                 else
                               1649                 :                :                 {
                               1650         [ +  + ]:           3477 :                     if (DatumGetBool(FunctionCall5Coll(&winstate->startInRangeFunc,
                               1651                 :                :                                                        winstate->inRangeColl,
                               1652                 :                :                                                        headval,
                               1653                 :                :                                                        currval,
                               1654                 :                :                                                        winstate->startOffsetValue,
                               1655                 :                :                                                        BoolGetDatum(sub),
                               1656                 :                :                                                        BoolGetDatum(less))))
                               1657                 :           2085 :                         break;  /* this row is the correct frame head */
                               1658                 :                :                 }
                               1659                 :                :                 /* Note we advance frameheadpos even if the fetch fails */
                               1660                 :           1398 :                 winstate->frameheadpos++;
                               1661                 :           1398 :                 spool_tuples(winstate, winstate->frameheadpos);
                               1662         [ +  + ]:           1398 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1663                 :                :                                              winstate->framehead_slot))
                               1664                 :             27 :                     break;      /* end of partition */
                               1665                 :                :             }
                               1666                 :           2238 :             winstate->framehead_valid = true;
                               1667                 :                :         }
                               1668         [ +  - ]:            690 :         else if (frameOptions & FRAMEOPTION_GROUPS)
                               1669                 :                :         {
                               1670                 :                :             /*
                               1671                 :                :              * In GROUPS START_OFFSET mode, frame head is the first row of the
                               1672                 :                :              * first peer group whose number satisfies the offset constraint.
                               1673                 :                :              * We keep a copy of the last-known frame head row in
                               1674                 :                :              * framehead_slot, and advance as necessary.  Note that if we
                               1675                 :                :              * reach end of partition, we will leave frameheadpos = end+1 and
                               1676                 :                :              * framehead_slot empty.
                               1677                 :                :              */
                               1678                 :            690 :             int64       offset = DatumGetInt64(winstate->startOffsetValue);
                               1679                 :                :             int64       minheadgroup;
                               1680                 :                : 
                               1681         [ +  + ]:            690 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
                               1682                 :            564 :                 minheadgroup = winstate->currentgroup - offset;
                               1683                 :                :             else
                               1684                 :            126 :                 minheadgroup = winstate->currentgroup + offset;
                               1685                 :                : 
                               1686                 :            690 :             tuplestore_select_read_pointer(winstate->buffer,
                               1687                 :                :                                            winstate->framehead_ptr);
                               1688         [ +  + ]:            690 :             if (winstate->frameheadpos == 0 &&
                               1689   [ +  -  +  + ]:            375 :                 TupIsNull(winstate->framehead_slot))
                               1690                 :                :             {
                               1691                 :                :                 /* fetch first row into framehead_slot, if we didn't already */
                               1692         [ -  + ]:            186 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1693                 :                :                                              winstate->framehead_slot))
 2258 tgl@sss.pgh.pa.us        1694         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1695                 :                :             }
                               1696                 :                : 
 2258 tgl@sss.pgh.pa.us        1697   [ +  -  +  + ]:CBC        1761 :             while (!TupIsNull(winstate->framehead_slot))
                               1698                 :                :             {
                               1699         [ +  + ]:           1059 :                 if (winstate->frameheadgroup >= minheadgroup)
                               1700                 :            660 :                     break;      /* this row is the correct frame head */
                               1701                 :            399 :                 ExecCopySlot(winstate->temp_slot_2, winstate->framehead_slot);
                               1702                 :                :                 /* Note we advance frameheadpos even if the fetch fails */
                               1703                 :            399 :                 winstate->frameheadpos++;
                               1704                 :            399 :                 spool_tuples(winstate, winstate->frameheadpos);
                               1705         [ +  + ]:            399 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1706                 :                :                                              winstate->framehead_slot))
                               1707                 :             18 :                     break;      /* end of partition */
                               1708         [ +  + ]:            381 :                 if (!are_peers(winstate, winstate->temp_slot_2,
                               1709                 :                :                                winstate->framehead_slot))
                               1710                 :            261 :                     winstate->frameheadgroup++;
                               1711                 :                :             }
                               1712                 :            690 :             ExecClearTuple(winstate->temp_slot_2);
                               1713                 :            690 :             winstate->framehead_valid = true;
                               1714                 :                :         }
                               1715                 :                :         else
 5175 tgl@sss.pgh.pa.us        1716                 :UBC           0 :             Assert(false);
                               1717                 :                :     }
                               1718                 :                :     else
                               1719                 :              0 :         Assert(false);
                               1720                 :                : 
 2258 tgl@sss.pgh.pa.us        1721                 :CBC       67397 :     MemoryContextSwitchTo(oldcontext);
                               1722                 :                : }
                               1723                 :                : 
                               1724                 :                : /*
                               1725                 :                :  * update_frametailpos
                               1726                 :                :  * make frametailpos valid for the current row
                               1727                 :                :  *
                               1728                 :                :  * Note that frametailpos is computed without regard for any window exclusion
                               1729                 :                :  * clause; the current row and/or its peers are considered part of the frame
                               1730                 :                :  * for this purpose even if they must be excluded later.
                               1731                 :                :  *
                               1732                 :                :  * May clobber winstate->temp_slot_2.
                               1733                 :                :  */
                               1734                 :                : static void
                               1735                 :          74344 : update_frametailpos(WindowAggState *winstate)
                               1736                 :                : {
 5175                          1737                 :          74344 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
                               1738                 :          74344 :     int         frameOptions = winstate->frameOptions;
                               1739                 :                :     MemoryContext oldcontext;
                               1740                 :                : 
                               1741         [ +  + ]:          74344 :     if (winstate->frametail_valid)
                               1742                 :           8910 :         return;                 /* already known for current row */
                               1743                 :                : 
                               1744                 :                :     /* We may be called in a short-lived context */
 2258                          1745                 :          65434 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               1746                 :                : 
 5175                          1747         [ +  + ]:          65434 :     if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
                               1748                 :                :     {
                               1749                 :                :         /* In UNBOUNDED FOLLOWING mode, all partition rows are in frame */
 5583                          1750                 :             90 :         spool_tuples(winstate, -1);
 2258                          1751                 :             90 :         winstate->frametailpos = winstate->spooled_rows;
 5583                          1752                 :             90 :         winstate->frametail_valid = true;
                               1753                 :                :     }
 5175                          1754         [ +  + ]:          65344 :     else if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
                               1755                 :                :     {
                               1756         [ +  + ]:          62152 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1757                 :                :         {
                               1758                 :                :             /* In ROWS mode, exactly the rows up to current are in frame */
 2258                          1759                 :             60 :             winstate->frametailpos = winstate->currentpos + 1;
 5175                          1760                 :             60 :             winstate->frametail_valid = true;
                               1761                 :                :         }
 2258                          1762         [ +  - ]:          62092 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1763                 :                :         {
                               1764                 :                :             /* If no ORDER BY, all rows are peers with each other */
 5175                          1765         [ +  + ]:          62092 :             if (node->ordNumCols == 0)
                               1766                 :                :             {
                               1767                 :             30 :                 spool_tuples(winstate, -1);
 2258                          1768                 :             30 :                 winstate->frametailpos = winstate->spooled_rows;
 5175                          1769                 :             30 :                 winstate->frametail_valid = true;
 2258                          1770                 :             30 :                 MemoryContextSwitchTo(oldcontext);
 5175                          1771                 :             30 :                 return;
                               1772                 :                :             }
                               1773                 :                : 
                               1774                 :                :             /*
                               1775                 :                :              * In RANGE or GROUPS END_CURRENT_ROW mode, frame end is the last
                               1776                 :                :              * row that is a peer of current row, frame tail is the row after
                               1777                 :                :              * that (if any).  We keep a copy of the last-known frame tail row
                               1778                 :                :              * in frametail_slot, and advance as necessary.  Note that if we
                               1779                 :                :              * reach end of partition, we will leave frametailpos = end+1 and
                               1780                 :                :              * frametail_slot empty.
                               1781                 :                :              */
 2258                          1782                 :          62062 :             tuplestore_select_read_pointer(winstate->buffer,
                               1783                 :                :                                            winstate->frametail_ptr);
                               1784         [ +  + ]:          62062 :             if (winstate->frametailpos == 0 &&
                               1785   [ +  -  +  - ]:            338 :                 TupIsNull(winstate->frametail_slot))
                               1786                 :                :             {
                               1787                 :                :                 /* fetch first row into frametail_slot, if we didn't already */
                               1788         [ -  + ]:            338 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1789                 :                :                                              winstate->frametail_slot))
 2258 tgl@sss.pgh.pa.us        1790         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1791                 :                :             }
                               1792                 :                : 
 2258 tgl@sss.pgh.pa.us        1793   [ +  -  +  + ]:CBC      123792 :             while (!TupIsNull(winstate->frametail_slot))
                               1794                 :                :             {
                               1795         [ +  + ]:         111732 :                 if (winstate->frametailpos > winstate->currentpos &&
                               1796         [ +  + ]:         109850 :                     !are_peers(winstate, winstate->frametail_slot,
                               1797                 :                :                                winstate->ss.ss_ScanTupleSlot))
                               1798                 :          49670 :                     break;      /* this row is the frame tail */
                               1799                 :                :                 /* Note we advance frametailpos even if the fetch fails */
                               1800                 :          62062 :                 winstate->frametailpos++;
                               1801                 :          62062 :                 spool_tuples(winstate, winstate->frametailpos);
                               1802         [ +  + ]:          62062 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1803                 :                :                                              winstate->frametail_slot))
 5161 bruce@momjian.us         1804                 :            332 :                     break;      /* end of partition */
                               1805                 :                :             }
 5175 tgl@sss.pgh.pa.us        1806                 :          62062 :             winstate->frametail_valid = true;
                               1807                 :                :         }
                               1808                 :                :         else
 5175 tgl@sss.pgh.pa.us        1809                 :UBC           0 :             Assert(false);
                               1810                 :                :     }
 2258 tgl@sss.pgh.pa.us        1811         [ +  - ]:CBC        3192 :     else if (frameOptions & FRAMEOPTION_END_OFFSET)
                               1812                 :                :     {
 5175                          1813         [ +  + ]:           3192 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1814                 :                :         {
                               1815                 :                :             /* In ROWS mode, bound is physically n before/after current */
 5161 bruce@momjian.us         1816                 :             90 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
                               1817                 :                : 
 2258 tgl@sss.pgh.pa.us        1818         [ -  + ]:             90 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
 5175 tgl@sss.pgh.pa.us        1819                 :UBC           0 :                 offset = -offset;
                               1820                 :                : 
 2258 tgl@sss.pgh.pa.us        1821                 :CBC          90 :             winstate->frametailpos = winstate->currentpos + offset + 1;
                               1822                 :                :             /* smallest allowable value of frametailpos is 0 */
 5175                          1823         [ -  + ]:             90 :             if (winstate->frametailpos < 0)
 2258 tgl@sss.pgh.pa.us        1824                 :UBC           0 :                 winstate->frametailpos = 0;
 2258 tgl@sss.pgh.pa.us        1825         [ +  - ]:CBC          90 :             else if (winstate->frametailpos > winstate->currentpos + 1)
                               1826                 :                :             {
                               1827                 :                :                 /* make sure frametailpos is not past end of partition */
                               1828                 :             90 :                 spool_tuples(winstate, winstate->frametailpos - 1);
                               1829         [ +  + ]:             90 :                 if (winstate->frametailpos > winstate->spooled_rows)
                               1830                 :             18 :                     winstate->frametailpos = winstate->spooled_rows;
                               1831                 :                :             }
 5175                          1832                 :             90 :             winstate->frametail_valid = true;
                               1833                 :                :         }
                               1834         [ +  + ]:           3102 :         else if (frameOptions & FRAMEOPTION_RANGE)
                               1835                 :                :         {
                               1836                 :                :             /*
                               1837                 :                :              * In RANGE END_OFFSET mode, frame end is the last row that
                               1838                 :                :              * satisfies the in_range constraint relative to the current row,
                               1839                 :                :              * frame tail is the row after that (if any).  We keep a copy of
                               1840                 :                :              * the last-known frame tail row in frametail_slot, and advance as
                               1841                 :                :              * necessary.  Note that if we reach end of partition, we will
                               1842                 :                :              * leave frametailpos = end+1 and frametail_slot empty.
                               1843                 :                :              */
 2242                          1844                 :           2442 :             int         sortCol = node->ordColIdx[0];
                               1845                 :                :             bool        sub,
                               1846                 :                :                         less;
                               1847                 :                : 
                               1848                 :                :             /* We must have an ordering column */
 2104                          1849         [ -  + ]:           2442 :             Assert(node->ordNumCols == 1);
                               1850                 :                : 
                               1851                 :                :             /* Precompute flags for in_range checks */
 2258                          1852         [ +  + ]:           2442 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
                               1853                 :            456 :                 sub = true;     /* subtract endOffset from current row */
                               1854                 :                :             else
                               1855                 :           1986 :                 sub = false;    /* add it */
                               1856                 :           2442 :             less = true;        /* normally, we want frame tail <= sum */
                               1857                 :                :             /* If sort order is descending, flip both flags */
                               1858         [ +  + ]:           2442 :             if (!winstate->inRangeAsc)
                               1859                 :                :             {
                               1860                 :            345 :                 sub = !sub;
                               1861                 :            345 :                 less = false;
                               1862                 :                :             }
                               1863                 :                : 
                               1864                 :           2442 :             tuplestore_select_read_pointer(winstate->buffer,
                               1865                 :                :                                            winstate->frametail_ptr);
                               1866         [ +  + ]:           2442 :             if (winstate->frametailpos == 0 &&
                               1867   [ +  -  +  + ]:            411 :                 TupIsNull(winstate->frametail_slot))
                               1868                 :                :             {
                               1869                 :                :                 /* fetch first row into frametail_slot, if we didn't already */
                               1870         [ -  + ]:            294 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1871                 :                :                                              winstate->frametail_slot))
 2258 tgl@sss.pgh.pa.us        1872         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1873                 :                :             }
                               1874                 :                : 
 2258 tgl@sss.pgh.pa.us        1875   [ +  -  +  + ]:CBC        4503 :             while (!TupIsNull(winstate->frametail_slot))
                               1876                 :                :             {
                               1877                 :                :                 Datum       tailval,
                               1878                 :                :                             currval;
                               1879                 :                :                 bool        tailisnull,
                               1880                 :                :                             currisnull;
                               1881                 :                : 
 2242                          1882                 :           3720 :                 tailval = slot_getattr(winstate->frametail_slot, sortCol,
                               1883                 :                :                                        &tailisnull);
                               1884                 :           3720 :                 currval = slot_getattr(winstate->ss.ss_ScanTupleSlot, sortCol,
                               1885                 :                :                                        &currisnull);
 2258                          1886   [ +  +  +  + ]:           3720 :                 if (tailisnull || currisnull)
                               1887                 :                :                 {
                               1888                 :                :                     /* order of the rows depends only on nulls_first */
                               1889         [ +  + ]:             54 :                     if (winstate->inRangeNullsFirst)
                               1890                 :                :                     {
                               1891                 :                :                         /* advance tail if tail is null or curr is not */
                               1892         [ +  + ]:             24 :                         if (!tailisnull)
                               1893                 :           1635 :                             break;
                               1894                 :                :                     }
                               1895                 :                :                     else
                               1896                 :                :                     {
                               1897                 :                :                         /* advance tail if tail is not null or curr is null */
                               1898         [ +  + ]:             30 :                         if (!currisnull)
                               1899                 :             18 :                             break;
                               1900                 :                :                     }
                               1901                 :                :                 }
                               1902                 :                :                 else
                               1903                 :                :                 {
                               1904         [ +  + ]:           3666 :                     if (!DatumGetBool(FunctionCall5Coll(&winstate->endInRangeFunc,
                               1905                 :                :                                                         winstate->inRangeColl,
                               1906                 :                :                                                         tailval,
                               1907                 :                :                                                         currval,
                               1908                 :                :                                                         winstate->endOffsetValue,
                               1909                 :                :                                                         BoolGetDatum(sub),
                               1910                 :                :                                                         BoolGetDatum(less))))
                               1911                 :           1365 :                         break;  /* this row is the correct frame tail */
                               1912                 :                :                 }
                               1913                 :                :                 /* Note we advance frametailpos even if the fetch fails */
                               1914                 :           2301 :                 winstate->frametailpos++;
                               1915                 :           2301 :                 spool_tuples(winstate, winstate->frametailpos);
                               1916         [ +  + ]:           2301 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1917                 :                :                                              winstate->frametail_slot))
                               1918                 :            240 :                     break;      /* end of partition */
                               1919                 :                :             }
                               1920                 :           2418 :             winstate->frametail_valid = true;
                               1921                 :                :         }
                               1922         [ +  - ]:            660 :         else if (frameOptions & FRAMEOPTION_GROUPS)
                               1923                 :                :         {
                               1924                 :                :             /*
                               1925                 :                :              * In GROUPS END_OFFSET mode, frame end is the last row of the
                               1926                 :                :              * last peer group whose number satisfies the offset constraint,
                               1927                 :                :              * and frame tail is the row after that (if any).  We keep a copy
                               1928                 :                :              * of the last-known frame tail row in frametail_slot, and advance
                               1929                 :                :              * as necessary.  Note that if we reach end of partition, we will
                               1930                 :                :              * leave frametailpos = end+1 and frametail_slot empty.
                               1931                 :                :              */
                               1932                 :            660 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
                               1933                 :                :             int64       maxtailgroup;
                               1934                 :                : 
                               1935         [ +  + ]:            660 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
                               1936                 :             36 :                 maxtailgroup = winstate->currentgroup - offset;
                               1937                 :                :             else
                               1938                 :            624 :                 maxtailgroup = winstate->currentgroup + offset;
                               1939                 :                : 
                               1940                 :            660 :             tuplestore_select_read_pointer(winstate->buffer,
                               1941                 :                :                                            winstate->frametail_ptr);
                               1942         [ +  + ]:            660 :             if (winstate->frametailpos == 0 &&
                               1943   [ +  -  +  + ]:            192 :                 TupIsNull(winstate->frametail_slot))
                               1944                 :                :             {
                               1945                 :                :                 /* fetch first row into frametail_slot, if we didn't already */
                               1946         [ -  + ]:            183 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1947                 :                :                                              winstate->frametail_slot))
 2258 tgl@sss.pgh.pa.us        1948         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1949                 :                :             }
                               1950                 :                : 
 2258 tgl@sss.pgh.pa.us        1951   [ +  -  +  + ]:CBC        1794 :             while (!TupIsNull(winstate->frametail_slot))
                               1952                 :                :             {
                               1953         [ +  + ]:           1020 :                 if (winstate->frametailgroup > maxtailgroup)
                               1954                 :            372 :                     break;      /* this row is the correct frame tail */
                               1955                 :            648 :                 ExecCopySlot(winstate->temp_slot_2, winstate->frametail_slot);
                               1956                 :                :                 /* Note we advance frametailpos even if the fetch fails */
                               1957                 :            648 :                 winstate->frametailpos++;
                               1958                 :            648 :                 spool_tuples(winstate, winstate->frametailpos);
                               1959         [ +  + ]:            648 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1960                 :                :                                              winstate->frametail_slot))
                               1961                 :            174 :                     break;      /* end of partition */
                               1962         [ +  + ]:            474 :                 if (!are_peers(winstate, winstate->temp_slot_2,
                               1963                 :                :                                winstate->frametail_slot))
                               1964                 :            300 :                     winstate->frametailgroup++;
                               1965                 :                :             }
                               1966                 :            660 :             ExecClearTuple(winstate->temp_slot_2);
                               1967                 :            660 :             winstate->frametail_valid = true;
                               1968                 :                :         }
                               1969                 :                :         else
 5175 tgl@sss.pgh.pa.us        1970                 :UBC           0 :             Assert(false);
                               1971                 :                :     }
                               1972                 :                :     else
                               1973                 :              0 :         Assert(false);
                               1974                 :                : 
 2258 tgl@sss.pgh.pa.us        1975                 :CBC       65380 :     MemoryContextSwitchTo(oldcontext);
                               1976                 :                : }
                               1977                 :                : 
                               1978                 :                : /*
                               1979                 :                :  * update_grouptailpos
                               1980                 :                :  * make grouptailpos valid for the current row
                               1981                 :                :  *
                               1982                 :                :  * May clobber winstate->temp_slot_2.
                               1983                 :                :  */
                               1984                 :                : static void
                               1985                 :           2436 : update_grouptailpos(WindowAggState *winstate)
                               1986                 :                : {
                               1987                 :           2436 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
                               1988                 :                :     MemoryContext oldcontext;
                               1989                 :                : 
                               1990         [ +  + ]:           2436 :     if (winstate->grouptail_valid)
                               1991                 :           1977 :         return;                 /* already known for current row */
                               1992                 :                : 
                               1993                 :                :     /* We may be called in a short-lived context */
                               1994                 :            459 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               1995                 :                : 
                               1996                 :                :     /* If no ORDER BY, all rows are peers with each other */
                               1997         [ -  + ]:            459 :     if (node->ordNumCols == 0)
                               1998                 :                :     {
 2258 tgl@sss.pgh.pa.us        1999                 :UBC           0 :         spool_tuples(winstate, -1);
                               2000                 :              0 :         winstate->grouptailpos = winstate->spooled_rows;
                               2001                 :              0 :         winstate->grouptail_valid = true;
                               2002                 :              0 :         MemoryContextSwitchTo(oldcontext);
                               2003                 :              0 :         return;
                               2004                 :                :     }
                               2005                 :                : 
                               2006                 :                :     /*
                               2007                 :                :      * Because grouptail_valid is reset only when current row advances into a
                               2008                 :                :      * new peer group, we always reach here knowing that grouptailpos needs to
                               2009                 :                :      * be advanced by at least one row.  Hence, unlike the otherwise similar
                               2010                 :                :      * case for frame tail tracking, we do not need persistent storage of the
                               2011                 :                :      * group tail row.
                               2012                 :                :      */
 2258 tgl@sss.pgh.pa.us        2013         [ -  + ]:CBC         459 :     Assert(winstate->grouptailpos <= winstate->currentpos);
                               2014                 :            459 :     tuplestore_select_read_pointer(winstate->buffer,
                               2015                 :                :                                    winstate->grouptail_ptr);
                               2016                 :                :     for (;;)
                               2017                 :                :     {
                               2018                 :                :         /* Note we advance grouptailpos even if the fetch fails */
                               2019                 :            879 :         winstate->grouptailpos++;
                               2020                 :            879 :         spool_tuples(winstate, winstate->grouptailpos);
                               2021         [ +  + ]:            879 :         if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               2022                 :                :                                      winstate->temp_slot_2))
                               2023                 :            129 :             break;              /* end of partition */
                               2024         [ +  + ]:            750 :         if (winstate->grouptailpos > winstate->currentpos &&
                               2025         [ +  + ]:            621 :             !are_peers(winstate, winstate->temp_slot_2,
                               2026                 :                :                        winstate->ss.ss_ScanTupleSlot))
                               2027                 :            330 :             break;              /* this row is the group tail */
                               2028                 :                :     }
                               2029                 :            459 :     ExecClearTuple(winstate->temp_slot_2);
                               2030                 :            459 :     winstate->grouptail_valid = true;
                               2031                 :                : 
                               2032                 :            459 :     MemoryContextSwitchTo(oldcontext);
                               2033                 :                : }
                               2034                 :                : 
                               2035                 :                : 
                               2036                 :                : /* -----------------
                               2037                 :                :  * ExecWindowAgg
                               2038                 :                :  *
                               2039                 :                :  *  ExecWindowAgg receives tuples from its outer subplan and
                               2040                 :                :  *  stores them into a tuplestore, then processes window functions.
                               2041                 :                :  *  This node doesn't reduce nor qualify any row so the number of
                               2042                 :                :  *  returned rows is exactly the same as its outer subplan's result.
                               2043                 :                :  * -----------------
                               2044                 :                :  */
                               2045                 :                : static TupleTableSlot *
 2463 andres@anarazel.de       2046                 :         411093 : ExecWindowAgg(PlanState *pstate)
                               2047                 :                : {
                               2048                 :         411093 :     WindowAggState *winstate = castNode(WindowAggState, pstate);
                               2049                 :                :     TupleTableSlot *slot;
                               2050                 :                :     ExprContext *econtext;
                               2051                 :                :     int         i;
                               2052                 :                :     int         numfuncs;
                               2053                 :                : 
 2455                          2054         [ -  + ]:         411093 :     CHECK_FOR_INTERRUPTS();
                               2055                 :                : 
  737 drowley@postgresql.o     2056         [ -  + ]:         411093 :     if (winstate->status == WINDOWAGG_DONE)
 5586 tgl@sss.pgh.pa.us        2057                 :UBC           0 :         return NULL;
                               2058                 :                : 
                               2059                 :                :     /*
                               2060                 :                :      * Compute frame offset values, if any, during first call (or after a
                               2061                 :                :      * rescan).  These are assumed to hold constant throughout the scan; if
                               2062                 :                :      * user gives us a volatile expression, we'll only use its initial value.
                               2063                 :                :      */
 5175 tgl@sss.pgh.pa.us        2064         [ +  + ]:CBC      411093 :     if (winstate->all_first)
                               2065                 :                :     {
 5161 bruce@momjian.us         2066                 :           1066 :         int         frameOptions = winstate->frameOptions;
                               2067                 :                :         Datum       value;
                               2068                 :                :         bool        isnull;
                               2069                 :                :         int16       len;
                               2070                 :                :         bool        byval;
                               2071                 :                : 
  597 drowley@postgresql.o     2072                 :           1066 :         econtext = winstate->ss.ps.ps_ExprContext;
                               2073                 :                : 
 2258 tgl@sss.pgh.pa.us        2074         [ +  + ]:           1066 :         if (frameOptions & FRAMEOPTION_START_OFFSET)
                               2075                 :                :         {
 5175                          2076         [ -  + ]:            408 :             Assert(winstate->startOffset != NULL);
                               2077                 :            408 :             value = ExecEvalExprSwitchContext(winstate->startOffset,
                               2078                 :                :                                               econtext,
                               2079                 :                :                                               &isnull);
                               2080         [ -  + ]:            408 :             if (isnull)
 5175 tgl@sss.pgh.pa.us        2081         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2082                 :                :                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                               2083                 :                :                          errmsg("frame starting offset must not be null")));
                               2084                 :                :             /* copy value into query-lifespan context */
 5175 tgl@sss.pgh.pa.us        2085                 :CBC         408 :             get_typlenbyval(exprType((Node *) winstate->startOffset->expr),
                               2086                 :                :                             &len, &byval);
                               2087                 :            408 :             winstate->startOffsetValue = datumCopy(value, byval, len);
 2258                          2088         [ +  + ]:            408 :             if (frameOptions & (FRAMEOPTION_ROWS | FRAMEOPTION_GROUPS))
                               2089                 :                :             {
                               2090                 :                :                 /* value is known to be int8 */
 5161 bruce@momjian.us         2091                 :            150 :                 int64       offset = DatumGetInt64(value);
                               2092                 :                : 
 5175 tgl@sss.pgh.pa.us        2093         [ -  + ]:            150 :                 if (offset < 0)
 5175 tgl@sss.pgh.pa.us        2094         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2095                 :                :                             (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
                               2096                 :                :                              errmsg("frame starting offset must not be negative")));
                               2097                 :                :             }
                               2098                 :                :         }
 2258 tgl@sss.pgh.pa.us        2099         [ +  + ]:CBC        1066 :         if (frameOptions & FRAMEOPTION_END_OFFSET)
                               2100                 :                :         {
 5175                          2101         [ -  + ]:            456 :             Assert(winstate->endOffset != NULL);
                               2102                 :            456 :             value = ExecEvalExprSwitchContext(winstate->endOffset,
                               2103                 :                :                                               econtext,
                               2104                 :                :                                               &isnull);
                               2105         [ -  + ]:            456 :             if (isnull)
 5175 tgl@sss.pgh.pa.us        2106         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2107                 :                :                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                               2108                 :                :                          errmsg("frame ending offset must not be null")));
                               2109                 :                :             /* copy value into query-lifespan context */
 5175 tgl@sss.pgh.pa.us        2110                 :CBC         456 :             get_typlenbyval(exprType((Node *) winstate->endOffset->expr),
                               2111                 :                :                             &len, &byval);
                               2112                 :            456 :             winstate->endOffsetValue = datumCopy(value, byval, len);
 2258                          2113         [ +  + ]:            456 :             if (frameOptions & (FRAMEOPTION_ROWS | FRAMEOPTION_GROUPS))
                               2114                 :                :             {
                               2115                 :                :                 /* value is known to be int8 */
 5161 bruce@momjian.us         2116                 :            165 :                 int64       offset = DatumGetInt64(value);
                               2117                 :                : 
 5175 tgl@sss.pgh.pa.us        2118         [ -  + ]:            165 :                 if (offset < 0)
 5175 tgl@sss.pgh.pa.us        2119         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2120                 :                :                             (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
                               2121                 :                :                              errmsg("frame ending offset must not be negative")));
                               2122                 :                :             }
                               2123                 :                :         }
 5175 tgl@sss.pgh.pa.us        2124                 :CBC        1066 :         winstate->all_first = false;
                               2125                 :                :     }
                               2126                 :                : 
                               2127                 :                :     /* We need to loop as the runCondition or qual may filter out tuples */
                               2128                 :                :     for (;;)
                               2129                 :                :     {
  737 drowley@postgresql.o     2130         [ +  + ]:         411129 :         if (winstate->buffer == NULL)
                               2131                 :                :         {
                               2132                 :                :             /* Initialize for first partition and set current row = 0 */
                               2133                 :           1066 :             begin_partition(winstate);
                               2134                 :                :             /* If there are no input rows, we'll detect that and exit below */
                               2135                 :                :         }
                               2136                 :                :         else
                               2137                 :                :         {
                               2138                 :                :             /* Advance current row within partition */
                               2139                 :         410063 :             winstate->currentpos++;
                               2140                 :                :             /* This might mean that the frame moves, too */
                               2141                 :         410063 :             winstate->framehead_valid = false;
                               2142                 :         410063 :             winstate->frametail_valid = false;
                               2143                 :                :             /* we don't need to invalidate grouptail here; see below */
                               2144                 :                :         }
                               2145                 :                : 
                               2146                 :                :         /*
                               2147                 :                :          * Spool all tuples up to and including the current row, if we haven't
                               2148                 :                :          * already
                               2149                 :                :          */
                               2150                 :         411129 :         spool_tuples(winstate, winstate->currentpos);
                               2151                 :                : 
                               2152                 :                :         /* Move to the next partition if we reached the end of this partition */
                               2153         [ +  + ]:         411129 :         if (winstate->partition_spooled &&
                               2154         [ +  + ]:          16227 :             winstate->currentpos >= winstate->spooled_rows)
                               2155                 :                :         {
                               2156                 :           1580 :             release_partition(winstate);
                               2157                 :                : 
                               2158         [ +  + ]:           1580 :             if (winstate->more_partitions)
                               2159                 :                :             {
                               2160                 :            604 :                 begin_partition(winstate);
                               2161         [ -  + ]:            604 :                 Assert(winstate->spooled_rows > 0);
                               2162                 :                : 
                               2163                 :                :                 /* Come out of pass-through mode when changing partition */
                               2164                 :            604 :                 winstate->status = WINDOWAGG_RUN;
                               2165                 :                :             }
                               2166                 :                :             else
                               2167                 :                :             {
                               2168                 :                :                 /* No further partitions?  We're done */
                               2169                 :            976 :                 winstate->status = WINDOWAGG_DONE;
                               2170                 :            976 :                 return NULL;
                               2171                 :                :             }
                               2172                 :                :         }
                               2173                 :                : 
                               2174                 :                :         /* final output execution is in ps_ExprContext */
                               2175                 :         410153 :         econtext = winstate->ss.ps.ps_ExprContext;
                               2176                 :                : 
                               2177                 :                :         /* Clear the per-output-tuple context for current row */
                               2178                 :         410153 :         ResetExprContext(econtext);
                               2179                 :                : 
                               2180                 :                :         /*
                               2181                 :                :          * Read the current row from the tuplestore, and save in
                               2182                 :                :          * ScanTupleSlot. (We can't rely on the outerplan's output slot
                               2183                 :                :          * because we may have to read beyond the current row.  Also, we have
                               2184                 :                :          * to actually copy the row out of the tuplestore, since window
                               2185                 :                :          * function evaluation might cause the tuplestore to dump its state to
                               2186                 :                :          * disk.)
                               2187                 :                :          *
                               2188                 :                :          * In GROUPS mode, or when tracking a group-oriented exclusion clause,
                               2189                 :                :          * we must also detect entering a new peer group and update associated
                               2190                 :                :          * state when that happens.  We use temp_slot_2 to temporarily hold
                               2191                 :                :          * the previous row for this purpose.
                               2192                 :                :          *
                               2193                 :                :          * Current row must be in the tuplestore, since we spooled it above.
                               2194                 :                :          */
                               2195                 :         410153 :         tuplestore_select_read_pointer(winstate->buffer, winstate->current_ptr);
                               2196         [ +  + ]:         410153 :         if ((winstate->frameOptions & (FRAMEOPTION_GROUPS |
                               2197                 :                :                                        FRAMEOPTION_EXCLUDE_GROUP |
                               2198                 :           1449 :                                        FRAMEOPTION_EXCLUDE_TIES)) &&
                               2199         [ +  + ]:           1449 :             winstate->currentpos > 0)
                               2200                 :                :         {
                               2201                 :           1179 :             ExecCopySlot(winstate->temp_slot_2, winstate->ss.ss_ScanTupleSlot);
                               2202         [ -  + ]:           1179 :             if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               2203                 :                :                                          winstate->ss.ss_ScanTupleSlot))
  737 drowley@postgresql.o     2204         [ #  # ]:UBC           0 :                 elog(ERROR, "unexpected end of tuplestore");
  737 drowley@postgresql.o     2205         [ +  + ]:CBC        1179 :             if (!are_peers(winstate, winstate->temp_slot_2,
                               2206                 :                :                            winstate->ss.ss_ScanTupleSlot))
                               2207                 :                :             {
                               2208                 :            621 :                 winstate->currentgroup++;
                               2209                 :            621 :                 winstate->groupheadpos = winstate->currentpos;
                               2210                 :            621 :                 winstate->grouptail_valid = false;
                               2211                 :                :             }
                               2212                 :           1179 :             ExecClearTuple(winstate->temp_slot_2);
                               2213                 :                :         }
                               2214                 :                :         else
                               2215                 :                :         {
                               2216         [ -  + ]:         408974 :             if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               2217                 :                :                                          winstate->ss.ss_ScanTupleSlot))
  737 drowley@postgresql.o     2218         [ #  # ]:UBC           0 :                 elog(ERROR, "unexpected end of tuplestore");
                               2219                 :                :         }
                               2220                 :                : 
                               2221                 :                :         /* don't evaluate the window functions when we're in pass-through mode */
  737 drowley@postgresql.o     2222         [ +  + ]:CBC      410153 :         if (winstate->status == WINDOWAGG_RUN)
                               2223                 :                :         {
                               2224                 :                :             /*
                               2225                 :                :              * Evaluate true window functions
                               2226                 :                :              */
                               2227                 :         410126 :             numfuncs = winstate->numfuncs;
                               2228         [ +  + ]:         883450 :             for (i = 0; i < numfuncs; i++)
                               2229                 :                :             {
                               2230                 :         473369 :                 WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
                               2231                 :                : 
                               2232         [ +  + ]:         473369 :                 if (perfuncstate->plain_agg)
                               2233                 :          65939 :                     continue;
                               2234                 :         407430 :                 eval_windowfunction(winstate, perfuncstate,
                               2235                 :         407430 :                                     &(econtext->ecxt_aggvalues[perfuncstate->wfuncstate->wfuncno]),
                               2236                 :         407430 :                                     &(econtext->ecxt_aggnulls[perfuncstate->wfuncstate->wfuncno]));
                               2237                 :                :             }
                               2238                 :                : 
                               2239                 :                :             /*
                               2240                 :                :              * Evaluate aggregates
                               2241                 :                :              */
                               2242         [ +  + ]:         410081 :             if (winstate->numaggs > 0)
                               2243                 :          65054 :                 eval_windowaggregates(winstate);
                               2244                 :                :         }
                               2245                 :                : 
                               2246                 :                :         /*
                               2247                 :                :          * If we have created auxiliary read pointers for the frame or group
                               2248                 :                :          * boundaries, force them to be kept up-to-date, because we don't know
                               2249                 :                :          * whether the window function(s) will do anything that requires that.
                               2250                 :                :          * Failing to advance the pointers would result in being unable to
                               2251                 :                :          * trim data from the tuplestore, which is bad.  (If we could know in
                               2252                 :                :          * advance whether the window functions will use frame boundary info,
                               2253                 :                :          * we could skip creating these pointers in the first place ... but
                               2254                 :                :          * unfortunately the window function API doesn't require that.)
                               2255                 :                :          */
                               2256         [ +  + ]:         410087 :         if (winstate->framehead_ptr >= 0)
                               2257                 :           3118 :             update_frameheadpos(winstate);
                               2258         [ +  + ]:         410087 :         if (winstate->frametail_ptr >= 0)
                               2259                 :          65140 :             update_frametailpos(winstate);
                               2260         [ +  + ]:         410087 :         if (winstate->grouptail_ptr >= 0)
                               2261                 :            750 :             update_grouptailpos(winstate);
                               2262                 :                : 
                               2263                 :                :         /*
                               2264                 :                :          * Truncate any no-longer-needed rows from the tuplestore.
                               2265                 :                :          */
                               2266                 :         410087 :         tuplestore_trim(winstate->buffer);
                               2267                 :                : 
                               2268                 :                :         /*
                               2269                 :                :          * Form and return a projection tuple using the windowfunc results and
                               2270                 :                :          * the current row.  Setting ecxt_outertuple arranges that any Vars
                               2271                 :                :          * will be evaluated with respect to that row.
                               2272                 :                :          */
                               2273                 :         410087 :         econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
                               2274                 :                : 
                               2275                 :         410087 :         slot = ExecProject(winstate->ss.ps.ps_ProjInfo);
                               2276                 :                : 
                               2277         [ +  + ]:         410087 :         if (winstate->status == WINDOWAGG_RUN)
                               2278                 :                :         {
                               2279                 :         410060 :             econtext->ecxt_scantuple = slot;
                               2280                 :                : 
                               2281                 :                :             /*
                               2282                 :                :              * Now evaluate the run condition to see if we need to go into
                               2283                 :                :              * pass-through mode, or maybe stop completely.
                               2284                 :                :              */
                               2285         [ +  + ]:         410060 :             if (!ExecQual(winstate->runcondition, econtext))
                               2286                 :                :             {
                               2287                 :                :                 /*
                               2288                 :                :                  * Determine which mode to move into.  If there is no
                               2289                 :                :                  * PARTITION BY clause and we're the top-level WindowAgg then
                               2290                 :                :                  * we're done.  This tuple and any future tuples cannot
                               2291                 :                :                  * possibly match the runcondition.  However, when there is a
                               2292                 :                :                  * PARTITION BY clause or we're not the top-level window we
                               2293                 :                :                  * can't just stop as we need to either process other
                               2294                 :                :                  * partitions or ensure WindowAgg nodes above us receive all
                               2295                 :                :                  * of the tuples they need to process their WindowFuncs.
                               2296                 :                :                  */
                               2297         [ +  + ]:             42 :                 if (winstate->use_pass_through)
                               2298                 :                :                 {
                               2299                 :                :                     /*
                               2300                 :                :                      * STRICT pass-through mode is required for the top window
                               2301                 :                :                      * when there is a PARTITION BY clause.  Otherwise we must
                               2302                 :                :                      * ensure we store tuples that don't match the
                               2303                 :                :                      * runcondition so they're available to WindowAggs above.
                               2304                 :                :                      */
                               2305         [ +  + ]:             21 :                     if (winstate->top_window)
                               2306                 :                :                     {
                               2307                 :             12 :                         winstate->status = WINDOWAGG_PASSTHROUGH_STRICT;
                               2308                 :             12 :                         continue;
                               2309                 :                :                     }
                               2310                 :                :                     else
                               2311                 :                :                     {
                               2312                 :              9 :                         winstate->status = WINDOWAGG_PASSTHROUGH;
                               2313                 :                : 
                               2314                 :                :                         /*
                               2315                 :                :                          * If we're not the top-window, we'd better NULLify
                               2316                 :                :                          * the aggregate results.  In pass-through mode we no
                               2317                 :                :                          * longer update these and this avoids the old stale
                               2318                 :                :                          * results lingering.  Some of these might be byref
                               2319                 :                :                          * types so we can't have them pointing to free'd
                               2320                 :                :                          * memory.  The planner insisted that quals used in
                               2321                 :                :                          * the runcondition are strict, so the top-level
                               2322                 :                :                          * WindowAgg will filter these NULLs out in the filter
                               2323                 :                :                          * clause.
                               2324                 :                :                          */
  494                          2325                 :              9 :                         numfuncs = winstate->numfuncs;
                               2326         [ +  + ]:             36 :                         for (i = 0; i < numfuncs; i++)
                               2327                 :                :                         {
                               2328                 :             27 :                             econtext->ecxt_aggvalues[i] = (Datum) 0;
                               2329                 :             27 :                             econtext->ecxt_aggnulls[i] = true;
                               2330                 :                :                         }
                               2331                 :                :                     }
                               2332                 :                :                 }
                               2333                 :                :                 else
                               2334                 :                :                 {
                               2335                 :                :                     /*
                               2336                 :                :                      * Pass-through not required.  We can just return NULL.
                               2337                 :                :                      * Nothing else will match the runcondition.
                               2338                 :                :                      */
  737                          2339                 :             21 :                     winstate->status = WINDOWAGG_DONE;
                               2340                 :             21 :                     return NULL;
                               2341                 :                :                 }
                               2342                 :                :             }
                               2343                 :                : 
                               2344                 :                :             /*
                               2345                 :                :              * Filter out any tuples we don't need in the top-level WindowAgg.
                               2346                 :                :              */
                               2347         [ +  + ]:         410027 :             if (!ExecQual(winstate->ss.ps.qual, econtext))
                               2348                 :                :             {
                               2349         [ -  + ]:              9 :                 InstrCountFiltered1(winstate, 1);
                               2350                 :              9 :                 continue;
                               2351                 :                :             }
                               2352                 :                : 
                               2353                 :         410018 :             break;
                               2354                 :                :         }
                               2355                 :                : 
                               2356                 :                :         /*
                               2357                 :                :          * When not in WINDOWAGG_RUN mode, we must still return this tuple if
                               2358                 :                :          * we're anything apart from the top window.
                               2359                 :                :          */
                               2360         [ +  + ]:             27 :         else if (!winstate->top_window)
                               2361                 :             12 :             break;
                               2362                 :                :     }
                               2363                 :                : 
                               2364                 :         410030 :     return slot;
                               2365                 :                : }
                               2366                 :                : 
                               2367                 :                : /* -----------------
                               2368                 :                :  * ExecInitWindowAgg
                               2369                 :                :  *
                               2370                 :                :  *  Creates the run-time information for the WindowAgg node produced by the
                               2371                 :                :  *  planner and initializes its outer subtree
                               2372                 :                :  * -----------------
                               2373                 :                :  */
                               2374                 :                : WindowAggState *
 5586 tgl@sss.pgh.pa.us        2375                 :           1222 : ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags)
                               2376                 :                : {
                               2377                 :                :     WindowAggState *winstate;
                               2378                 :                :     Plan       *outerPlan;
                               2379                 :                :     ExprContext *econtext;
                               2380                 :                :     ExprContext *tmpcontext;
                               2381                 :                :     WindowStatePerFunc perfunc;
                               2382                 :                :     WindowStatePerAgg peragg;
 2258                          2383                 :           1222 :     int         frameOptions = node->frameOptions;
                               2384                 :                :     int         numfuncs,
                               2385                 :                :                 wfuncno,
                               2386                 :                :                 numaggs,
                               2387                 :                :                 aggno;
                               2388                 :                :     TupleDesc   scanDesc;
                               2389                 :                :     ListCell   *l;
                               2390                 :                : 
                               2391                 :                :     /* check for unsupported flags */
 5586                          2392         [ -  + ]:           1222 :     Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
                               2393                 :                : 
                               2394                 :                :     /*
                               2395                 :                :      * create state structure
                               2396                 :                :      */
                               2397                 :           1222 :     winstate = makeNode(WindowAggState);
                               2398                 :           1222 :     winstate->ss.ps.plan = (Plan *) node;
                               2399                 :           1222 :     winstate->ss.ps.state = estate;
 2463 andres@anarazel.de       2400                 :           1222 :     winstate->ss.ps.ExecProcNode = ExecWindowAgg;
                               2401                 :                : 
                               2402                 :                :     /* copy frame options to state node for easy access */
   18 tgl@sss.pgh.pa.us        2403                 :           1222 :     winstate->frameOptions = frameOptions;
                               2404                 :                : 
                               2405                 :                :     /*
                               2406                 :                :      * Create expression contexts.  We need two, one for per-input-tuple
                               2407                 :                :      * processing and one for per-output-tuple processing.  We cheat a little
                               2408                 :                :      * by using ExecAssignExprContext() to build both.
                               2409                 :                :      */
 5586                          2410                 :           1222 :     ExecAssignExprContext(estate, &winstate->ss.ps);
                               2411                 :           1222 :     tmpcontext = winstate->ss.ps.ps_ExprContext;
                               2412                 :           1222 :     winstate->tmpcontext = tmpcontext;
                               2413                 :           1222 :     ExecAssignExprContext(estate, &winstate->ss.ps);
                               2414                 :                : 
                               2415                 :                :     /* Create long-lived context for storage of partition-local memory etc */
 5175                          2416                 :           1222 :     winstate->partcontext =
 5586                          2417                 :           1222 :         AllocSetContextCreate(CurrentMemoryContext,
                               2418                 :                :                               "WindowAgg Partition",
                               2419                 :                :                               ALLOCSET_DEFAULT_SIZES);
                               2420                 :                : 
                               2421                 :                :     /*
                               2422                 :                :      * Create mid-lived context for aggregate trans values etc.
                               2423                 :                :      *
                               2424                 :                :      * Note that moving aggregates each use their own private context, not
                               2425                 :                :      * this one.
                               2426                 :                :      */
 5175                          2427                 :           1222 :     winstate->aggcontext =
                               2428                 :           1222 :         AllocSetContextCreate(CurrentMemoryContext,
                               2429                 :                :                               "WindowAgg Aggregates",
                               2430                 :                :                               ALLOCSET_DEFAULT_SIZES);
                               2431                 :                : 
                               2432                 :                :     /* Only the top-level WindowAgg may have a qual */
  737 drowley@postgresql.o     2433   [ +  +  -  + ]:           1222 :     Assert(node->plan.qual == NIL || node->topWindow);
                               2434                 :                : 
                               2435                 :                :     /* Initialize the qual */
                               2436                 :           1222 :     winstate->ss.ps.qual = ExecInitQual(node->plan.qual,
                               2437                 :                :                                         (PlanState *) winstate);
                               2438                 :                : 
                               2439                 :                :     /*
                               2440                 :                :      * Setup the run condition, if we received one from the query planner.
                               2441                 :                :      * When set, this may allow us to move into pass-through mode so that we
                               2442                 :                :      * don't have to perform any further evaluation of WindowFuncs in the
                               2443                 :                :      * current partition or possibly stop returning tuples altogether when all
                               2444                 :                :      * tuples are in the same partition.
                               2445                 :                :      */
                               2446                 :           1222 :     winstate->runcondition = ExecInitQual(node->runCondition,
                               2447                 :                :                                           (PlanState *) winstate);
                               2448                 :                : 
                               2449                 :                :     /*
                               2450                 :                :      * When we're not the top-level WindowAgg node or we are but have a
                               2451                 :                :      * PARTITION BY clause we must move into one of the WINDOWAGG_PASSTHROUGH*
                               2452                 :                :      * modes when the runCondition becomes false.
                               2453                 :                :      */
                               2454   [ +  +  +  + ]:           1222 :     winstate->use_pass_through = !node->topWindow || node->partNumCols > 0;
                               2455                 :                : 
                               2456                 :                :     /* remember if we're the top-window or we are below the top-window */
                               2457                 :           1222 :     winstate->top_window = node->topWindow;
                               2458                 :                : 
                               2459                 :                :     /*
                               2460                 :                :      * initialize child nodes
                               2461                 :                :      */
 5586 tgl@sss.pgh.pa.us        2462                 :           1222 :     outerPlan = outerPlan(node);
                               2463                 :           1222 :     outerPlanState(winstate) = ExecInitNode(outerPlan, estate, eflags);
                               2464                 :                : 
                               2465                 :                :     /*
                               2466                 :                :      * initialize source tuple type (which is also the tuple type that we'll
                               2467                 :                :      * store in the tuplestore and use in all our working slots).
                               2468                 :                :      */
 1977 andres@anarazel.de       2469                 :           1222 :     ExecCreateScanSlotFromOuterPlan(estate, &winstate->ss, &TTSOpsMinimalTuple);
 2250                          2470                 :           1222 :     scanDesc = winstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
                               2471                 :                : 
                               2472                 :                :     /* the outer tuple isn't the child's tuple, but always a minimal tuple */
 1977                          2473                 :           1222 :     winstate->ss.ps.outeropsset = true;
                               2474                 :           1222 :     winstate->ss.ps.outerops = &TTSOpsMinimalTuple;
                               2475                 :           1222 :     winstate->ss.ps.outeropsfixed = true;
                               2476                 :                : 
                               2477                 :                :     /*
                               2478                 :                :      * tuple table initialization
                               2479                 :                :      */
                               2480                 :           1222 :     winstate->first_part_slot = ExecInitExtraTupleSlot(estate, scanDesc,
                               2481                 :                :                                                        &TTSOpsMinimalTuple);
                               2482                 :           1222 :     winstate->agg_row_slot = ExecInitExtraTupleSlot(estate, scanDesc,
                               2483                 :                :                                                     &TTSOpsMinimalTuple);
                               2484                 :           1222 :     winstate->temp_slot_1 = ExecInitExtraTupleSlot(estate, scanDesc,
                               2485                 :                :                                                    &TTSOpsMinimalTuple);
                               2486                 :           1222 :     winstate->temp_slot_2 = ExecInitExtraTupleSlot(estate, scanDesc,
                               2487                 :                :                                                    &TTSOpsMinimalTuple);
                               2488                 :                : 
                               2489                 :                :     /*
                               2490                 :                :      * create frame head and tail slots only if needed (must create slots in
                               2491                 :                :      * exactly the same cases that update_frameheadpos and update_frametailpos
                               2492                 :                :      * need them)
                               2493                 :                :      */
 2249                          2494                 :           1222 :     winstate->framehead_slot = winstate->frametail_slot = NULL;
                               2495                 :                : 
                               2496         [ +  + ]:           1222 :     if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               2497                 :                :     {
 2104 tgl@sss.pgh.pa.us        2498         [ +  + ]:            724 :         if (((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
                               2499         [ -  + ]:             38 :              node->ordNumCols != 0) ||
                               2500         [ +  + ]:            686 :             (frameOptions & FRAMEOPTION_START_OFFSET))
 1977 andres@anarazel.de       2501                 :            371 :             winstate->framehead_slot = ExecInitExtraTupleSlot(estate, scanDesc,
                               2502                 :                :                                                               &TTSOpsMinimalTuple);
 2104 tgl@sss.pgh.pa.us        2503         [ +  + ]:            724 :         if (((frameOptions & FRAMEOPTION_END_CURRENT_ROW) &&
                               2504         [ +  + ]:            332 :              node->ordNumCols != 0) ||
                               2505         [ +  + ]:            503 :             (frameOptions & FRAMEOPTION_END_OFFSET))
 1977 andres@anarazel.de       2506                 :            584 :             winstate->frametail_slot = ExecInitExtraTupleSlot(estate, scanDesc,
                               2507                 :                :                                                               &TTSOpsMinimalTuple);
                               2508                 :                :     }
                               2509                 :                : 
                               2510                 :                :     /*
                               2511                 :                :      * Initialize result slot, type and projection.
                               2512                 :                :      */
                               2513                 :           1222 :     ExecInitResultTupleSlotTL(&winstate->ss.ps, &TTSOpsVirtual);
 5586 tgl@sss.pgh.pa.us        2514                 :           1222 :     ExecAssignProjectionInfo(&winstate->ss.ps, NULL);
                               2515                 :                : 
                               2516                 :                :     /* Set up data for comparing tuples */
                               2517         [ +  + ]:           1222 :     if (node->partNumCols > 0)
 2250 andres@anarazel.de       2518                 :            304 :         winstate->partEqfunction =
                               2519                 :            304 :             execTuplesMatchPrepare(scanDesc,
                               2520                 :                :                                    node->partNumCols,
                               2521                 :            304 :                                    node->partColIdx,
                               2522                 :            304 :                                    node->partOperators,
 1850 peter@eisentraut.org     2523                 :            304 :                                    node->partCollations,
                               2524                 :                :                                    &winstate->ss.ps);
                               2525                 :                : 
 5586 tgl@sss.pgh.pa.us        2526         [ +  + ]:           1222 :     if (node->ordNumCols > 0)
 2250 andres@anarazel.de       2527                 :           1033 :         winstate->ordEqfunction =
                               2528                 :           1033 :             execTuplesMatchPrepare(scanDesc,
                               2529                 :                :                                    node->ordNumCols,
                               2530                 :           1033 :                                    node->ordColIdx,
                               2531                 :           1033 :                                    node->ordOperators,
 1850 peter@eisentraut.org     2532                 :           1033 :                                    node->ordCollations,
                               2533                 :                :                                    &winstate->ss.ps);
                               2534                 :                : 
                               2535                 :                :     /*
                               2536                 :                :      * WindowAgg nodes use aggvalues and aggnulls as well as Agg nodes.
                               2537                 :                :      */
 5586 tgl@sss.pgh.pa.us        2538                 :           1222 :     numfuncs = winstate->numfuncs;
                               2539                 :           1222 :     numaggs = winstate->numaggs;
                               2540                 :           1222 :     econtext = winstate->ss.ps.ps_ExprContext;
                               2541                 :           1222 :     econtext->ecxt_aggvalues = (Datum *) palloc0(sizeof(Datum) * numfuncs);
                               2542                 :           1222 :     econtext->ecxt_aggnulls = (bool *) palloc0(sizeof(bool) * numfuncs);
                               2543                 :                : 
                               2544                 :                :     /*
                               2545                 :                :      * allocate per-wfunc/per-agg state information.
                               2546                 :                :      */
                               2547                 :           1222 :     perfunc = (WindowStatePerFunc) palloc0(sizeof(WindowStatePerFuncData) * numfuncs);
                               2548                 :           1222 :     peragg = (WindowStatePerAgg) palloc0(sizeof(WindowStatePerAggData) * numaggs);
                               2549                 :           1222 :     winstate->perfunc = perfunc;
                               2550                 :           1222 :     winstate->peragg = peragg;
                               2551                 :                : 
                               2552                 :           1222 :     wfuncno = -1;
                               2553                 :           1222 :     aggno = -1;
                               2554   [ +  -  +  +  :           2780 :     foreach(l, winstate->funcs)
                                              +  + ]
                               2555                 :                :     {
 5421 bruce@momjian.us         2556                 :           1558 :         WindowFuncExprState *wfuncstate = (WindowFuncExprState *) lfirst(l);
 2588 andres@anarazel.de       2557                 :           1558 :         WindowFunc *wfunc = wfuncstate->wfunc;
                               2558                 :                :         WindowStatePerFunc perfuncstate;
                               2559                 :                :         AclResult   aclresult;
                               2560                 :                :         int         i;
                               2561                 :                : 
 2489 tgl@sss.pgh.pa.us        2562         [ -  + ]:           1558 :         if (wfunc->winref != node->winref)    /* planner screwed up? */
 5583 tgl@sss.pgh.pa.us        2563         [ #  # ]:UBC           0 :             elog(ERROR, "WindowFunc with winref %u assigned to WindowAgg with winref %u",
                               2564                 :                :                  wfunc->winref, node->winref);
                               2565                 :                : 
                               2566                 :                :         /* Look for a previous duplicate window function */
 5586 tgl@sss.pgh.pa.us        2567         [ +  + ]:CBC        1960 :         for (i = 0; i <= wfuncno; i++)
                               2568                 :                :         {
                               2569         [ +  + ]:            405 :             if (equal(wfunc, perfunc[i].wfunc) &&
                               2570         [ +  - ]:              3 :                 !contain_volatile_functions((Node *) wfunc))
                               2571                 :              3 :                 break;
                               2572                 :                :         }
                               2573         [ +  + ]:           1558 :         if (i <= wfuncno)
                               2574                 :                :         {
                               2575                 :                :             /* Found a match to an existing entry, so just mark it */
                               2576                 :              3 :             wfuncstate->wfuncno = i;
                               2577                 :              3 :             continue;
                               2578                 :                :         }
                               2579                 :                : 
                               2580                 :                :         /* Nope, so assign a new PerAgg record */
                               2581                 :           1555 :         perfuncstate = &perfunc[++wfuncno];
                               2582                 :                : 
                               2583                 :                :         /* Mark WindowFunc state node with assigned index in the result array */
                               2584                 :           1555 :         wfuncstate->wfuncno = wfuncno;
                               2585                 :                : 
                               2586                 :                :         /* Check permission to call window function */
  518 peter@eisentraut.org     2587                 :           1555 :         aclresult = object_aclcheck(ProcedureRelationId, wfunc->winfnoid, GetUserId(),
                               2588                 :                :                                     ACL_EXECUTE);
 5586 tgl@sss.pgh.pa.us        2589         [ -  + ]:           1555 :         if (aclresult != ACLCHECK_OK)
 2325 peter_e@gmx.net          2590                 :UBC           0 :             aclcheck_error(aclresult, OBJECT_FUNCTION,
 5586 tgl@sss.pgh.pa.us        2591                 :              0 :                            get_func_name(wfunc->winfnoid));
 4020 rhaas@postgresql.org     2592         [ -  + ]:CBC        1555 :         InvokeFunctionExecuteHook(wfunc->winfnoid);
                               2593                 :                : 
                               2594                 :                :         /* Fill in the perfuncstate data */
 5586 tgl@sss.pgh.pa.us        2595                 :           1555 :         perfuncstate->wfuncstate = wfuncstate;
                               2596                 :           1555 :         perfuncstate->wfunc = wfunc;
                               2597                 :           1555 :         perfuncstate->numArguments = list_length(wfuncstate->args);
 4751                          2598                 :           1555 :         perfuncstate->winCollation = wfunc->inputcollid;
                               2599                 :                : 
 5586                          2600                 :           1555 :         get_typlenbyval(wfunc->wintype,
                               2601                 :                :                         &perfuncstate->resulttypeLen,
                               2602                 :                :                         &perfuncstate->resulttypeByVal);
                               2603                 :                : 
                               2604                 :                :         /*
                               2605                 :                :          * If it's really just a plain aggregate function, we'll emulate the
                               2606                 :                :          * Agg environment for it.
                               2607                 :                :          */
                               2608                 :           1555 :         perfuncstate->plain_agg = wfunc->winagg;
                               2609         [ +  + ]:           1555 :         if (wfunc->winagg)
                               2610                 :                :         {
                               2611                 :                :             WindowStatePerAgg peraggstate;
                               2612                 :                : 
                               2613                 :            715 :             perfuncstate->aggno = ++aggno;
                               2614                 :            715 :             peraggstate = &winstate->peragg[aggno];
                               2615                 :            715 :             initialize_peragg(winstate, wfunc, peraggstate);
                               2616                 :            715 :             peraggstate->wfuncno = wfuncno;
                               2617                 :                :         }
                               2618                 :                :         else
                               2619                 :                :         {
                               2620                 :            840 :             WindowObject winobj = makeNode(WindowObjectData);
                               2621                 :                : 
                               2622                 :            840 :             winobj->winstate = winstate;
                               2623                 :            840 :             winobj->argstates = wfuncstate->args;
                               2624                 :            840 :             winobj->localmem = NULL;
                               2625                 :            840 :             perfuncstate->winobj = winobj;
                               2626                 :                : 
                               2627                 :                :             /* It's a real window function, so set up to call it. */
 1257                          2628                 :            840 :             fmgr_info_cxt(wfunc->winfnoid, &perfuncstate->flinfo,
                               2629                 :                :                           econtext->ecxt_per_query_memory);
                               2630                 :            840 :             fmgr_info_set_expr((Node *) wfunc, &perfuncstate->flinfo);
                               2631                 :                :         }
                               2632                 :                :     }
                               2633                 :                : 
                               2634                 :                :     /* Update numfuncs, numaggs to match number of unique functions found */
 5586                          2635                 :           1222 :     winstate->numfuncs = wfuncno + 1;
                               2636                 :           1222 :     winstate->numaggs = aggno + 1;
                               2637                 :                : 
                               2638                 :                :     /* Set up WindowObject for aggregates, if needed */
 5175                          2639         [ +  + ]:           1222 :     if (winstate->numaggs > 0)
                               2640                 :                :     {
                               2641                 :            673 :         WindowObject agg_winobj = makeNode(WindowObjectData);
                               2642                 :                : 
                               2643                 :            673 :         agg_winobj->winstate = winstate;
                               2644                 :            673 :         agg_winobj->argstates = NIL;
                               2645                 :            673 :         agg_winobj->localmem = NULL;
                               2646                 :                :         /* make sure markptr = -1 to invalidate. It may not get used */
                               2647                 :            673 :         agg_winobj->markptr = -1;
                               2648                 :            673 :         agg_winobj->readptr = -1;
                               2649                 :            673 :         winstate->agg_winobj = agg_winobj;
                               2650                 :                :     }
                               2651                 :                : 
                               2652                 :                :     /* Set the status to running */
  737 drowley@postgresql.o     2653                 :           1222 :     winstate->status = WINDOWAGG_RUN;
                               2654                 :                : 
                               2655                 :                :     /* initialize frame bound offset expressions */
 5175 tgl@sss.pgh.pa.us        2656                 :           1222 :     winstate->startOffset = ExecInitExpr((Expr *) node->startOffset,
                               2657                 :                :                                          (PlanState *) winstate);
                               2658                 :           1222 :     winstate->endOffset = ExecInitExpr((Expr *) node->endOffset,
                               2659                 :                :                                        (PlanState *) winstate);
                               2660                 :                : 
                               2661                 :                :     /* Lookup in_range support functions if needed */
 2258                          2662         [ +  + ]:           1222 :     if (OidIsValid(node->startInRangeFunc))
                               2663                 :            261 :         fmgr_info(node->startInRangeFunc, &winstate->startInRangeFunc);
                               2664         [ +  + ]:           1222 :     if (OidIsValid(node->endInRangeFunc))
                               2665                 :            294 :         fmgr_info(node->endInRangeFunc, &winstate->endInRangeFunc);
                               2666                 :           1222 :     winstate->inRangeColl = node->inRangeColl;
                               2667                 :           1222 :     winstate->inRangeAsc = node->inRangeAsc;
                               2668                 :           1222 :     winstate->inRangeNullsFirst = node->inRangeNullsFirst;
                               2669                 :                : 
 5175                          2670                 :           1222 :     winstate->all_first = true;
 5586                          2671                 :           1222 :     winstate->partition_spooled = false;
                               2672                 :           1222 :     winstate->more_partitions = false;
                               2673                 :                : 
                               2674                 :           1222 :     return winstate;
                               2675                 :                : }
                               2676                 :                : 
                               2677                 :                : /* -----------------
                               2678                 :                :  * ExecEndWindowAgg
                               2679                 :                :  * -----------------
                               2680                 :                :  */
                               2681                 :                : void
                               2682                 :           1156 : ExecEndWindowAgg(WindowAggState *node)
                               2683                 :                : {
                               2684                 :                :     PlanState  *outerPlan;
                               2685                 :                :     int         i;
                               2686                 :                : 
                               2687                 :           1156 :     release_partition(node);
                               2688                 :                : 
 3655                          2689         [ +  + ]:           1850 :     for (i = 0; i < node->numaggs; i++)
                               2690                 :                :     {
                               2691         [ +  + ]:            694 :         if (node->peragg[i].aggcontext != node->aggcontext)
                               2692                 :            390 :             MemoryContextDelete(node->peragg[i].aggcontext);
                               2693                 :                :     }
 5175                          2694                 :           1156 :     MemoryContextDelete(node->partcontext);
                               2695                 :           1156 :     MemoryContextDelete(node->aggcontext);
                               2696                 :                : 
 3655                          2697                 :           1156 :     pfree(node->perfunc);
                               2698                 :           1156 :     pfree(node->peragg);
                               2699                 :                : 
 5586                          2700                 :           1156 :     outerPlan = outerPlanState(node);
                               2701                 :           1156 :     ExecEndNode(outerPlan);
                               2702                 :           1156 : }
                               2703                 :                : 
                               2704                 :                : /* -----------------
                               2705                 :                :  * ExecReScanWindowAgg
                               2706                 :                :  * -----------------
                               2707                 :                :  */
                               2708                 :                : void
 5025                          2709                 :             39 : ExecReScanWindowAgg(WindowAggState *node)
                               2710                 :                : {
 3249 bruce@momjian.us         2711                 :             39 :     PlanState  *outerPlan = outerPlanState(node);
 5421                          2712                 :             39 :     ExprContext *econtext = node->ss.ps.ps_ExprContext;
                               2713                 :                : 
  737 drowley@postgresql.o     2714                 :             39 :     node->status = WINDOWAGG_RUN;
 5175 tgl@sss.pgh.pa.us        2715                 :             39 :     node->all_first = true;
                               2716                 :                : 
                               2717                 :                :     /* release tuplestore et al */
 5586                          2718                 :             39 :     release_partition(node);
                               2719                 :                : 
                               2720                 :                :     /* release all temp tuples, but especially first_part_slot */
                               2721                 :             39 :     ExecClearTuple(node->ss.ss_ScanTupleSlot);
                               2722                 :             39 :     ExecClearTuple(node->first_part_slot);
 5583                          2723                 :             39 :     ExecClearTuple(node->agg_row_slot);
 5586                          2724                 :             39 :     ExecClearTuple(node->temp_slot_1);
                               2725                 :             39 :     ExecClearTuple(node->temp_slot_2);
 2258                          2726         [ -  + ]:             39 :     if (node->framehead_slot)
 2258 tgl@sss.pgh.pa.us        2727                 :UBC           0 :         ExecClearTuple(node->framehead_slot);
 2258 tgl@sss.pgh.pa.us        2728         [ +  + ]:CBC          39 :     if (node->frametail_slot)
                               2729                 :              3 :         ExecClearTuple(node->frametail_slot);
                               2730                 :                : 
                               2731                 :                :     /* Forget current wfunc values */
 5586                          2732   [ +  -  +  -  :             78 :     MemSet(econtext->ecxt_aggvalues, 0, sizeof(Datum) * node->numfuncs);
                                     +  -  +  -  +  
                                                 + ]
                               2733   [ +  -  -  +  :             39 :     MemSet(econtext->ecxt_aggnulls, 0, sizeof(bool) * node->numfuncs);
                                     -  -  -  -  -  
                                                 - ]
                               2734                 :                : 
                               2735                 :                :     /*
                               2736                 :                :      * if chgParam of subnode is not null then plan will be re-scanned by
                               2737                 :                :      * first ExecProcNode.
                               2738                 :                :      */
 3268 rhaas@postgresql.org     2739         [ +  + ]:             39 :     if (outerPlan->chgParam == NULL)
                               2740                 :              3 :         ExecReScan(outerPlan);
 5586 tgl@sss.pgh.pa.us        2741                 :             39 : }
                               2742                 :                : 
                               2743                 :                : /*
                               2744                 :                :  * initialize_peragg
                               2745                 :                :  *
                               2746                 :                :  * Almost same as in nodeAgg.c, except we don't support DISTINCT currently.
                               2747                 :                :  */
                               2748                 :                : static WindowStatePerAggData *
                               2749                 :            715 : initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc,
                               2750                 :                :                   WindowStatePerAgg peraggstate)
                               2751                 :                : {
                               2752                 :                :     Oid         inputTypes[FUNC_MAX_ARGS];
                               2753                 :                :     int         numArguments;
                               2754                 :                :     HeapTuple   aggTuple;
                               2755                 :                :     Form_pg_aggregate aggform;
                               2756                 :                :     Oid         aggtranstype;
                               2757                 :                :     AttrNumber  initvalAttNo;
                               2758                 :                :     AclResult   aclresult;
                               2759                 :                :     bool        use_ma_code;
                               2760                 :                :     Oid         transfn_oid,
                               2761                 :                :                 invtransfn_oid,
                               2762                 :                :                 finalfn_oid;
                               2763                 :                :     bool        finalextra;
                               2764                 :                :     char        finalmodify;
                               2765                 :                :     Expr       *transfnexpr,
                               2766                 :                :                *invtransfnexpr,
                               2767                 :                :                *finalfnexpr;
                               2768                 :                :     Datum       textInitVal;
                               2769                 :                :     int         i;
                               2770                 :                :     ListCell   *lc;
                               2771                 :                : 
                               2772                 :            715 :     numArguments = list_length(wfunc->args);
                               2773                 :                : 
                               2774                 :            715 :     i = 0;
                               2775   [ +  +  +  +  :           1367 :     foreach(lc, wfunc->args)
                                              +  + ]
                               2776                 :                :     {
                               2777                 :            652 :         inputTypes[i++] = exprType((Node *) lfirst(lc));
                               2778                 :                :     }
                               2779                 :                : 
 5173 rhaas@postgresql.org     2780                 :            715 :     aggTuple = SearchSysCache1(AGGFNOID, ObjectIdGetDatum(wfunc->winfnoid));
 5586 tgl@sss.pgh.pa.us        2781         [ -  + ]:            715 :     if (!HeapTupleIsValid(aggTuple))
 5586 tgl@sss.pgh.pa.us        2782         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for aggregate %u",
                               2783                 :                :              wfunc->winfnoid);
 5586 tgl@sss.pgh.pa.us        2784                 :CBC         715 :     aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
                               2785                 :                : 
                               2786                 :                :     /*
                               2787                 :                :      * Figure out whether we want to use the moving-aggregate implementation,
                               2788                 :                :      * and collect the right set of fields from the pg_aggregate entry.
                               2789                 :                :      *
                               2790                 :                :      * It's possible that an aggregate would supply a safe moving-aggregate
                               2791                 :                :      * implementation and an unsafe normal one, in which case our hand is
                               2792                 :                :      * forced.  Otherwise, if the frame head can't move, we don't need
                               2793                 :                :      * moving-aggregate code.  Even if we'd like to use it, don't do so if the
                               2794                 :                :      * aggregate's arguments (and FILTER clause if any) contain any calls to
                               2795                 :                :      * volatile functions.  Otherwise, the difference between restarting and
                               2796                 :                :      * not restarting the aggregation would be user-visible.
                               2797                 :                :      *
                               2798                 :                :      * We also don't risk using moving aggregates when there are subplans in
                               2799                 :                :      * the arguments or FILTER clause.  This is partly because
                               2800                 :                :      * contain_volatile_functions() doesn't look inside subplans; but there
                               2801                 :                :      * are other reasons why a subplan's output might be volatile.  For
                               2802                 :                :      * example, syncscan mode can render the results nonrepeatable.
                               2803                 :                :      */
 2374                          2804         [ +  + ]:            715 :     if (!OidIsValid(aggform->aggminvtransfn))
                               2805                 :             92 :         use_ma_code = false;    /* sine qua non */
                               2806         [ +  - ]:            623 :     else if (aggform->aggmfinalmodify == AGGMODIFY_READ_ONLY &&
  331                          2807         [ -  + ]:            623 :              aggform->aggfinalmodify != AGGMODIFY_READ_ONLY)
 2374 tgl@sss.pgh.pa.us        2808                 :UBC           0 :         use_ma_code = true;     /* decision forced by safety */
 2374 tgl@sss.pgh.pa.us        2809         [ +  + ]:CBC         623 :     else if (winstate->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
                               2810                 :            221 :         use_ma_code = false;    /* non-moving frame head */
                               2811         [ +  + ]:            402 :     else if (contain_volatile_functions((Node *) wfunc))
                               2812                 :              6 :         use_ma_code = false;    /* avoid possible behavioral change */
  426 drowley@postgresql.o     2813         [ -  + ]:            396 :     else if (contain_subplans((Node *) wfunc))
  426 drowley@postgresql.o     2814                 :UBC           0 :         use_ma_code = false;    /* subplans might contain volatile functions */
                               2815                 :                :     else
 2374 tgl@sss.pgh.pa.us        2816                 :CBC         396 :         use_ma_code = true;     /* yes, let's use it */
                               2817         [ +  + ]:            715 :     if (use_ma_code)
                               2818                 :                :     {
 3655                          2819                 :            396 :         peraggstate->transfn_oid = transfn_oid = aggform->aggmtransfn;
                               2820                 :            396 :         peraggstate->invtransfn_oid = invtransfn_oid = aggform->aggminvtransfn;
                               2821                 :            396 :         peraggstate->finalfn_oid = finalfn_oid = aggform->aggmfinalfn;
 3644                          2822                 :            396 :         finalextra = aggform->aggmfinalextra;
 2374                          2823                 :            396 :         finalmodify = aggform->aggmfinalmodify;
 3655                          2824                 :            396 :         aggtranstype = aggform->aggmtranstype;
                               2825                 :            396 :         initvalAttNo = Anum_pg_aggregate_aggminitval;
                               2826                 :                :     }
                               2827                 :                :     else
                               2828                 :                :     {
                               2829                 :            319 :         peraggstate->transfn_oid = transfn_oid = aggform->aggtransfn;
                               2830                 :            319 :         peraggstate->invtransfn_oid = invtransfn_oid = InvalidOid;
                               2831                 :            319 :         peraggstate->finalfn_oid = finalfn_oid = aggform->aggfinalfn;
 3644                          2832                 :            319 :         finalextra = aggform->aggfinalextra;
 2374                          2833                 :            319 :         finalmodify = aggform->aggfinalmodify;
 3655                          2834                 :            319 :         aggtranstype = aggform->aggtranstype;
                               2835                 :            319 :         initvalAttNo = Anum_pg_aggregate_agginitval;
                               2836                 :                :     }
                               2837                 :                : 
                               2838                 :                :     /*
                               2839                 :                :      * ExecInitWindowAgg already checked permission to call aggregate function
                               2840                 :                :      * ... but we still need to check the component functions
                               2841                 :                :      */
                               2842                 :                : 
                               2843                 :                :     /* Check that aggregate owner has permission to call component fns */
                               2844                 :                :     {
                               2845                 :                :         HeapTuple   procTuple;
                               2846                 :                :         Oid         aggOwner;
                               2847                 :                : 
 5173 rhaas@postgresql.org     2848                 :            715 :         procTuple = SearchSysCache1(PROCOID,
                               2849                 :                :                                     ObjectIdGetDatum(wfunc->winfnoid));
 5586 tgl@sss.pgh.pa.us        2850         [ -  + ]:            715 :         if (!HeapTupleIsValid(procTuple))
 5586 tgl@sss.pgh.pa.us        2851         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for function %u",
                               2852                 :                :                  wfunc->winfnoid);
 5586 tgl@sss.pgh.pa.us        2853                 :CBC         715 :         aggOwner = ((Form_pg_proc) GETSTRUCT(procTuple))->proowner;
                               2854                 :            715 :         ReleaseSysCache(procTuple);
                               2855                 :                : 
  518 peter@eisentraut.org     2856                 :            715 :         aclresult = object_aclcheck(ProcedureRelationId, transfn_oid, aggOwner,
                               2857                 :                :                                     ACL_EXECUTE);
 5586 tgl@sss.pgh.pa.us        2858         [ -  + ]:            715 :         if (aclresult != ACLCHECK_OK)
 2325 peter_e@gmx.net          2859                 :UBC           0 :             aclcheck_error(aclresult, OBJECT_FUNCTION,
 5586 tgl@sss.pgh.pa.us        2860                 :              0 :                            get_func_name(transfn_oid));
 4020 rhaas@postgresql.org     2861         [ -  + ]:CBC         715 :         InvokeFunctionExecuteHook(transfn_oid);
                               2862                 :                : 
 3655 tgl@sss.pgh.pa.us        2863         [ +  + ]:            715 :         if (OidIsValid(invtransfn_oid))
                               2864                 :                :         {
  518 peter@eisentraut.org     2865                 :            396 :             aclresult = object_aclcheck(ProcedureRelationId, invtransfn_oid, aggOwner,
                               2866                 :                :                                         ACL_EXECUTE);
 3655 tgl@sss.pgh.pa.us        2867         [ -  + ]:            396 :             if (aclresult != ACLCHECK_OK)
 2325 peter_e@gmx.net          2868                 :UBC           0 :                 aclcheck_error(aclresult, OBJECT_FUNCTION,
 3655 tgl@sss.pgh.pa.us        2869                 :              0 :                                get_func_name(invtransfn_oid));
 3655 tgl@sss.pgh.pa.us        2870         [ -  + ]:CBC         396 :             InvokeFunctionExecuteHook(invtransfn_oid);
                               2871                 :                :         }
                               2872                 :                : 
 5586                          2873         [ +  + ]:            715 :         if (OidIsValid(finalfn_oid))
                               2874                 :                :         {
  518 peter@eisentraut.org     2875                 :            418 :             aclresult = object_aclcheck(ProcedureRelationId, finalfn_oid, aggOwner,
                               2876                 :                :                                         ACL_EXECUTE);
 5586 tgl@sss.pgh.pa.us        2877         [ -  + ]:            418 :             if (aclresult != ACLCHECK_OK)
 2325 peter_e@gmx.net          2878                 :UBC           0 :                 aclcheck_error(aclresult, OBJECT_FUNCTION,
 5586 tgl@sss.pgh.pa.us        2879                 :              0 :                                get_func_name(finalfn_oid));
 4020 rhaas@postgresql.org     2880         [ -  + ]:CBC         418 :             InvokeFunctionExecuteHook(finalfn_oid);
                               2881                 :                :         }
                               2882                 :                :     }
                               2883                 :                : 
                               2884                 :                :     /*
                               2885                 :                :      * If the selected finalfn isn't read-only, we can't run this aggregate as
                               2886                 :                :      * a window function.  This is a user-facing error, so we take a bit more
                               2887                 :                :      * care with the error message than elsewhere in this function.
                               2888                 :                :      */
 2374 tgl@sss.pgh.pa.us        2889         [ -  + ]:            715 :     if (finalmodify != AGGMODIFY_READ_ONLY)
 2374 tgl@sss.pgh.pa.us        2890         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2891                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               2892                 :                :                  errmsg("aggregate function %s does not support use as a window function",
                               2893                 :                :                         format_procedure(wfunc->winfnoid))));
                               2894                 :                : 
                               2895                 :                :     /* Detect how many arguments to pass to the finalfn */
 3644 tgl@sss.pgh.pa.us        2896         [ +  + ]:CBC         715 :     if (finalextra)
                               2897                 :             13 :         peraggstate->numFinalArgs = numArguments + 1;
                               2898                 :                :     else
                               2899                 :            702 :         peraggstate->numFinalArgs = 1;
                               2900                 :                : 
                               2901                 :                :     /* resolve actual type of transition state, if polymorphic */
 3765                          2902                 :            715 :     aggtranstype = resolve_aggregate_transtype(wfunc->winfnoid,
                               2903                 :                :                                                aggtranstype,
                               2904                 :                :                                                inputTypes,
                               2905                 :                :                                                numArguments);
                               2906                 :                : 
                               2907                 :                :     /* build expression trees using actual argument & result types */
 3176 heikki.linnakangas@i     2908                 :            715 :     build_aggregate_transfn_expr(inputTypes,
                               2909                 :                :                                  numArguments,
                               2910                 :                :                                  0, /* no ordered-set window functions yet */
                               2911                 :                :                                  false, /* no variadic window functions yet */
                               2912                 :                :                                  aggtranstype,
                               2913                 :                :                                  wfunc->inputcollid,
                               2914                 :                :                                  transfn_oid,
                               2915                 :                :                                  invtransfn_oid,
                               2916                 :                :                                  &transfnexpr,
                               2917                 :                :                                  &invtransfnexpr);
                               2918                 :                : 
                               2919                 :                :     /* set up infrastructure for calling the transfn(s) and finalfn */
 5586 tgl@sss.pgh.pa.us        2920                 :            715 :     fmgr_info(transfn_oid, &peraggstate->transfn);
 4775                          2921                 :            715 :     fmgr_info_set_expr((Node *) transfnexpr, &peraggstate->transfn);
                               2922                 :                : 
 3655                          2923         [ +  + ]:            715 :     if (OidIsValid(invtransfn_oid))
                               2924                 :                :     {
                               2925                 :            396 :         fmgr_info(invtransfn_oid, &peraggstate->invtransfn);
                               2926                 :            396 :         fmgr_info_set_expr((Node *) invtransfnexpr, &peraggstate->invtransfn);
                               2927                 :                :     }
                               2928                 :                : 
 5586                          2929         [ +  + ]:            715 :     if (OidIsValid(finalfn_oid))
                               2930                 :                :     {
 3176 heikki.linnakangas@i     2931                 :            418 :         build_aggregate_finalfn_expr(inputTypes,
                               2932                 :                :                                      peraggstate->numFinalArgs,
                               2933                 :                :                                      aggtranstype,
                               2934                 :                :                                      wfunc->wintype,
                               2935                 :                :                                      wfunc->inputcollid,
                               2936                 :                :                                      finalfn_oid,
                               2937                 :                :                                      &finalfnexpr);
 5586 tgl@sss.pgh.pa.us        2938                 :            418 :         fmgr_info(finalfn_oid, &peraggstate->finalfn);
 4775                          2939                 :            418 :         fmgr_info_set_expr((Node *) finalfnexpr, &peraggstate->finalfn);
                               2940                 :                :     }
                               2941                 :                : 
                               2942                 :                :     /* get info about relevant datatypes */
 5586                          2943                 :            715 :     get_typlenbyval(wfunc->wintype,
                               2944                 :                :                     &peraggstate->resulttypeLen,
                               2945                 :                :                     &peraggstate->resulttypeByVal);
                               2946                 :            715 :     get_typlenbyval(aggtranstype,
                               2947                 :                :                     &peraggstate->transtypeLen,
                               2948                 :                :                     &peraggstate->transtypeByVal);
                               2949                 :                : 
                               2950                 :                :     /*
                               2951                 :                :      * initval is potentially null, so don't try to access it as a struct
                               2952                 :                :      * field. Must do it the hard way with SysCacheGetAttr.
                               2953                 :                :      */
 3655                          2954                 :            715 :     textInitVal = SysCacheGetAttr(AGGFNOID, aggTuple, initvalAttNo,
                               2955                 :                :                                   &peraggstate->initValueIsNull);
                               2956                 :                : 
 5586                          2957         [ +  + ]:            715 :     if (peraggstate->initValueIsNull)
                               2958                 :            368 :         peraggstate->initValue = (Datum) 0;
                               2959                 :                :     else
                               2960                 :            347 :         peraggstate->initValue = GetAggInitVal(textInitVal,
                               2961                 :                :                                                aggtranstype);
                               2962                 :                : 
                               2963                 :                :     /*
                               2964                 :                :      * If the transfn is strict and the initval is NULL, make sure input type
                               2965                 :                :      * and transtype are the same (or at least binary-compatible), so that
                               2966                 :                :      * it's OK to use the first input value as the initial transValue.  This
                               2967                 :                :      * should have been checked at agg definition time, but we must check
                               2968                 :                :      * again in case the transfn's strictness property has been changed.
                               2969                 :                :      */
                               2970   [ +  +  +  + ]:            715 :     if (peraggstate->transfn.fn_strict && peraggstate->initValueIsNull)
                               2971                 :                :     {
                               2972         [ +  - ]:             76 :         if (numArguments < 1 ||
                               2973         [ -  + ]:             76 :             !IsBinaryCoercible(inputTypes[0], aggtranstype))
 5586 tgl@sss.pgh.pa.us        2974         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2975                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2976                 :                :                      errmsg("aggregate %u needs to have compatible input type and transition type",
                               2977                 :                :                             wfunc->winfnoid)));
                               2978                 :                :     }
                               2979                 :                : 
                               2980                 :                :     /*
                               2981                 :                :      * Insist that forward and inverse transition functions have the same
                               2982                 :                :      * strictness setting.  Allowing them to differ would require handling
                               2983                 :                :      * more special cases in advance_windowaggregate and
                               2984                 :                :      * advance_windowaggregate_base, for no discernible benefit.  This should
                               2985                 :                :      * have been checked at agg definition time, but we must check again in
                               2986                 :                :      * case either function's strictness property has been changed.
                               2987                 :                :      */
 3655 tgl@sss.pgh.pa.us        2988         [ +  + ]:CBC         715 :     if (OidIsValid(invtransfn_oid) &&
                               2989         [ -  + ]:            396 :         peraggstate->transfn.fn_strict != peraggstate->invtransfn.fn_strict)
 3655 tgl@sss.pgh.pa.us        2990         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2991                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2992                 :                :                  errmsg("strictness of aggregate's forward and inverse transition functions must match")));
                               2993                 :                : 
                               2994                 :                :     /*
                               2995                 :                :      * Moving aggregates use their own aggcontext.
                               2996                 :                :      *
                               2997                 :                :      * This is necessary because they might restart at different times, so we
                               2998                 :                :      * might never be able to reset the shared context otherwise.  We can't
                               2999                 :                :      * make it the aggregates' responsibility to clean up after themselves,
                               3000                 :                :      * because strict aggregates must be restarted whenever we remove their
                               3001                 :                :      * last non-NULL input, which the aggregate won't be aware is happening.
                               3002                 :                :      * Also, just pfree()ing the transValue upon restarting wouldn't help,
                               3003                 :                :      * since we'd miss any indirectly referenced data.  We could, in theory,
                               3004                 :                :      * make the memory allocation rules for moving aggregates different than
                               3005                 :                :      * they have historically been for plain aggregates, but that seems grotty
                               3006                 :                :      * and likely to lead to memory leaks.
                               3007                 :                :      */
 3655 tgl@sss.pgh.pa.us        3008         [ +  + ]:CBC         715 :     if (OidIsValid(invtransfn_oid))
                               3009                 :            396 :         peraggstate->aggcontext =
                               3010                 :            396 :             AllocSetContextCreate(CurrentMemoryContext,
                               3011                 :                :                                   "WindowAgg Per Aggregate",
                               3012                 :                :                                   ALLOCSET_DEFAULT_SIZES);
                               3013                 :                :     else
                               3014                 :            319 :         peraggstate->aggcontext = winstate->aggcontext;
                               3015                 :                : 
 5586                          3016                 :            715 :     ReleaseSysCache(aggTuple);
                               3017                 :                : 
                               3018                 :            715 :     return peraggstate;
                               3019                 :                : }
                               3020                 :                : 
                               3021                 :                : static Datum
                               3022                 :            347 : GetAggInitVal(Datum textInitVal, Oid transtype)
                               3023                 :                : {
                               3024                 :                :     Oid         typinput,
                               3025                 :                :                 typioparam;
                               3026                 :                :     char       *strInitVal;
                               3027                 :                :     Datum       initVal;
                               3028                 :                : 
                               3029                 :            347 :     getTypeInputInfo(transtype, &typinput, &typioparam);
                               3030                 :            347 :     strInitVal = TextDatumGetCString(textInitVal);
                               3031                 :            347 :     initVal = OidInputFunctionCall(typinput, strInitVal,
                               3032                 :                :                                    typioparam, -1);
                               3033                 :            347 :     pfree(strInitVal);
                               3034                 :            347 :     return initVal;
                               3035                 :                : }
                               3036                 :                : 
                               3037                 :                : /*
                               3038                 :                :  * are_peers
                               3039                 :                :  * compare two rows to see if they are equal according to the ORDER BY clause
                               3040                 :                :  *
                               3041                 :                :  * NB: this does not consider the window frame mode.
                               3042                 :                :  */
                               3043                 :                : static bool
                               3044                 :         256755 : are_peers(WindowAggState *winstate, TupleTableSlot *slot1,
                               3045                 :                :           TupleTableSlot *slot2)
                               3046                 :                : {
                               3047                 :         256755 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
 2250 andres@anarazel.de       3048                 :         256755 :     ExprContext *econtext = winstate->tmpcontext;
                               3049                 :                : 
                               3050                 :                :     /* If no ORDER BY, all rows are peers with each other */
 5586 tgl@sss.pgh.pa.us        3051         [ +  + ]:         256755 :     if (node->ordNumCols == 0)
                               3052                 :            479 :         return true;
                               3053                 :                : 
 2250 andres@anarazel.de       3054                 :         256276 :     econtext->ecxt_outertuple = slot1;
                               3055                 :         256276 :     econtext->ecxt_innertuple = slot2;
                               3056                 :         256276 :     return ExecQualAndReset(winstate->ordEqfunction, econtext);
                               3057                 :                : }
                               3058                 :                : 
                               3059                 :                : /*
                               3060                 :                :  * window_gettupleslot
                               3061                 :                :  *  Fetch the pos'th tuple of the current partition into the slot,
                               3062                 :                :  *  using the winobj's read pointer
                               3063                 :                :  *
                               3064                 :                :  * Returns true if successful, false if no such row
                               3065                 :                :  */
                               3066                 :                : static bool
 5586 tgl@sss.pgh.pa.us        3067                 :         338173 : window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
                               3068                 :                : {
                               3069                 :         338173 :     WindowAggState *winstate = winobj->winstate;
                               3070                 :                :     MemoryContext oldcontext;
                               3071                 :                : 
                               3072                 :                :     /* often called repeatedly in a row */
 2455 andres@anarazel.de       3073         [ -  + ]:         338173 :     CHECK_FOR_INTERRUPTS();
                               3074                 :                : 
                               3075                 :                :     /* Don't allow passing -1 to spool_tuples here */
 5586 tgl@sss.pgh.pa.us        3076         [ +  + ]:         338173 :     if (pos < 0)
                               3077                 :            156 :         return false;
                               3078                 :                : 
                               3079                 :                :     /* If necessary, fetch the tuple into the spool */
                               3080                 :         338017 :     spool_tuples(winstate, pos);
                               3081                 :                : 
                               3082         [ +  + ]:         338017 :     if (pos >= winstate->spooled_rows)
                               3083                 :           2254 :         return false;
                               3084                 :                : 
                               3085         [ -  + ]:         335763 :     if (pos < winobj->markpos)
 5586 tgl@sss.pgh.pa.us        3086         [ #  # ]:UBC           0 :         elog(ERROR, "cannot fetch row before WindowObject's mark position");
                               3087                 :                : 
 5586 tgl@sss.pgh.pa.us        3088                 :CBC      335763 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               3089                 :                : 
                               3090                 :         335763 :     tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
                               3091                 :                : 
                               3092                 :                :     /*
                               3093                 :                :      * Advance or rewind until we are within one tuple of the one we want.
                               3094                 :                :      */
 3654                          3095         [ +  + ]:         335763 :     if (winobj->seekpos < pos - 1)
                               3096                 :                :     {
                               3097         [ -  + ]:           1122 :         if (!tuplestore_skiptuples(winstate->buffer,
                               3098                 :           1122 :                                    pos - 1 - winobj->seekpos,
                               3099                 :                :                                    true))
 3654 tgl@sss.pgh.pa.us        3100         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected end of tuplestore");
 3654 tgl@sss.pgh.pa.us        3101                 :CBC        1122 :         winobj->seekpos = pos - 1;
                               3102                 :                :     }
                               3103         [ +  + ]:         334641 :     else if (winobj->seekpos > pos + 1)
                               3104                 :                :     {
                               3105         [ -  + ]:           1364 :         if (!tuplestore_skiptuples(winstate->buffer,
                               3106                 :           1364 :                                    winobj->seekpos - (pos + 1),
                               3107                 :                :                                    false))
 3654 tgl@sss.pgh.pa.us        3108         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected end of tuplestore");
 3654 tgl@sss.pgh.pa.us        3109                 :CBC        1364 :         winobj->seekpos = pos + 1;
                               3110                 :                :     }
                               3111         [ +  + ]:         333277 :     else if (winobj->seekpos == pos)
                               3112                 :                :     {
                               3113                 :                :         /*
                               3114                 :                :          * There's no API to refetch the tuple at the current position.  We
                               3115                 :                :          * have to move one tuple forward, and then one backward.  (We don't
                               3116                 :                :          * do it the other way because we might try to fetch the row before
                               3117                 :                :          * our mark, which isn't allowed.)  XXX this case could stand to be
                               3118                 :                :          * optimized.
                               3119                 :                :          */
 5586                          3120                 :          86126 :         tuplestore_advance(winstate->buffer, true);
                               3121                 :          86126 :         winobj->seekpos++;
                               3122                 :                :     }
                               3123                 :                : 
                               3124                 :                :     /*
                               3125                 :                :      * Now we should be on the tuple immediately before or after the one we
                               3126                 :                :      * want, so just fetch forwards or backwards as appropriate.
                               3127                 :                :      *
                               3128                 :                :      * Notice that we tell tuplestore_gettupleslot to make a physical copy of
                               3129                 :                :      * the fetched tuple.  This ensures that the slot's contents remain valid
                               3130                 :                :      * through manipulations of the tuplestore, which some callers depend on.
                               3131                 :                :      */
 3654                          3132         [ +  + ]:         335763 :     if (winobj->seekpos > pos)
                               3133                 :                :     {
 5497                          3134         [ -  + ]:          87557 :         if (!tuplestore_gettupleslot(winstate->buffer, false, true, slot))
 5586 tgl@sss.pgh.pa.us        3135         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected end of tuplestore");
 5586 tgl@sss.pgh.pa.us        3136                 :CBC       87557 :         winobj->seekpos--;
                               3137                 :                :     }
                               3138                 :                :     else
                               3139                 :                :     {
 5497                          3140         [ -  + ]:         248206 :         if (!tuplestore_gettupleslot(winstate->buffer, true, true, slot))
 5586 tgl@sss.pgh.pa.us        3141         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected end of tuplestore");
 5586 tgl@sss.pgh.pa.us        3142                 :CBC      248206 :         winobj->seekpos++;
                               3143                 :                :     }
                               3144                 :                : 
 3654                          3145         [ -  + ]:         335763 :     Assert(winobj->seekpos == pos);
                               3146                 :                : 
 5586                          3147                 :         335763 :     MemoryContextSwitchTo(oldcontext);
                               3148                 :                : 
                               3149                 :         335763 :     return true;
                               3150                 :                : }
                               3151                 :                : 
                               3152                 :                : 
                               3153                 :                : /***********************************************************************
                               3154                 :                :  * API exposed to window functions
                               3155                 :                :  ***********************************************************************/
                               3156                 :                : 
                               3157                 :                : 
                               3158                 :                : /*
                               3159                 :                :  * WinGetPartitionLocalMemory
                               3160                 :                :  *      Get working memory that lives till end of partition processing
                               3161                 :                :  *
                               3162                 :                :  * On first call within a given partition, this allocates and zeroes the
                               3163                 :                :  * requested amount of space.  Subsequent calls just return the same chunk.
                               3164                 :                :  *
                               3165                 :                :  * Memory obtained this way is normally used to hold state that should be
                               3166                 :                :  * automatically reset for each new partition.  If a window function wants
                               3167                 :                :  * to hold state across the whole query, fcinfo->fn_extra can be used in the
                               3168                 :                :  * usual way for that.
                               3169                 :                :  */
                               3170                 :                : void *
                               3171                 :         165735 : WinGetPartitionLocalMemory(WindowObject winobj, Size sz)
                               3172                 :                : {
                               3173   [ +  -  -  + ]:         165735 :     Assert(WindowObjectIsValid(winobj));
                               3174         [ +  + ]:         165735 :     if (winobj->localmem == NULL)
 5175                          3175                 :            222 :         winobj->localmem =
                               3176                 :            222 :             MemoryContextAllocZero(winobj->winstate->partcontext, sz);
 5586                          3177                 :         165735 :     return winobj->localmem;
                               3178                 :                : }
                               3179                 :                : 
                               3180                 :                : /*
                               3181                 :                :  * WinGetCurrentPosition
                               3182                 :                :  *      Return the current row's position (counting from 0) within the current
                               3183                 :                :  *      partition.
                               3184                 :                :  */
                               3185                 :                : int64
                               3186                 :         378789 : WinGetCurrentPosition(WindowObject winobj)
                               3187                 :                : {
                               3188   [ +  -  -  + ]:         378789 :     Assert(WindowObjectIsValid(winobj));
                               3189                 :         378789 :     return winobj->winstate->currentpos;
                               3190                 :                : }
                               3191                 :                : 
                               3192                 :                : /*
                               3193                 :                :  * WinGetPartitionRowCount
                               3194                 :                :  *      Return total number of rows contained in the current partition.
                               3195                 :                :  *
                               3196                 :                :  * Note: this is a relatively expensive operation because it forces the
                               3197                 :                :  * whole partition to be "spooled" into the tuplestore at once.  Once
                               3198                 :                :  * executed, however, additional calls within the same partition are cheap.
                               3199                 :                :  */
                               3200                 :                : int64
                               3201                 :             81 : WinGetPartitionRowCount(WindowObject winobj)
                               3202                 :                : {
                               3203   [ +  -  -  + ]:             81 :     Assert(WindowObjectIsValid(winobj));
                               3204                 :             81 :     spool_tuples(winobj->winstate, -1);
                               3205                 :             81 :     return winobj->winstate->spooled_rows;
                               3206                 :                : }
                               3207                 :                : 
                               3208                 :                : /*
                               3209                 :                :  * WinSetMarkPosition
                               3210                 :                :  *      Set the "mark" position for the window object, which is the oldest row
                               3211                 :                :  *      number (counting from 0) it is allowed to fetch during all subsequent
                               3212                 :                :  *      operations within the current partition.
                               3213                 :                :  *
                               3214                 :                :  * Window functions do not have to call this, but are encouraged to move the
                               3215                 :                :  * mark forward when possible to keep the tuplestore size down and prevent
                               3216                 :                :  * having to spill rows to disk.
                               3217                 :                :  */
                               3218                 :                : void
                               3219                 :         409807 : WinSetMarkPosition(WindowObject winobj, int64 markpos)
                               3220                 :                : {
                               3221                 :                :     WindowAggState *winstate;
                               3222                 :                : 
                               3223   [ +  -  -  + ]:         409807 :     Assert(WindowObjectIsValid(winobj));
                               3224                 :         409807 :     winstate = winobj->winstate;
                               3225                 :                : 
                               3226         [ -  + ]:         409807 :     if (markpos < winobj->markpos)
 5586 tgl@sss.pgh.pa.us        3227         [ #  # ]:UBC           0 :         elog(ERROR, "cannot move WindowObject's mark position backward");
 5586 tgl@sss.pgh.pa.us        3228                 :CBC      409807 :     tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
 3654                          3229         [ +  + ]:         409807 :     if (markpos > winobj->markpos)
                               3230                 :                :     {
                               3231                 :         407038 :         tuplestore_skiptuples(winstate->buffer,
                               3232                 :         407038 :                               markpos - winobj->markpos,
                               3233                 :                :                               true);
                               3234                 :         407038 :         winobj->markpos = markpos;
                               3235                 :                :     }
 5586                          3236                 :         409807 :     tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
 3654                          3237         [ +  + ]:         409807 :     if (markpos > winobj->seekpos)
                               3238                 :                :     {
                               3239                 :         231059 :         tuplestore_skiptuples(winstate->buffer,
                               3240                 :         231059 :                               markpos - winobj->seekpos,
                               3241                 :                :                               true);
                               3242                 :         231059 :         winobj->seekpos = markpos;
                               3243                 :                :     }
 5586                          3244                 :         409807 : }
                               3245                 :                : 
                               3246                 :                : /*
                               3247                 :                :  * WinRowsArePeers
                               3248                 :                :  *      Compare two rows (specified by absolute position in partition) to see
                               3249                 :                :  *      if they are equal according to the ORDER BY clause.
                               3250                 :                :  *
                               3251                 :                :  * NB: this does not consider the window frame mode.
                               3252                 :                :  */
                               3253                 :                : bool
                               3254                 :          82653 : WinRowsArePeers(WindowObject winobj, int64 pos1, int64 pos2)
                               3255                 :                : {
                               3256                 :                :     WindowAggState *winstate;
                               3257                 :                :     WindowAgg  *node;
                               3258                 :                :     TupleTableSlot *slot1;
                               3259                 :                :     TupleTableSlot *slot2;
                               3260                 :                :     bool        res;
                               3261                 :                : 
                               3262   [ +  -  -  + ]:          82653 :     Assert(WindowObjectIsValid(winobj));
                               3263                 :          82653 :     winstate = winobj->winstate;
                               3264                 :          82653 :     node = (WindowAgg *) winstate->ss.ps.plan;
                               3265                 :                : 
                               3266                 :                :     /* If no ORDER BY, all rows are peers; don't bother to fetch them */
                               3267         [ -  + ]:          82653 :     if (node->ordNumCols == 0)
 5586 tgl@sss.pgh.pa.us        3268                 :UBC           0 :         return true;
                               3269                 :                : 
                               3270                 :                :     /*
                               3271                 :                :      * Note: OK to use temp_slot_2 here because we aren't calling any
                               3272                 :                :      * frame-related functions (those tend to clobber temp_slot_2).
                               3273                 :                :      */
 5586 tgl@sss.pgh.pa.us        3274                 :CBC       82653 :     slot1 = winstate->temp_slot_1;
                               3275                 :          82653 :     slot2 = winstate->temp_slot_2;
                               3276                 :                : 
                               3277         [ -  + ]:          82653 :     if (!window_gettupleslot(winobj, pos1, slot1))
 5586 tgl@sss.pgh.pa.us        3278         [ #  # ]:UBC           0 :         elog(ERROR, "specified position is out of window: " INT64_FORMAT,
                               3279                 :                :              pos1);
 5586 tgl@sss.pgh.pa.us        3280         [ -  + ]:CBC       82653 :     if (!window_gettupleslot(winobj, pos2, slot2))
 5586 tgl@sss.pgh.pa.us        3281         [ #  # ]:UBC           0 :         elog(ERROR, "specified position is out of window: " INT64_FORMAT,
                               3282                 :                :              pos2);
                               3283                 :                : 
 5586 tgl@sss.pgh.pa.us        3284                 :CBC       82653 :     res = are_peers(winstate, slot1, slot2);
                               3285                 :                : 
                               3286                 :          82653 :     ExecClearTuple(slot1);
                               3287                 :          82653 :     ExecClearTuple(slot2);
                               3288                 :                : 
                               3289                 :          82653 :     return res;
                               3290                 :                : }
                               3291                 :                : 
                               3292                 :                : /*
                               3293                 :                :  * WinGetFuncArgInPartition
                               3294                 :                :  *      Evaluate a window function's argument expression on a specified
                               3295                 :                :  *      row of the partition.  The row is identified in lseek(2) style,
                               3296                 :                :  *      i.e. relative to the current, first, or last row.
                               3297                 :                :  *
                               3298                 :                :  * argno: argument number to evaluate (counted from 0)
                               3299                 :                :  * relpos: signed rowcount offset from the seek position
                               3300                 :                :  * seektype: WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, or WINDOW_SEEK_TAIL
                               3301                 :                :  * set_mark: If the row is found and set_mark is true, the mark is moved to
                               3302                 :                :  *      the row as a side-effect.
                               3303                 :                :  * isnull: output argument, receives isnull status of result
                               3304                 :                :  * isout: output argument, set to indicate whether target row position
                               3305                 :                :  *      is out of partition (can pass NULL if caller doesn't care about this)
                               3306                 :                :  *
                               3307                 :                :  * Specifying a nonexistent row is not an error, it just causes a null result
                               3308                 :                :  * (plus setting *isout true, if isout isn't NULL).
                               3309                 :                :  */
                               3310                 :                : Datum
                               3311                 :          91389 : WinGetFuncArgInPartition(WindowObject winobj, int argno,
                               3312                 :                :                          int relpos, int seektype, bool set_mark,
                               3313                 :                :                          bool *isnull, bool *isout)
                               3314                 :                : {
                               3315                 :                :     WindowAggState *winstate;
                               3316                 :                :     ExprContext *econtext;
                               3317                 :                :     TupleTableSlot *slot;
                               3318                 :                :     bool        gottuple;
                               3319                 :                :     int64       abs_pos;
                               3320                 :                : 
                               3321   [ +  -  -  + ]:          91389 :     Assert(WindowObjectIsValid(winobj));
 5583                          3322                 :          91389 :     winstate = winobj->winstate;
                               3323                 :          91389 :     econtext = winstate->ss.ps.ps_ExprContext;
                               3324                 :          91389 :     slot = winstate->temp_slot_1;
                               3325                 :                : 
 5586                          3326   [ +  -  -  - ]:          91389 :     switch (seektype)
                               3327                 :                :     {
                               3328                 :          91389 :         case WINDOW_SEEK_CURRENT:
 5583                          3329                 :          91389 :             abs_pos = winstate->currentpos + relpos;
 5586                          3330                 :          91389 :             break;
 5586 tgl@sss.pgh.pa.us        3331                 :UBC           0 :         case WINDOW_SEEK_HEAD:
                               3332                 :              0 :             abs_pos = relpos;
                               3333                 :              0 :             break;
                               3334                 :              0 :         case WINDOW_SEEK_TAIL:
 5583                          3335                 :              0 :             spool_tuples(winstate, -1);
                               3336                 :              0 :             abs_pos = winstate->spooled_rows - 1 + relpos;
 5586                          3337                 :              0 :             break;
                               3338                 :              0 :         default:
                               3339         [ #  # ]:              0 :             elog(ERROR, "unrecognized window seek type: %d", seektype);
                               3340                 :                :             abs_pos = 0;        /* keep compiler quiet */
                               3341                 :                :             break;
                               3342                 :                :     }
                               3343                 :                : 
 5583 tgl@sss.pgh.pa.us        3344                 :CBC       91389 :     gottuple = window_gettupleslot(winobj, abs_pos, slot);
                               3345                 :                : 
 5586                          3346         [ +  + ]:          91389 :     if (!gottuple)
                               3347                 :                :     {
                               3348         [ +  - ]:            159 :         if (isout)
                               3349                 :            159 :             *isout = true;
                               3350                 :            159 :         *isnull = true;
                               3351                 :            159 :         return (Datum) 0;
                               3352                 :                :     }
                               3353                 :                :     else
                               3354                 :                :     {
                               3355         [ +  - ]:          91230 :         if (isout)
                               3356                 :          91230 :             *isout = false;
                               3357         [ +  + ]:          91230 :         if (set_mark)
 2258                          3358                 :          91152 :             WinSetMarkPosition(winobj, abs_pos);
 5586                          3359                 :          91230 :         econtext->ecxt_outertuple = slot;
                               3360                 :          91230 :         return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
                               3361                 :                :                             econtext, isnull);
                               3362                 :                :     }
                               3363                 :                : }
                               3364                 :                : 
                               3365                 :                : /*
                               3366                 :                :  * WinGetFuncArgInFrame
                               3367                 :                :  *      Evaluate a window function's argument expression on a specified
                               3368                 :                :  *      row of the window frame.  The row is identified in lseek(2) style,
                               3369                 :                :  *      i.e. relative to the first or last row of the frame.  (We do not
                               3370                 :                :  *      support WINDOW_SEEK_CURRENT here, because it's not very clear what
                               3371                 :                :  *      that should mean if the current row isn't part of the frame.)
                               3372                 :                :  *
                               3373                 :                :  * argno: argument number to evaluate (counted from 0)
                               3374                 :                :  * relpos: signed rowcount offset from the seek position
                               3375                 :                :  * seektype: WINDOW_SEEK_HEAD or WINDOW_SEEK_TAIL
                               3376                 :                :  * set_mark: If the row is found/in frame and set_mark is true, the mark is
                               3377                 :                :  *      moved to the row as a side-effect.
                               3378                 :                :  * isnull: output argument, receives isnull status of result
                               3379                 :                :  * isout: output argument, set to indicate whether target row position
                               3380                 :                :  *      is out of frame (can pass NULL if caller doesn't care about this)
                               3381                 :                :  *
                               3382                 :                :  * Specifying a nonexistent or not-in-frame row is not an error, it just
                               3383                 :                :  * causes a null result (plus setting *isout true, if isout isn't NULL).
                               3384                 :                :  *
                               3385                 :                :  * Note that some exclusion-clause options lead to situations where the
                               3386                 :                :  * rows that are in-frame are not consecutive in the partition.  But we
                               3387                 :                :  * count only in-frame rows when measuring relpos.
                               3388                 :                :  *
                               3389                 :                :  * The set_mark flag is interpreted as meaning that the caller will specify
                               3390                 :                :  * a constant (or, perhaps, monotonically increasing) relpos in successive
                               3391                 :                :  * calls, so that *if there is no exclusion clause* there will be no need
                               3392                 :                :  * to fetch a row before the previously fetched row.  But we do not expect
                               3393                 :                :  * the caller to know how to account for exclusion clauses.  Therefore,
                               3394                 :                :  * if there is an exclusion clause we take responsibility for adjusting the
                               3395                 :                :  * mark request to something that will be safe given the above assumption
                               3396                 :                :  * about relpos.
                               3397                 :                :  */
                               3398                 :                : Datum
                               3399                 :           4314 : WinGetFuncArgInFrame(WindowObject winobj, int argno,
                               3400                 :                :                      int relpos, int seektype, bool set_mark,
                               3401                 :                :                      bool *isnull, bool *isout)
                               3402                 :                : {
                               3403                 :                :     WindowAggState *winstate;
                               3404                 :                :     ExprContext *econtext;
                               3405                 :                :     TupleTableSlot *slot;
                               3406                 :                :     int64       abs_pos;
                               3407                 :                :     int64       mark_pos;
                               3408                 :                : 
                               3409   [ +  -  -  + ]:           4314 :     Assert(WindowObjectIsValid(winobj));
 5583                          3410                 :           4314 :     winstate = winobj->winstate;
                               3411                 :           4314 :     econtext = winstate->ss.ps.ps_ExprContext;
                               3412                 :           4314 :     slot = winstate->temp_slot_1;
                               3413                 :                : 
 5586                          3414   [ -  +  +  - ]:           4314 :     switch (seektype)
                               3415                 :                :     {
 5586 tgl@sss.pgh.pa.us        3416                 :UBC           0 :         case WINDOW_SEEK_CURRENT:
 2258                          3417         [ #  # ]:              0 :             elog(ERROR, "WINDOW_SEEK_CURRENT is not supported for WinGetFuncArgInFrame");
                               3418                 :                :             abs_pos = mark_pos = 0; /* keep compiler quiet */
                               3419                 :                :             break;
 5586 tgl@sss.pgh.pa.us        3420                 :CBC        2103 :         case WINDOW_SEEK_HEAD:
                               3421                 :                :             /* rejecting relpos < 0 is easy and simplifies code below */
 2258                          3422         [ -  + ]:           2103 :             if (relpos < 0)
 2258 tgl@sss.pgh.pa.us        3423                 :UBC           0 :                 goto out_of_frame;
 2258 tgl@sss.pgh.pa.us        3424                 :CBC        2103 :             update_frameheadpos(winstate);
 5175                          3425                 :           2082 :             abs_pos = winstate->frameheadpos + relpos;
 2258                          3426                 :           2082 :             mark_pos = abs_pos;
                               3427                 :                : 
                               3428                 :                :             /*
                               3429                 :                :              * Account for exclusion option if one is active, but advance only
                               3430                 :                :              * abs_pos not mark_pos.  This prevents changes of the current
                               3431                 :                :              * row's peer group from resulting in trying to fetch a row before
                               3432                 :                :              * some previous mark position.
                               3433                 :                :              *
                               3434                 :                :              * Note that in some corner cases such as current row being
                               3435                 :                :              * outside frame, these calculations are theoretically too simple,
                               3436                 :                :              * but it doesn't matter because we'll end up deciding the row is
                               3437                 :                :              * out of frame.  We do not attempt to avoid fetching rows past
                               3438                 :                :              * end of frame; that would happen in some cases anyway.
                               3439                 :                :              */
                               3440   [ +  +  +  +  :           2082 :             switch (winstate->frameOptions & FRAMEOPTION_EXCLUSION)
                                                 - ]
                               3441                 :                :             {
                               3442                 :           1752 :                 case 0:
                               3443                 :                :                     /* no adjustment needed */
                               3444                 :           1752 :                     break;
                               3445                 :            120 :                 case FRAMEOPTION_EXCLUDE_CURRENT_ROW:
                               3446         [ +  + ]:            120 :                     if (abs_pos >= winstate->currentpos &&
                               3447         [ +  + ]:             93 :                         winstate->currentpos >= winstate->frameheadpos)
                               3448                 :             33 :                         abs_pos++;
                               3449                 :            120 :                     break;
                               3450                 :             60 :                 case FRAMEOPTION_EXCLUDE_GROUP:
                               3451                 :             60 :                     update_grouptailpos(winstate);
                               3452         [ +  + ]:             60 :                     if (abs_pos >= winstate->groupheadpos &&
                               3453         [ +  - ]:             36 :                         winstate->grouptailpos > winstate->frameheadpos)
                               3454                 :                :                     {
                               3455                 :             36 :                         int64       overlapstart = Max(winstate->groupheadpos,
                               3456                 :                :                                                        winstate->frameheadpos);
                               3457                 :                : 
                               3458                 :             36 :                         abs_pos += winstate->grouptailpos - overlapstart;
                               3459                 :                :                     }
                               3460                 :             60 :                     break;
                               3461                 :            150 :                 case FRAMEOPTION_EXCLUDE_TIES:
                               3462                 :            150 :                     update_grouptailpos(winstate);
                               3463         [ +  + ]:            150 :                     if (abs_pos >= winstate->groupheadpos &&
                               3464         [ +  + ]:            102 :                         winstate->grouptailpos > winstate->frameheadpos)
                               3465                 :                :                     {
                               3466                 :             42 :                         int64       overlapstart = Max(winstate->groupheadpos,
                               3467                 :                :                                                        winstate->frameheadpos);
                               3468                 :                : 
                               3469         [ +  - ]:             42 :                         if (abs_pos == overlapstart)
                               3470                 :             42 :                             abs_pos = winstate->currentpos;
                               3471                 :                :                         else
 2258 tgl@sss.pgh.pa.us        3472                 :UBC           0 :                             abs_pos += winstate->grouptailpos - overlapstart - 1;
                               3473                 :                :                     }
 2258 tgl@sss.pgh.pa.us        3474                 :CBC         150 :                     break;
 2258 tgl@sss.pgh.pa.us        3475                 :UBC           0 :                 default:
                               3476         [ #  # ]:              0 :                     elog(ERROR, "unrecognized frame option state: 0x%x",
                               3477                 :                :                          winstate->frameOptions);
                               3478                 :                :                     break;
                               3479                 :                :             }
 5586 tgl@sss.pgh.pa.us        3480                 :CBC        2082 :             break;
                               3481                 :           2211 :         case WINDOW_SEEK_TAIL:
                               3482                 :                :             /* rejecting relpos > 0 is easy and simplifies code below */
 2258                          3483         [ -  + ]:           2211 :             if (relpos > 0)
 2258 tgl@sss.pgh.pa.us        3484                 :UBC           0 :                 goto out_of_frame;
 2258 tgl@sss.pgh.pa.us        3485                 :CBC        2211 :             update_frametailpos(winstate);
                               3486                 :           2208 :             abs_pos = winstate->frametailpos - 1 + relpos;
                               3487                 :                : 
                               3488                 :                :             /*
                               3489                 :                :              * Account for exclusion option if one is active.  If there is no
                               3490                 :                :              * exclusion, we can safely set the mark at the accessed row.  But
                               3491                 :                :              * if there is, we can only mark the frame start, because we can't
                               3492                 :                :              * be sure how far back in the frame the exclusion might cause us
                               3493                 :                :              * to fetch in future.  Furthermore, we have to actually check
                               3494                 :                :              * against frameheadpos here, since it's unsafe to try to fetch a
                               3495                 :                :              * row before frame start if the mark might be there already.
                               3496                 :                :              */
                               3497   [ +  +  +  +  :           2208 :             switch (winstate->frameOptions & FRAMEOPTION_EXCLUSION)
                                                 - ]
                               3498                 :                :             {
                               3499                 :           1968 :                 case 0:
                               3500                 :                :                     /* no adjustment needed */
                               3501                 :           1968 :                     mark_pos = abs_pos;
                               3502                 :           1968 :                     break;
                               3503                 :             60 :                 case FRAMEOPTION_EXCLUDE_CURRENT_ROW:
                               3504         [ +  + ]:             60 :                     if (abs_pos <= winstate->currentpos &&
                               3505         [ +  - ]:              6 :                         winstate->currentpos < winstate->frametailpos)
                               3506                 :              6 :                         abs_pos--;
                               3507                 :             60 :                     update_frameheadpos(winstate);
                               3508         [ +  + ]:             60 :                     if (abs_pos < winstate->frameheadpos)
                               3509                 :              3 :                         goto out_of_frame;
                               3510                 :             57 :                     mark_pos = winstate->frameheadpos;
                               3511                 :             57 :                     break;
                               3512                 :            120 :                 case FRAMEOPTION_EXCLUDE_GROUP:
                               3513                 :            120 :                     update_grouptailpos(winstate);
                               3514         [ +  + ]:            120 :                     if (abs_pos < winstate->grouptailpos &&
                               3515         [ +  - ]:             27 :                         winstate->groupheadpos < winstate->frametailpos)
                               3516                 :                :                     {
                               3517                 :             27 :                         int64       overlapend = Min(winstate->grouptailpos,
                               3518                 :                :                                                      winstate->frametailpos);
                               3519                 :                : 
                               3520                 :             27 :                         abs_pos -= overlapend - winstate->groupheadpos;
                               3521                 :                :                     }
                               3522                 :            120 :                     update_frameheadpos(winstate);
                               3523         [ +  + ]:            120 :                     if (abs_pos < winstate->frameheadpos)
                               3524                 :             27 :                         goto out_of_frame;
                               3525                 :             93 :                     mark_pos = winstate->frameheadpos;
                               3526                 :             93 :                     break;
                               3527                 :             60 :                 case FRAMEOPTION_EXCLUDE_TIES:
                               3528                 :             60 :                     update_grouptailpos(winstate);
                               3529         [ +  + ]:             60 :                     if (abs_pos < winstate->grouptailpos &&
                               3530         [ +  - ]:             18 :                         winstate->groupheadpos < winstate->frametailpos)
                               3531                 :                :                     {
                               3532                 :             18 :                         int64       overlapend = Min(winstate->grouptailpos,
                               3533                 :                :                                                      winstate->frametailpos);
                               3534                 :                : 
                               3535         [ +  - ]:             18 :                         if (abs_pos == overlapend - 1)
                               3536                 :             18 :                             abs_pos = winstate->currentpos;
                               3537                 :                :                         else
 2258 tgl@sss.pgh.pa.us        3538                 :UBC           0 :                             abs_pos -= overlapend - 1 - winstate->groupheadpos;
                               3539                 :                :                     }
 2258 tgl@sss.pgh.pa.us        3540                 :CBC          60 :                     update_frameheadpos(winstate);
                               3541         [ -  + ]:             60 :                     if (abs_pos < winstate->frameheadpos)
 2258 tgl@sss.pgh.pa.us        3542                 :UBC           0 :                         goto out_of_frame;
 2258 tgl@sss.pgh.pa.us        3543                 :CBC          60 :                     mark_pos = winstate->frameheadpos;
                               3544                 :             60 :                     break;
 2258 tgl@sss.pgh.pa.us        3545                 :UBC           0 :                 default:
                               3546         [ #  # ]:              0 :                     elog(ERROR, "unrecognized frame option state: 0x%x",
                               3547                 :                :                          winstate->frameOptions);
                               3548                 :                :                     mark_pos = 0;   /* keep compiler quiet */
                               3549                 :                :                     break;
                               3550                 :                :             }
 5586 tgl@sss.pgh.pa.us        3551                 :CBC        2178 :             break;
 5586 tgl@sss.pgh.pa.us        3552                 :UBC           0 :         default:
                               3553         [ #  # ]:              0 :             elog(ERROR, "unrecognized window seek type: %d", seektype);
                               3554                 :                :             abs_pos = mark_pos = 0; /* keep compiler quiet */
                               3555                 :                :             break;
                               3556                 :                :     }
                               3557                 :                : 
 2258 tgl@sss.pgh.pa.us        3558         [ +  + ]:CBC        4260 :     if (!window_gettupleslot(winobj, abs_pos, slot))
                               3559                 :            198 :         goto out_of_frame;
                               3560                 :                : 
                               3561                 :                :     /* The code above does not detect all out-of-frame cases, so check */
                               3562         [ +  + ]:           4062 :     if (row_is_in_frame(winstate, abs_pos, slot) <= 0)
                               3563                 :            150 :         goto out_of_frame;
                               3564                 :                : 
                               3565         [ -  + ]:           3897 :     if (isout)
 2258 tgl@sss.pgh.pa.us        3566                 :UBC           0 :         *isout = false;
 2258 tgl@sss.pgh.pa.us        3567         [ +  + ]:CBC        3897 :     if (set_mark)
                               3568                 :           3876 :         WinSetMarkPosition(winobj, mark_pos);
                               3569                 :           3897 :     econtext->ecxt_outertuple = slot;
                               3570                 :           3897 :     return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
                               3571                 :                :                         econtext, isnull);
                               3572                 :                : 
                               3573                 :            378 : out_of_frame:
                               3574         [ -  + ]:            378 :     if (isout)
 2258 tgl@sss.pgh.pa.us        3575                 :UBC           0 :         *isout = true;
 2258 tgl@sss.pgh.pa.us        3576                 :CBC         378 :     *isnull = true;
                               3577                 :            378 :     return (Datum) 0;
                               3578                 :                : }
                               3579                 :                : 
                               3580                 :                : /*
                               3581                 :                :  * WinGetFuncArgCurrent
                               3582                 :                :  *      Evaluate a window function's argument expression on the current row.
                               3583                 :                :  *
                               3584                 :                :  * argno: argument number to evaluate (counted from 0)
                               3585                 :                :  * isnull: output argument, receives isnull status of result
                               3586                 :                :  *
                               3587                 :                :  * Note: this isn't quite equivalent to WinGetFuncArgInPartition or
                               3588                 :                :  * WinGetFuncArgInFrame targeting the current row, because it will succeed
                               3589                 :                :  * even if the WindowObject's mark has been set beyond the current row.
                               3590                 :                :  * This should generally be used for "ordinary" arguments of a window
                               3591                 :                :  * function, such as the offset argument of lead() or lag().
                               3592                 :                :  */
                               3593                 :                : Datum
 5586                          3594                 :            582 : WinGetFuncArgCurrent(WindowObject winobj, int argno, bool *isnull)
                               3595                 :                : {
                               3596                 :                :     WindowAggState *winstate;
                               3597                 :                :     ExprContext *econtext;
                               3598                 :                : 
                               3599   [ +  -  -  + ]:            582 :     Assert(WindowObjectIsValid(winobj));
                               3600                 :            582 :     winstate = winobj->winstate;
                               3601                 :                : 
                               3602                 :            582 :     econtext = winstate->ss.ps.ps_ExprContext;
                               3603                 :                : 
                               3604                 :            582 :     econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
                               3605                 :            582 :     return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
                               3606                 :                :                         econtext, isnull);
                               3607                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622