LCOV - code coverage report
Current view: top level - src/backend/executor - nodeWindowAgg.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 1306 1417 92.2 %
Date: 2025-10-10 10:17:52 Functions: 38 38 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.16