LCOV - differential code coverage report
Current view: top level - src/backend/executor - nodeWindowAgg.c (source / functions) Coverage Total Hit UBC CBC EUB ECB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 92.3 % 1290 1191 99 1191
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 31 31 31
Baseline: lcov-20250906-005545-baseline Branches: 75.7 % 998 755 243 755 7 1
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 4 4 4
(360..) days: 92.3 % 1286 1187 99 1187
Function coverage date bins:
(360..) days: 100.0 % 31 31 31
Branch coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 75.0 % 1004 753 243 753 7 1

 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-2025, 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
 6096 tgl@sss.pgh.pa.us         207                 :CBC        2012 : 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                 :                :      */
 4165                           218         [ +  + ]:           2012 :     if (peraggstate->aggcontext != winstate->aggcontext)
  661 nathan@postgresql.or      219                 :           1447 :         MemoryContextReset(peraggstate->aggcontext);
                                220                 :                : 
 6096 tgl@sss.pgh.pa.us         221         [ +  + ]:           2012 :     if (peraggstate->initValueIsNull)
                                222                 :            765 :         peraggstate->transValue = peraggstate->initValue;
                                223                 :                :     else
                                224                 :                :     {
 4165                           225                 :           1247 :         oldContext = MemoryContextSwitchTo(peraggstate->aggcontext);
 6096                           226                 :           2494 :         peraggstate->transValue = datumCopy(peraggstate->initValue,
                                227                 :           1247 :                                             peraggstate->transtypeByVal,
                                228                 :           1247 :                                             peraggstate->transtypeLen);
                                229                 :           1247 :         MemoryContextSwitchTo(oldContext);
                                230                 :                :     }
                                231                 :           2012 :     peraggstate->transValueIsNull = peraggstate->initValueIsNull;
 4165                           232                 :           2012 :     peraggstate->transValueCount = 0;
                                233                 :           2012 :     peraggstate->resultValue = (Datum) 0;
 6093                           234                 :           2012 :     peraggstate->resultValueIsNull = true;
 6096                           235                 :           2012 : }
                                236                 :                : 
                                237                 :                : /*
                                238                 :                :  * advance_windowaggregate
                                239                 :                :  * parallel to advance_aggregates in nodeAgg.c
                                240                 :                :  */
                                241                 :                : static void
                                242                 :          88617 : advance_windowaggregate(WindowAggState *winstate,
                                243                 :                :                         WindowStatePerFunc perfuncstate,
                                244                 :                :                         WindowStatePerAgg peraggstate)
                                245                 :                : {
 2415 andres@anarazel.de        246                 :          88617 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
 5931 bruce@momjian.us          247                 :          88617 :     WindowFuncExprState *wfuncstate = perfuncstate->wfuncstate;
                                248                 :          88617 :     int         numArguments = perfuncstate->numArguments;
                                249                 :                :     Datum       newVal;
                                250                 :                :     ListCell   *arg;
                                251                 :                :     int         i;
                                252                 :                :     MemoryContext oldContext;
 6096 tgl@sss.pgh.pa.us         253                 :          88617 :     ExprContext *econtext = winstate->tmpcontext;
 4435 noah@leadboat.com         254                 :          88617 :     ExprState  *filter = wfuncstate->aggfilter;
                                255                 :                : 
 6096 tgl@sss.pgh.pa.us         256                 :          88617 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
                                257                 :                : 
                                258                 :                :     /* Skip anything FILTERed out */
 4435 noah@leadboat.com         259         [ +  + ]:          88617 :     if (filter)
                                260                 :                :     {
                                261                 :                :         bool        isnull;
 3152 andres@anarazel.de        262                 :            171 :         Datum       res = ExecEvalExpr(filter, econtext, &isnull);
                                263                 :                : 
 4435 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 */
 6096 tgl@sss.pgh.pa.us         272                 :          88536 :     i = 1;
                                273   [ +  +  +  +  :         146847 :     foreach(arg, wfuncstate->args)
                                              +  + ]
                                274                 :                :     {
 5931 bruce@momjian.us          275                 :          58311 :         ExprState  *argstate = (ExprState *) lfirst(arg);
                                276                 :                : 
 2415 andres@anarazel.de        277                 :          58311 :         fcinfo->args[i].value = ExecEvalExpr(argstate, econtext,
                                278                 :                :                                              &fcinfo->args[i].isnull);
 6096 tgl@sss.pgh.pa.us         279                 :          58311 :         i++;
                                280                 :                :     }
                                281                 :                : 
                                282         [ +  + ]:          88536 :     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         [ +  + ]:          50914 :         for (i = 1; i <= numArguments; i++)
                                290                 :                :         {
 2415 andres@anarazel.de        291         [ +  + ]:          10376 :             if (fcinfo->args[i].isnull)
                                292                 :                :             {
 6096 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                 :                :          */
 4165                           307   [ +  +  +  + ]:          40538 :         if (peraggstate->transValueCount == 0 && peraggstate->transValueIsNull)
                                308                 :                :         {
                                309                 :            216 :             MemoryContextSwitchTo(peraggstate->aggcontext);
 2415 andres@anarazel.de        310                 :            432 :             peraggstate->transValue = datumCopy(fcinfo->args[1].value,
 5931 bruce@momjian.us          311                 :            216 :                                                 peraggstate->transtypeByVal,
                                312                 :            216 :                                                 peraggstate->transtypeLen);
 6096 tgl@sss.pgh.pa.us         313                 :            216 :             peraggstate->transValueIsNull = false;
 4165                           314                 :            216 :             peraggstate->transValueCount = 1;
 6096                           315                 :            216 :             MemoryContextSwitchTo(oldContext);
                                316                 :            216 :             return;
                                317                 :                :         }
                                318                 :                : 
                                319         [ -  + ]:          40322 :         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                 :                :              */
 6096 tgl@sss.pgh.pa.us         329                 :UBC           0 :             MemoryContextSwitchTo(oldContext);
 4165                           330         [ #  # ]:              0 :             Assert(!OidIsValid(peraggstate->invtransfn_oid));
 6096                           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                 :                :      */
 6096 tgl@sss.pgh.pa.us         339                 :CBC       88221 :     InitFunctionCallInfoData(*fcinfo, &(peraggstate->transfn),
                                340                 :                :                              numArguments + 1,
                                341                 :                :                              perfuncstate->winCollation,
                                342                 :                :                              (Node *) winstate, NULL);
 2415 andres@anarazel.de        343                 :          88221 :     fcinfo->args[0].value = peraggstate->transValue;
                                344                 :          88221 :     fcinfo->args[0].isnull = peraggstate->transValueIsNull;
 4165 tgl@sss.pgh.pa.us         345                 :          88221 :     winstate->curaggcontext = peraggstate->aggcontext;
 6096                           346                 :          88221 :     newVal = FunctionCallInvoke(fcinfo);
 4165                           347                 :          88215 :     winstate->curaggcontext = NULL;
                                348                 :                : 
                                349                 :                :     /*
                                350                 :                :      * Moving-aggregate transition functions must not return null, see
                                351                 :                :      * advance_windowaggregate_base().
                                352                 :                :      */
                                353   [ -  +  -  - ]:          88215 :     if (fcinfo->isnull && OidIsValid(peraggstate->invtransfn_oid))
 4165 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                 :                :      */
 4165 tgl@sss.pgh.pa.us         364                 :CBC       88215 :     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                 :                :      */
 6096                           374   [ +  +  +  + ]:          92394 :     if (!peraggstate->transtypeByVal &&
                                375                 :           4179 :         DatumGetPointer(newVal) != DatumGetPointer(peraggstate->transValue))
                                376                 :                :     {
                                377         [ +  - ]:            480 :         if (!fcinfo->isnull)
                                378                 :                :         {
 4165                           379                 :            480 :             MemoryContextSwitchTo(peraggstate->aggcontext);
 3233                           380   [ +  -  +  - ]:            480 :             if (DatumIsReadWriteExpandedObject(newVal,
                                381                 :                :                                                false,
                                382         [ +  + ]:            483 :                                                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                 :                :         }
 6096                           390         [ +  + ]:            480 :         if (!peraggstate->transValueIsNull)
                                391                 :                :         {
 3233                           392   [ +  -  -  +  :            450 :             if (DatumIsReadWriteExpandedObject(peraggstate->transValue,
                                              -  - ]
                                393                 :                :                                                false,
                                394                 :                :                                                peraggstate->transtypeLen))
 3233 tgl@sss.pgh.pa.us         395                 :UBC           0 :                 DeleteExpandedObject(peraggstate->transValue);
                                396                 :                :             else
 3233 tgl@sss.pgh.pa.us         397                 :CBC         450 :                 pfree(DatumGetPointer(peraggstate->transValue));
                                398                 :                :         }
                                399                 :                :     }
                                400                 :                : 
 6096                           401                 :          88215 :     MemoryContextSwitchTo(oldContext);
                                402                 :          88215 :     peraggstate->transValue = newVal;
                                403                 :          88215 :     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
 4165                           419                 :           2313 : advance_windowaggregate_base(WindowAggState *winstate,
                                420                 :                :                              WindowStatePerFunc perfuncstate,
                                421                 :                :                              WindowStatePerAgg peraggstate)
                                422                 :                : {
 2415 andres@anarazel.de        423                 :           2313 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
 4165 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;
 3152 andres@anarazel.de        439                 :             51 :         Datum       res = ExecEvalExpr(filter, econtext, &isnull);
                                440                 :                : 
 4165 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                 :                : 
 2415 andres@anarazel.de        454                 :           2280 :         fcinfo->args[i].value = ExecEvalExpr(argstate, econtext,
                                455                 :                :                                              &fcinfo->args[i].isnull);
 4165 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                 :                :         {
 2415 andres@anarazel.de        468         [ +  + ]:           1416 :             if (fcinfo->args[i].isnull)
                                469                 :                :             {
 4165 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)
 4165 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                 :                :      */
 4165 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                 :                :                              (Node *) winstate, NULL);
 2415 andres@anarazel.de        514                 :           2205 :     fcinfo->args[0].value = peraggstate->transValue;
                                515                 :           2205 :     fcinfo->args[0].isnull = peraggstate->transValueIsNull;
 4165 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                 :            112 :         MemoryContextSwitchTo(oldContext);
                                526                 :            112 :         return false;
                                527                 :                :     }
                                528                 :                : 
                                529                 :                :     /* Update number of rows included in transValue */
                                530                 :           2093 :     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   [ +  +  +  + ]:           3158 :     if (!peraggstate->transtypeByVal &&
                                544                 :           1065 :         DatumGetPointer(newVal) != DatumGetPointer(peraggstate->transValue))
                                545                 :                :     {
                                546         [ +  - ]:            333 :         if (!fcinfo->isnull)
                                547                 :                :         {
                                548                 :            333 :             MemoryContextSwitchTo(peraggstate->aggcontext);
 3233                           549   [ +  -  -  - ]:            333 :             if (DatumIsReadWriteExpandedObject(newVal,
                                550                 :                :                                                false,
                                551         [ -  + ]:            333 :                                                peraggstate->transtypeLen) &&
 3233 tgl@sss.pgh.pa.us         552         [ #  # ]:UBC           0 :                 MemoryContextGetParent(DatumGetEOHP(newVal)->eoh_context) == CurrentMemoryContext)
                                553                 :                :                  /* do nothing */ ;
                                554                 :                :             else
 3233 tgl@sss.pgh.pa.us         555                 :CBC         333 :                 newVal = datumCopy(newVal,
                                556                 :            333 :                                    peraggstate->transtypeByVal,
                                557                 :            333 :                                    peraggstate->transtypeLen);
                                558                 :                :         }
 4165                           559         [ +  - ]:            333 :         if (!peraggstate->transValueIsNull)
                                560                 :                :         {
 3233                           561   [ +  -  -  +  :            333 :             if (DatumIsReadWriteExpandedObject(peraggstate->transValue,
                                              -  - ]
                                562                 :                :                                                false,
                                563                 :                :                                                peraggstate->transtypeLen))
 3233 tgl@sss.pgh.pa.us         564                 :UBC           0 :                 DeleteExpandedObject(peraggstate->transValue);
                                565                 :                :             else
 3233 tgl@sss.pgh.pa.us         566                 :CBC         333 :                 pfree(DatumGetPointer(peraggstate->transValue));
                                567                 :                :         }
                                568                 :                :     }
                                569                 :                : 
 4165                           570                 :           2093 :     MemoryContextSwitchTo(oldContext);
                                571                 :           2093 :     peraggstate->transValue = newVal;
                                572                 :           2093 :     peraggstate->transValueIsNull = fcinfo->isnull;
                                573                 :                : 
                                574                 :           2093 :     return true;
                                575                 :                : }
                                576                 :                : 
                                577                 :                : /*
                                578                 :                :  * finalize_windowaggregate
                                579                 :                :  * parallel to finalize_aggregate in nodeAgg.c
                                580                 :                :  */
                                581                 :                : static void
 6096                           582                 :           5313 : finalize_windowaggregate(WindowAggState *winstate,
                                583                 :                :                          WindowStatePerFunc perfuncstate,
                                584                 :                :                          WindowStatePerAgg peraggstate,
                                585                 :                :                          Datum *result, bool *isnull)
                                586                 :                : {
                                587                 :                :     MemoryContext oldContext;
                                588                 :                : 
                                589                 :           5313 :     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         [ +  + ]:           5313 :     if (OidIsValid(peraggstate->finalfn_oid))
                                595                 :                :     {
 2415 andres@anarazel.de        596                 :           2942 :         LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
 4154 tgl@sss.pgh.pa.us         597                 :           2942 :         int         numFinalArgs = peraggstate->numFinalArgs;
                                598                 :                :         bool        anynull;
                                599                 :                :         int         i;
                                600                 :                : 
 2415 andres@anarazel.de        601                 :           2942 :         InitFunctionCallInfoData(fcinfodata.fcinfo, &(peraggstate->finalfn),
                                602                 :                :                                  numFinalArgs,
                                603                 :                :                                  perfuncstate->winCollation,
                                604                 :                :                                  (Node *) 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;
 4154 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                 :                :         {
 2415 andres@anarazel.de        615                 :             50 :             fcinfo->args[i].value = (Datum) 0;
                                616                 :             50 :             fcinfo->args[i].isnull = true;
 4154 tgl@sss.pgh.pa.us         617                 :             50 :             anynull = true;
                                618                 :                :         }
                                619                 :                : 
 2415 andres@anarazel.de        620   [ +  +  -  + ]:           2942 :         if (fcinfo->flinfo->fn_strict && anynull)
                                621                 :                :         {
                                622                 :                :             /* don't call a strict function with NULL inputs */
 6096 tgl@sss.pgh.pa.us         623                 :UBC           0 :             *result = (Datum) 0;
                                624                 :              0 :             *isnull = true;
                                625                 :                :         }
                                626                 :                :         else
                                627                 :                :         {
                                628                 :                :             Datum       res;
                                629                 :                : 
 4165 tgl@sss.pgh.pa.us         630                 :CBC        2942 :             winstate->curaggcontext = peraggstate->aggcontext;
  874                           631                 :           2942 :             res = FunctionCallInvoke(fcinfo);
 4165                           632                 :           2936 :             winstate->curaggcontext = NULL;
 2415 andres@anarazel.de        633                 :           2936 :             *isnull = fcinfo->isnull;
  874 tgl@sss.pgh.pa.us         634   [ +  +  +  + ]:           2936 :             *result = MakeExpandedObjectReadOnly(res,
                                635                 :                :                                                  fcinfo->isnull,
                                636                 :                :                                                  peraggstate->resulttypeLen);
                                637                 :                :         }
                                638                 :                :     }
                                639                 :                :     else
                                640                 :                :     {
 1066                           641                 :           2371 :         *result =
                                642   [ +  +  +  + ]:           2371 :             MakeExpandedObjectReadOnly(peraggstate->transValue,
                                643                 :                :                                        peraggstate->transValueIsNull,
                                644                 :                :                                        peraggstate->transtypeLen);
 6096                           645                 :           2371 :         *isnull = peraggstate->transValueIsNull;
                                646                 :                :     }
                                647                 :                : 
                                648                 :           5307 :     MemoryContextSwitchTo(oldContext);
                                649                 :           5307 : }
                                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                 :          80092 : 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                 :          80092 :     numaggs = winstate->numaggs;
                                678         [ -  + ]:          80092 :     if (numaggs == 0)
 6096 tgl@sss.pgh.pa.us         679                 :UBC           0 :         return;                 /* nothing to do */
                                680                 :                : 
                                681                 :                :     /* final output execution is in ps_ExprContext */
 6096 tgl@sss.pgh.pa.us         682                 :CBC       80092 :     econtext = winstate->ss.ps.ps_ExprContext;
 5685                           683                 :          80092 :     agg_winobj = winstate->agg_winobj;
                                684                 :          80092 :     agg_row_slot = winstate->agg_row_slot;
 4165                           685                 :          80092 :     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                 :                :      */
 2768                           736                 :          80092 :     update_frameheadpos(winstate);
 4165                           737         [ -  + ]:          80089 :     if (winstate->frameheadpos < winstate->aggregatedbase)
 4165 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                 :                :      */
 4165 tgl@sss.pgh.pa.us         751         [ +  + ]:CBC       80089 :     if (winstate->aggregatedbase == winstate->frameheadpos &&
                                752         [ +  + ]:          78200 :         (winstate->frameOptions & (FRAMEOPTION_END_UNBOUNDED_FOLLOWING |
                                753                 :          77240 :                                    FRAMEOPTION_END_CURRENT_ROW)) &&
 2768                           754         [ +  + ]:          77240 :         !(winstate->frameOptions & FRAMEOPTION_EXCLUSION) &&
 4165                           755         [ +  + ]:          77150 :         winstate->aggregatedbase <= winstate->currentpos &&
                                756         [ +  + ]:          77132 :         winstate->aggregatedupto > winstate->currentpos)
                                757                 :                :     {
 6096                           758         [ +  + ]:         151316 :         for (i = 0; i < numaggs; i++)
                                759                 :                :         {
                                760                 :          75661 :             peraggstate = &winstate->peragg[i];
                                761                 :          75661 :             wfuncno = peraggstate->wfuncno;
 4165                           762                 :          75661 :             econtext->ecxt_aggvalues[wfuncno] = peraggstate->resultValue;
                                763                 :          75661 :             econtext->ecxt_aggnulls[wfuncno] = peraggstate->resultValueIsNull;
                                764                 :                :         }
                                765                 :          75655 :         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                 :           4434 :     numaggs_restart = 0;
                                784         [ +  + ]:           9759 :     for (i = 0; i < numaggs; i++)
                                785                 :                :     {
                                786                 :           5325 :         peraggstate = &winstate->peragg[i];
                                787         [ +  + ]:           5325 :         if (winstate->currentpos == 0 ||
                                788         [ +  + ]:           4300 :             (winstate->aggregatedbase != winstate->frameheadpos &&
                                789         [ +  + ]:           2603 :              !OidIsValid(peraggstate->invtransfn_oid)) ||
 2768                           790         [ +  + ]:           4262 :             (winstate->frameOptions & FRAMEOPTION_EXCLUSION) ||
 4165                           791         [ +  + ]:           3704 :             winstate->aggregatedupto <= winstate->frameheadpos)
                                792                 :                :         {
                                793                 :           1855 :             peraggstate->restart = true;
                                794                 :           1855 :             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         [ +  + ]:           6018 :     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))
 4165 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 */
 4165 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                 :            112 :                 peraggstate->restart = true;
                                841                 :            112 :                 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                 :           4434 :     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         [ +  + ]:           4434 :     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         [ +  + ]:           4434 :     if (numaggs_restart > 0)
  661 nathan@postgresql.or      878                 :           1859 :         MemoryContextReset(winstate->aggcontext);
 4165 tgl@sss.pgh.pa.us         879         [ +  + ]:           9759 :     for (i = 0; i < numaggs; i++)
                                880                 :                :     {
                                881                 :           5325 :         peraggstate = &winstate->peragg[i];
                                882                 :                : 
                                883                 :                :         /* Aggregates using the shared ctx must restart if *any* agg does */
                                884   [ +  +  +  +  :           5325 :         Assert(peraggstate->aggcontext != winstate->aggcontext ||
                                              -  + ]
                                885                 :                :                numaggs_restart == 0 ||
                                886                 :                :                peraggstate->restart);
                                887                 :                : 
                                888         [ +  + ]:           5325 :         if (peraggstate->restart)
                                889                 :                :         {
 6093                           890                 :           1967 :             wfuncno = peraggstate->wfuncno;
 4165                           891                 :           1967 :             initialize_windowaggregate(winstate,
                                892                 :           1967 :                                        &winstate->perfunc[wfuncno],
                                893                 :                :                                        peraggstate);
                                894                 :                :         }
                                895         [ +  + ]:           3358 :         else if (!peraggstate->resultValueIsNull)
                                896                 :                :         {
                                897         [ +  + ]:           3241 :             if (!peraggstate->resulttypeByVal)
                                898                 :           1076 :                 pfree(DatumGetPointer(peraggstate->resultValue));
                                899                 :           3241 :             peraggstate->resultValue = (Datum) 0;
                                900                 :           3241 :             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                 :           4434 :     aggregatedupto_nonrestarted = winstate->aggregatedupto;
                                915         [ +  + ]:           4434 :     if (numaggs_restart > 0 &&
                                916         [ +  + ]:           1859 :         winstate->aggregatedupto != winstate->frameheadpos)
                                917                 :                :     {
                                918                 :            699 :         winstate->aggregatedupto = winstate->frameheadpos;
                                919                 :            699 :         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 (;;)
 6096                           930                 :          88161 :     {
                                931                 :                :         int         ret;
                                932                 :                : 
                                933                 :                :         /* Fetch next row if we didn't already */
 6093                           934   [ +  -  +  + ]:          92595 :         if (TupIsNull(agg_row_slot))
                                935                 :                :         {
 5685                           936         [ +  + ]:          90664 :             if (!window_gettupleslot(agg_winobj, winstate->aggregatedupto,
                                937                 :                :                                      agg_row_slot))
 6093                           938                 :           2071 :                 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                 :                :          */
 2768                           945                 :          90524 :         ret = row_is_in_frame(winstate, winstate->aggregatedupto, agg_row_slot);
                                946         [ +  + ]:          90518 :         if (ret < 0)
 6093                           947                 :           2351 :             break;
 2768                           948         [ +  + ]:          88167 :         if (ret == 0)
                                949                 :            948 :             goto next_tuple;
                                950                 :                : 
                                951                 :                :         /* Set tuple context for evaluation of aggregate arguments */
 6093                           952                 :          87219 :         winstate->tmpcontext->ecxt_outertuple = agg_row_slot;
                                953                 :                : 
                                954                 :                :         /* Accumulate row into the aggregates */
 6096                           955         [ +  + ]:         185613 :         for (i = 0; i < numaggs; i++)
                                956                 :                :         {
 6093                           957                 :          98400 :             peraggstate = &winstate->peragg[i];
                                958                 :                : 
                                959                 :                :             /* Non-restarted aggs skip until aggregatedupto_nonrestarted */
 4165                           960         [ +  + ]:          98400 :             if (!peraggstate->restart &&
                                961         [ +  + ]:          59609 :                 winstate->aggregatedupto < aggregatedupto_nonrestarted)
                                962                 :           9783 :                 continue;
                                963                 :                : 
 6093                           964                 :          88617 :             wfuncno = peraggstate->wfuncno;
 6096                           965                 :          88617 :             advance_windowaggregate(winstate,
                                966                 :          88617 :                                     &winstate->perfunc[wfuncno],
                                967                 :                :                                     peraggstate);
                                968                 :                :         }
                                969                 :                : 
 2768                           970                 :          87213 : next_tuple:
                                971                 :                :         /* Reset per-input-tuple context after each tuple */
 6096                           972                 :          88161 :         ResetExprContext(winstate->tmpcontext);
                                973                 :                : 
                                974                 :                :         /* And advance the aggregated-row state */
                                975                 :          88161 :         winstate->aggregatedupto++;
 6093                           976                 :          88161 :         ExecClearTuple(agg_row_slot);
                                977                 :                :     }
                                978                 :                : 
                                979                 :                :     /* The frame's end is not supposed to move backwards, ever */
 4165                           980         [ -  + ]:           4422 :     Assert(aggregatedupto_nonrestarted <= winstate->aggregatedupto);
                                981                 :                : 
                                982                 :                :     /*
                                983                 :                :      * finalize aggregates and fill result/isnull fields.
                                984                 :                :      */
 6096                           985         [ +  + ]:           9729 :     for (i = 0; i < numaggs; i++)
                                986                 :                :     {
                                987                 :                :         Datum      *result;
                                988                 :                :         bool       *isnull;
                                989                 :                : 
                                990                 :           5313 :         peraggstate = &winstate->peragg[i];
                                991                 :           5313 :         wfuncno = peraggstate->wfuncno;
                                992                 :           5313 :         result = &econtext->ecxt_aggvalues[wfuncno];
                                993                 :           5313 :         isnull = &econtext->ecxt_aggnulls[wfuncno];
                                994                 :           5313 :         finalize_windowaggregate(winstate,
                                995                 :           5313 :                                  &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                 :                :          */
 4165                          1006   [ +  +  +  + ]:           5307 :         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                 :                :         {
 6096                          1017                 :           3931 :             peraggstate->resultValue = *result;
                               1018                 :                :         }
                               1019                 :           5307 :         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                 :         434228 : eval_windowfunction(WindowAggState *winstate, WindowStatePerFunc perfuncstate,
                               1034                 :                :                     Datum *result, bool *isnull)
                               1035                 :                : {
 2415 andres@anarazel.de       1036                 :         434228 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
                               1037                 :                :     MemoryContext oldContext;
                               1038                 :                : 
 6096 tgl@sss.pgh.pa.us        1039                 :         434228 :     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                 :                :      */
 2415 andres@anarazel.de       1047                 :         434228 :     InitFunctionCallInfoData(*fcinfo, &(perfuncstate->flinfo),
                               1048                 :                :                              perfuncstate->numArguments,
                               1049                 :                :                              perfuncstate->winCollation,
                               1050                 :                :                              (Node *) perfuncstate->winobj, NULL);
                               1051                 :                :     /* Just in case, make all the regular argument slots be null */
                               1052         [ +  + ]:         557405 :     for (int argno = 0; argno < perfuncstate->numArguments; argno++)
                               1053                 :         123177 :         fcinfo->args[argno].isnull = true;
                               1054                 :                :     /* Window functions don't have a current aggregate context, either */
 4165 tgl@sss.pgh.pa.us        1055                 :         434228 :     winstate->curaggcontext = NULL;
                               1056                 :                : 
 2415 andres@anarazel.de       1057                 :         434228 :     *result = FunctionCallInvoke(fcinfo);
                               1058                 :         434183 :     *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   [ +  +  +  + ]:         434183 :     if (!perfuncstate->resulttypeByVal && !fcinfo->isnull &&
 1066 tgl@sss.pgh.pa.us        1068         [ +  + ]:            510 :         winstate->numfuncs > 1)
 6096                          1069                 :             54 :         *result = datumCopy(*result,
                               1070                 :             54 :                             perfuncstate->resulttypeByVal,
                               1071                 :             54 :                             perfuncstate->resulttypeLen);
                               1072                 :                : 
                               1073                 :         434183 :     MemoryContextSwitchTo(oldContext);
                               1074                 :         434183 : }
                               1075                 :                : 
                               1076                 :                : /*
                               1077                 :                :  * prepare_tuplestore
                               1078                 :                :  *      Prepare the tuplestore and all of the required read pointers for the
                               1079                 :                :  *      WindowAggState's frameOptions.
                               1080                 :                :  *
                               1081                 :                :  * Note: We use pg_noinline to avoid bloating the calling function with code
                               1082                 :                :  * which is only called once.
                               1083                 :                :  */
                               1084                 :                : static pg_noinline void
  366 drowley@postgresql.o     1085                 :           1047 : prepare_tuplestore(WindowAggState *winstate)
                               1086                 :                : {
 2768 tgl@sss.pgh.pa.us        1087                 :           1047 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
 2614                          1088                 :           1047 :     int         frameOptions = winstate->frameOptions;
 5931 bruce@momjian.us         1089                 :           1047 :     int         numfuncs = winstate->numfuncs;
                               1090                 :                : 
                               1091                 :                :     /* we shouldn't be called if this was done already */
  366 drowley@postgresql.o     1092         [ -  + ]:           1047 :     Assert(winstate->buffer == NULL);
                               1093                 :                : 
                               1094                 :                :     /* Create new tuplestore */
 6096 tgl@sss.pgh.pa.us        1095                 :           1047 :     winstate->buffer = tuplestore_begin_heap(false, false, work_mem);
                               1096                 :                : 
                               1097                 :                :     /*
                               1098                 :                :      * Set up read pointers for the tuplestore.  The current pointer doesn't
                               1099                 :                :      * need BACKWARD capability, but the per-window-function read pointers do,
                               1100                 :                :      * and the aggregate pointer does if we might need to restart aggregation.
                               1101                 :                :      */
                               1102                 :           1047 :     winstate->current_ptr = 0;   /* read pointer 0 is pre-allocated */
                               1103                 :                : 
                               1104                 :                :     /* reset default REWIND capability bit for current ptr */
                               1105                 :           1047 :     tuplestore_set_eflags(winstate->buffer, 0);
                               1106                 :                : 
                               1107                 :                :     /* create read pointers for aggregates, if needed */
                               1108         [ +  + ]:           1047 :     if (winstate->numaggs > 0)
                               1109                 :                :     {
 5685                          1110                 :            559 :         WindowObject agg_winobj = winstate->agg_winobj;
                               1111                 :            559 :         int         readptr_flags = 0;
                               1112                 :                : 
                               1113                 :                :         /*
                               1114                 :                :          * If the frame head is potentially movable, or we have an EXCLUSION
                               1115                 :                :          * clause, we might need to restart aggregation ...
                               1116                 :                :          */
 2614                          1117         [ +  + ]:            559 :         if (!(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) ||
                               1118         [ +  + ]:            192 :             (frameOptions & FRAMEOPTION_EXCLUSION))
                               1119                 :                :         {
                               1120                 :                :             /* ... so create a mark pointer to track the frame head */
 5685                          1121                 :            376 :             agg_winobj->markptr = tuplestore_alloc_read_pointer(winstate->buffer, 0);
                               1122                 :                :             /* and the read pointer will need BACKWARD capability */
                               1123                 :            376 :             readptr_flags |= EXEC_FLAG_BACKWARD;
                               1124                 :                :         }
                               1125                 :                : 
                               1126                 :            559 :         agg_winobj->readptr = tuplestore_alloc_read_pointer(winstate->buffer,
                               1127                 :                :                                                             readptr_flags);
                               1128                 :                :     }
                               1129                 :                : 
                               1130                 :                :     /* create mark and read pointers for each real window function */
  366 drowley@postgresql.o     1131         [ +  + ]:           2409 :     for (int i = 0; i < numfuncs; i++)
                               1132                 :                :     {
 5931 bruce@momjian.us         1133                 :           1362 :         WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
                               1134                 :                : 
 6096 tgl@sss.pgh.pa.us        1135         [ +  + ]:           1362 :         if (!perfuncstate->plain_agg)
                               1136                 :                :         {
 5931 bruce@momjian.us         1137                 :            755 :             WindowObject winobj = perfuncstate->winobj;
                               1138                 :                : 
 6096 tgl@sss.pgh.pa.us        1139                 :            755 :             winobj->markptr = tuplestore_alloc_read_pointer(winstate->buffer,
                               1140                 :                :                                                             0);
                               1141                 :            755 :             winobj->readptr = tuplestore_alloc_read_pointer(winstate->buffer,
                               1142                 :                :                                                             EXEC_FLAG_BACKWARD);
                               1143                 :                :         }
                               1144                 :                :     }
                               1145                 :                : 
                               1146                 :                :     /*
                               1147                 :                :      * If we are in RANGE or GROUPS mode, then determining frame boundaries
                               1148                 :                :      * requires physical access to the frame endpoint rows, except in certain
                               1149                 :                :      * degenerate cases.  We create read pointers to point to those rows, to
                               1150                 :                :      * simplify access and ensure that the tuplestore doesn't discard the
                               1151                 :                :      * endpoint rows prematurely.  (Must create pointers in exactly the same
                               1152                 :                :      * cases that update_frameheadpos and update_frametailpos need them.)
                               1153                 :                :      */
 2768                          1154                 :           1047 :     winstate->framehead_ptr = winstate->frametail_ptr = -1; /* if not used */
                               1155                 :                : 
 2614                          1156         [ +  + ]:           1047 :     if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1157                 :                :     {
                               1158         [ +  + ]:            622 :         if (((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
                               1159         [ -  + ]:             34 :              node->ordNumCols != 0) ||
                               1160         [ +  + ]:            588 :             (frameOptions & FRAMEOPTION_START_OFFSET))
 2768                          1161                 :            361 :             winstate->framehead_ptr =
                               1162                 :            361 :                 tuplestore_alloc_read_pointer(winstate->buffer, 0);
 2614                          1163         [ +  + ]:            622 :         if (((frameOptions & FRAMEOPTION_END_CURRENT_ROW) &&
                               1164         [ +  + ]:            237 :              node->ordNumCols != 0) ||
                               1165         [ +  + ]:            465 :             (frameOptions & FRAMEOPTION_END_OFFSET))
 2768                          1166                 :            514 :             winstate->frametail_ptr =
                               1167                 :            514 :                 tuplestore_alloc_read_pointer(winstate->buffer, 0);
                               1168                 :                :     }
                               1169                 :                : 
                               1170                 :                :     /*
                               1171                 :                :      * If we have an exclusion clause that requires knowing the boundaries of
                               1172                 :                :      * the current row's peer group, we create a read pointer to track the
                               1173                 :                :      * tail position of the peer group (i.e., first row of the next peer
                               1174                 :                :      * group).  The head position does not require its own pointer because we
                               1175                 :                :      * maintain that as a side effect of advancing the current row.
                               1176                 :                :      */
                               1177                 :           1047 :     winstate->grouptail_ptr = -1;
                               1178                 :                : 
 2614                          1179         [ +  + ]:           1047 :     if ((frameOptions & (FRAMEOPTION_EXCLUDE_GROUP |
                               1180                 :             90 :                          FRAMEOPTION_EXCLUDE_TIES)) &&
 2768                          1181         [ +  + ]:             90 :         node->ordNumCols != 0)
                               1182                 :                :     {
                               1183                 :             84 :         winstate->grouptail_ptr =
                               1184                 :             84 :             tuplestore_alloc_read_pointer(winstate->buffer, 0);
                               1185                 :                :     }
  366 drowley@postgresql.o     1186                 :           1047 : }
                               1187                 :                : 
                               1188                 :                : /*
                               1189                 :                :  * begin_partition
                               1190                 :                :  * Start buffering rows of the next partition.
                               1191                 :                :  */
                               1192                 :                : static void
                               1193                 :           1705 : begin_partition(WindowAggState *winstate)
                               1194                 :                : {
                               1195                 :           1705 :     PlanState  *outerPlan = outerPlanState(winstate);
                               1196                 :           1705 :     int         numfuncs = winstate->numfuncs;
                               1197                 :                : 
                               1198                 :           1705 :     winstate->partition_spooled = false;
                               1199                 :           1705 :     winstate->framehead_valid = false;
                               1200                 :           1705 :     winstate->frametail_valid = false;
                               1201                 :           1705 :     winstate->grouptail_valid = false;
                               1202                 :           1705 :     winstate->spooled_rows = 0;
                               1203                 :           1705 :     winstate->currentpos = 0;
                               1204                 :           1705 :     winstate->frameheadpos = 0;
                               1205                 :           1705 :     winstate->frametailpos = 0;
                               1206                 :           1705 :     winstate->currentgroup = 0;
                               1207                 :           1705 :     winstate->frameheadgroup = 0;
                               1208                 :           1705 :     winstate->frametailgroup = 0;
                               1209                 :           1705 :     winstate->groupheadpos = 0;
                               1210                 :           1705 :     winstate->grouptailpos = -1; /* see update_grouptailpos */
                               1211                 :           1705 :     ExecClearTuple(winstate->agg_row_slot);
                               1212         [ +  + ]:           1705 :     if (winstate->framehead_slot)
                               1213                 :            512 :         ExecClearTuple(winstate->framehead_slot);
                               1214         [ +  + ]:           1705 :     if (winstate->frametail_slot)
                               1215                 :            851 :         ExecClearTuple(winstate->frametail_slot);
                               1216                 :                : 
                               1217                 :                :     /*
                               1218                 :                :      * If this is the very first partition, we need to fetch the first input
                               1219                 :                :      * row to store in first_part_slot.
                               1220                 :                :      */
                               1221   [ +  -  +  + ]:           1705 :     if (TupIsNull(winstate->first_part_slot))
                               1222                 :                :     {
                               1223                 :           1086 :         TupleTableSlot *outerslot = ExecProcNode(outerPlan);
                               1224                 :                : 
                               1225   [ +  +  +  + ]:           1086 :         if (!TupIsNull(outerslot))
                               1226                 :           1077 :             ExecCopySlot(winstate->first_part_slot, outerslot);
                               1227                 :                :         else
                               1228                 :                :         {
                               1229                 :                :             /* outer plan is empty, so we have nothing to do */
                               1230                 :              9 :             winstate->partition_spooled = true;
                               1231                 :              9 :             winstate->more_partitions = false;
                               1232                 :              9 :             return;
                               1233                 :                :         }
                               1234                 :                :     }
                               1235                 :                : 
                               1236                 :                :     /* Create new tuplestore if not done already. */
                               1237         [ +  + ]:           1696 :     if (unlikely(winstate->buffer == NULL))
                               1238                 :           1047 :         prepare_tuplestore(winstate);
                               1239                 :                : 
                               1240                 :           1696 :     winstate->next_partition = false;
                               1241                 :                : 
                               1242         [ +  + ]:           1696 :     if (winstate->numaggs > 0)
                               1243                 :                :     {
                               1244                 :            929 :         WindowObject agg_winobj = winstate->agg_winobj;
                               1245                 :                : 
                               1246                 :                :         /* reset mark and see positions for aggregate functions */
                               1247                 :            929 :         agg_winobj->markpos = -1;
                               1248                 :            929 :         agg_winobj->seekpos = -1;
                               1249                 :                : 
                               1250                 :                :         /* Also reset the row counters for aggregates */
                               1251                 :            929 :         winstate->aggregatedbase = 0;
                               1252                 :            929 :         winstate->aggregatedupto = 0;
                               1253                 :                :     }
                               1254                 :                : 
                               1255                 :                :     /* reset mark and seek positions for each real window function */
                               1256         [ +  + ]:           3848 :     for (int i = 0; i < numfuncs; i++)
                               1257                 :                :     {
                               1258                 :           2152 :         WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
                               1259                 :                : 
                               1260         [ +  + ]:           2152 :         if (!perfuncstate->plain_agg)
                               1261                 :                :         {
                               1262                 :           1124 :             WindowObject winobj = perfuncstate->winobj;
                               1263                 :                : 
                               1264                 :           1124 :             winobj->markpos = -1;
                               1265                 :           1124 :             winobj->seekpos = -1;
                               1266                 :                :         }
                               1267                 :                :     }
                               1268                 :                : 
                               1269                 :                :     /*
                               1270                 :                :      * Store the first tuple into the tuplestore (it's always available now;
                               1271                 :                :      * we either read it above, or saved it at the end of previous partition)
                               1272                 :                :      */
 6096 tgl@sss.pgh.pa.us        1273                 :           1696 :     tuplestore_puttupleslot(winstate->buffer, winstate->first_part_slot);
                               1274                 :           1696 :     winstate->spooled_rows++;
                               1275                 :                : }
                               1276                 :                : 
                               1277                 :                : /*
                               1278                 :                :  * Read tuples from the outer node, up to and including position 'pos', and
                               1279                 :                :  * store them into the tuplestore. If pos is -1, reads the whole partition.
                               1280                 :                :  */
                               1281                 :                : static void
                               1282                 :         927673 : spool_tuples(WindowAggState *winstate, int64 pos)
                               1283                 :                : {
 5931 bruce@momjian.us         1284                 :         927673 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
                               1285                 :                :     PlanState  *outerPlan;
                               1286                 :                :     TupleTableSlot *outerslot;
                               1287                 :                :     MemoryContext oldcontext;
                               1288                 :                : 
 6096 tgl@sss.pgh.pa.us        1289         [ +  + ]:         927673 :     if (!winstate->buffer)
                               1290                 :              3 :         return;                 /* just a safety check */
                               1291         [ +  + ]:         927670 :     if (winstate->partition_spooled)
                               1292                 :          63853 :         return;                 /* whole partition done already */
                               1293                 :                : 
                               1294                 :                :     /*
                               1295                 :                :      * When in pass-through mode we can just exhaust all tuples in the current
                               1296                 :                :      * partition.  We don't need these tuples for any further window function
                               1297                 :                :      * evaluation, however, we do need to keep them around if we're not the
                               1298                 :                :      * top-level window as another WindowAgg node above must see these.
                               1299                 :                :      */
 1247 drowley@postgresql.o     1300         [ +  + ]:         863817 :     if (winstate->status != WINDOWAGG_RUN)
                               1301                 :                :     {
                               1302   [ +  -  -  + ]:             15 :         Assert(winstate->status == WINDOWAGG_PASSTHROUGH ||
                               1303                 :                :                winstate->status == WINDOWAGG_PASSTHROUGH_STRICT);
                               1304                 :                : 
                               1305                 :             15 :         pos = -1;
                               1306                 :                :     }
                               1307                 :                : 
                               1308                 :                :     /*
                               1309                 :                :      * If the tuplestore has spilled to disk, alternate reading and writing
                               1310                 :                :      * becomes quite expensive due to frequent buffer flushes.  It's cheaper
                               1311                 :                :      * to force the entire partition to get spooled in one go.
                               1312                 :                :      *
                               1313                 :                :      * XXX this is a horrid kluge --- it'd be better to fix the performance
                               1314                 :                :      * problem inside tuplestore.  FIXME
                               1315                 :                :      */
                               1316         [ +  + ]:         863802 :     else if (!tuplestore_in_memory(winstate->buffer))
 6096 tgl@sss.pgh.pa.us        1317                 :              6 :         pos = -1;
                               1318                 :                : 
                               1319                 :         863817 :     outerPlan = outerPlanState(winstate);
                               1320                 :                : 
                               1321                 :                :     /* Must be in query context to call outerplan */
                               1322                 :         863817 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               1323                 :                : 
                               1324   [ +  +  +  + ]:        2237963 :     while (winstate->spooled_rows <= pos || pos == -1)
                               1325                 :                :     {
                               1326                 :         511950 :         outerslot = ExecProcNode(outerPlan);
                               1327   [ +  +  +  + ]:         511950 :         if (TupIsNull(outerslot))
                               1328                 :                :         {
                               1329                 :                :             /* reached the end of the last partition */
                               1330                 :           1002 :             winstate->partition_spooled = true;
                               1331                 :           1002 :             winstate->more_partitions = false;
                               1332                 :           1002 :             break;
                               1333                 :                :         }
                               1334                 :                : 
                               1335         [ +  + ]:         510948 :         if (node->partNumCols > 0)
                               1336                 :                :         {
 2760 andres@anarazel.de       1337                 :          69324 :             ExprContext *econtext = winstate->tmpcontext;
                               1338                 :                : 
                               1339                 :          69324 :             econtext->ecxt_innertuple = winstate->first_part_slot;
                               1340                 :          69324 :             econtext->ecxt_outertuple = outerslot;
                               1341                 :                : 
                               1342                 :                :             /* Check if this tuple still belongs to the current partition */
                               1343         [ +  + ]:          69324 :             if (!ExecQualAndReset(winstate->partEqfunction, econtext))
                               1344                 :                :             {
                               1345                 :                :                 /*
                               1346                 :                :                  * end of partition; copy the tuple for the next cycle.
                               1347                 :                :                  */
 6096 tgl@sss.pgh.pa.us        1348                 :            619 :                 ExecCopySlot(winstate->first_part_slot, outerslot);
                               1349                 :            619 :                 winstate->partition_spooled = true;
                               1350                 :            619 :                 winstate->more_partitions = true;
                               1351                 :            619 :                 break;
                               1352                 :                :             }
                               1353                 :                :         }
                               1354                 :                : 
                               1355                 :                :         /*
                               1356                 :                :          * Remember the tuple unless we're the top-level window and we're in
                               1357                 :                :          * pass-through mode.
                               1358                 :                :          */
 1247 drowley@postgresql.o     1359         [ +  + ]:         510329 :         if (winstate->status != WINDOWAGG_PASSTHROUGH_STRICT)
                               1360                 :                :         {
                               1361                 :                :             /* Still in partition, so save it into the tuplestore */
                               1362                 :         510323 :             tuplestore_puttupleslot(winstate->buffer, outerslot);
                               1363                 :         510323 :             winstate->spooled_rows++;
                               1364                 :                :         }
                               1365                 :                :     }
                               1366                 :                : 
 6096 tgl@sss.pgh.pa.us        1367                 :         863817 :     MemoryContextSwitchTo(oldcontext);
                               1368                 :                : }
                               1369                 :                : 
                               1370                 :                : /*
                               1371                 :                :  * release_partition
                               1372                 :                :  * clear information kept within a partition, including
                               1373                 :                :  * tuplestore and aggregate results.
                               1374                 :                :  */
                               1375                 :                : static void
                               1376                 :           2861 : release_partition(WindowAggState *winstate)
                               1377                 :                : {
                               1378                 :                :     int         i;
                               1379                 :                : 
                               1380         [ +  + ]:           6451 :     for (i = 0; i < winstate->numfuncs; i++)
                               1381                 :                :     {
 5931 bruce@momjian.us         1382                 :           3590 :         WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
                               1383                 :                : 
                               1384                 :                :         /* Release any partition-local state of this window function */
 6096 tgl@sss.pgh.pa.us        1385         [ +  + ]:           3590 :         if (perfuncstate->winobj)
                               1386                 :           1799 :             perfuncstate->winobj->localmem = NULL;
                               1387                 :                :     }
                               1388                 :                : 
                               1389                 :                :     /*
                               1390                 :                :      * Release all partition-local memory (in particular, any partition-local
                               1391                 :                :      * state that we might have trashed our pointers to in the above loop, and
                               1392                 :                :      * any aggregate temp data).  We don't rely on retail pfree because some
                               1393                 :                :      * aggregates might have allocated data we don't have direct pointers to.
                               1394                 :                :      */
  661 nathan@postgresql.or     1395                 :           2861 :     MemoryContextReset(winstate->partcontext);
                               1396                 :           2861 :     MemoryContextReset(winstate->aggcontext);
 4165 tgl@sss.pgh.pa.us        1397         [ +  + ]:           4652 :     for (i = 0; i < winstate->numaggs; i++)
                               1398                 :                :     {
                               1399         [ +  + ]:           1791 :         if (winstate->peragg[i].aggcontext != winstate->aggcontext)
  661 nathan@postgresql.or     1400                 :            966 :             MemoryContextReset(winstate->peragg[i].aggcontext);
                               1401                 :                :     }
                               1402                 :                : 
 6096 tgl@sss.pgh.pa.us        1403         [ +  + ]:           2861 :     if (winstate->buffer)
  366 drowley@postgresql.o     1404                 :           1648 :         tuplestore_clear(winstate->buffer);
 6096 tgl@sss.pgh.pa.us        1405                 :           2861 :     winstate->partition_spooled = false;
  366 drowley@postgresql.o     1406                 :           2861 :     winstate->next_partition = true;
 6096 tgl@sss.pgh.pa.us        1407                 :           2861 : }
                               1408                 :                : 
                               1409                 :                : /*
                               1410                 :                :  * row_is_in_frame
                               1411                 :                :  * Determine whether a row is in the current row's window frame according
                               1412                 :                :  * to our window framing rule
                               1413                 :                :  *
                               1414                 :                :  * The caller must have already determined that the row is in the partition
                               1415                 :                :  * and fetched it into a slot.  This function just encapsulates the framing
                               1416                 :                :  * rules.
                               1417                 :                :  *
                               1418                 :                :  * Returns:
                               1419                 :                :  * -1, if the row is out of frame and no succeeding rows can be in frame
                               1420                 :                :  * 0, if the row is out of frame but succeeding rows might be in frame
                               1421                 :                :  * 1, if the row is in frame
                               1422                 :                :  *
                               1423                 :                :  * May clobber winstate->temp_slot_2.
                               1424                 :                :  */
                               1425                 :                : static int
 6093                          1426                 :          94586 : row_is_in_frame(WindowAggState *winstate, int64 pos, TupleTableSlot *slot)
                               1427                 :                : {
 5685                          1428                 :          94586 :     int         frameOptions = winstate->frameOptions;
                               1429                 :                : 
 6093                          1430         [ -  + ]:          94586 :     Assert(pos >= 0);            /* else caller error */
                               1431                 :                : 
                               1432                 :                :     /*
                               1433                 :                :      * First, check frame starting conditions.  We might as well delegate this
                               1434                 :                :      * to update_frameheadpos always; it doesn't add any notable cost.
                               1435                 :                :      */
 2768                          1436                 :          94586 :     update_frameheadpos(winstate);
                               1437         [ +  + ]:          94586 :     if (pos < winstate->frameheadpos)
                               1438                 :             72 :         return 0;
                               1439                 :                : 
                               1440                 :                :     /*
                               1441                 :                :      * Okay so far, now check frame ending conditions.  Here, we avoid calling
                               1442                 :                :      * update_frametailpos in simple cases, so as not to spool tuples further
                               1443                 :                :      * ahead than necessary.
                               1444                 :                :      */
 5685                          1445         [ +  + ]:          94514 :     if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
                               1446                 :                :     {
                               1447         [ +  + ]:          79031 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1448                 :                :         {
                               1449                 :                :             /* rows after current row are out of frame */
                               1450         [ +  + ]:           1104 :             if (pos > winstate->currentpos)
 2768                          1451                 :            486 :                 return -1;
                               1452                 :                :         }
                               1453         [ +  - ]:          77927 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1454                 :                :         {
                               1455                 :                :             /* following row that is not peer is out of frame */
 5685                          1456         [ +  + ]:          77927 :             if (pos > winstate->currentpos &&
                               1457         [ +  + ]:          76245 :                 !are_peers(winstate, slot, winstate->ss.ss_ScanTupleSlot))
 2768                          1458                 :            632 :                 return -1;
                               1459                 :                :         }
                               1460                 :                :         else
 5685 tgl@sss.pgh.pa.us        1461                 :UBC           0 :             Assert(false);
                               1462                 :                :     }
 2768 tgl@sss.pgh.pa.us        1463         [ +  + ]:CBC       15483 :     else if (frameOptions & FRAMEOPTION_END_OFFSET)
                               1464                 :                :     {
 5685                          1465         [ +  + ]:           8967 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1466                 :                :         {
 5671 bruce@momjian.us         1467                 :           1974 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
                               1468                 :                : 
                               1469                 :                :             /* rows after current row + offset are out of frame */
 2768 tgl@sss.pgh.pa.us        1470         [ +  + ]:           1974 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
 5685                          1471                 :             57 :                 offset = -offset;
                               1472                 :                : 
                               1473         [ +  + ]:           1974 :             if (pos > winstate->currentpos + offset)
 2768                          1474                 :            576 :                 return -1;
                               1475                 :                :         }
                               1476         [ +  - ]:           6993 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1477                 :                :         {
                               1478                 :                :             /* hard cases, so delegate to update_frametailpos */
                               1479                 :           6993 :             update_frametailpos(winstate);
                               1480         [ +  + ]:           6972 :             if (pos >= winstate->frametailpos)
                               1481                 :            735 :                 return -1;
                               1482                 :                :         }
                               1483                 :                :         else
 5685 tgl@sss.pgh.pa.us        1484                 :UBC           0 :             Assert(false);
                               1485                 :                :     }
                               1486                 :                : 
                               1487                 :                :     /* Check exclusion clause */
 2768 tgl@sss.pgh.pa.us        1488         [ +  + ]:CBC       92064 :     if (frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
                               1489                 :                :     {
                               1490         [ +  + ]:           1233 :         if (pos == winstate->currentpos)
                               1491                 :            210 :             return 0;
                               1492                 :                :     }
                               1493         [ +  + ]:          90831 :     else if ((frameOptions & FRAMEOPTION_EXCLUDE_GROUP) ||
                               1494         [ +  + ]:          89400 :              ((frameOptions & FRAMEOPTION_EXCLUDE_TIES) &&
                               1495         [ +  + ]:           1485 :               pos != winstate->currentpos))
                               1496                 :                :     {
                               1497                 :           2646 :         WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
                               1498                 :                : 
                               1499                 :                :         /* If no ORDER BY, all rows are peers with each other */
                               1500         [ +  + ]:           2646 :         if (node->ordNumCols == 0)
                               1501                 :            234 :             return 0;
                               1502                 :                :         /* Otherwise, check the group boundaries */
                               1503         [ +  + ]:           2412 :         if (pos >= winstate->groupheadpos)
                               1504                 :                :         {
                               1505                 :           1296 :             update_grouptailpos(winstate);
                               1506         [ +  + ]:           1296 :             if (pos < winstate->grouptailpos)
                               1507                 :            504 :                 return 0;
                               1508                 :                :         }
                               1509                 :                :     }
                               1510                 :                : 
                               1511                 :                :     /* If we get here, it's in frame */
                               1512                 :          91116 :     return 1;
                               1513                 :                : }
                               1514                 :                : 
                               1515                 :                : /*
                               1516                 :                :  * update_frameheadpos
                               1517                 :                :  * make frameheadpos valid for the current row
                               1518                 :                :  *
                               1519                 :                :  * Note that frameheadpos is computed without regard for any window exclusion
                               1520                 :                :  * clause; the current row and/or its peers are considered part of the frame
                               1521                 :                :  * for this purpose even if they must be excluded later.
                               1522                 :                :  *
                               1523                 :                :  * May clobber winstate->temp_slot_2.
                               1524                 :                :  */
                               1525                 :                : static void
                               1526                 :         180139 : update_frameheadpos(WindowAggState *winstate)
                               1527                 :                : {
 6093                          1528                 :         180139 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
 5685                          1529                 :         180139 :     int         frameOptions = winstate->frameOptions;
                               1530                 :                :     MemoryContext oldcontext;
                               1531                 :                : 
                               1532         [ +  + ]:         180139 :     if (winstate->framehead_valid)
 6093                          1533                 :          97680 :         return;                 /* already known for current row */
                               1534                 :                : 
                               1535                 :                :     /* We may be called in a short-lived context */
 2768                          1536                 :          82459 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               1537                 :                : 
 5685                          1538         [ +  + ]:          82459 :     if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
                               1539                 :                :     {
                               1540                 :                :         /* In UNBOUNDED PRECEDING mode, frame head is always row 0 */
                               1541                 :          77319 :         winstate->frameheadpos = 0;
                               1542                 :          77319 :         winstate->framehead_valid = true;
                               1543                 :                :     }
                               1544         [ +  + ]:           5140 :     else if (frameOptions & FRAMEOPTION_START_CURRENT_ROW)
                               1545                 :                :     {
                               1546         [ +  + ]:           1402 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1547                 :                :         {
                               1548                 :                :             /* In ROWS mode, frame head is the same as current */
                               1549                 :           1188 :             winstate->frameheadpos = winstate->currentpos;
                               1550                 :           1188 :             winstate->framehead_valid = true;
                               1551                 :                :         }
 2768                          1552         [ +  - ]:            214 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1553                 :                :         {
                               1554                 :                :             /* If no ORDER BY, all rows are peers with each other */
 5685                          1555         [ -  + ]:            214 :             if (node->ordNumCols == 0)
                               1556                 :                :             {
 5685 tgl@sss.pgh.pa.us        1557                 :UBC           0 :                 winstate->frameheadpos = 0;
                               1558                 :              0 :                 winstate->framehead_valid = true;
 2768                          1559                 :              0 :                 MemoryContextSwitchTo(oldcontext);
 5685                          1560                 :              0 :                 return;
                               1561                 :                :             }
                               1562                 :                : 
                               1563                 :                :             /*
                               1564                 :                :              * In RANGE or GROUPS START_CURRENT_ROW mode, frame head is the
                               1565                 :                :              * first row that is a peer of current row.  We keep a copy of the
                               1566                 :                :              * last-known frame head row in framehead_slot, and advance as
                               1567                 :                :              * necessary.  Note that if we reach end of partition, we will
                               1568                 :                :              * leave frameheadpos = end+1 and framehead_slot empty.
                               1569                 :                :              */
 2768 tgl@sss.pgh.pa.us        1570                 :CBC         214 :             tuplestore_select_read_pointer(winstate->buffer,
                               1571                 :                :                                            winstate->framehead_ptr);
                               1572         [ +  + ]:            214 :             if (winstate->frameheadpos == 0 &&
                               1573   [ +  -  +  + ]:            106 :                 TupIsNull(winstate->framehead_slot))
                               1574                 :                :             {
                               1575                 :                :                 /* fetch first row into framehead_slot, if we didn't already */
                               1576         [ -  + ]:             41 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1577                 :                :                                              winstate->framehead_slot))
 2768 tgl@sss.pgh.pa.us        1578         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1579                 :                :             }
                               1580                 :                : 
 2768 tgl@sss.pgh.pa.us        1581   [ +  -  +  - ]:CBC         372 :             while (!TupIsNull(winstate->framehead_slot))
                               1582                 :                :             {
                               1583         [ +  + ]:            372 :                 if (are_peers(winstate, winstate->framehead_slot,
                               1584                 :                :                               winstate->ss.ss_ScanTupleSlot))
                               1585                 :            214 :                     break;      /* this row is the correct frame head */
                               1586                 :                :                 /* Note we advance frameheadpos even if the fetch fails */
                               1587                 :            158 :                 winstate->frameheadpos++;
                               1588                 :            158 :                 spool_tuples(winstate, winstate->frameheadpos);
                               1589         [ -  + ]:            158 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1590                 :                :                                              winstate->framehead_slot))
 2768 tgl@sss.pgh.pa.us        1591                 :UBC           0 :                     break;      /* end of partition */
                               1592                 :                :             }
 5685 tgl@sss.pgh.pa.us        1593                 :CBC         214 :             winstate->framehead_valid = true;
                               1594                 :                :         }
                               1595                 :                :         else
 5685 tgl@sss.pgh.pa.us        1596                 :UBC           0 :             Assert(false);
                               1597                 :                :     }
 2768 tgl@sss.pgh.pa.us        1598         [ +  - ]:CBC        3738 :     else if (frameOptions & FRAMEOPTION_START_OFFSET)
                               1599                 :                :     {
 5685                          1600         [ +  + ]:           3738 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1601                 :                :         {
                               1602                 :                :             /* In ROWS mode, bound is physically n before/after current */
 5671 bruce@momjian.us         1603                 :            786 :             int64       offset = DatumGetInt64(winstate->startOffsetValue);
                               1604                 :                : 
 2768 tgl@sss.pgh.pa.us        1605         [ +  + ]:            786 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
 5685                          1606                 :            756 :                 offset = -offset;
                               1607                 :                : 
                               1608                 :            786 :             winstate->frameheadpos = winstate->currentpos + offset;
                               1609                 :                :             /* frame head can't go before first row */
                               1610         [ +  + ]:            786 :             if (winstate->frameheadpos < 0)
                               1611                 :            114 :                 winstate->frameheadpos = 0;
 2768                          1612         [ -  + ]:            672 :             else if (winstate->frameheadpos > winstate->currentpos + 1)
                               1613                 :                :             {
                               1614                 :                :                 /* make sure frameheadpos is not past end of partition */
 5685 tgl@sss.pgh.pa.us        1615                 :UBC           0 :                 spool_tuples(winstate, winstate->frameheadpos - 1);
                               1616         [ #  # ]:              0 :                 if (winstate->frameheadpos > winstate->spooled_rows)
                               1617                 :              0 :                     winstate->frameheadpos = winstate->spooled_rows;
                               1618                 :                :             }
 5685 tgl@sss.pgh.pa.us        1619                 :CBC         786 :             winstate->framehead_valid = true;
                               1620                 :                :         }
                               1621         [ +  + ]:           2952 :         else if (frameOptions & FRAMEOPTION_RANGE)
                               1622                 :                :         {
                               1623                 :                :             /*
                               1624                 :                :              * In RANGE START_OFFSET mode, frame head is the first row that
                               1625                 :                :              * satisfies the in_range constraint relative to the current row.
                               1626                 :                :              * We keep a copy of the last-known frame head row in
                               1627                 :                :              * framehead_slot, and advance as necessary.  Note that if we
                               1628                 :                :              * reach end of partition, we will leave frameheadpos = end+1 and
                               1629                 :                :              * framehead_slot empty.
                               1630                 :                :              */
 2752                          1631                 :           2262 :             int         sortCol = node->ordColIdx[0];
                               1632                 :                :             bool        sub,
                               1633                 :                :                         less;
                               1634                 :                : 
                               1635                 :                :             /* We must have an ordering column */
 2614                          1636         [ -  + ]:           2262 :             Assert(node->ordNumCols == 1);
                               1637                 :                : 
                               1638                 :                :             /* Precompute flags for in_range checks */
 2768                          1639         [ +  + ]:           2262 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
                               1640                 :           1851 :                 sub = true;     /* subtract startOffset from current row */
                               1641                 :                :             else
                               1642                 :            411 :                 sub = false;    /* add it */
                               1643                 :           2262 :             less = false;       /* normally, we want frame head >= sum */
                               1644                 :                :             /* If sort order is descending, flip both flags */
                               1645         [ +  + ]:           2262 :             if (!winstate->inRangeAsc)
                               1646                 :                :             {
                               1647                 :            327 :                 sub = !sub;
                               1648                 :            327 :                 less = true;
                               1649                 :                :             }
                               1650                 :                : 
                               1651                 :           2262 :             tuplestore_select_read_pointer(winstate->buffer,
                               1652                 :                :                                            winstate->framehead_ptr);
                               1653         [ +  + ]:           2262 :             if (winstate->frameheadpos == 0 &&
                               1654   [ +  -  +  + ]:           1251 :                 TupIsNull(winstate->framehead_slot))
                               1655                 :                :             {
                               1656                 :                :                 /* fetch first row into framehead_slot, if we didn't already */
                               1657         [ -  + ]:            285 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1658                 :                :                                              winstate->framehead_slot))
 2768 tgl@sss.pgh.pa.us        1659         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1660                 :                :             }
                               1661                 :                : 
 2768 tgl@sss.pgh.pa.us        1662   [ +  -  +  + ]:CBC        3633 :             while (!TupIsNull(winstate->framehead_slot))
                               1663                 :                :             {
                               1664                 :                :                 Datum       headval,
                               1665                 :                :                             currval;
                               1666                 :                :                 bool        headisnull,
                               1667                 :                :                             currisnull;
                               1668                 :                : 
 2752                          1669                 :           3531 :                 headval = slot_getattr(winstate->framehead_slot, sortCol,
                               1670                 :                :                                        &headisnull);
                               1671                 :           3531 :                 currval = slot_getattr(winstate->ss.ss_ScanTupleSlot, sortCol,
                               1672                 :                :                                        &currisnull);
 2768                          1673   [ +  +  +  + ]:           3531 :                 if (headisnull || currisnull)
                               1674                 :                :                 {
                               1675                 :                :                     /* order of the rows depends only on nulls_first */
                               1676         [ +  + ]:             54 :                     if (winstate->inRangeNullsFirst)
                               1677                 :                :                     {
                               1678                 :                :                         /* advance head if head is null and curr is not */
                               1679   [ +  -  +  + ]:             24 :                         if (!headisnull || currisnull)
                               1680                 :                :                             break;
                               1681                 :                :                     }
                               1682                 :                :                     else
                               1683                 :                :                     {
                               1684                 :                :                         /* advance head if head is not null and curr is null */
                               1685   [ +  +  +  - ]:             30 :                         if (headisnull || !currisnull)
                               1686                 :                :                             break;
                               1687                 :                :                     }
                               1688                 :                :                 }
                               1689                 :                :                 else
                               1690                 :                :                 {
                               1691         [ +  + ]:           3477 :                     if (DatumGetBool(FunctionCall5Coll(&winstate->startInRangeFunc,
                               1692                 :                :                                                        winstate->inRangeColl,
                               1693                 :                :                                                        headval,
                               1694                 :                :                                                        currval,
                               1695                 :                :                                                        winstate->startOffsetValue,
                               1696                 :                :                                                        BoolGetDatum(sub),
                               1697                 :                :                                                        BoolGetDatum(less))))
                               1698                 :           2085 :                         break;  /* this row is the correct frame head */
                               1699                 :                :                 }
                               1700                 :                :                 /* Note we advance frameheadpos even if the fetch fails */
                               1701                 :           1398 :                 winstate->frameheadpos++;
                               1702                 :           1398 :                 spool_tuples(winstate, winstate->frameheadpos);
                               1703         [ +  + ]:           1398 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1704                 :                :                                              winstate->framehead_slot))
                               1705                 :             27 :                     break;      /* end of partition */
                               1706                 :                :             }
                               1707                 :           2238 :             winstate->framehead_valid = true;
                               1708                 :                :         }
                               1709         [ +  - ]:            690 :         else if (frameOptions & FRAMEOPTION_GROUPS)
                               1710                 :                :         {
                               1711                 :                :             /*
                               1712                 :                :              * In GROUPS START_OFFSET mode, frame head is the first row of the
                               1713                 :                :              * first peer group whose number satisfies the offset constraint.
                               1714                 :                :              * We keep a copy of the last-known frame head row in
                               1715                 :                :              * framehead_slot, and advance as necessary.  Note that if we
                               1716                 :                :              * reach end of partition, we will leave frameheadpos = end+1 and
                               1717                 :                :              * framehead_slot empty.
                               1718                 :                :              */
                               1719                 :            690 :             int64       offset = DatumGetInt64(winstate->startOffsetValue);
                               1720                 :                :             int64       minheadgroup;
                               1721                 :                : 
                               1722         [ +  + ]:            690 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
                               1723                 :            564 :                 minheadgroup = winstate->currentgroup - offset;
                               1724                 :                :             else
                               1725                 :            126 :                 minheadgroup = winstate->currentgroup + offset;
                               1726                 :                : 
                               1727                 :            690 :             tuplestore_select_read_pointer(winstate->buffer,
                               1728                 :                :                                            winstate->framehead_ptr);
                               1729         [ +  + ]:            690 :             if (winstate->frameheadpos == 0 &&
                               1730   [ +  -  +  + ]:            375 :                 TupIsNull(winstate->framehead_slot))
                               1731                 :                :             {
                               1732                 :                :                 /* fetch first row into framehead_slot, if we didn't already */
                               1733         [ -  + ]:            186 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1734                 :                :                                              winstate->framehead_slot))
 2768 tgl@sss.pgh.pa.us        1735         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1736                 :                :             }
                               1737                 :                : 
 2768 tgl@sss.pgh.pa.us        1738   [ +  -  +  + ]:CBC        1761 :             while (!TupIsNull(winstate->framehead_slot))
                               1739                 :                :             {
                               1740         [ +  + ]:           1059 :                 if (winstate->frameheadgroup >= minheadgroup)
                               1741                 :            660 :                     break;      /* this row is the correct frame head */
                               1742                 :            399 :                 ExecCopySlot(winstate->temp_slot_2, winstate->framehead_slot);
                               1743                 :                :                 /* Note we advance frameheadpos even if the fetch fails */
                               1744                 :            399 :                 winstate->frameheadpos++;
                               1745                 :            399 :                 spool_tuples(winstate, winstate->frameheadpos);
                               1746         [ +  + ]:            399 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1747                 :                :                                              winstate->framehead_slot))
                               1748                 :             18 :                     break;      /* end of partition */
                               1749         [ +  + ]:            381 :                 if (!are_peers(winstate, winstate->temp_slot_2,
                               1750                 :                :                                winstate->framehead_slot))
                               1751                 :            261 :                     winstate->frameheadgroup++;
                               1752                 :                :             }
                               1753                 :            690 :             ExecClearTuple(winstate->temp_slot_2);
                               1754                 :            690 :             winstate->framehead_valid = true;
                               1755                 :                :         }
                               1756                 :                :         else
 5685 tgl@sss.pgh.pa.us        1757                 :UBC           0 :             Assert(false);
                               1758                 :                :     }
                               1759                 :                :     else
                               1760                 :              0 :         Assert(false);
                               1761                 :                : 
 2768 tgl@sss.pgh.pa.us        1762                 :CBC       82435 :     MemoryContextSwitchTo(oldcontext);
                               1763                 :                : }
                               1764                 :                : 
                               1765                 :                : /*
                               1766                 :                :  * update_frametailpos
                               1767                 :                :  * make frametailpos valid for the current row
                               1768                 :                :  *
                               1769                 :                :  * Note that frametailpos is computed without regard for any window exclusion
                               1770                 :                :  * clause; the current row and/or its peers are considered part of the frame
                               1771                 :                :  * for this purpose even if they must be excluded later.
                               1772                 :                :  *
                               1773                 :                :  * May clobber winstate->temp_slot_2.
                               1774                 :                :  */
                               1775                 :                : static void
                               1776                 :         101101 : update_frametailpos(WindowAggState *winstate)
                               1777                 :                : {
 5685                          1778                 :         101101 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
                               1779                 :         101101 :     int         frameOptions = winstate->frameOptions;
                               1780                 :                :     MemoryContext oldcontext;
                               1781                 :                : 
                               1782         [ +  + ]:         101101 :     if (winstate->frametail_valid)
                               1783                 :           8910 :         return;                 /* already known for current row */
                               1784                 :                : 
                               1785                 :                :     /* We may be called in a short-lived context */
 2768                          1786                 :          92191 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               1787                 :                : 
 5685                          1788         [ +  + ]:          92191 :     if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
                               1789                 :                :     {
                               1790                 :                :         /* In UNBOUNDED FOLLOWING mode, all partition rows are in frame */
 6093                          1791                 :             90 :         spool_tuples(winstate, -1);
 2768                          1792                 :             90 :         winstate->frametailpos = winstate->spooled_rows;
 6093                          1793                 :             90 :         winstate->frametail_valid = true;
                               1794                 :                :     }
 5685                          1795         [ +  + ]:          92101 :     else if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
                               1796                 :                :     {
                               1797         [ +  + ]:          88909 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1798                 :                :         {
                               1799                 :                :             /* In ROWS mode, exactly the rows up to current are in frame */
 2768                          1800                 :             60 :             winstate->frametailpos = winstate->currentpos + 1;
 5685                          1801                 :             60 :             winstate->frametail_valid = true;
                               1802                 :                :         }
 2768                          1803         [ +  - ]:          88849 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               1804                 :                :         {
                               1805                 :                :             /* If no ORDER BY, all rows are peers with each other */
 5685                          1806         [ +  + ]:          88849 :             if (node->ordNumCols == 0)
                               1807                 :                :             {
                               1808                 :             30 :                 spool_tuples(winstate, -1);
 2768                          1809                 :             30 :                 winstate->frametailpos = winstate->spooled_rows;
 5685                          1810                 :             30 :                 winstate->frametail_valid = true;
 2768                          1811                 :             30 :                 MemoryContextSwitchTo(oldcontext);
 5685                          1812                 :             30 :                 return;
                               1813                 :                :             }
                               1814                 :                : 
                               1815                 :                :             /*
                               1816                 :                :              * In RANGE or GROUPS END_CURRENT_ROW mode, frame end is the last
                               1817                 :                :              * row that is a peer of current row, frame tail is the row after
                               1818                 :                :              * that (if any).  We keep a copy of the last-known frame tail row
                               1819                 :                :              * in frametail_slot, and advance as necessary.  Note that if we
                               1820                 :                :              * reach end of partition, we will leave frametailpos = end+1 and
                               1821                 :                :              * frametail_slot empty.
                               1822                 :                :              */
 2768                          1823                 :          88819 :             tuplestore_select_read_pointer(winstate->buffer,
                               1824                 :                :                                            winstate->frametail_ptr);
                               1825         [ +  + ]:          88819 :             if (winstate->frametailpos == 0 &&
                               1826   [ +  -  +  - ]:            341 :                 TupIsNull(winstate->frametail_slot))
                               1827                 :                :             {
                               1828                 :                :                 /* fetch first row into frametail_slot, if we didn't already */
                               1829         [ -  + ]:            341 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1830                 :                :                                              winstate->frametail_slot))
 2768 tgl@sss.pgh.pa.us        1831         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1832                 :                :             }
                               1833                 :                : 
 2768 tgl@sss.pgh.pa.us        1834   [ +  -  +  + ]:CBC      177303 :             while (!TupIsNull(winstate->frametail_slot))
                               1835                 :                :             {
                               1836         [ +  + ]:         165243 :                 if (winstate->frametailpos > winstate->currentpos &&
                               1837         [ +  + ]:         136604 :                     !are_peers(winstate, winstate->frametail_slot,
                               1838                 :                :                                winstate->ss.ss_ScanTupleSlot))
                               1839                 :          76424 :                     break;      /* this row is the frame tail */
                               1840                 :                :                 /* Note we advance frametailpos even if the fetch fails */
                               1841                 :          88819 :                 winstate->frametailpos++;
                               1842                 :          88819 :                 spool_tuples(winstate, winstate->frametailpos);
                               1843         [ +  + ]:          88819 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1844                 :                :                                              winstate->frametail_slot))
 5671 bruce@momjian.us         1845                 :            335 :                     break;      /* end of partition */
                               1846                 :                :             }
 5685 tgl@sss.pgh.pa.us        1847                 :          88819 :             winstate->frametail_valid = true;
                               1848                 :                :         }
                               1849                 :                :         else
 5685 tgl@sss.pgh.pa.us        1850                 :UBC           0 :             Assert(false);
                               1851                 :                :     }
 2768 tgl@sss.pgh.pa.us        1852         [ +  - ]:CBC        3192 :     else if (frameOptions & FRAMEOPTION_END_OFFSET)
                               1853                 :                :     {
 5685                          1854         [ +  + ]:           3192 :         if (frameOptions & FRAMEOPTION_ROWS)
                               1855                 :                :         {
                               1856                 :                :             /* In ROWS mode, bound is physically n before/after current */
 5671 bruce@momjian.us         1857                 :             90 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
                               1858                 :                : 
 2768 tgl@sss.pgh.pa.us        1859         [ -  + ]:             90 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
 5685 tgl@sss.pgh.pa.us        1860                 :UBC           0 :                 offset = -offset;
                               1861                 :                : 
 2768 tgl@sss.pgh.pa.us        1862                 :CBC          90 :             winstate->frametailpos = winstate->currentpos + offset + 1;
                               1863                 :                :             /* smallest allowable value of frametailpos is 0 */
 5685                          1864         [ -  + ]:             90 :             if (winstate->frametailpos < 0)
 2768 tgl@sss.pgh.pa.us        1865                 :UBC           0 :                 winstate->frametailpos = 0;
 2768 tgl@sss.pgh.pa.us        1866         [ +  - ]:CBC          90 :             else if (winstate->frametailpos > winstate->currentpos + 1)
                               1867                 :                :             {
                               1868                 :                :                 /* make sure frametailpos is not past end of partition */
                               1869                 :             90 :                 spool_tuples(winstate, winstate->frametailpos - 1);
                               1870         [ +  + ]:             90 :                 if (winstate->frametailpos > winstate->spooled_rows)
                               1871                 :             18 :                     winstate->frametailpos = winstate->spooled_rows;
                               1872                 :                :             }
 5685                          1873                 :             90 :             winstate->frametail_valid = true;
                               1874                 :                :         }
                               1875         [ +  + ]:           3102 :         else if (frameOptions & FRAMEOPTION_RANGE)
                               1876                 :                :         {
                               1877                 :                :             /*
                               1878                 :                :              * In RANGE END_OFFSET mode, frame end is the last row that
                               1879                 :                :              * satisfies the in_range constraint relative to the current row,
                               1880                 :                :              * frame tail is the row after that (if any).  We keep a copy of
                               1881                 :                :              * the last-known frame tail row in frametail_slot, and advance as
                               1882                 :                :              * necessary.  Note that if we reach end of partition, we will
                               1883                 :                :              * leave frametailpos = end+1 and frametail_slot empty.
                               1884                 :                :              */
 2752                          1885                 :           2442 :             int         sortCol = node->ordColIdx[0];
                               1886                 :                :             bool        sub,
                               1887                 :                :                         less;
                               1888                 :                : 
                               1889                 :                :             /* We must have an ordering column */
 2614                          1890         [ -  + ]:           2442 :             Assert(node->ordNumCols == 1);
                               1891                 :                : 
                               1892                 :                :             /* Precompute flags for in_range checks */
 2768                          1893         [ +  + ]:           2442 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
                               1894                 :            456 :                 sub = true;     /* subtract endOffset from current row */
                               1895                 :                :             else
                               1896                 :           1986 :                 sub = false;    /* add it */
                               1897                 :           2442 :             less = true;        /* normally, we want frame tail <= sum */
                               1898                 :                :             /* If sort order is descending, flip both flags */
                               1899         [ +  + ]:           2442 :             if (!winstate->inRangeAsc)
                               1900                 :                :             {
                               1901                 :            345 :                 sub = !sub;
                               1902                 :            345 :                 less = false;
                               1903                 :                :             }
                               1904                 :                : 
                               1905                 :           2442 :             tuplestore_select_read_pointer(winstate->buffer,
                               1906                 :                :                                            winstate->frametail_ptr);
                               1907         [ +  + ]:           2442 :             if (winstate->frametailpos == 0 &&
                               1908   [ +  -  +  + ]:            411 :                 TupIsNull(winstate->frametail_slot))
                               1909                 :                :             {
                               1910                 :                :                 /* fetch first row into frametail_slot, if we didn't already */
                               1911         [ -  + ]:            294 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1912                 :                :                                              winstate->frametail_slot))
 2768 tgl@sss.pgh.pa.us        1913         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1914                 :                :             }
                               1915                 :                : 
 2768 tgl@sss.pgh.pa.us        1916   [ +  -  +  + ]:CBC        4503 :             while (!TupIsNull(winstate->frametail_slot))
                               1917                 :                :             {
                               1918                 :                :                 Datum       tailval,
                               1919                 :                :                             currval;
                               1920                 :                :                 bool        tailisnull,
                               1921                 :                :                             currisnull;
                               1922                 :                : 
 2752                          1923                 :           3720 :                 tailval = slot_getattr(winstate->frametail_slot, sortCol,
                               1924                 :                :                                        &tailisnull);
                               1925                 :           3720 :                 currval = slot_getattr(winstate->ss.ss_ScanTupleSlot, sortCol,
                               1926                 :                :                                        &currisnull);
 2768                          1927   [ +  +  +  + ]:           3720 :                 if (tailisnull || currisnull)
                               1928                 :                :                 {
                               1929                 :                :                     /* order of the rows depends only on nulls_first */
                               1930         [ +  + ]:             54 :                     if (winstate->inRangeNullsFirst)
                               1931                 :                :                     {
                               1932                 :                :                         /* advance tail if tail is null or curr is not */
                               1933         [ +  + ]:             24 :                         if (!tailisnull)
                               1934                 :           1635 :                             break;
                               1935                 :                :                     }
                               1936                 :                :                     else
                               1937                 :                :                     {
                               1938                 :                :                         /* advance tail if tail is not null or curr is null */
                               1939         [ +  + ]:             30 :                         if (!currisnull)
                               1940                 :             18 :                             break;
                               1941                 :                :                     }
                               1942                 :                :                 }
                               1943                 :                :                 else
                               1944                 :                :                 {
                               1945         [ +  + ]:           3666 :                     if (!DatumGetBool(FunctionCall5Coll(&winstate->endInRangeFunc,
                               1946                 :                :                                                         winstate->inRangeColl,
                               1947                 :                :                                                         tailval,
                               1948                 :                :                                                         currval,
                               1949                 :                :                                                         winstate->endOffsetValue,
                               1950                 :                :                                                         BoolGetDatum(sub),
                               1951                 :                :                                                         BoolGetDatum(less))))
                               1952                 :           1365 :                         break;  /* this row is the correct frame tail */
                               1953                 :                :                 }
                               1954                 :                :                 /* Note we advance frametailpos even if the fetch fails */
                               1955                 :           2301 :                 winstate->frametailpos++;
                               1956                 :           2301 :                 spool_tuples(winstate, winstate->frametailpos);
                               1957         [ +  + ]:           2301 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1958                 :                :                                              winstate->frametail_slot))
                               1959                 :            240 :                     break;      /* end of partition */
                               1960                 :                :             }
                               1961                 :           2418 :             winstate->frametail_valid = true;
                               1962                 :                :         }
                               1963         [ +  - ]:            660 :         else if (frameOptions & FRAMEOPTION_GROUPS)
                               1964                 :                :         {
                               1965                 :                :             /*
                               1966                 :                :              * In GROUPS END_OFFSET mode, frame end is the last row of the
                               1967                 :                :              * last peer group whose number satisfies the offset constraint,
                               1968                 :                :              * and frame tail is the row after that (if any).  We keep a copy
                               1969                 :                :              * of the last-known frame tail row in frametail_slot, and advance
                               1970                 :                :              * as necessary.  Note that if we reach end of partition, we will
                               1971                 :                :              * leave frametailpos = end+1 and frametail_slot empty.
                               1972                 :                :              */
                               1973                 :            660 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
                               1974                 :                :             int64       maxtailgroup;
                               1975                 :                : 
                               1976         [ +  + ]:            660 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
                               1977                 :             36 :                 maxtailgroup = winstate->currentgroup - offset;
                               1978                 :                :             else
                               1979                 :            624 :                 maxtailgroup = winstate->currentgroup + offset;
                               1980                 :                : 
                               1981                 :            660 :             tuplestore_select_read_pointer(winstate->buffer,
                               1982                 :                :                                            winstate->frametail_ptr);
                               1983         [ +  + ]:            660 :             if (winstate->frametailpos == 0 &&
                               1984   [ +  -  +  + ]:            192 :                 TupIsNull(winstate->frametail_slot))
                               1985                 :                :             {
                               1986                 :                :                 /* fetch first row into frametail_slot, if we didn't already */
                               1987         [ -  + ]:            183 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               1988                 :                :                                              winstate->frametail_slot))
 2768 tgl@sss.pgh.pa.us        1989         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected end of tuplestore");
                               1990                 :                :             }
                               1991                 :                : 
 2768 tgl@sss.pgh.pa.us        1992   [ +  -  +  + ]:CBC        1794 :             while (!TupIsNull(winstate->frametail_slot))
                               1993                 :                :             {
                               1994         [ +  + ]:           1020 :                 if (winstate->frametailgroup > maxtailgroup)
                               1995                 :            372 :                     break;      /* this row is the correct frame tail */
                               1996                 :            648 :                 ExecCopySlot(winstate->temp_slot_2, winstate->frametail_slot);
                               1997                 :                :                 /* Note we advance frametailpos even if the fetch fails */
                               1998                 :            648 :                 winstate->frametailpos++;
                               1999                 :            648 :                 spool_tuples(winstate, winstate->frametailpos);
                               2000         [ +  + ]:            648 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               2001                 :                :                                              winstate->frametail_slot))
                               2002                 :            174 :                     break;      /* end of partition */
                               2003         [ +  + ]:            474 :                 if (!are_peers(winstate, winstate->temp_slot_2,
                               2004                 :                :                                winstate->frametail_slot))
                               2005                 :            300 :                     winstate->frametailgroup++;
                               2006                 :                :             }
                               2007                 :            660 :             ExecClearTuple(winstate->temp_slot_2);
                               2008                 :            660 :             winstate->frametail_valid = true;
                               2009                 :                :         }
                               2010                 :                :         else
 5685 tgl@sss.pgh.pa.us        2011                 :UBC           0 :             Assert(false);
                               2012                 :                :     }
                               2013                 :                :     else
                               2014                 :              0 :         Assert(false);
                               2015                 :                : 
 2768 tgl@sss.pgh.pa.us        2016                 :CBC       92137 :     MemoryContextSwitchTo(oldcontext);
                               2017                 :                : }
                               2018                 :                : 
                               2019                 :                : /*
                               2020                 :                :  * update_grouptailpos
                               2021                 :                :  * make grouptailpos valid for the current row
                               2022                 :                :  *
                               2023                 :                :  * May clobber winstate->temp_slot_2.
                               2024                 :                :  */
                               2025                 :                : static void
                               2026                 :           2436 : update_grouptailpos(WindowAggState *winstate)
                               2027                 :                : {
                               2028                 :           2436 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
                               2029                 :                :     MemoryContext oldcontext;
                               2030                 :                : 
                               2031         [ +  + ]:           2436 :     if (winstate->grouptail_valid)
                               2032                 :           1977 :         return;                 /* already known for current row */
                               2033                 :                : 
                               2034                 :                :     /* We may be called in a short-lived context */
                               2035                 :            459 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               2036                 :                : 
                               2037                 :                :     /* If no ORDER BY, all rows are peers with each other */
                               2038         [ -  + ]:            459 :     if (node->ordNumCols == 0)
                               2039                 :                :     {
 2768 tgl@sss.pgh.pa.us        2040                 :UBC           0 :         spool_tuples(winstate, -1);
                               2041                 :              0 :         winstate->grouptailpos = winstate->spooled_rows;
                               2042                 :              0 :         winstate->grouptail_valid = true;
                               2043                 :              0 :         MemoryContextSwitchTo(oldcontext);
                               2044                 :              0 :         return;
                               2045                 :                :     }
                               2046                 :                : 
                               2047                 :                :     /*
                               2048                 :                :      * Because grouptail_valid is reset only when current row advances into a
                               2049                 :                :      * new peer group, we always reach here knowing that grouptailpos needs to
                               2050                 :                :      * be advanced by at least one row.  Hence, unlike the otherwise similar
                               2051                 :                :      * case for frame tail tracking, we do not need persistent storage of the
                               2052                 :                :      * group tail row.
                               2053                 :                :      */
 2768 tgl@sss.pgh.pa.us        2054         [ -  + ]:CBC         459 :     Assert(winstate->grouptailpos <= winstate->currentpos);
                               2055                 :            459 :     tuplestore_select_read_pointer(winstate->buffer,
                               2056                 :                :                                    winstate->grouptail_ptr);
                               2057                 :                :     for (;;)
                               2058                 :                :     {
                               2059                 :                :         /* Note we advance grouptailpos even if the fetch fails */
                               2060                 :            879 :         winstate->grouptailpos++;
                               2061                 :            879 :         spool_tuples(winstate, winstate->grouptailpos);
                               2062         [ +  + ]:            879 :         if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               2063                 :                :                                      winstate->temp_slot_2))
                               2064                 :            129 :             break;              /* end of partition */
                               2065         [ +  + ]:            750 :         if (winstate->grouptailpos > winstate->currentpos &&
                               2066         [ +  + ]:            621 :             !are_peers(winstate, winstate->temp_slot_2,
                               2067                 :                :                        winstate->ss.ss_ScanTupleSlot))
                               2068                 :            330 :             break;              /* this row is the group tail */
                               2069                 :                :     }
                               2070                 :            459 :     ExecClearTuple(winstate->temp_slot_2);
                               2071                 :            459 :     winstate->grouptail_valid = true;
                               2072                 :                : 
                               2073                 :            459 :     MemoryContextSwitchTo(oldcontext);
                               2074                 :                : }
                               2075                 :                : 
                               2076                 :                : /*
                               2077                 :                :  * calculate_frame_offsets
                               2078                 :                :  *      Determine the startOffsetValue and endOffsetValue values for the
                               2079                 :                :  *      WindowAgg's frame options.
                               2080                 :                :  */
                               2081                 :                : static pg_noinline void
  366 drowley@postgresql.o     2082                 :           1086 : calculate_frame_offsets(PlanState *pstate)
                               2083                 :                : {
                               2084                 :           1086 :     WindowAggState *winstate = castNode(WindowAggState, pstate);
                               2085                 :                :     ExprContext *econtext;
                               2086                 :           1086 :     int         frameOptions = winstate->frameOptions;
                               2087                 :                :     Datum       value;
                               2088                 :                :     bool        isnull;
                               2089                 :                :     int16       len;
                               2090                 :                :     bool        byval;
                               2091                 :                : 
                               2092                 :                :     /* Ensure we've not been called before for this scan */
                               2093         [ -  + ]:           1086 :     Assert(winstate->all_first);
                               2094                 :                : 
                               2095                 :           1086 :     econtext = winstate->ss.ps.ps_ExprContext;
                               2096                 :                : 
                               2097         [ +  + ]:           1086 :     if (frameOptions & FRAMEOPTION_START_OFFSET)
                               2098                 :                :     {
                               2099         [ -  + ]:            408 :         Assert(winstate->startOffset != NULL);
                               2100                 :            408 :         value = ExecEvalExprSwitchContext(winstate->startOffset,
                               2101                 :                :                                           econtext,
                               2102                 :                :                                           &isnull);
                               2103         [ -  + ]:            408 :         if (isnull)
  366 drowley@postgresql.o     2104         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2105                 :                :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                               2106                 :                :                      errmsg("frame starting offset must not be null")));
                               2107                 :                :         /* copy value into query-lifespan context */
  366 drowley@postgresql.o     2108                 :CBC         408 :         get_typlenbyval(exprType((Node *) winstate->startOffset->expr),
                               2109                 :                :                         &len,
                               2110                 :                :                         &byval);
                               2111                 :            408 :         winstate->startOffsetValue = datumCopy(value, byval, len);
                               2112         [ +  + ]:            408 :         if (frameOptions & (FRAMEOPTION_ROWS | FRAMEOPTION_GROUPS))
                               2113                 :                :         {
                               2114                 :                :             /* value is known to be int8 */
                               2115                 :            150 :             int64       offset = DatumGetInt64(value);
                               2116                 :                : 
                               2117         [ -  + ]:            150 :             if (offset < 0)
  366 drowley@postgresql.o     2118         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2119                 :                :                         (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
                               2120                 :                :                          errmsg("frame starting offset must not be negative")));
                               2121                 :                :         }
                               2122                 :                :     }
                               2123                 :                : 
  366 drowley@postgresql.o     2124         [ +  + ]:CBC        1086 :     if (frameOptions & FRAMEOPTION_END_OFFSET)
                               2125                 :                :     {
                               2126         [ -  + ]:            456 :         Assert(winstate->endOffset != NULL);
                               2127                 :            456 :         value = ExecEvalExprSwitchContext(winstate->endOffset,
                               2128                 :                :                                           econtext,
                               2129                 :                :                                           &isnull);
                               2130         [ -  + ]:            456 :         if (isnull)
  366 drowley@postgresql.o     2131         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2132                 :                :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                               2133                 :                :                      errmsg("frame ending offset must not be null")));
                               2134                 :                :         /* copy value into query-lifespan context */
  366 drowley@postgresql.o     2135                 :CBC         456 :         get_typlenbyval(exprType((Node *) winstate->endOffset->expr),
                               2136                 :                :                         &len,
                               2137                 :                :                         &byval);
                               2138                 :            456 :         winstate->endOffsetValue = datumCopy(value, byval, len);
                               2139         [ +  + ]:            456 :         if (frameOptions & (FRAMEOPTION_ROWS | FRAMEOPTION_GROUPS))
                               2140                 :                :         {
                               2141                 :                :             /* value is known to be int8 */
                               2142                 :            165 :             int64       offset = DatumGetInt64(value);
                               2143                 :                : 
                               2144         [ -  + ]:            165 :             if (offset < 0)
  366 drowley@postgresql.o     2145         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2146                 :                :                         (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
                               2147                 :                :                          errmsg("frame ending offset must not be negative")));
                               2148                 :                :         }
                               2149                 :                :     }
  366 drowley@postgresql.o     2150                 :CBC        1086 :     winstate->all_first = false;
                               2151                 :           1086 : }
                               2152                 :                : 
                               2153                 :                : /* -----------------
                               2154                 :                :  * ExecWindowAgg
                               2155                 :                :  *
                               2156                 :                :  *  ExecWindowAgg receives tuples from its outer subplan and
                               2157                 :                :  *  stores them into a tuplestore, then processes window functions.
                               2158                 :                :  *  This node doesn't reduce nor qualify any row so the number of
                               2159                 :                :  *  returned rows is exactly the same as its outer subplan's result.
                               2160                 :                :  * -----------------
                               2161                 :                :  */
                               2162                 :                : static TupleTableSlot *
 2973 andres@anarazel.de       2163                 :         452901 : ExecWindowAgg(PlanState *pstate)
                               2164                 :                : {
                               2165                 :         452901 :     WindowAggState *winstate = castNode(WindowAggState, pstate);
                               2166                 :                :     TupleTableSlot *slot;
                               2167                 :                :     ExprContext *econtext;
                               2168                 :                :     int         i;
                               2169                 :                :     int         numfuncs;
                               2170                 :                : 
 2965                          2171         [ -  + ]:         452901 :     CHECK_FOR_INTERRUPTS();
                               2172                 :                : 
 1247 drowley@postgresql.o     2173         [ -  + ]:         452901 :     if (winstate->status == WINDOWAGG_DONE)
 6096 tgl@sss.pgh.pa.us        2174                 :UBC           0 :         return NULL;
                               2175                 :                : 
                               2176                 :                :     /*
                               2177                 :                :      * Compute frame offset values, if any, during first call (or after a
                               2178                 :                :      * rescan).  These are assumed to hold constant throughout the scan; if
                               2179                 :                :      * user gives us a volatile expression, we'll only use its initial value.
                               2180                 :                :      */
  366 drowley@postgresql.o     2181         [ +  + ]:CBC      452901 :     if (unlikely(winstate->all_first))
                               2182                 :           1086 :         calculate_frame_offsets(pstate);
                               2183                 :                : 
                               2184                 :                :     /* We need to loop as the runCondition or qual may filter out tuples */
                               2185                 :                :     for (;;)
                               2186                 :                :     {
                               2187         [ +  + ]:         452967 :         if (winstate->next_partition)
                               2188                 :                :         {
                               2189                 :                :             /* Initialize for first partition and set current row = 0 */
 1247                          2190                 :           1086 :             begin_partition(winstate);
                               2191                 :                :             /* If there are no input rows, we'll detect that and exit below */
                               2192                 :                :         }
                               2193                 :                :         else
                               2194                 :                :         {
                               2195                 :                :             /* Advance current row within partition */
                               2196                 :         451881 :             winstate->currentpos++;
                               2197                 :                :             /* This might mean that the frame moves, too */
                               2198                 :         451881 :             winstate->framehead_valid = false;
                               2199                 :         451881 :             winstate->frametail_valid = false;
                               2200                 :                :             /* we don't need to invalidate grouptail here; see below */
                               2201                 :                :         }
                               2202                 :                : 
                               2203                 :                :         /*
                               2204                 :                :          * Spool all tuples up to and including the current row, if we haven't
                               2205                 :                :          * already
                               2206                 :                :          */
                               2207                 :         452967 :         spool_tuples(winstate, winstate->currentpos);
                               2208                 :                : 
                               2209                 :                :         /* Move to the next partition if we reached the end of this partition */
                               2210         [ +  + ]:         452967 :         if (winstate->partition_spooled &&
                               2211         [ +  + ]:          31288 :             winstate->currentpos >= winstate->spooled_rows)
                               2212                 :                :         {
                               2213                 :           1615 :             release_partition(winstate);
                               2214                 :                : 
                               2215         [ +  + ]:           1615 :             if (winstate->more_partitions)
                               2216                 :                :             {
                               2217                 :            619 :                 begin_partition(winstate);
                               2218         [ -  + ]:            619 :                 Assert(winstate->spooled_rows > 0);
                               2219                 :                : 
                               2220                 :                :                 /* Come out of pass-through mode when changing partition */
                               2221                 :            619 :                 winstate->status = WINDOWAGG_RUN;
                               2222                 :                :             }
                               2223                 :                :             else
                               2224                 :                :             {
                               2225                 :                :                 /* No further partitions?  We're done */
                               2226                 :            996 :                 winstate->status = WINDOWAGG_DONE;
                               2227                 :            996 :                 return NULL;
                               2228                 :                :             }
                               2229                 :                :         }
                               2230                 :                : 
                               2231                 :                :         /* final output execution is in ps_ExprContext */
                               2232                 :         451971 :         econtext = winstate->ss.ps.ps_ExprContext;
                               2233                 :                : 
                               2234                 :                :         /* Clear the per-output-tuple context for current row */
                               2235                 :         451971 :         ResetExprContext(econtext);
                               2236                 :                : 
                               2237                 :                :         /*
                               2238                 :                :          * Read the current row from the tuplestore, and save in
                               2239                 :                :          * ScanTupleSlot. (We can't rely on the outerplan's output slot
                               2240                 :                :          * because we may have to read beyond the current row.  Also, we have
                               2241                 :                :          * to actually copy the row out of the tuplestore, since window
                               2242                 :                :          * function evaluation might cause the tuplestore to dump its state to
                               2243                 :                :          * disk.)
                               2244                 :                :          *
                               2245                 :                :          * In GROUPS mode, or when tracking a group-oriented exclusion clause,
                               2246                 :                :          * we must also detect entering a new peer group and update associated
                               2247                 :                :          * state when that happens.  We use temp_slot_2 to temporarily hold
                               2248                 :                :          * the previous row for this purpose.
                               2249                 :                :          *
                               2250                 :                :          * Current row must be in the tuplestore, since we spooled it above.
                               2251                 :                :          */
                               2252                 :         451971 :         tuplestore_select_read_pointer(winstate->buffer, winstate->current_ptr);
                               2253         [ +  + ]:         451971 :         if ((winstate->frameOptions & (FRAMEOPTION_GROUPS |
                               2254                 :                :                                        FRAMEOPTION_EXCLUDE_GROUP |
                               2255                 :           1449 :                                        FRAMEOPTION_EXCLUDE_TIES)) &&
                               2256         [ +  + ]:           1449 :             winstate->currentpos > 0)
                               2257                 :                :         {
                               2258                 :           1179 :             ExecCopySlot(winstate->temp_slot_2, winstate->ss.ss_ScanTupleSlot);
                               2259         [ -  + ]:           1179 :             if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               2260                 :                :                                          winstate->ss.ss_ScanTupleSlot))
 1247 drowley@postgresql.o     2261         [ #  # ]:UBC           0 :                 elog(ERROR, "unexpected end of tuplestore");
 1247 drowley@postgresql.o     2262         [ +  + ]:CBC        1179 :             if (!are_peers(winstate, winstate->temp_slot_2,
                               2263                 :                :                            winstate->ss.ss_ScanTupleSlot))
                               2264                 :                :             {
                               2265                 :            621 :                 winstate->currentgroup++;
                               2266                 :            621 :                 winstate->groupheadpos = winstate->currentpos;
                               2267                 :            621 :                 winstate->grouptail_valid = false;
                               2268                 :                :             }
                               2269                 :           1179 :             ExecClearTuple(winstate->temp_slot_2);
                               2270                 :                :         }
                               2271                 :                :         else
                               2272                 :                :         {
                               2273         [ -  + ]:         450792 :             if (!tuplestore_gettupleslot(winstate->buffer, true, true,
                               2274                 :                :                                          winstate->ss.ss_ScanTupleSlot))
 1247 drowley@postgresql.o     2275         [ #  # ]:UBC           0 :                 elog(ERROR, "unexpected end of tuplestore");
                               2276                 :                :         }
                               2277                 :                : 
                               2278                 :                :         /* don't evaluate the window functions when we're in pass-through mode */
 1247 drowley@postgresql.o     2279         [ +  + ]:CBC      451971 :         if (winstate->status == WINDOWAGG_RUN)
                               2280                 :                :         {
                               2281                 :                :             /*
                               2282                 :                :              * Evaluate true window functions
                               2283                 :                :              */
                               2284                 :         451938 :             numfuncs = winstate->numfuncs;
                               2285         [ +  + ]:         967110 :             for (i = 0; i < numfuncs; i++)
                               2286                 :                :             {
                               2287                 :         515217 :                 WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
                               2288                 :                : 
                               2289         [ +  + ]:         515217 :                 if (perfuncstate->plain_agg)
                               2290                 :          80989 :                     continue;
                               2291                 :         434228 :                 eval_windowfunction(winstate, perfuncstate,
                               2292                 :         434228 :                                     &(econtext->ecxt_aggvalues[perfuncstate->wfuncstate->wfuncno]),
                               2293                 :         434228 :                                     &(econtext->ecxt_aggnulls[perfuncstate->wfuncstate->wfuncno]));
                               2294                 :                :             }
                               2295                 :                : 
                               2296                 :                :             /*
                               2297                 :                :              * Evaluate aggregates
                               2298                 :                :              */
                               2299         [ +  + ]:         451893 :             if (winstate->numaggs > 0)
                               2300                 :          80092 :                 eval_windowaggregates(winstate);
                               2301                 :                :         }
                               2302                 :                : 
                               2303                 :                :         /*
                               2304                 :                :          * If we have created auxiliary read pointers for the frame or group
                               2305                 :                :          * boundaries, force them to be kept up-to-date, because we don't know
                               2306                 :                :          * whether the window function(s) will do anything that requires that.
                               2307                 :                :          * Failing to advance the pointers would result in being unable to
                               2308                 :                :          * trim data from the tuplestore, which is bad.  (If we could know in
                               2309                 :                :          * advance whether the window functions will use frame boundary info,
                               2310                 :                :          * we could skip creating these pointers in the first place ... but
                               2311                 :                :          * unfortunately the window function API doesn't require that.)
                               2312                 :                :          */
                               2313         [ +  + ]:         451905 :         if (winstate->framehead_ptr >= 0)
                               2314                 :           3118 :             update_frameheadpos(winstate);
                               2315         [ +  + ]:         451905 :         if (winstate->frametail_ptr >= 0)
                               2316                 :          91897 :             update_frametailpos(winstate);
                               2317         [ +  + ]:         451905 :         if (winstate->grouptail_ptr >= 0)
                               2318                 :            750 :             update_grouptailpos(winstate);
                               2319                 :                : 
                               2320                 :                :         /*
                               2321                 :                :          * Truncate any no-longer-needed rows from the tuplestore.
                               2322                 :                :          */
                               2323                 :         451905 :         tuplestore_trim(winstate->buffer);
                               2324                 :                : 
                               2325                 :                :         /*
                               2326                 :                :          * Form and return a projection tuple using the windowfunc results and
                               2327                 :                :          * the current row.  Setting ecxt_outertuple arranges that any Vars
                               2328                 :                :          * will be evaluated with respect to that row.
                               2329                 :                :          */
                               2330                 :         451905 :         econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
                               2331                 :                : 
                               2332                 :         451905 :         slot = ExecProject(winstate->ss.ps.ps_ProjInfo);
                               2333                 :                : 
                               2334         [ +  + ]:         451905 :         if (winstate->status == WINDOWAGG_RUN)
                               2335                 :                :         {
                               2336                 :         451872 :             econtext->ecxt_scantuple = slot;
                               2337                 :                : 
                               2338                 :                :             /*
                               2339                 :                :              * Now evaluate the run condition to see if we need to go into
                               2340                 :                :              * pass-through mode, or maybe stop completely.
                               2341                 :                :              */
                               2342         [ +  + ]:         451872 :             if (!ExecQual(winstate->runcondition, econtext))
                               2343                 :                :             {
                               2344                 :                :                 /*
                               2345                 :                :                  * Determine which mode to move into.  If there is no
                               2346                 :                :                  * PARTITION BY clause and we're the top-level WindowAgg then
                               2347                 :                :                  * we're done.  This tuple and any future tuples cannot
                               2348                 :                :                  * possibly match the runcondition.  However, when there is a
                               2349                 :                :                  * PARTITION BY clause or we're not the top-level window we
                               2350                 :                :                  * can't just stop as we need to either process other
                               2351                 :                :                  * partitions or ensure WindowAgg nodes above us receive all
                               2352                 :                :                  * of the tuples they need to process their WindowFuncs.
                               2353                 :                :                  */
                               2354         [ +  + ]:             66 :                 if (winstate->use_pass_through)
                               2355                 :                :                 {
                               2356                 :                :                     /*
                               2357                 :                :                      * When switching into a pass-through mode, we'd better
                               2358                 :                :                      * NULLify the aggregate results as these are no longer
                               2359                 :                :                      * updated and NULLifying them avoids the old stale
                               2360                 :                :                      * results lingering.  Some of these might be byref types
                               2361                 :                :                      * so we can't have them pointing to free'd memory.  The
                               2362                 :                :                      * planner insisted that quals used in the runcondition
                               2363                 :                :                      * are strict, so the top-level WindowAgg will always
                               2364                 :                :                      * filter these NULLs out in the filter clause.
                               2365                 :                :                      */
  271                          2366                 :             45 :                     numfuncs = winstate->numfuncs;
                               2367         [ +  + ]:            132 :                     for (i = 0; i < numfuncs; i++)
                               2368                 :                :                     {
                               2369                 :             87 :                         econtext->ecxt_aggvalues[i] = (Datum) 0;
                               2370                 :             87 :                         econtext->ecxt_aggnulls[i] = true;
                               2371                 :                :                     }
                               2372                 :                : 
                               2373                 :                :                     /*
                               2374                 :                :                      * STRICT pass-through mode is required for the top window
                               2375                 :                :                      * when there is a PARTITION BY clause.  Otherwise we must
                               2376                 :                :                      * ensure we store tuples that don't match the
                               2377                 :                :                      * runcondition so they're available to WindowAggs above.
                               2378                 :                :                      */
 1247                          2379         [ +  + ]:             45 :                     if (winstate->top_window)
                               2380                 :                :                     {
                               2381                 :             36 :                         winstate->status = WINDOWAGG_PASSTHROUGH_STRICT;
                               2382                 :             36 :                         continue;
                               2383                 :                :                     }
                               2384                 :                :                     else
                               2385                 :                :                     {
                               2386                 :              9 :                         winstate->status = WINDOWAGG_PASSTHROUGH;
                               2387                 :                :                     }
                               2388                 :                :                 }
                               2389                 :                :                 else
                               2390                 :                :                 {
                               2391                 :                :                     /*
                               2392                 :                :                      * Pass-through not required.  We can just return NULL.
                               2393                 :                :                      * Nothing else will match the runcondition.
                               2394                 :                :                      */
                               2395                 :             21 :                     winstate->status = WINDOWAGG_DONE;
                               2396                 :             21 :                     return NULL;
                               2397                 :                :                 }
                               2398                 :                :             }
                               2399                 :                : 
                               2400                 :                :             /*
                               2401                 :                :              * Filter out any tuples we don't need in the top-level WindowAgg.
                               2402                 :                :              */
                               2403         [ +  + ]:         451815 :             if (!ExecQual(winstate->ss.ps.qual, econtext))
                               2404                 :                :             {
                               2405         [ -  + ]:              9 :                 InstrCountFiltered1(winstate, 1);
                               2406                 :              9 :                 continue;
                               2407                 :                :             }
                               2408                 :                : 
                               2409                 :         451806 :             break;
                               2410                 :                :         }
                               2411                 :                : 
                               2412                 :                :         /*
                               2413                 :                :          * When not in WINDOWAGG_RUN mode, we must still return this tuple if
                               2414                 :                :          * we're anything apart from the top window.
                               2415                 :                :          */
                               2416         [ +  + ]:             33 :         else if (!winstate->top_window)
                               2417                 :             12 :             break;
                               2418                 :                :     }
                               2419                 :                : 
                               2420                 :         451818 :     return slot;
                               2421                 :                : }
                               2422                 :                : 
                               2423                 :                : /* -----------------
                               2424                 :                :  * ExecInitWindowAgg
                               2425                 :                :  *
                               2426                 :                :  *  Creates the run-time information for the WindowAgg node produced by the
                               2427                 :                :  *  planner and initializes its outer subtree
                               2428                 :                :  * -----------------
                               2429                 :                :  */
                               2430                 :                : WindowAggState *
 6096 tgl@sss.pgh.pa.us        2431                 :           1273 : ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags)
                               2432                 :                : {
                               2433                 :                :     WindowAggState *winstate;
                               2434                 :                :     Plan       *outerPlan;
                               2435                 :                :     ExprContext *econtext;
                               2436                 :                :     ExprContext *tmpcontext;
                               2437                 :                :     WindowStatePerFunc perfunc;
                               2438                 :                :     WindowStatePerAgg peragg;
 2768                          2439                 :           1273 :     int         frameOptions = node->frameOptions;
                               2440                 :                :     int         numfuncs,
                               2441                 :                :                 wfuncno,
                               2442                 :                :                 numaggs,
                               2443                 :                :                 aggno;
                               2444                 :                :     TupleDesc   scanDesc;
                               2445                 :                :     ListCell   *l;
                               2446                 :                : 
                               2447                 :                :     /* check for unsupported flags */
 6096                          2448         [ -  + ]:           1273 :     Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
                               2449                 :                : 
                               2450                 :                :     /*
                               2451                 :                :      * create state structure
                               2452                 :                :      */
                               2453                 :           1273 :     winstate = makeNode(WindowAggState);
                               2454                 :           1273 :     winstate->ss.ps.plan = (Plan *) node;
                               2455                 :           1273 :     winstate->ss.ps.state = estate;
 2973 andres@anarazel.de       2456                 :           1273 :     winstate->ss.ps.ExecProcNode = ExecWindowAgg;
                               2457                 :                : 
                               2458                 :                :     /* copy frame options to state node for easy access */
  528 tgl@sss.pgh.pa.us        2459                 :           1273 :     winstate->frameOptions = frameOptions;
                               2460                 :                : 
                               2461                 :                :     /*
                               2462                 :                :      * Create expression contexts.  We need two, one for per-input-tuple
                               2463                 :                :      * processing and one for per-output-tuple processing.  We cheat a little
                               2464                 :                :      * by using ExecAssignExprContext() to build both.
                               2465                 :                :      */
 6096                          2466                 :           1273 :     ExecAssignExprContext(estate, &winstate->ss.ps);
                               2467                 :           1273 :     tmpcontext = winstate->ss.ps.ps_ExprContext;
                               2468                 :           1273 :     winstate->tmpcontext = tmpcontext;
                               2469                 :           1273 :     ExecAssignExprContext(estate, &winstate->ss.ps);
                               2470                 :                : 
                               2471                 :                :     /* Create long-lived context for storage of partition-local memory etc */
 5685                          2472                 :           1273 :     winstate->partcontext =
 6096                          2473                 :           1273 :         AllocSetContextCreate(CurrentMemoryContext,
                               2474                 :                :                               "WindowAgg Partition",
                               2475                 :                :                               ALLOCSET_DEFAULT_SIZES);
                               2476                 :                : 
                               2477                 :                :     /*
                               2478                 :                :      * Create mid-lived context for aggregate trans values etc.
                               2479                 :                :      *
                               2480                 :                :      * Note that moving aggregates each use their own private context, not
                               2481                 :                :      * this one.
                               2482                 :                :      */
 5685                          2483                 :           1273 :     winstate->aggcontext =
                               2484                 :           1273 :         AllocSetContextCreate(CurrentMemoryContext,
                               2485                 :                :                               "WindowAgg Aggregates",
                               2486                 :                :                               ALLOCSET_DEFAULT_SIZES);
                               2487                 :                : 
                               2488                 :                :     /* Only the top-level WindowAgg may have a qual */
 1247 drowley@postgresql.o     2489   [ +  +  -  + ]:           1273 :     Assert(node->plan.qual == NIL || node->topWindow);
                               2490                 :                : 
                               2491                 :                :     /* Initialize the qual */
                               2492                 :           1273 :     winstate->ss.ps.qual = ExecInitQual(node->plan.qual,
                               2493                 :                :                                         (PlanState *) winstate);
                               2494                 :                : 
                               2495                 :                :     /*
                               2496                 :                :      * Setup the run condition, if we received one from the query planner.
                               2497                 :                :      * When set, this may allow us to move into pass-through mode so that we
                               2498                 :                :      * don't have to perform any further evaluation of WindowFuncs in the
                               2499                 :                :      * current partition or possibly stop returning tuples altogether when all
                               2500                 :                :      * tuples are in the same partition.
                               2501                 :                :      */
                               2502                 :           1273 :     winstate->runcondition = ExecInitQual(node->runCondition,
                               2503                 :                :                                           (PlanState *) winstate);
                               2504                 :                : 
                               2505                 :                :     /*
                               2506                 :                :      * When we're not the top-level WindowAgg node or we are but have a
                               2507                 :                :      * PARTITION BY clause we must move into one of the WINDOWAGG_PASSTHROUGH*
                               2508                 :                :      * modes when the runCondition becomes false.
                               2509                 :                :      */
                               2510   [ +  +  +  + ]:           1273 :     winstate->use_pass_through = !node->topWindow || node->partNumCols > 0;
                               2511                 :                : 
                               2512                 :                :     /* remember if we're the top-window or we are below the top-window */
                               2513                 :           1273 :     winstate->top_window = node->topWindow;
                               2514                 :                : 
                               2515                 :                :     /*
                               2516                 :                :      * initialize child nodes
                               2517                 :                :      */
 6096 tgl@sss.pgh.pa.us        2518                 :           1273 :     outerPlan = outerPlan(node);
                               2519                 :           1273 :     outerPlanState(winstate) = ExecInitNode(outerPlan, estate, eflags);
                               2520                 :                : 
                               2521                 :                :     /*
                               2522                 :                :      * initialize source tuple type (which is also the tuple type that we'll
                               2523                 :                :      * store in the tuplestore and use in all our working slots).
                               2524                 :                :      */
 2487 andres@anarazel.de       2525                 :           1273 :     ExecCreateScanSlotFromOuterPlan(estate, &winstate->ss, &TTSOpsMinimalTuple);
 2760                          2526                 :           1273 :     scanDesc = winstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
                               2527                 :                : 
                               2528                 :                :     /* the outer tuple isn't the child's tuple, but always a minimal tuple */
 2487                          2529                 :           1273 :     winstate->ss.ps.outeropsset = true;
                               2530                 :           1273 :     winstate->ss.ps.outerops = &TTSOpsMinimalTuple;
                               2531                 :           1273 :     winstate->ss.ps.outeropsfixed = true;
                               2532                 :                : 
                               2533                 :                :     /*
                               2534                 :                :      * tuple table initialization
                               2535                 :                :      */
                               2536                 :           1273 :     winstate->first_part_slot = ExecInitExtraTupleSlot(estate, scanDesc,
                               2537                 :                :                                                        &TTSOpsMinimalTuple);
                               2538                 :           1273 :     winstate->agg_row_slot = ExecInitExtraTupleSlot(estate, scanDesc,
                               2539                 :                :                                                     &TTSOpsMinimalTuple);
                               2540                 :           1273 :     winstate->temp_slot_1 = ExecInitExtraTupleSlot(estate, scanDesc,
                               2541                 :                :                                                    &TTSOpsMinimalTuple);
                               2542                 :           1273 :     winstate->temp_slot_2 = ExecInitExtraTupleSlot(estate, scanDesc,
                               2543                 :                :                                                    &TTSOpsMinimalTuple);
                               2544                 :                : 
                               2545                 :                :     /*
                               2546                 :                :      * create frame head and tail slots only if needed (must create slots in
                               2547                 :                :      * exactly the same cases that update_frameheadpos and update_frametailpos
                               2548                 :                :      * need them)
                               2549                 :                :      */
 2759                          2550                 :           1273 :     winstate->framehead_slot = winstate->frametail_slot = NULL;
                               2551                 :                : 
                               2552         [ +  + ]:           1273 :     if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
                               2553                 :                :     {
 2614 tgl@sss.pgh.pa.us        2554         [ +  + ]:            763 :         if (((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
                               2555         [ -  + ]:             38 :              node->ordNumCols != 0) ||
                               2556         [ +  + ]:            725 :             (frameOptions & FRAMEOPTION_START_OFFSET))
 2487 andres@anarazel.de       2557                 :            371 :             winstate->framehead_slot = ExecInitExtraTupleSlot(estate, scanDesc,
                               2558                 :                :                                                               &TTSOpsMinimalTuple);
 2614 tgl@sss.pgh.pa.us        2559         [ +  + ]:            763 :         if (((frameOptions & FRAMEOPTION_END_CURRENT_ROW) &&
                               2560         [ +  + ]:            371 :              node->ordNumCols != 0) ||
                               2561         [ +  + ]:            527 :             (frameOptions & FRAMEOPTION_END_OFFSET))
 2487 andres@anarazel.de       2562                 :            599 :             winstate->frametail_slot = ExecInitExtraTupleSlot(estate, scanDesc,
                               2563                 :                :                                                               &TTSOpsMinimalTuple);
                               2564                 :                :     }
                               2565                 :                : 
                               2566                 :                :     /*
                               2567                 :                :      * Initialize result slot, type and projection.
                               2568                 :                :      */
                               2569                 :           1273 :     ExecInitResultTupleSlotTL(&winstate->ss.ps, &TTSOpsVirtual);
 6096 tgl@sss.pgh.pa.us        2570                 :           1273 :     ExecAssignProjectionInfo(&winstate->ss.ps, NULL);
                               2571                 :                : 
                               2572                 :                :     /* Set up data for comparing tuples */
                               2573         [ +  + ]:           1273 :     if (node->partNumCols > 0)
 2760 andres@anarazel.de       2574                 :            335 :         winstate->partEqfunction =
                               2575                 :            335 :             execTuplesMatchPrepare(scanDesc,
                               2576                 :                :                                    node->partNumCols,
                               2577                 :            335 :                                    node->partColIdx,
                               2578                 :            335 :                                    node->partOperators,
 2360 peter@eisentraut.org     2579                 :            335 :                                    node->partCollations,
                               2580                 :                :                                    &winstate->ss.ps);
                               2581                 :                : 
 6096 tgl@sss.pgh.pa.us        2582         [ +  + ]:           1273 :     if (node->ordNumCols > 0)
 2760 andres@anarazel.de       2583                 :           1055 :         winstate->ordEqfunction =
                               2584                 :           1055 :             execTuplesMatchPrepare(scanDesc,
                               2585                 :                :                                    node->ordNumCols,
                               2586                 :           1055 :                                    node->ordColIdx,
                               2587                 :           1055 :                                    node->ordOperators,
 2360 peter@eisentraut.org     2588                 :           1055 :                                    node->ordCollations,
                               2589                 :                :                                    &winstate->ss.ps);
                               2590                 :                : 
                               2591                 :                :     /*
                               2592                 :                :      * WindowAgg nodes use aggvalues and aggnulls as well as Agg nodes.
                               2593                 :                :      */
 6096 tgl@sss.pgh.pa.us        2594                 :           1273 :     numfuncs = winstate->numfuncs;
                               2595                 :           1273 :     numaggs = winstate->numaggs;
                               2596                 :           1273 :     econtext = winstate->ss.ps.ps_ExprContext;
                               2597                 :           1273 :     econtext->ecxt_aggvalues = (Datum *) palloc0(sizeof(Datum) * numfuncs);
                               2598                 :           1273 :     econtext->ecxt_aggnulls = (bool *) palloc0(sizeof(bool) * numfuncs);
                               2599                 :                : 
                               2600                 :                :     /*
                               2601                 :                :      * allocate per-wfunc/per-agg state information.
                               2602                 :                :      */
                               2603                 :           1273 :     perfunc = (WindowStatePerFunc) palloc0(sizeof(WindowStatePerFuncData) * numfuncs);
                               2604                 :           1273 :     peragg = (WindowStatePerAgg) palloc0(sizeof(WindowStatePerAggData) * numaggs);
                               2605                 :           1273 :     winstate->perfunc = perfunc;
                               2606                 :           1273 :     winstate->peragg = peragg;
                               2607                 :                : 
                               2608                 :           1273 :     wfuncno = -1;
                               2609                 :           1273 :     aggno = -1;
                               2610   [ +  -  +  +  :           2900 :     foreach(l, winstate->funcs)
                                              +  + ]
                               2611                 :                :     {
 5931 bruce@momjian.us         2612                 :           1627 :         WindowFuncExprState *wfuncstate = (WindowFuncExprState *) lfirst(l);
 3098 andres@anarazel.de       2613                 :           1627 :         WindowFunc *wfunc = wfuncstate->wfunc;
                               2614                 :                :         WindowStatePerFunc perfuncstate;
                               2615                 :                :         AclResult   aclresult;
                               2616                 :                :         int         i;
                               2617                 :                : 
 2999 tgl@sss.pgh.pa.us        2618         [ -  + ]:           1627 :         if (wfunc->winref != node->winref)    /* planner screwed up? */
 6093 tgl@sss.pgh.pa.us        2619         [ #  # ]:UBC           0 :             elog(ERROR, "WindowFunc with winref %u assigned to WindowAgg with winref %u",
                               2620                 :                :                  wfunc->winref, node->winref);
                               2621                 :                : 
                               2622                 :                :         /* Look for a previous duplicate window function */
 6096 tgl@sss.pgh.pa.us        2623         [ +  + ]:CBC        2053 :         for (i = 0; i <= wfuncno; i++)
                               2624                 :                :         {
                               2625         [ +  + ]:            429 :             if (equal(wfunc, perfunc[i].wfunc) &&
                               2626         [ +  - ]:              3 :                 !contain_volatile_functions((Node *) wfunc))
                               2627                 :              3 :                 break;
                               2628                 :                :         }
                               2629         [ +  + ]:           1627 :         if (i <= wfuncno)
                               2630                 :                :         {
                               2631                 :                :             /* Found a match to an existing entry, so just mark it */
                               2632                 :              3 :             wfuncstate->wfuncno = i;
                               2633                 :              3 :             continue;
                               2634                 :                :         }
                               2635                 :                : 
                               2636                 :                :         /* Nope, so assign a new PerAgg record */
                               2637                 :           1624 :         perfuncstate = &perfunc[++wfuncno];
                               2638                 :                : 
                               2639                 :                :         /* Mark WindowFunc state node with assigned index in the result array */
                               2640                 :           1624 :         wfuncstate->wfuncno = wfuncno;
                               2641                 :                : 
                               2642                 :                :         /* Check permission to call window function */
 1028 peter@eisentraut.org     2643                 :           1624 :         aclresult = object_aclcheck(ProcedureRelationId, wfunc->winfnoid, GetUserId(),
                               2644                 :                :                                     ACL_EXECUTE);
 6096 tgl@sss.pgh.pa.us        2645         [ -  + ]:           1624 :         if (aclresult != ACLCHECK_OK)
 2835 peter_e@gmx.net          2646                 :UBC           0 :             aclcheck_error(aclresult, OBJECT_FUNCTION,
 6096 tgl@sss.pgh.pa.us        2647                 :              0 :                            get_func_name(wfunc->winfnoid));
 4530 rhaas@postgresql.org     2648         [ -  + ]:CBC        1624 :         InvokeFunctionExecuteHook(wfunc->winfnoid);
                               2649                 :                : 
                               2650                 :                :         /* Fill in the perfuncstate data */
 6096 tgl@sss.pgh.pa.us        2651                 :           1624 :         perfuncstate->wfuncstate = wfuncstate;
                               2652                 :           1624 :         perfuncstate->wfunc = wfunc;
                               2653                 :           1624 :         perfuncstate->numArguments = list_length(wfuncstate->args);
 5261                          2654                 :           1624 :         perfuncstate->winCollation = wfunc->inputcollid;
                               2655                 :                : 
 6096                          2656                 :           1624 :         get_typlenbyval(wfunc->wintype,
                               2657                 :                :                         &perfuncstate->resulttypeLen,
                               2658                 :                :                         &perfuncstate->resulttypeByVal);
                               2659                 :                : 
                               2660                 :                :         /*
                               2661                 :                :          * If it's really just a plain aggregate function, we'll emulate the
                               2662                 :                :          * Agg environment for it.
                               2663                 :                :          */
                               2664                 :           1624 :         perfuncstate->plain_agg = wfunc->winagg;
                               2665         [ +  + ]:           1624 :         if (wfunc->winagg)
                               2666                 :                :         {
                               2667                 :                :             WindowStatePerAgg peraggstate;
                               2668                 :                : 
                               2669                 :            766 :             perfuncstate->aggno = ++aggno;
                               2670                 :            766 :             peraggstate = &winstate->peragg[aggno];
                               2671                 :            766 :             initialize_peragg(winstate, wfunc, peraggstate);
                               2672                 :            766 :             peraggstate->wfuncno = wfuncno;
                               2673                 :                :         }
                               2674                 :                :         else
                               2675                 :                :         {
                               2676                 :            858 :             WindowObject winobj = makeNode(WindowObjectData);
                               2677                 :                : 
                               2678                 :            858 :             winobj->winstate = winstate;
                               2679                 :            858 :             winobj->argstates = wfuncstate->args;
                               2680                 :            858 :             winobj->localmem = NULL;
                               2681                 :            858 :             perfuncstate->winobj = winobj;
                               2682                 :                : 
                               2683                 :                :             /* It's a real window function, so set up to call it. */
 1767                          2684                 :            858 :             fmgr_info_cxt(wfunc->winfnoid, &perfuncstate->flinfo,
                               2685                 :                :                           econtext->ecxt_per_query_memory);
                               2686                 :            858 :             fmgr_info_set_expr((Node *) wfunc, &perfuncstate->flinfo);
                               2687                 :                :         }
                               2688                 :                :     }
                               2689                 :                : 
                               2690                 :                :     /* Update numfuncs, numaggs to match number of unique functions found */
 6096                          2691                 :           1273 :     winstate->numfuncs = wfuncno + 1;
                               2692                 :           1273 :     winstate->numaggs = aggno + 1;
                               2693                 :                : 
                               2694                 :                :     /* Set up WindowObject for aggregates, if needed */
 5685                          2695         [ +  + ]:           1273 :     if (winstate->numaggs > 0)
                               2696                 :                :     {
                               2697                 :            709 :         WindowObject agg_winobj = makeNode(WindowObjectData);
                               2698                 :                : 
                               2699                 :            709 :         agg_winobj->winstate = winstate;
                               2700                 :            709 :         agg_winobj->argstates = NIL;
                               2701                 :            709 :         agg_winobj->localmem = NULL;
                               2702                 :                :         /* make sure markptr = -1 to invalidate. It may not get used */
                               2703                 :            709 :         agg_winobj->markptr = -1;
                               2704                 :            709 :         agg_winobj->readptr = -1;
                               2705                 :            709 :         winstate->agg_winobj = agg_winobj;
                               2706                 :                :     }
                               2707                 :                : 
                               2708                 :                :     /* Set the status to running */
 1247 drowley@postgresql.o     2709                 :           1273 :     winstate->status = WINDOWAGG_RUN;
                               2710                 :                : 
                               2711                 :                :     /* initialize frame bound offset expressions */
 5685 tgl@sss.pgh.pa.us        2712                 :           1273 :     winstate->startOffset = ExecInitExpr((Expr *) node->startOffset,
                               2713                 :                :                                          (PlanState *) winstate);
                               2714                 :           1273 :     winstate->endOffset = ExecInitExpr((Expr *) node->endOffset,
                               2715                 :                :                                        (PlanState *) winstate);
                               2716                 :                : 
                               2717                 :                :     /* Lookup in_range support functions if needed */
 2768                          2718         [ +  + ]:           1273 :     if (OidIsValid(node->startInRangeFunc))
                               2719                 :            261 :         fmgr_info(node->startInRangeFunc, &winstate->startInRangeFunc);
                               2720         [ +  + ]:           1273 :     if (OidIsValid(node->endInRangeFunc))
                               2721                 :            294 :         fmgr_info(node->endInRangeFunc, &winstate->endInRangeFunc);
                               2722                 :           1273 :     winstate->inRangeColl = node->inRangeColl;
                               2723                 :           1273 :     winstate->inRangeAsc = node->inRangeAsc;
                               2724                 :           1273 :     winstate->inRangeNullsFirst = node->inRangeNullsFirst;
                               2725                 :                : 
 5685                          2726                 :           1273 :     winstate->all_first = true;
 6096                          2727                 :           1273 :     winstate->partition_spooled = false;
                               2728                 :           1273 :     winstate->more_partitions = false;
  366 drowley@postgresql.o     2729                 :           1273 :     winstate->next_partition = true;
                               2730                 :                : 
 6096 tgl@sss.pgh.pa.us        2731                 :           1273 :     return winstate;
                               2732                 :                : }
                               2733                 :                : 
                               2734                 :                : /* -----------------
                               2735                 :                :  * ExecEndWindowAgg
                               2736                 :                :  * -----------------
                               2737                 :                :  */
                               2738                 :                : void
                               2739                 :           1207 : ExecEndWindowAgg(WindowAggState *node)
                               2740                 :                : {
                               2741                 :                :     PlanState  *outerPlan;
                               2742                 :                :     int         i;
                               2743                 :                : 
  366 drowley@postgresql.o     2744         [ +  + ]:           1207 :     if (node->buffer != NULL)
                               2745                 :                :     {
                               2746                 :            981 :         tuplestore_end(node->buffer);
                               2747                 :                : 
                               2748                 :                :         /* nullify so that release_partition skips the tuplestore_clear() */
                               2749                 :            981 :         node->buffer = NULL;
                               2750                 :                :     }
                               2751                 :                : 
 6096 tgl@sss.pgh.pa.us        2752                 :           1207 :     release_partition(node);
                               2753                 :                : 
 4165                          2754         [ +  + ]:           1952 :     for (i = 0; i < node->numaggs; i++)
                               2755                 :                :     {
                               2756         [ +  + ]:            745 :         if (node->peragg[i].aggcontext != node->aggcontext)
                               2757                 :            393 :             MemoryContextDelete(node->peragg[i].aggcontext);
                               2758                 :                :     }
 5685                          2759                 :           1207 :     MemoryContextDelete(node->partcontext);
                               2760                 :           1207 :     MemoryContextDelete(node->aggcontext);
                               2761                 :                : 
 4165                          2762                 :           1207 :     pfree(node->perfunc);
                               2763                 :           1207 :     pfree(node->peragg);
                               2764                 :                : 
 6096                          2765                 :           1207 :     outerPlan = outerPlanState(node);
                               2766                 :           1207 :     ExecEndNode(outerPlan);
                               2767                 :           1207 : }
                               2768                 :                : 
                               2769                 :                : /* -----------------
                               2770                 :                :  * ExecReScanWindowAgg
                               2771                 :                :  * -----------------
                               2772                 :                :  */
                               2773                 :                : void
 5535                          2774                 :             39 : ExecReScanWindowAgg(WindowAggState *node)
                               2775                 :                : {
 3759 bruce@momjian.us         2776                 :             39 :     PlanState  *outerPlan = outerPlanState(node);
 5931                          2777                 :             39 :     ExprContext *econtext = node->ss.ps.ps_ExprContext;
                               2778                 :                : 
 1247 drowley@postgresql.o     2779                 :             39 :     node->status = WINDOWAGG_RUN;
 5685 tgl@sss.pgh.pa.us        2780                 :             39 :     node->all_first = true;
                               2781                 :                : 
                               2782                 :                :     /* release tuplestore et al */
 6096                          2783                 :             39 :     release_partition(node);
                               2784                 :                : 
                               2785                 :                :     /* release all temp tuples, but especially first_part_slot */
                               2786                 :             39 :     ExecClearTuple(node->ss.ss_ScanTupleSlot);
                               2787                 :             39 :     ExecClearTuple(node->first_part_slot);
 6093                          2788                 :             39 :     ExecClearTuple(node->agg_row_slot);
 6096                          2789                 :             39 :     ExecClearTuple(node->temp_slot_1);
                               2790                 :             39 :     ExecClearTuple(node->temp_slot_2);
 2768                          2791         [ -  + ]:             39 :     if (node->framehead_slot)
 2768 tgl@sss.pgh.pa.us        2792                 :UBC           0 :         ExecClearTuple(node->framehead_slot);
 2768 tgl@sss.pgh.pa.us        2793         [ +  + ]:CBC          39 :     if (node->frametail_slot)
                               2794                 :              3 :         ExecClearTuple(node->frametail_slot);
                               2795                 :                : 
                               2796                 :                :     /* Forget current wfunc values */
 6096                          2797   [ +  -  +  -  :             78 :     MemSet(econtext->ecxt_aggvalues, 0, sizeof(Datum) * node->numfuncs);
                                     +  -  +  -  +  
                                                 + ]
                               2798   [ +  -  -  +  :             39 :     MemSet(econtext->ecxt_aggnulls, 0, sizeof(bool) * node->numfuncs);
                                     -  -  -  -  -  
                                                 - ]
                               2799                 :                : 
                               2800                 :                :     /*
                               2801                 :                :      * if chgParam of subnode is not null then plan will be re-scanned by
                               2802                 :                :      * first ExecProcNode.
                               2803                 :                :      */
 3778 rhaas@postgresql.org     2804         [ +  + ]:             39 :     if (outerPlan->chgParam == NULL)
                               2805                 :              3 :         ExecReScan(outerPlan);
 6096 tgl@sss.pgh.pa.us        2806                 :             39 : }
                               2807                 :                : 
                               2808                 :                : /*
                               2809                 :                :  * initialize_peragg
                               2810                 :                :  *
                               2811                 :                :  * Almost same as in nodeAgg.c, except we don't support DISTINCT currently.
                               2812                 :                :  */
                               2813                 :                : static WindowStatePerAggData *
                               2814                 :            766 : initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc,
                               2815                 :                :                   WindowStatePerAgg peraggstate)
                               2816                 :                : {
                               2817                 :                :     Oid         inputTypes[FUNC_MAX_ARGS];
                               2818                 :                :     int         numArguments;
                               2819                 :                :     HeapTuple   aggTuple;
                               2820                 :                :     Form_pg_aggregate aggform;
                               2821                 :                :     Oid         aggtranstype;
                               2822                 :                :     AttrNumber  initvalAttNo;
                               2823                 :                :     AclResult   aclresult;
                               2824                 :                :     bool        use_ma_code;
                               2825                 :                :     Oid         transfn_oid,
                               2826                 :                :                 invtransfn_oid,
                               2827                 :                :                 finalfn_oid;
                               2828                 :                :     bool        finalextra;
                               2829                 :                :     char        finalmodify;
                               2830                 :                :     Expr       *transfnexpr,
                               2831                 :                :                *invtransfnexpr,
                               2832                 :                :                *finalfnexpr;
                               2833                 :                :     Datum       textInitVal;
                               2834                 :                :     int         i;
                               2835                 :                :     ListCell   *lc;
                               2836                 :                : 
                               2837                 :            766 :     numArguments = list_length(wfunc->args);
                               2838                 :                : 
                               2839                 :            766 :     i = 0;
                               2840   [ +  +  +  +  :           1469 :     foreach(lc, wfunc->args)
                                              +  + ]
                               2841                 :                :     {
                               2842                 :            703 :         inputTypes[i++] = exprType((Node *) lfirst(lc));
                               2843                 :                :     }
                               2844                 :                : 
 5683 rhaas@postgresql.org     2845                 :            766 :     aggTuple = SearchSysCache1(AGGFNOID, ObjectIdGetDatum(wfunc->winfnoid));
 6096 tgl@sss.pgh.pa.us        2846         [ -  + ]:            766 :     if (!HeapTupleIsValid(aggTuple))
 6096 tgl@sss.pgh.pa.us        2847         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for aggregate %u",
                               2848                 :                :              wfunc->winfnoid);
 6096 tgl@sss.pgh.pa.us        2849                 :CBC         766 :     aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
                               2850                 :                : 
                               2851                 :                :     /*
                               2852                 :                :      * Figure out whether we want to use the moving-aggregate implementation,
                               2853                 :                :      * and collect the right set of fields from the pg_aggregate entry.
                               2854                 :                :      *
                               2855                 :                :      * It's possible that an aggregate would supply a safe moving-aggregate
                               2856                 :                :      * implementation and an unsafe normal one, in which case our hand is
                               2857                 :                :      * forced.  Otherwise, if the frame head can't move, we don't need
                               2858                 :                :      * moving-aggregate code.  Even if we'd like to use it, don't do so if the
                               2859                 :                :      * aggregate's arguments (and FILTER clause if any) contain any calls to
                               2860                 :                :      * volatile functions.  Otherwise, the difference between restarting and
                               2861                 :                :      * not restarting the aggregation would be user-visible.
                               2862                 :                :      *
                               2863                 :                :      * We also don't risk using moving aggregates when there are subplans in
                               2864                 :                :      * the arguments or FILTER clause.  This is partly because
                               2865                 :                :      * contain_volatile_functions() doesn't look inside subplans; but there
                               2866                 :                :      * are other reasons why a subplan's output might be volatile.  For
                               2867                 :                :      * example, syncscan mode can render the results nonrepeatable.
                               2868                 :                :      */
 2884                          2869         [ +  + ]:            766 :     if (!OidIsValid(aggform->aggminvtransfn))
                               2870                 :             98 :         use_ma_code = false;    /* sine qua non */
                               2871         [ +  - ]:            668 :     else if (aggform->aggmfinalmodify == AGGMODIFY_READ_ONLY &&
  841                          2872         [ -  + ]:            668 :              aggform->aggfinalmodify != AGGMODIFY_READ_ONLY)
 2884 tgl@sss.pgh.pa.us        2873                 :UBC           0 :         use_ma_code = true;     /* decision forced by safety */
 2884 tgl@sss.pgh.pa.us        2874         [ +  + ]:CBC         668 :     else if (winstate->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
                               2875                 :            263 :         use_ma_code = false;    /* non-moving frame head */
                               2876         [ +  + ]:            405 :     else if (contain_volatile_functions((Node *) wfunc))
                               2877                 :              6 :         use_ma_code = false;    /* avoid possible behavioral change */
  936 drowley@postgresql.o     2878         [ -  + ]:            399 :     else if (contain_subplans((Node *) wfunc))
  936 drowley@postgresql.o     2879                 :UBC           0 :         use_ma_code = false;    /* subplans might contain volatile functions */
                               2880                 :                :     else
 2884 tgl@sss.pgh.pa.us        2881                 :CBC         399 :         use_ma_code = true;     /* yes, let's use it */
                               2882         [ +  + ]:            766 :     if (use_ma_code)
                               2883                 :                :     {
 4165                          2884                 :            399 :         peraggstate->transfn_oid = transfn_oid = aggform->aggmtransfn;
                               2885                 :            399 :         peraggstate->invtransfn_oid = invtransfn_oid = aggform->aggminvtransfn;
                               2886                 :            399 :         peraggstate->finalfn_oid = finalfn_oid = aggform->aggmfinalfn;
 4154                          2887                 :            399 :         finalextra = aggform->aggmfinalextra;
 2884                          2888                 :            399 :         finalmodify = aggform->aggmfinalmodify;
 4165                          2889                 :            399 :         aggtranstype = aggform->aggmtranstype;
                               2890                 :            399 :         initvalAttNo = Anum_pg_aggregate_aggminitval;
                               2891                 :                :     }
                               2892                 :                :     else
                               2893                 :                :     {
                               2894                 :            367 :         peraggstate->transfn_oid = transfn_oid = aggform->aggtransfn;
                               2895                 :            367 :         peraggstate->invtransfn_oid = invtransfn_oid = InvalidOid;
                               2896                 :            367 :         peraggstate->finalfn_oid = finalfn_oid = aggform->aggfinalfn;
 4154                          2897                 :            367 :         finalextra = aggform->aggfinalextra;
 2884                          2898                 :            367 :         finalmodify = aggform->aggfinalmodify;
 4165                          2899                 :            367 :         aggtranstype = aggform->aggtranstype;
                               2900                 :            367 :         initvalAttNo = Anum_pg_aggregate_agginitval;
                               2901                 :                :     }
                               2902                 :                : 
                               2903                 :                :     /*
                               2904                 :                :      * ExecInitWindowAgg already checked permission to call aggregate function
                               2905                 :                :      * ... but we still need to check the component functions
                               2906                 :                :      */
                               2907                 :                : 
                               2908                 :                :     /* Check that aggregate owner has permission to call component fns */
                               2909                 :                :     {
                               2910                 :                :         HeapTuple   procTuple;
                               2911                 :                :         Oid         aggOwner;
                               2912                 :                : 
 5683 rhaas@postgresql.org     2913                 :            766 :         procTuple = SearchSysCache1(PROCOID,
                               2914                 :                :                                     ObjectIdGetDatum(wfunc->winfnoid));
 6096 tgl@sss.pgh.pa.us        2915         [ -  + ]:            766 :         if (!HeapTupleIsValid(procTuple))
 6096 tgl@sss.pgh.pa.us        2916         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for function %u",
                               2917                 :                :                  wfunc->winfnoid);
 6096 tgl@sss.pgh.pa.us        2918                 :CBC         766 :         aggOwner = ((Form_pg_proc) GETSTRUCT(procTuple))->proowner;
                               2919                 :            766 :         ReleaseSysCache(procTuple);
                               2920                 :                : 
 1028 peter@eisentraut.org     2921                 :            766 :         aclresult = object_aclcheck(ProcedureRelationId, transfn_oid, aggOwner,
                               2922                 :                :                                     ACL_EXECUTE);
 6096 tgl@sss.pgh.pa.us        2923         [ -  + ]:            766 :         if (aclresult != ACLCHECK_OK)
 2835 peter_e@gmx.net          2924                 :UBC           0 :             aclcheck_error(aclresult, OBJECT_FUNCTION,
 6096 tgl@sss.pgh.pa.us        2925                 :              0 :                            get_func_name(transfn_oid));
 4530 rhaas@postgresql.org     2926         [ -  + ]:CBC         766 :         InvokeFunctionExecuteHook(transfn_oid);
                               2927                 :                : 
 4165 tgl@sss.pgh.pa.us        2928         [ +  + ]:            766 :         if (OidIsValid(invtransfn_oid))
                               2929                 :                :         {
 1028 peter@eisentraut.org     2930                 :            399 :             aclresult = object_aclcheck(ProcedureRelationId, invtransfn_oid, aggOwner,
                               2931                 :                :                                         ACL_EXECUTE);
 4165 tgl@sss.pgh.pa.us        2932         [ -  + ]:            399 :             if (aclresult != ACLCHECK_OK)
 2835 peter_e@gmx.net          2933                 :UBC           0 :                 aclcheck_error(aclresult, OBJECT_FUNCTION,
 4165 tgl@sss.pgh.pa.us        2934                 :              0 :                                get_func_name(invtransfn_oid));
 4165 tgl@sss.pgh.pa.us        2935         [ -  + ]:CBC         399 :             InvokeFunctionExecuteHook(invtransfn_oid);
                               2936                 :                :         }
                               2937                 :                : 
 6096                          2938         [ +  + ]:            766 :         if (OidIsValid(finalfn_oid))
                               2939                 :                :         {
 1028 peter@eisentraut.org     2940                 :            421 :             aclresult = object_aclcheck(ProcedureRelationId, finalfn_oid, aggOwner,
                               2941                 :                :                                         ACL_EXECUTE);
 6096 tgl@sss.pgh.pa.us        2942         [ -  + ]:            421 :             if (aclresult != ACLCHECK_OK)
 2835 peter_e@gmx.net          2943                 :UBC           0 :                 aclcheck_error(aclresult, OBJECT_FUNCTION,
 6096 tgl@sss.pgh.pa.us        2944                 :              0 :                                get_func_name(finalfn_oid));
 4530 rhaas@postgresql.org     2945         [ -  + ]:CBC         421 :             InvokeFunctionExecuteHook(finalfn_oid);
                               2946                 :                :         }
                               2947                 :                :     }
                               2948                 :                : 
                               2949                 :                :     /*
                               2950                 :                :      * If the selected finalfn isn't read-only, we can't run this aggregate as
                               2951                 :                :      * a window function.  This is a user-facing error, so we take a bit more
                               2952                 :                :      * care with the error message than elsewhere in this function.
                               2953                 :                :      */
 2884 tgl@sss.pgh.pa.us        2954         [ -  + ]:            766 :     if (finalmodify != AGGMODIFY_READ_ONLY)
 2884 tgl@sss.pgh.pa.us        2955         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2956                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               2957                 :                :                  errmsg("aggregate function %s does not support use as a window function",
                               2958                 :                :                         format_procedure(wfunc->winfnoid))));
                               2959                 :                : 
                               2960                 :                :     /* Detect how many arguments to pass to the finalfn */
 4154 tgl@sss.pgh.pa.us        2961         [ +  + ]:CBC         766 :     if (finalextra)
                               2962                 :             13 :         peraggstate->numFinalArgs = numArguments + 1;
                               2963                 :                :     else
                               2964                 :            753 :         peraggstate->numFinalArgs = 1;
                               2965                 :                : 
                               2966                 :                :     /* resolve actual type of transition state, if polymorphic */
 4275                          2967                 :            766 :     aggtranstype = resolve_aggregate_transtype(wfunc->winfnoid,
                               2968                 :                :                                                aggtranstype,
                               2969                 :                :                                                inputTypes,
                               2970                 :                :                                                numArguments);
                               2971                 :                : 
                               2972                 :                :     /* build expression trees using actual argument & result types */
 3686 heikki.linnakangas@i     2973                 :            766 :     build_aggregate_transfn_expr(inputTypes,
                               2974                 :                :                                  numArguments,
                               2975                 :                :                                  0, /* no ordered-set window functions yet */
                               2976                 :                :                                  false, /* no variadic window functions yet */
                               2977                 :                :                                  aggtranstype,
                               2978                 :                :                                  wfunc->inputcollid,
                               2979                 :                :                                  transfn_oid,
                               2980                 :                :                                  invtransfn_oid,
                               2981                 :                :                                  &transfnexpr,
                               2982                 :                :                                  &invtransfnexpr);
                               2983                 :                : 
                               2984                 :                :     /* set up infrastructure for calling the transfn(s) and finalfn */
 6096 tgl@sss.pgh.pa.us        2985                 :            766 :     fmgr_info(transfn_oid, &peraggstate->transfn);
 5285                          2986                 :            766 :     fmgr_info_set_expr((Node *) transfnexpr, &peraggstate->transfn);
                               2987                 :                : 
 4165                          2988         [ +  + ]:            766 :     if (OidIsValid(invtransfn_oid))
                               2989                 :                :     {
                               2990                 :            399 :         fmgr_info(invtransfn_oid, &peraggstate->invtransfn);
                               2991                 :            399 :         fmgr_info_set_expr((Node *) invtransfnexpr, &peraggstate->invtransfn);
                               2992                 :                :     }
                               2993                 :                : 
 6096                          2994         [ +  + ]:            766 :     if (OidIsValid(finalfn_oid))
                               2995                 :                :     {
 3686 heikki.linnakangas@i     2996                 :            421 :         build_aggregate_finalfn_expr(inputTypes,
                               2997                 :                :                                      peraggstate->numFinalArgs,
                               2998                 :                :                                      aggtranstype,
                               2999                 :                :                                      wfunc->wintype,
                               3000                 :                :                                      wfunc->inputcollid,
                               3001                 :                :                                      finalfn_oid,
                               3002                 :                :                                      &finalfnexpr);
 6096 tgl@sss.pgh.pa.us        3003                 :            421 :         fmgr_info(finalfn_oid, &peraggstate->finalfn);
 5285                          3004                 :            421 :         fmgr_info_set_expr((Node *) finalfnexpr, &peraggstate->finalfn);
                               3005                 :                :     }
                               3006                 :                : 
                               3007                 :                :     /* get info about relevant datatypes */
 6096                          3008                 :            766 :     get_typlenbyval(wfunc->wintype,
                               3009                 :                :                     &peraggstate->resulttypeLen,
                               3010                 :                :                     &peraggstate->resulttypeByVal);
                               3011                 :            766 :     get_typlenbyval(aggtranstype,
                               3012                 :                :                     &peraggstate->transtypeLen,
                               3013                 :                :                     &peraggstate->transtypeByVal);
                               3014                 :                : 
                               3015                 :                :     /*
                               3016                 :                :      * initval is potentially null, so don't try to access it as a struct
                               3017                 :                :      * field. Must do it the hard way with SysCacheGetAttr.
                               3018                 :                :      */
 4165                          3019                 :            766 :     textInitVal = SysCacheGetAttr(AGGFNOID, aggTuple, initvalAttNo,
                               3020                 :                :                                   &peraggstate->initValueIsNull);
                               3021                 :                : 
 6096                          3022         [ +  + ]:            766 :     if (peraggstate->initValueIsNull)
                               3023                 :            416 :         peraggstate->initValue = (Datum) 0;
                               3024                 :                :     else
                               3025                 :            350 :         peraggstate->initValue = GetAggInitVal(textInitVal,
                               3026                 :                :                                                aggtranstype);
                               3027                 :                : 
                               3028                 :                :     /*
                               3029                 :                :      * If the transfn is strict and the initval is NULL, make sure input type
                               3030                 :                :      * and transtype are the same (or at least binary-compatible), so that
                               3031                 :                :      * it's OK to use the first input value as the initial transValue.  This
                               3032                 :                :      * should have been checked at agg definition time, but we must check
                               3033                 :                :      * again in case the transfn's strictness property has been changed.
                               3034                 :                :      */
                               3035   [ +  +  +  + ]:            766 :     if (peraggstate->transfn.fn_strict && peraggstate->initValueIsNull)
                               3036                 :                :     {
                               3037         [ +  - ]:             82 :         if (numArguments < 1 ||
                               3038         [ -  + ]:             82 :             !IsBinaryCoercible(inputTypes[0], aggtranstype))
 6096 tgl@sss.pgh.pa.us        3039         [ #  # ]:UBC           0 :             ereport(ERROR,
                               3040                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               3041                 :                :                      errmsg("aggregate %u needs to have compatible input type and transition type",
                               3042                 :                :                             wfunc->winfnoid)));
                               3043                 :                :     }
                               3044                 :                : 
                               3045                 :                :     /*
                               3046                 :                :      * Insist that forward and inverse transition functions have the same
                               3047                 :                :      * strictness setting.  Allowing them to differ would require handling
                               3048                 :                :      * more special cases in advance_windowaggregate and
                               3049                 :                :      * advance_windowaggregate_base, for no discernible benefit.  This should
                               3050                 :                :      * have been checked at agg definition time, but we must check again in
                               3051                 :                :      * case either function's strictness property has been changed.
                               3052                 :                :      */
 4165 tgl@sss.pgh.pa.us        3053         [ +  + ]:CBC         766 :     if (OidIsValid(invtransfn_oid) &&
                               3054         [ -  + ]:            399 :         peraggstate->transfn.fn_strict != peraggstate->invtransfn.fn_strict)
 4165 tgl@sss.pgh.pa.us        3055         [ #  # ]:UBC           0 :         ereport(ERROR,
                               3056                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               3057                 :                :                  errmsg("strictness of aggregate's forward and inverse transition functions must match")));
                               3058                 :                : 
                               3059                 :                :     /*
                               3060                 :                :      * Moving aggregates use their own aggcontext.
                               3061                 :                :      *
                               3062                 :                :      * This is necessary because they might restart at different times, so we
                               3063                 :                :      * might never be able to reset the shared context otherwise.  We can't
                               3064                 :                :      * make it the aggregates' responsibility to clean up after themselves,
                               3065                 :                :      * because strict aggregates must be restarted whenever we remove their
                               3066                 :                :      * last non-NULL input, which the aggregate won't be aware is happening.
                               3067                 :                :      * Also, just pfree()ing the transValue upon restarting wouldn't help,
                               3068                 :                :      * since we'd miss any indirectly referenced data.  We could, in theory,
                               3069                 :                :      * make the memory allocation rules for moving aggregates different than
                               3070                 :                :      * they have historically been for plain aggregates, but that seems grotty
                               3071                 :                :      * and likely to lead to memory leaks.
                               3072                 :                :      */
 4165 tgl@sss.pgh.pa.us        3073         [ +  + ]:CBC         766 :     if (OidIsValid(invtransfn_oid))
                               3074                 :            399 :         peraggstate->aggcontext =
                               3075                 :            399 :             AllocSetContextCreate(CurrentMemoryContext,
                               3076                 :                :                                   "WindowAgg Per Aggregate",
                               3077                 :                :                                   ALLOCSET_DEFAULT_SIZES);
                               3078                 :                :     else
                               3079                 :            367 :         peraggstate->aggcontext = winstate->aggcontext;
                               3080                 :                : 
 6096                          3081                 :            766 :     ReleaseSysCache(aggTuple);
                               3082                 :                : 
                               3083                 :            766 :     return peraggstate;
                               3084                 :                : }
                               3085                 :                : 
                               3086                 :                : static Datum
                               3087                 :            350 : GetAggInitVal(Datum textInitVal, Oid transtype)
                               3088                 :                : {
                               3089                 :                :     Oid         typinput,
                               3090                 :                :                 typioparam;
                               3091                 :                :     char       *strInitVal;
                               3092                 :                :     Datum       initVal;
                               3093                 :                : 
                               3094                 :            350 :     getTypeInputInfo(transtype, &typinput, &typioparam);
                               3095                 :            350 :     strInitVal = TextDatumGetCString(textInitVal);
                               3096                 :            350 :     initVal = OidInputFunctionCall(typinput, strInitVal,
                               3097                 :                :                                    typioparam, -1);
                               3098                 :            350 :     pfree(strInitVal);
                               3099                 :            350 :     return initVal;
                               3100                 :                : }
                               3101                 :                : 
                               3102                 :                : /*
                               3103                 :                :  * are_peers
                               3104                 :                :  * compare two rows to see if they are equal according to the ORDER BY clause
                               3105                 :                :  *
                               3106                 :                :  * NB: this does not consider the window frame mode.
                               3107                 :                :  */
                               3108                 :                : static bool
                               3109                 :         298523 : are_peers(WindowAggState *winstate, TupleTableSlot *slot1,
                               3110                 :                :           TupleTableSlot *slot2)
                               3111                 :                : {
                               3112                 :         298523 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
 2760 andres@anarazel.de       3113                 :         298523 :     ExprContext *econtext = winstate->tmpcontext;
                               3114                 :                : 
                               3115                 :                :     /* If no ORDER BY, all rows are peers with each other */
 6096 tgl@sss.pgh.pa.us        3116         [ +  + ]:         298523 :     if (node->ordNumCols == 0)
                               3117                 :          15499 :         return true;
                               3118                 :                : 
 2760 andres@anarazel.de       3119                 :         283024 :     econtext->ecxt_outertuple = slot1;
                               3120                 :         283024 :     econtext->ecxt_innertuple = slot2;
                               3121                 :         283024 :     return ExecQualAndReset(winstate->ordEqfunction, econtext);
                               3122                 :                : }
                               3123                 :                : 
                               3124                 :                : /*
                               3125                 :                :  * window_gettupleslot
                               3126                 :                :  *  Fetch the pos'th tuple of the current partition into the slot,
                               3127                 :                :  *  using the winobj's read pointer
                               3128                 :                :  *
                               3129                 :                :  * Returns true if successful, false if no such row
                               3130                 :                :  */
                               3131                 :                : static bool
 6096 tgl@sss.pgh.pa.us        3132                 :         379972 : window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
                               3133                 :                : {
                               3134                 :         379972 :     WindowAggState *winstate = winobj->winstate;
                               3135                 :                :     MemoryContext oldcontext;
                               3136                 :                : 
                               3137                 :                :     /* often called repeatedly in a row */
 2965 andres@anarazel.de       3138         [ -  + ]:         379972 :     CHECK_FOR_INTERRUPTS();
                               3139                 :                : 
                               3140                 :                :     /* Don't allow passing -1 to spool_tuples here */
 6096 tgl@sss.pgh.pa.us        3141         [ +  + ]:         379972 :     if (pos < 0)
                               3142                 :            159 :         return false;
                               3143                 :                : 
                               3144                 :                :     /* If necessary, fetch the tuple into the spool */
                               3145                 :         379813 :     spool_tuples(winstate, pos);
                               3146                 :                : 
                               3147         [ +  + ]:         379813 :     if (pos >= winstate->spooled_rows)
                               3148                 :           2290 :         return false;
                               3149                 :                : 
                               3150         [ -  + ]:         377523 :     if (pos < winobj->markpos)
 6096 tgl@sss.pgh.pa.us        3151         [ #  # ]:UBC           0 :         elog(ERROR, "cannot fetch row before WindowObject's mark position");
                               3152                 :                : 
 6096 tgl@sss.pgh.pa.us        3153                 :CBC      377523 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
                               3154                 :                : 
                               3155                 :         377523 :     tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
                               3156                 :                : 
                               3157                 :                :     /*
                               3158                 :                :      * Advance or rewind until we are within one tuple of the one we want.
                               3159                 :                :      */
 4164                          3160         [ +  + ]:         377523 :     if (winobj->seekpos < pos - 1)
                               3161                 :                :     {
                               3162         [ -  + ]:           1128 :         if (!tuplestore_skiptuples(winstate->buffer,
                               3163                 :           1128 :                                    pos - 1 - winobj->seekpos,
                               3164                 :                :                                    true))
 4164 tgl@sss.pgh.pa.us        3165         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected end of tuplestore");
 4164 tgl@sss.pgh.pa.us        3166                 :CBC        1128 :         winobj->seekpos = pos - 1;
                               3167                 :                :     }
                               3168         [ +  + ]:         376395 :     else if (winobj->seekpos > pos + 1)
                               3169                 :                :     {
                               3170         [ -  + ]:           1360 :         if (!tuplestore_skiptuples(winstate->buffer,
                               3171                 :           1360 :                                    winobj->seekpos - (pos + 1),
                               3172                 :                :                                    false))
 4164 tgl@sss.pgh.pa.us        3173         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected end of tuplestore");
 4164 tgl@sss.pgh.pa.us        3174                 :CBC        1360 :         winobj->seekpos = pos + 1;
                               3175                 :                :     }
                               3176         [ +  + ]:         375035 :     else if (winobj->seekpos == pos)
                               3177                 :                :     {
                               3178                 :                :         /*
                               3179                 :                :          * There's no API to refetch the tuple at the current position.  We
                               3180                 :                :          * have to move one tuple forward, and then one backward.  (We don't
                               3181                 :                :          * do it the other way because we might try to fetch the row before
                               3182                 :                :          * our mark, which isn't allowed.)  XXX this case could stand to be
                               3183                 :                :          * optimized.
                               3184                 :                :          */
 6096                          3185                 :          86120 :         tuplestore_advance(winstate->buffer, true);
                               3186                 :          86120 :         winobj->seekpos++;
                               3187                 :                :     }
                               3188                 :                : 
                               3189                 :                :     /*
                               3190                 :                :      * Now we should be on the tuple immediately before or after the one we
                               3191                 :                :      * want, so just fetch forwards or backwards as appropriate.
                               3192                 :                :      *
                               3193                 :                :      * Notice that we tell tuplestore_gettupleslot to make a physical copy of
                               3194                 :                :      * the fetched tuple.  This ensures that the slot's contents remain valid
                               3195                 :                :      * through manipulations of the tuplestore, which some callers depend on.
                               3196                 :                :      */
 4164                          3197         [ +  + ]:         377523 :     if (winobj->seekpos > pos)
                               3198                 :                :     {
 6007                          3199         [ -  + ]:          87545 :         if (!tuplestore_gettupleslot(winstate->buffer, false, true, slot))
 6096 tgl@sss.pgh.pa.us        3200         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected end of tuplestore");
 6096 tgl@sss.pgh.pa.us        3201                 :CBC       87545 :         winobj->seekpos--;
                               3202                 :                :     }
                               3203                 :                :     else
                               3204                 :                :     {
 6007                          3205         [ -  + ]:         289978 :         if (!tuplestore_gettupleslot(winstate->buffer, true, true, slot))
 6096 tgl@sss.pgh.pa.us        3206         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected end of tuplestore");
 6096 tgl@sss.pgh.pa.us        3207                 :CBC      289978 :         winobj->seekpos++;
                               3208                 :                :     }
                               3209                 :                : 
 4164                          3210         [ -  + ]:         377523 :     Assert(winobj->seekpos == pos);
                               3211                 :                : 
 6096                          3212                 :         377523 :     MemoryContextSwitchTo(oldcontext);
                               3213                 :                : 
                               3214                 :         377523 :     return true;
                               3215                 :                : }
                               3216                 :                : 
                               3217                 :                : 
                               3218                 :                : /***********************************************************************
                               3219                 :                :  * API exposed to window functions
                               3220                 :                :  ***********************************************************************/
                               3221                 :                : 
                               3222                 :                : 
                               3223                 :                : /*
                               3224                 :                :  * WinGetPartitionLocalMemory
                               3225                 :                :  *      Get working memory that lives till end of partition processing
                               3226                 :                :  *
                               3227                 :                :  * On first call within a given partition, this allocates and zeroes the
                               3228                 :                :  * requested amount of space.  Subsequent calls just return the same chunk.
                               3229                 :                :  *
                               3230                 :                :  * Memory obtained this way is normally used to hold state that should be
                               3231                 :                :  * automatically reset for each new partition.  If a window function wants
                               3232                 :                :  * to hold state across the whole query, fcinfo->fn_extra can be used in the
                               3233                 :                :  * usual way for that.
                               3234                 :                :  */
                               3235                 :                : void *
                               3236                 :         165695 : WinGetPartitionLocalMemory(WindowObject winobj, Size sz)
                               3237                 :                : {
                               3238   [ +  -  -  + ]:         165695 :     Assert(WindowObjectIsValid(winobj));
                               3239         [ +  + ]:         165695 :     if (winobj->localmem == NULL)
 5685                          3240                 :            208 :         winobj->localmem =
                               3241                 :            208 :             MemoryContextAllocZero(winobj->winstate->partcontext, sz);
 6096                          3242                 :         165695 :     return winobj->localmem;
                               3243                 :                : }
                               3244                 :                : 
                               3245                 :                : /*
                               3246                 :                :  * WinGetCurrentPosition
                               3247                 :                :  *      Return the current row's position (counting from 0) within the current
                               3248                 :                :  *      partition.
                               3249                 :                :  */
                               3250                 :                : int64
                               3251                 :         378800 : WinGetCurrentPosition(WindowObject winobj)
                               3252                 :                : {
                               3253   [ +  -  -  + ]:         378800 :     Assert(WindowObjectIsValid(winobj));
                               3254                 :         378800 :     return winobj->winstate->currentpos;
                               3255                 :                : }
                               3256                 :                : 
                               3257                 :                : /*
                               3258                 :                :  * WinGetPartitionRowCount
                               3259                 :                :  *      Return total number of rows contained in the current partition.
                               3260                 :                :  *
                               3261                 :                :  * Note: this is a relatively expensive operation because it forces the
                               3262                 :                :  * whole partition to be "spooled" into the tuplestore at once.  Once
                               3263                 :                :  * executed, however, additional calls within the same partition are cheap.
                               3264                 :                :  */
                               3265                 :                : int64
                               3266                 :             81 : WinGetPartitionRowCount(WindowObject winobj)
                               3267                 :                : {
                               3268   [ +  -  -  + ]:             81 :     Assert(WindowObjectIsValid(winobj));
                               3269                 :             81 :     spool_tuples(winobj->winstate, -1);
                               3270                 :             81 :     return winobj->winstate->spooled_rows;
                               3271                 :                : }
                               3272                 :                : 
                               3273                 :                : /*
                               3274                 :                :  * WinSetMarkPosition
                               3275                 :                :  *      Set the "mark" position for the window object, which is the oldest row
                               3276                 :                :  *      number (counting from 0) it is allowed to fetch during all subsequent
                               3277                 :                :  *      operations within the current partition.
                               3278                 :                :  *
                               3279                 :                :  * Window functions do not have to call this, but are encouraged to move the
                               3280                 :                :  * mark forward when possible to keep the tuplestore size down and prevent
                               3281                 :                :  * having to spill rows to disk.
                               3282                 :                :  */
                               3283                 :                : void
                               3284                 :         436584 : WinSetMarkPosition(WindowObject winobj, int64 markpos)
                               3285                 :                : {
                               3286                 :                :     WindowAggState *winstate;
                               3287                 :                : 
                               3288   [ +  -  -  + ]:         436584 :     Assert(WindowObjectIsValid(winobj));
                               3289                 :         436584 :     winstate = winobj->winstate;
                               3290                 :                : 
                               3291         [ -  + ]:         436584 :     if (markpos < winobj->markpos)
 6096 tgl@sss.pgh.pa.us        3292         [ #  # ]:UBC           0 :         elog(ERROR, "cannot move WindowObject's mark position backward");
 6096 tgl@sss.pgh.pa.us        3293                 :CBC      436584 :     tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
 4164                          3294         [ +  + ]:         436584 :     if (markpos > winobj->markpos)
                               3295                 :                :     {
                               3296                 :         433815 :         tuplestore_skiptuples(winstate->buffer,
                               3297                 :         433815 :                               markpos - winobj->markpos,
                               3298                 :                :                               true);
                               3299                 :         433815 :         winobj->markpos = markpos;
                               3300                 :                :     }
 6096                          3301                 :         436584 :     tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
 4164                          3302         [ +  + ]:         436584 :     if (markpos > winobj->seekpos)
                               3303                 :                :     {
                               3304                 :         231082 :         tuplestore_skiptuples(winstate->buffer,
                               3305                 :         231082 :                               markpos - winobj->seekpos,
                               3306                 :                :                               true);
                               3307                 :         231082 :         winobj->seekpos = markpos;
                               3308                 :                :     }
 6096                          3309                 :         436584 : }
                               3310                 :                : 
                               3311                 :                : /*
                               3312                 :                :  * WinRowsArePeers
                               3313                 :                :  *      Compare two rows (specified by absolute position in partition) to see
                               3314                 :                :  *      if they are equal according to the ORDER BY clause.
                               3315                 :                :  *
                               3316                 :                :  * NB: this does not consider the window frame mode.
                               3317                 :                :  */
                               3318                 :                : bool
                               3319                 :          82647 : WinRowsArePeers(WindowObject winobj, int64 pos1, int64 pos2)
                               3320                 :                : {
                               3321                 :                :     WindowAggState *winstate;
                               3322                 :                :     WindowAgg  *node;
                               3323                 :                :     TupleTableSlot *slot1;
                               3324                 :                :     TupleTableSlot *slot2;
                               3325                 :                :     bool        res;
                               3326                 :                : 
                               3327   [ +  -  -  + ]:          82647 :     Assert(WindowObjectIsValid(winobj));
                               3328                 :          82647 :     winstate = winobj->winstate;
                               3329                 :          82647 :     node = (WindowAgg *) winstate->ss.ps.plan;
                               3330                 :                : 
                               3331                 :                :     /* If no ORDER BY, all rows are peers; don't bother to fetch them */
                               3332         [ -  + ]:          82647 :     if (node->ordNumCols == 0)
 6096 tgl@sss.pgh.pa.us        3333                 :UBC           0 :         return true;
                               3334                 :                : 
                               3335                 :                :     /*
                               3336                 :                :      * Note: OK to use temp_slot_2 here because we aren't calling any
                               3337                 :                :      * frame-related functions (those tend to clobber temp_slot_2).
                               3338                 :                :      */
 6096 tgl@sss.pgh.pa.us        3339                 :CBC       82647 :     slot1 = winstate->temp_slot_1;
                               3340                 :          82647 :     slot2 = winstate->temp_slot_2;
                               3341                 :                : 
                               3342         [ -  + ]:          82647 :     if (!window_gettupleslot(winobj, pos1, slot1))
 6096 tgl@sss.pgh.pa.us        3343         [ #  # ]:UBC           0 :         elog(ERROR, "specified position is out of window: " INT64_FORMAT,
                               3344                 :                :              pos1);
 6096 tgl@sss.pgh.pa.us        3345         [ -  + ]:CBC       82647 :     if (!window_gettupleslot(winobj, pos2, slot2))
 6096 tgl@sss.pgh.pa.us        3346         [ #  # ]:UBC           0 :         elog(ERROR, "specified position is out of window: " INT64_FORMAT,
                               3347                 :                :              pos2);
                               3348                 :                : 
 6096 tgl@sss.pgh.pa.us        3349                 :CBC       82647 :     res = are_peers(winstate, slot1, slot2);
                               3350                 :                : 
                               3351                 :          82647 :     ExecClearTuple(slot1);
                               3352                 :          82647 :     ExecClearTuple(slot2);
                               3353                 :                : 
                               3354                 :          82647 :     return res;
                               3355                 :                : }
                               3356                 :                : 
                               3357                 :                : /*
                               3358                 :                :  * WinGetFuncArgInPartition
                               3359                 :                :  *      Evaluate a window function's argument expression on a specified
                               3360                 :                :  *      row of the partition.  The row is identified in lseek(2) style,
                               3361                 :                :  *      i.e. relative to the current, first, or last row.
                               3362                 :                :  *
                               3363                 :                :  * argno: argument number to evaluate (counted from 0)
                               3364                 :                :  * relpos: signed rowcount offset from the seek position
                               3365                 :                :  * seektype: WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, or WINDOW_SEEK_TAIL
                               3366                 :                :  * set_mark: If the row is found and set_mark is true, the mark is moved to
                               3367                 :                :  *      the row as a side-effect.
                               3368                 :                :  * isnull: output argument, receives isnull status of result
                               3369                 :                :  * isout: output argument, set to indicate whether target row position
                               3370                 :                :  *      is out of partition (can pass NULL if caller doesn't care about this)
                               3371                 :                :  *
                               3372                 :                :  * Specifying a nonexistent row is not an error, it just causes a null result
                               3373                 :                :  * (plus setting *isout true, if isout isn't NULL).
                               3374                 :                :  */
                               3375                 :                : Datum
                               3376                 :         118170 : WinGetFuncArgInPartition(WindowObject winobj, int argno,
                               3377                 :                :                          int relpos, int seektype, bool set_mark,
                               3378                 :                :                          bool *isnull, bool *isout)
                               3379                 :                : {
                               3380                 :                :     WindowAggState *winstate;
                               3381                 :                :     ExprContext *econtext;
                               3382                 :                :     TupleTableSlot *slot;
                               3383                 :                :     bool        gottuple;
                               3384                 :                :     int64       abs_pos;
                               3385                 :                : 
                               3386   [ +  -  -  + ]:         118170 :     Assert(WindowObjectIsValid(winobj));
 6093                          3387                 :         118170 :     winstate = winobj->winstate;
                               3388                 :         118170 :     econtext = winstate->ss.ps.ps_ExprContext;
                               3389                 :         118170 :     slot = winstate->temp_slot_1;
                               3390                 :                : 
 6096                          3391   [ +  -  -  - ]:         118170 :     switch (seektype)
                               3392                 :                :     {
                               3393                 :         118170 :         case WINDOW_SEEK_CURRENT:
 6093                          3394                 :         118170 :             abs_pos = winstate->currentpos + relpos;
 6096                          3395                 :         118170 :             break;
 6096 tgl@sss.pgh.pa.us        3396                 :UBC           0 :         case WINDOW_SEEK_HEAD:
                               3397                 :              0 :             abs_pos = relpos;
                               3398                 :              0 :             break;
                               3399                 :              0 :         case WINDOW_SEEK_TAIL:
 6093                          3400                 :              0 :             spool_tuples(winstate, -1);
                               3401                 :              0 :             abs_pos = winstate->spooled_rows - 1 + relpos;
 6096                          3402                 :              0 :             break;
                               3403                 :              0 :         default:
                               3404         [ #  # ]:              0 :             elog(ERROR, "unrecognized window seek type: %d", seektype);
                               3405                 :                :             abs_pos = 0;        /* keep compiler quiet */
                               3406                 :                :             break;
                               3407                 :                :     }
                               3408                 :                : 
 6093 tgl@sss.pgh.pa.us        3409                 :CBC      118170 :     gottuple = window_gettupleslot(winobj, abs_pos, slot);
                               3410                 :                : 
 6096                          3411         [ +  + ]:         118170 :     if (!gottuple)
                               3412                 :                :     {
                               3413         [ +  - ]:            180 :         if (isout)
                               3414                 :            180 :             *isout = true;
                               3415                 :            180 :         *isnull = true;
                               3416                 :            180 :         return (Datum) 0;
                               3417                 :                :     }
                               3418                 :                :     else
                               3419                 :                :     {
                               3420         [ +  - ]:         117990 :         if (isout)
                               3421                 :         117990 :             *isout = false;
                               3422         [ +  + ]:         117990 :         if (set_mark)
 2768                          3423                 :         117912 :             WinSetMarkPosition(winobj, abs_pos);
 6096                          3424                 :         117990 :         econtext->ecxt_outertuple = slot;
                               3425                 :         117990 :         return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
                               3426                 :                :                             econtext, isnull);
                               3427                 :                :     }
                               3428                 :                : }
                               3429                 :                : 
                               3430                 :                : /*
                               3431                 :                :  * WinGetFuncArgInFrame
                               3432                 :                :  *      Evaluate a window function's argument expression on a specified
                               3433                 :                :  *      row of the window frame.  The row is identified in lseek(2) style,
                               3434                 :                :  *      i.e. relative to the first or last row of the frame.  (We do not
                               3435                 :                :  *      support WINDOW_SEEK_CURRENT here, because it's not very clear what
                               3436                 :                :  *      that should mean if the current row isn't part of the frame.)
                               3437                 :                :  *
                               3438                 :                :  * argno: argument number to evaluate (counted from 0)
                               3439                 :                :  * relpos: signed rowcount offset from the seek position
                               3440                 :                :  * seektype: WINDOW_SEEK_HEAD or WINDOW_SEEK_TAIL
                               3441                 :                :  * set_mark: If the row is found/in frame and set_mark is true, the mark is
                               3442                 :                :  *      moved to the row as a side-effect.
                               3443                 :                :  * isnull: output argument, receives isnull status of result
                               3444                 :                :  * isout: output argument, set to indicate whether target row position
                               3445                 :                :  *      is out of frame (can pass NULL if caller doesn't care about this)
                               3446                 :                :  *
                               3447                 :                :  * Specifying a nonexistent or not-in-frame row is not an error, it just
                               3448                 :                :  * causes a null result (plus setting *isout true, if isout isn't NULL).
                               3449                 :                :  *
                               3450                 :                :  * Note that some exclusion-clause options lead to situations where the
                               3451                 :                :  * rows that are in-frame are not consecutive in the partition.  But we
                               3452                 :                :  * count only in-frame rows when measuring relpos.
                               3453                 :                :  *
                               3454                 :                :  * The set_mark flag is interpreted as meaning that the caller will specify
                               3455                 :                :  * a constant (or, perhaps, monotonically increasing) relpos in successive
                               3456                 :                :  * calls, so that *if there is no exclusion clause* there will be no need
                               3457                 :                :  * to fetch a row before the previously fetched row.  But we do not expect
                               3458                 :                :  * the caller to know how to account for exclusion clauses.  Therefore,
                               3459                 :                :  * if there is an exclusion clause we take responsibility for adjusting the
                               3460                 :                :  * mark request to something that will be safe given the above assumption
                               3461                 :                :  * about relpos.
                               3462                 :                :  */
                               3463                 :                : Datum
                               3464                 :           4314 : WinGetFuncArgInFrame(WindowObject winobj, int argno,
                               3465                 :                :                      int relpos, int seektype, bool set_mark,
                               3466                 :                :                      bool *isnull, bool *isout)
                               3467                 :                : {
                               3468                 :                :     WindowAggState *winstate;
                               3469                 :                :     ExprContext *econtext;
                               3470                 :                :     TupleTableSlot *slot;
                               3471                 :                :     int64       abs_pos;
                               3472                 :                :     int64       mark_pos;
                               3473                 :                : 
                               3474   [ +  -  -  + ]:           4314 :     Assert(WindowObjectIsValid(winobj));
 6093                          3475                 :           4314 :     winstate = winobj->winstate;
                               3476                 :           4314 :     econtext = winstate->ss.ps.ps_ExprContext;
                               3477                 :           4314 :     slot = winstate->temp_slot_1;
                               3478                 :                : 
 6096                          3479   [ -  +  +  - ]:           4314 :     switch (seektype)
                               3480                 :                :     {
 6096 tgl@sss.pgh.pa.us        3481                 :UBC           0 :         case WINDOW_SEEK_CURRENT:
 2768                          3482         [ #  # ]:              0 :             elog(ERROR, "WINDOW_SEEK_CURRENT is not supported for WinGetFuncArgInFrame");
                               3483                 :                :             abs_pos = mark_pos = 0; /* keep compiler quiet */
                               3484                 :                :             break;
 6096 tgl@sss.pgh.pa.us        3485                 :CBC        2103 :         case WINDOW_SEEK_HEAD:
                               3486                 :                :             /* rejecting relpos < 0 is easy and simplifies code below */
 2768                          3487         [ -  + ]:           2103 :             if (relpos < 0)
 2768 tgl@sss.pgh.pa.us        3488                 :UBC           0 :                 goto out_of_frame;
 2768 tgl@sss.pgh.pa.us        3489                 :CBC        2103 :             update_frameheadpos(winstate);
 5685                          3490                 :           2082 :             abs_pos = winstate->frameheadpos + relpos;
 2768                          3491                 :           2082 :             mark_pos = abs_pos;
                               3492                 :                : 
                               3493                 :                :             /*
                               3494                 :                :              * Account for exclusion option if one is active, but advance only
                               3495                 :                :              * abs_pos not mark_pos.  This prevents changes of the current
                               3496                 :                :              * row's peer group from resulting in trying to fetch a row before
                               3497                 :                :              * some previous mark position.
                               3498                 :                :              *
                               3499                 :                :              * Note that in some corner cases such as current row being
                               3500                 :                :              * outside frame, these calculations are theoretically too simple,
                               3501                 :                :              * but it doesn't matter because we'll end up deciding the row is
                               3502                 :                :              * out of frame.  We do not attempt to avoid fetching rows past
                               3503                 :                :              * end of frame; that would happen in some cases anyway.
                               3504                 :                :              */
                               3505   [ +  +  +  +  :           2082 :             switch (winstate->frameOptions & FRAMEOPTION_EXCLUSION)
                                                 - ]
                               3506                 :                :             {
                               3507                 :           1752 :                 case 0:
                               3508                 :                :                     /* no adjustment needed */
                               3509                 :           1752 :                     break;
                               3510                 :            120 :                 case FRAMEOPTION_EXCLUDE_CURRENT_ROW:
                               3511         [ +  + ]:            120 :                     if (abs_pos >= winstate->currentpos &&
                               3512         [ +  + ]:             93 :                         winstate->currentpos >= winstate->frameheadpos)
                               3513                 :             33 :                         abs_pos++;
                               3514                 :            120 :                     break;
                               3515                 :             60 :                 case FRAMEOPTION_EXCLUDE_GROUP:
                               3516                 :             60 :                     update_grouptailpos(winstate);
                               3517         [ +  + ]:             60 :                     if (abs_pos >= winstate->groupheadpos &&
                               3518         [ +  - ]:             36 :                         winstate->grouptailpos > winstate->frameheadpos)
                               3519                 :                :                     {
                               3520                 :             36 :                         int64       overlapstart = Max(winstate->groupheadpos,
                               3521                 :                :                                                        winstate->frameheadpos);
                               3522                 :                : 
                               3523                 :             36 :                         abs_pos += winstate->grouptailpos - overlapstart;
                               3524                 :                :                     }
                               3525                 :             60 :                     break;
                               3526                 :            150 :                 case FRAMEOPTION_EXCLUDE_TIES:
                               3527                 :            150 :                     update_grouptailpos(winstate);
                               3528         [ +  + ]:            150 :                     if (abs_pos >= winstate->groupheadpos &&
                               3529         [ +  + ]:            102 :                         winstate->grouptailpos > winstate->frameheadpos)
                               3530                 :                :                     {
                               3531                 :             42 :                         int64       overlapstart = Max(winstate->groupheadpos,
                               3532                 :                :                                                        winstate->frameheadpos);
                               3533                 :                : 
                               3534         [ +  - ]:             42 :                         if (abs_pos == overlapstart)
                               3535                 :             42 :                             abs_pos = winstate->currentpos;
                               3536                 :                :                         else
 2768 tgl@sss.pgh.pa.us        3537                 :UBC           0 :                             abs_pos += winstate->grouptailpos - overlapstart - 1;
                               3538                 :                :                     }
 2768 tgl@sss.pgh.pa.us        3539                 :CBC         150 :                     break;
 2768 tgl@sss.pgh.pa.us        3540                 :UBC           0 :                 default:
                               3541         [ #  # ]:              0 :                     elog(ERROR, "unrecognized frame option state: 0x%x",
                               3542                 :                :                          winstate->frameOptions);
                               3543                 :                :                     break;
                               3544                 :                :             }
 6096 tgl@sss.pgh.pa.us        3545                 :CBC        2082 :             break;
                               3546                 :           2211 :         case WINDOW_SEEK_TAIL:
                               3547                 :                :             /* rejecting relpos > 0 is easy and simplifies code below */
 2768                          3548         [ -  + ]:           2211 :             if (relpos > 0)
 2768 tgl@sss.pgh.pa.us        3549                 :UBC           0 :                 goto out_of_frame;
 2768 tgl@sss.pgh.pa.us        3550                 :CBC        2211 :             update_frametailpos(winstate);
                               3551                 :           2208 :             abs_pos = winstate->frametailpos - 1 + relpos;
                               3552                 :                : 
                               3553                 :                :             /*
                               3554                 :                :              * Account for exclusion option if one is active.  If there is no
                               3555                 :                :              * exclusion, we can safely set the mark at the accessed row.  But
                               3556                 :                :              * if there is, we can only mark the frame start, because we can't
                               3557                 :                :              * be sure how far back in the frame the exclusion might cause us
                               3558                 :                :              * to fetch in future.  Furthermore, we have to actually check
                               3559                 :                :              * against frameheadpos here, since it's unsafe to try to fetch a
                               3560                 :                :              * row before frame start if the mark might be there already.
                               3561                 :                :              */
                               3562   [ +  +  +  +  :           2208 :             switch (winstate->frameOptions & FRAMEOPTION_EXCLUSION)
                                                 - ]
                               3563                 :                :             {
                               3564                 :           1968 :                 case 0:
                               3565                 :                :                     /* no adjustment needed */
                               3566                 :           1968 :                     mark_pos = abs_pos;
                               3567                 :           1968 :                     break;
                               3568                 :             60 :                 case FRAMEOPTION_EXCLUDE_CURRENT_ROW:
                               3569         [ +  + ]:             60 :                     if (abs_pos <= winstate->currentpos &&
                               3570         [ +  - ]:              6 :                         winstate->currentpos < winstate->frametailpos)
                               3571                 :              6 :                         abs_pos--;
                               3572                 :             60 :                     update_frameheadpos(winstate);
                               3573         [ +  + ]:             60 :                     if (abs_pos < winstate->frameheadpos)
                               3574                 :              3 :                         goto out_of_frame;
                               3575                 :             57 :                     mark_pos = winstate->frameheadpos;
                               3576                 :             57 :                     break;
                               3577                 :            120 :                 case FRAMEOPTION_EXCLUDE_GROUP:
                               3578                 :            120 :                     update_grouptailpos(winstate);
                               3579         [ +  + ]:            120 :                     if (abs_pos < winstate->grouptailpos &&
                               3580         [ +  - ]:             27 :                         winstate->groupheadpos < winstate->frametailpos)
                               3581                 :                :                     {
                               3582                 :             27 :                         int64       overlapend = Min(winstate->grouptailpos,
                               3583                 :                :                                                      winstate->frametailpos);
                               3584                 :                : 
                               3585                 :             27 :                         abs_pos -= overlapend - winstate->groupheadpos;
                               3586                 :                :                     }
                               3587                 :            120 :                     update_frameheadpos(winstate);
                               3588         [ +  + ]:            120 :                     if (abs_pos < winstate->frameheadpos)
                               3589                 :             27 :                         goto out_of_frame;
                               3590                 :             93 :                     mark_pos = winstate->frameheadpos;
                               3591                 :             93 :                     break;
                               3592                 :             60 :                 case FRAMEOPTION_EXCLUDE_TIES:
                               3593                 :             60 :                     update_grouptailpos(winstate);
                               3594         [ +  + ]:             60 :                     if (abs_pos < winstate->grouptailpos &&
                               3595         [ +  - ]:             18 :                         winstate->groupheadpos < winstate->frametailpos)
                               3596                 :                :                     {
                               3597                 :             18 :                         int64       overlapend = Min(winstate->grouptailpos,
                               3598                 :                :                                                      winstate->frametailpos);
                               3599                 :                : 
                               3600         [ +  - ]:             18 :                         if (abs_pos == overlapend - 1)
                               3601                 :             18 :                             abs_pos = winstate->currentpos;
                               3602                 :                :                         else
 2768 tgl@sss.pgh.pa.us        3603                 :UBC           0 :                             abs_pos -= overlapend - 1 - winstate->groupheadpos;
                               3604                 :                :                     }
 2768 tgl@sss.pgh.pa.us        3605                 :CBC          60 :                     update_frameheadpos(winstate);
                               3606         [ -  + ]:             60 :                     if (abs_pos < winstate->frameheadpos)
 2768 tgl@sss.pgh.pa.us        3607                 :UBC           0 :                         goto out_of_frame;
 2768 tgl@sss.pgh.pa.us        3608                 :CBC          60 :                     mark_pos = winstate->frameheadpos;
                               3609                 :             60 :                     break;
 2768 tgl@sss.pgh.pa.us        3610                 :UBC           0 :                 default:
                               3611         [ #  # ]:              0 :                     elog(ERROR, "unrecognized frame option state: 0x%x",
                               3612                 :                :                          winstate->frameOptions);
                               3613                 :                :                     mark_pos = 0;   /* keep compiler quiet */
                               3614                 :                :                     break;
                               3615                 :                :             }
 6096 tgl@sss.pgh.pa.us        3616                 :CBC        2178 :             break;
 6096 tgl@sss.pgh.pa.us        3617                 :UBC           0 :         default:
                               3618         [ #  # ]:              0 :             elog(ERROR, "unrecognized window seek type: %d", seektype);
                               3619                 :                :             abs_pos = mark_pos = 0; /* keep compiler quiet */
                               3620                 :                :             break;
                               3621                 :                :     }
                               3622                 :                : 
 2768 tgl@sss.pgh.pa.us        3623         [ +  + ]:CBC        4260 :     if (!window_gettupleslot(winobj, abs_pos, slot))
                               3624                 :            198 :         goto out_of_frame;
                               3625                 :                : 
                               3626                 :                :     /* The code above does not detect all out-of-frame cases, so check */
                               3627         [ +  + ]:           4062 :     if (row_is_in_frame(winstate, abs_pos, slot) <= 0)
                               3628                 :            150 :         goto out_of_frame;
                               3629                 :                : 
                               3630         [ -  + ]:           3897 :     if (isout)
 2768 tgl@sss.pgh.pa.us        3631                 :UBC           0 :         *isout = false;
 2768 tgl@sss.pgh.pa.us        3632         [ +  + ]:CBC        3897 :     if (set_mark)
                               3633                 :           3876 :         WinSetMarkPosition(winobj, mark_pos);
                               3634                 :           3897 :     econtext->ecxt_outertuple = slot;
                               3635                 :           3897 :     return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
                               3636                 :                :                         econtext, isnull);
                               3637                 :                : 
                               3638                 :            378 : out_of_frame:
                               3639         [ -  + ]:            378 :     if (isout)
 2768 tgl@sss.pgh.pa.us        3640                 :UBC           0 :         *isout = true;
 2768 tgl@sss.pgh.pa.us        3641                 :CBC         378 :     *isnull = true;
                               3642                 :            378 :     return (Datum) 0;
                               3643                 :                : }
                               3644                 :                : 
                               3645                 :                : /*
                               3646                 :                :  * WinGetFuncArgCurrent
                               3647                 :                :  *      Evaluate a window function's argument expression on the current row.
                               3648                 :                :  *
                               3649                 :                :  * argno: argument number to evaluate (counted from 0)
                               3650                 :                :  * isnull: output argument, receives isnull status of result
                               3651                 :                :  *
                               3652                 :                :  * Note: this isn't quite equivalent to WinGetFuncArgInPartition or
                               3653                 :                :  * WinGetFuncArgInFrame targeting the current row, because it will succeed
                               3654                 :                :  * even if the WindowObject's mark has been set beyond the current row.
                               3655                 :                :  * This should generally be used for "ordinary" arguments of a window
                               3656                 :                :  * function, such as the offset argument of lead() or lag().
                               3657                 :                :  */
                               3658                 :                : Datum
 6096                          3659                 :            582 : WinGetFuncArgCurrent(WindowObject winobj, int argno, bool *isnull)
                               3660                 :                : {
                               3661                 :                :     WindowAggState *winstate;
                               3662                 :                :     ExprContext *econtext;
                               3663                 :                : 
                               3664   [ +  -  -  + ]:            582 :     Assert(WindowObjectIsValid(winobj));
                               3665                 :            582 :     winstate = winobj->winstate;
                               3666                 :                : 
                               3667                 :            582 :     econtext = winstate->ss.ps.ps_ExprContext;
                               3668                 :                : 
                               3669                 :            582 :     econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
                               3670                 :            582 :     return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
                               3671                 :                :                         econtext, isnull);
                               3672                 :                : }
        

Generated by: LCOV version 2.4-beta