PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
windowapi.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define WINDOW_SEEK_CURRENT   0
 
#define WINDOW_SEEK_HEAD   1
 
#define WINDOW_SEEK_TAIL   2
 
#define PG_WINDOW_OBJECT()   ((WindowObject) fcinfo->context)
 
#define WindowObjectIsValid(winobj)   ((winobj) != NULL && IsA(winobj, WindowObjectData))
 

Typedefs

typedef struct WindowObjectDataWindowObject
 

Functions

void * WinGetPartitionLocalMemory (WindowObject winobj, Size sz)
 
int64 WinGetCurrentPosition (WindowObject winobj)
 
int64 WinGetPartitionRowCount (WindowObject winobj)
 
void WinSetMarkPosition (WindowObject winobj, int64 markpos)
 
bool WinRowsArePeers (WindowObject winobj, int64 pos1, int64 pos2)
 
Datum WinGetFuncArgInPartition (WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
 
Datum WinGetFuncArgInFrame (WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
 
Datum WinGetFuncArgCurrent (WindowObject winobj, int argno, bool *isnull)
 

Macro Definition Documentation

#define WINDOW_SEEK_CURRENT   0

Definition at line 32 of file windowapi.h.

Referenced by leadlag_common(), WinGetFuncArgInFrame(), and WinGetFuncArgInPartition().

#define WINDOW_SEEK_HEAD   1
#define WINDOW_SEEK_TAIL   2

Definition at line 34 of file windowapi.h.

Referenced by window_last_value(), WinGetFuncArgInFrame(), and WinGetFuncArgInPartition().

Typedef Documentation

Definition at line 37 of file windowapi.h.

Function Documentation

int64 WinGetCurrentPosition ( WindowObject  winobj)

Definition at line 2481 of file nodeWindowAgg.c.

References Assert, WindowAggState::currentpos, WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by rank_up(), window_cume_dist(), window_percent_rank(), window_rank(), and window_row_number().

2482 {
2483  Assert(WindowObjectIsValid(winobj));
2484  return winobj->winstate->currentpos;
2485 }
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:675
int64 currentpos
Definition: execnodes.h:1798
Datum WinGetFuncArgCurrent ( WindowObject  winobj,
int  argno,
bool isnull 
)

Definition at line 2792 of file nodeWindowAgg.c.

References WindowObjectData::argstates, Assert, ExprContext::ecxt_outertuple, ExecEvalExpr(), list_nth(), ScanState::ps, PlanState::ps_ExprContext, WindowAggState::ss, ScanState::ss_ScanTupleSlot, WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by leadlag_common(), window_nth_value(), and window_ntile().

2793 {
2794  WindowAggState *winstate;
2795  ExprContext *econtext;
2796 
2797  Assert(WindowObjectIsValid(winobj));
2798  winstate = winobj->winstate;
2799 
2800  econtext = winstate->ss.ps.ps_ExprContext;
2801 
2802  econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
2803  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2804  econtext, isnull);
2805 }
ScanState ss
Definition: execnodes.h:1784
ExprContext * ps_ExprContext
Definition: execnodes.h:833
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1050
PlanState ps
Definition: execnodes.h:1047
void * list_nth(const List *list, int n)
Definition: list.c:410
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:265
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define Assert(condition)
Definition: c.h:675
Datum WinGetFuncArgInFrame ( WindowObject  winobj,
int  argno,
int  relpos,
int  seektype,
bool  set_mark,
bool isnull,
bool isout 
)

Definition at line 2698 of file nodeWindowAgg.c.

References WindowObjectData::argstates, Assert, WindowAggState::currentpos, ExprContext::ecxt_outertuple, elog, ERROR, ExecEvalExpr(), WindowAggState::frameheadpos, FRAMEOPTION_RANGE, FRAMEOPTION_START_UNBOUNDED_PRECEDING, WindowAggState::frameOptions, WindowAggState::frametailpos, list_nth(), ScanState::ps, PlanState::ps_ExprContext, row_is_in_frame(), WindowAggState::ss, WindowAggState::temp_slot_1, WindowAggState::temp_slot_2, update_frameheadpos(), update_frametailpos(), window_gettupleslot(), WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, WINDOW_SEEK_TAIL, WindowObjectIsValid, WinSetMarkPosition(), and WindowObjectData::winstate.

Referenced by window_first_value(), window_last_value(), and window_nth_value().

2701 {
2702  WindowAggState *winstate;
2703  ExprContext *econtext;
2704  TupleTableSlot *slot;
2705  bool gottuple;
2706  int64 abs_pos;
2707 
2708  Assert(WindowObjectIsValid(winobj));
2709  winstate = winobj->winstate;
2710  econtext = winstate->ss.ps.ps_ExprContext;
2711  slot = winstate->temp_slot_1;
2712 
2713  switch (seektype)
2714  {
2715  case WINDOW_SEEK_CURRENT:
2716  abs_pos = winstate->currentpos + relpos;
2717  break;
2718  case WINDOW_SEEK_HEAD:
2719  update_frameheadpos(winobj, slot);
2720  abs_pos = winstate->frameheadpos + relpos;
2721  break;
2722  case WINDOW_SEEK_TAIL:
2723  update_frametailpos(winobj, slot);
2724  abs_pos = winstate->frametailpos + relpos;
2725  break;
2726  default:
2727  elog(ERROR, "unrecognized window seek type: %d", seektype);
2728  abs_pos = 0; /* keep compiler quiet */
2729  break;
2730  }
2731 
2732  gottuple = window_gettupleslot(winobj, abs_pos, slot);
2733  if (gottuple)
2734  gottuple = row_is_in_frame(winstate, abs_pos, slot);
2735 
2736  if (!gottuple)
2737  {
2738  if (isout)
2739  *isout = true;
2740  *isnull = true;
2741  return (Datum) 0;
2742  }
2743  else
2744  {
2745  if (isout)
2746  *isout = false;
2747  if (set_mark)
2748  {
2749  int frameOptions = winstate->frameOptions;
2750  int64 mark_pos = abs_pos;
2751 
2752  /*
2753  * In RANGE mode with a moving frame head, we must not let the
2754  * mark advance past frameheadpos, since that row has to be
2755  * fetchable during future update_frameheadpos calls.
2756  *
2757  * XXX it is very ugly to pollute window functions' marks with
2758  * this consideration; it could for instance mask a logic bug that
2759  * lets a window function fetch rows before what it had claimed
2760  * was its mark. Perhaps use a separate mark for frame head
2761  * probes?
2762  */
2763  if ((frameOptions & FRAMEOPTION_RANGE) &&
2764  !(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING))
2765  {
2766  update_frameheadpos(winobj, winstate->temp_slot_2);
2767  if (mark_pos > winstate->frameheadpos)
2768  mark_pos = winstate->frameheadpos;
2769  }
2770  WinSetMarkPosition(winobj, mark_pos);
2771  }
2772  econtext->ecxt_outertuple = slot;
2773  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2774  econtext, isnull);
2775  }
2776 }
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:1784
ExprContext * ps_ExprContext
Definition: execnodes.h:833
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition: parsenodes.h:509
TupleTableSlot * temp_slot_1
Definition: execnodes.h:1835
#define WINDOW_SEEK_TAIL
Definition: windowapi.h:34
PlanState ps
Definition: execnodes.h:1047
int64 frameheadpos
Definition: execnodes.h:1799
#define ERROR
Definition: elog.h:43
static bool row_is_in_frame(WindowAggState *winstate, int64 pos, TupleTableSlot *slot)
TupleTableSlot * temp_slot_2
Definition: execnodes.h:1836
void * list_nth(const List *list, int n)
Definition: list.c:410
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:265
static void update_frameheadpos(WindowObject winobj, TupleTableSlot *slot)
#define WINDOW_SEEK_HEAD
Definition: windowapi.h:33
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
uintptr_t Datum
Definition: postgres.h:372
static void update_frametailpos(WindowObject winobj, TupleTableSlot *slot)
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define WINDOW_SEEK_CURRENT
Definition: windowapi.h:32
int64 frametailpos
Definition: execnodes.h:1800
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:506
#define Assert(condition)
Definition: c.h:675
int64 currentpos
Definition: execnodes.h:1798
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
#define elog
Definition: elog.h:219
Datum WinGetFuncArgInPartition ( WindowObject  winobj,
int  argno,
int  relpos,
int  seektype,
bool  set_mark,
bool isnull,
bool isout 
)

