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().

#define WindowObjectIsValid (   winobj)    ((winobj) != NULL && IsA(winobj, WindowObjectData))

Typedef Documentation

Definition at line 37 of file windowapi.h.

Function Documentation

int64 WinGetCurrentPosition ( WindowObject  winobj)

Definition at line 2488 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().

2489 {
2490  Assert(WindowObjectIsValid(winobj));
2491  return winobj->winstate->currentpos;
2492 }
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:664
int64 currentpos
Definition: execnodes.h:1861
Datum WinGetFuncArgCurrent ( WindowObject  winobj,
int  argno,
bool isnull 
)

Definition at line 2799 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().

2800 {
2801  WindowAggState *winstate;
2802  ExprContext *econtext;
2803 
2804  Assert(WindowObjectIsValid(winobj));
2805  winstate = winobj->winstate;
2806 
2807  econtext = winstate->ss.ps.ps_ExprContext;
2808 
2809  econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
2810  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2811  econtext, isnull);
2812 }
ScanState ss
Definition: execnodes.h:1847
ExprContext * ps_ExprContext
Definition: execnodes.h:881
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1103
PlanState ps
Definition: execnodes.h:1100
void * list_nth(const List *list, int n)
Definition: list.c:410
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:286
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:664
Datum WinGetFuncArgInFrame ( WindowObject  winobj,
int  argno,
int  relpos,
int  seektype,
bool  set_mark,
bool isnull,
bool isout 
)

Definition at line 2705 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().

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

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

Definition at line 2473 of file nodeWindowAgg.c.

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

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

2474 {
2475  Assert(WindowObjectIsValid(winobj));
2476  if (winobj->localmem == NULL)
2477  winobj->localmem =
2479  return winobj->localmem;
2480 }
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:741
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:664
MemoryContext partcontext
Definition: execnodes.h:1875
int64 WinGetPartitionRowCount ( WindowObject  winobj)

Definition at line 2503 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().

2504 {
2505  Assert(WindowObjectIsValid(winobj));
2506  spool_tuples(winobj->winstate, -1);
2507  return winobj->winstate->spooled_rows;
2508 }
static void spool_tuples(WindowAggState *winstate, int64 pos)
int64 spooled_rows
Definition: execnodes.h:1860
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:664
bool WinRowsArePeers ( WindowObject  winobj,
int64  pos1,
int64  pos2 
)

Definition at line 2556 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().

2557 {
2558  WindowAggState *winstate;
2559  WindowAgg *node;
2560  TupleTableSlot *slot1;
2561  TupleTableSlot *slot2;
2562  bool res;
2563 
2564  Assert(WindowObjectIsValid(winobj));
2565  winstate = winobj->winstate;
2566  node = (WindowAgg *) winstate->ss.ps.plan;
2567 
2568  /* If no ORDER BY, all rows are peers; don't bother to fetch them */
2569  if (node->ordNumCols == 0)
2570  return true;
2571 
2572  slot1 = winstate->temp_slot_1;
2573  slot2 = winstate->temp_slot_2;
2574 
2575  if (!window_gettupleslot(winobj, pos1, slot1))
2576  elog(ERROR, "specified position is out of window: " INT64_FORMAT,
2577  pos1);
2578  if (!window_gettupleslot(winobj, pos2, slot2))
2579  elog(ERROR, "specified position is out of window: " INT64_FORMAT,
2580  pos2);
2581 
2582  res = are_peers(winstate, slot1, slot2);
2583 
2584  ExecClearTuple(slot1);
2585  ExecClearTuple(slot2);
2586 
2587  return res;
2588 }
int ordNumCols
Definition: plannodes.h:806
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:1847
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * temp_slot_1
Definition: execnodes.h:1896
PlanState ps
Definition: execnodes.h:1100
#define ERROR
Definition: elog.h:43
TupleTableSlot * temp_slot_2
Definition: execnodes.h:1897
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
Plan * plan
Definition: execnodes.h:847
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:664
#define INT64_FORMAT
Definition: c.h:300
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 2521 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().

2522 {
2523  WindowAggState *winstate;
2524 
2525  Assert(WindowObjectIsValid(winobj));
2526  winstate = winobj->winstate;
2527 
2528  if (markpos < winobj->markpos)
2529  elog(ERROR, "cannot move WindowObject's mark position backward");
2530  tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
2531  if (markpos > winobj->markpos)
2532  {
2533  tuplestore_skiptuples(winstate->buffer,
2534  markpos - winobj->markpos,
2535  true);
2536  winobj->markpos = markpos;
2537  }
2538  tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
2539  if (markpos > winobj->seekpos)
2540  {
2541  tuplestore_skiptuples(winstate->buffer,
2542  markpos - winobj->seekpos,
2543  true);
2544  winobj->seekpos = markpos;
2545  }
2546 }
#define ERROR
Definition: elog.h:43
Tuplestorestate * buffer
Definition: execnodes.h:1858
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:664
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