Definition at line 2602 of file nodeWindowAgg.c.

References WindowObjectData::argstates, Assert, WindowAggState::currentpos, ExprContext::ecxt_outertuple, elog, ERROR, ExecEvalExpr(), WindowAggState::frameheadpos, FRAMEOPTION_RANGE, FRAMEOPTION_START_UNBOUNDED_PRECEDING, WindowAggState::frameOptions, list_nth(), ScanState::ps, PlanState::ps_ExprContext, spool_tuples(), WindowAggState::spooled_rows, WindowAggState::ss, WindowAggState::temp_slot_1, WindowAggState::temp_slot_2, update_frameheadpos(), window_gettupleslot(), WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, WINDOW_SEEK_TAIL, WindowObjectIsValid, WinSetMarkPosition(), and WindowObjectData::winstate.

Referenced by leadlag_common().

2605 {
2606  WindowAggState *winstate;
2607  ExprContext *econtext;
2608  TupleTableSlot *slot;
2609  bool gottuple;
2610  int64 abs_pos;
2611 
2612  Assert(WindowObjectIsValid(winobj));
2613  winstate = winobj->winstate;
2614  econtext = winstate->ss.ps.ps_ExprContext;
2615  slot = winstate->temp_slot_1;
2616 
2617  switch (seektype)
2618  {
2619  case WINDOW_SEEK_CURRENT:
2620  abs_pos = winstate->currentpos + relpos;
2621  break;
2622  case WINDOW_SEEK_HEAD:
2623  abs_pos = relpos;
2624  break;
2625  case WINDOW_SEEK_TAIL:
2626  spool_tuples(winstate, -1);
2627  abs_pos = winstate->spooled_rows - 1 + relpos;
2628  break;
2629  default:
2630  elog(ERROR, "unrecognized window seek type: %d", seektype);
2631  abs_pos = 0; /* keep compiler quiet */
2632  break;
2633  }
2634 
2635  gottuple = window_gettupleslot(winobj, abs_pos, slot);
2636 
2637  if (!gottuple)
2638  {
2639  if (isout)
2640  *isout = true;
2641  *isnull = true;
2642  return (Datum) 0;
2643  }
2644  else
2645  {
2646  if (isout)
2647  *isout = false;
2648  if (set_mark)
2649  {
2650  int frameOptions = winstate->frameOptions;
2651  int64 mark_pos = abs_pos;
2652 
2653  /*
2654  * In RANGE mode with a moving frame head, we must not let the
2655  * mark advance past frameheadpos, since that row has to be
2656  * fetchable during future update_frameheadpos calls.
2657  *
2658  * XXX it is very ugly to pollute window functions' marks with
2659  * this consideration; it could for instance mask a logic bug that
2660  * lets a window function fetch rows before what it had claimed
2661  * was its mark. Perhaps use a separate mark for frame head
2662  * probes?
2663  */
2664  if ((frameOptions & FRAMEOPTION_RANGE) &&
2665  !(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING))
2666  {
2667  update_frameheadpos(winobj, winstate->temp_slot_2);
2668  if (mark_pos > winstate->frameheadpos)
2669  mark_pos = winstate->frameheadpos;
2670  }
2671  WinSetMarkPosition(winobj, mark_pos);
2672  }
2673  econtext->ecxt_outertuple = slot;
2674  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2675  econtext, isnull);
2676  }
2677 }
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:1784
ExprContext * ps_ExprContext
Definition: execnodes.h:833
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition: parsenodes.h:509
TupleTableSlot * temp_slot_1
Definition: execnodes.h:1835
static void spool_tuples(WindowAggState *winstate, int64 pos)
#define WINDOW_SEEK_TAIL
Definition: windowapi.h:34
PlanState ps
Definition: execnodes.h:1047
int64 frameheadpos
Definition: execnodes.h:1799
#define ERROR
Definition: elog.h:43
TupleTableSlot * temp_slot_2
Definition: execnodes.h:1836
void * list_nth(const List *list, int n)
Definition: list.c:410
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:265
static void update_frameheadpos(WindowObject winobj, TupleTableSlot *slot)
int64 spooled_rows
Definition: execnodes.h:1797
#define WINDOW_SEEK_HEAD
Definition: windowapi.h:33
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
uintptr_t Datum
Definition: postgres.h:372
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define WINDOW_SEEK_CURRENT
Definition: windowapi.h:32
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:506
#define Assert(condition)
Definition: c.h:675
int64 currentpos
Definition: execnodes.h:1798
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
#define elog
Definition: elog.h:219
void* WinGetPartitionLocalMemory ( WindowObject  winobj,
Size  sz 
)

Definition at line 2466 of file nodeWindowAgg.c.

References Assert, WindowObjectData::localmem, MemoryContextAllocZero(), NULL, WindowAggState::partcontext, WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by rank_up(), window_cume_dist(), window_dense_rank(), window_ntile(), window_percent_rank(), and window_rank().

2467 {
2468  Assert(WindowObjectIsValid(winobj));
2469  if (winobj->localmem == NULL)
2470  winobj->localmem =
2472  return winobj->localmem;
2473 }
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:742
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
MemoryContext partcontext
Definition: execnodes.h:1813
int64 WinGetPartitionRowCount ( WindowObject  winobj)

Definition at line 2496 of file nodeWindowAgg.c.

References Assert, spool_tuples(), WindowAggState::spooled_rows, WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by window_cume_dist(), window_ntile(), and window_percent_rank().

2497 {
2498  Assert(WindowObjectIsValid(winobj));
2499  spool_tuples(winobj->winstate, -1);
2500  return winobj->winstate->spooled_rows;
2501 }
static void spool_tuples(WindowAggState *winstate, int64 pos)
int64 spooled_rows
Definition: execnodes.h:1797
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:675
bool WinRowsArePeers ( WindowObject  winobj,
int64  pos1,
int64  pos2 
)

Definition at line 2549 of file nodeWindowAgg.c.

References are_peers(), Assert, elog, ERROR, ExecClearTuple(), INT64_FORMAT, WindowAgg::ordNumCols, PlanState::plan, ScanState::ps, WindowAggState::ss, WindowAggState::temp_slot_1, WindowAggState::temp_slot_2, window_gettupleslot(), WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by rank_up(), and window_cume_dist().

2550 {
2551  WindowAggState *winstate;
2552  WindowAgg *node;
2553  TupleTableSlot *slot1;
2554  TupleTableSlot *slot2;
2555  bool res;
2556 
2557  Assert(WindowObjectIsValid(winobj));
2558  winstate = winobj->winstate;
2559  node = (WindowAgg *) winstate->ss.ps.plan;
2560 
2561  /* If no ORDER BY, all rows are peers; don't bother to fetch them */
2562  if (node->ordNumCols == 0)
2563  return true;
2564 
2565  slot1 = winstate->temp_slot_1;
2566  slot2 = winstate->temp_slot_2;
2567 
2568  if (!window_gettupleslot(winobj, pos1, slot1))
2569  elog(ERROR, "specified position is out of window: " INT64_FORMAT,
2570  pos1);
2571  if (!window_gettupleslot(winobj, pos2, slot2))
2572  elog(ERROR, "specified position is out of window: " INT64_FORMAT,
2573  pos2);
2574 
2575  res = are_peers(winstate, slot1, slot2);
2576 
2577  ExecClearTuple(slot1);
2578  ExecClearTuple(slot2);
2579 
2580  return res;
2581 }
int ordNumCols
Definition: plannodes.h:797
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:1784
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * temp_slot_1
Definition: execnodes.h:1835
PlanState ps
Definition: execnodes.h:1047
#define ERROR
Definition: elog.h:43
TupleTableSlot * temp_slot_2
Definition: execnodes.h:1836
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
Plan * plan
Definition: execnodes.h:803
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:675
#define INT64_FORMAT
Definition: c.h:315
static bool are_peers(WindowAggState *winstate, TupleTableSlot *slot1, TupleTableSlot *slot2)
#define elog
Definition: elog.h:219
void WinSetMarkPosition ( WindowObject  winobj,
int64  markpos 
)

Definition at line 2514 of file nodeWindowAgg.c.

References Assert, WindowAggState::buffer, elog, ERROR, WindowObjectData::markpos, WindowObjectData::markptr, WindowObjectData::readptr, WindowObjectData::seekpos, tuplestore_select_read_pointer(), tuplestore_skiptuples(), WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by eval_windowaggregates(), rank_up(), window_row_number(), WinGetFuncArgInFrame(), and WinGetFuncArgInPartition().

2515 {
2516  WindowAggState *winstate;
2517 
2518  Assert(WindowObjectIsValid(winobj));
2519  winstate = winobj->winstate;
2520 
2521  if (markpos < winobj->markpos)
2522  elog(ERROR, "cannot move WindowObject's mark position backward");
2523  tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
2524  if (markpos > winobj->markpos)
2525  {
2526  tuplestore_skiptuples(winstate->buffer,
2527  markpos - winobj->markpos,
2528  true);
2529  winobj->markpos = markpos;
2530  }
2531  tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
2532  if (markpos > winobj->seekpos)
2533  {
2534  tuplestore_skiptuples(winstate->buffer,
2535  markpos - winobj->seekpos,
2536  true);
2537  winobj->seekpos = markpos;
2538  }
2539 }
#define ERROR
Definition: elog.h:43
Tuplestorestate * buffer
Definition: execnodes.h:1795
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:675
bool tuplestore_skiptuples(Tuplestorestate *state, int64 ntuples, bool forward)
Definition: tuplestore.c:1135
void tuplestore_select_read_pointer(Tuplestorestate *state, int ptr)
Definition: tuplestore.c:473
#define elog
Definition: elog.h:219