PostgreSQL Source Code  git master
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

◆ PG_WINDOW_OBJECT

◆ WINDOW_SEEK_CURRENT

#define WINDOW_SEEK_CURRENT   0

Definition at line 32 of file windowapi.h.

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

◆ WINDOW_SEEK_HEAD

#define WINDOW_SEEK_HEAD   1

◆ WINDOW_SEEK_TAIL

#define WINDOW_SEEK_TAIL   2

Definition at line 34 of file windowapi.h.

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

◆ WindowObjectIsValid

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

Typedef Documentation

◆ WindowObject

Definition at line 37 of file windowapi.h.

Function Documentation

◆ WinGetCurrentPosition()

int64 WinGetCurrentPosition ( WindowObject  winobj)

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

2516 {
2517  Assert(WindowObjectIsValid(winobj));
2518  return winobj->winstate->currentpos;
2519 }
WindowAggState * winstate
Definition: nodeWindowAgg.c:63
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:670
int64 currentpos
Definition: execnodes.h:1871

◆ WinGetFuncArgCurrent()

Datum WinGetFuncArgCurrent ( WindowObject  winobj,
int  argno,
bool isnull 
)

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

2827 {
2828  WindowAggState *winstate;
2829  ExprContext *econtext;
2830 
2831  Assert(WindowObjectIsValid(winobj));
2832  winstate = winobj->winstate;
2833 
2834  econtext = winstate->ss.ps.ps_ExprContext;
2835 
2836  econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
2837  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2838  econtext, isnull);
2839 }
ScanState ss
Definition: execnodes.h:1857
ExprContext * ps_ExprContext
Definition: execnodes.h:883
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1106
PlanState ps
Definition: execnodes.h:1103
void * list_nth(const List *list, int n)
Definition: list.c:410
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:276
WindowAggState * winstate
Definition: nodeWindowAgg.c:63
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:199
#define Assert(condition)
Definition: c.h:670

◆ WinGetFuncArgInFrame()

Datum WinGetFuncArgInFrame ( WindowObject  winobj,
int  argno,
int  relpos,
int  seektype,
bool  set_mark,
bool isnull,
bool isout 
)

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

2735 {
2736  WindowAggState *winstate;
2737  ExprContext *econtext;
2738  TupleTableSlot *slot;
2739  bool gottuple;
2740  int64 abs_pos;
2741 
2742  Assert(WindowObjectIsValid(winobj));
2743  winstate = winobj->winstate;
2744  econtext = winstate->ss.ps.ps_ExprContext;
2745  slot = winstate->temp_slot_1;
2746 
2747  switch (seektype)
2748  {
2749  case WINDOW_SEEK_CURRENT:
2750  abs_pos = winstate->currentpos + relpos;
2751  break;
2752  case WINDOW_SEEK_HEAD:
2753  update_frameheadpos(winobj, slot);
2754  abs_pos = winstate->frameheadpos + relpos;
2755  break;
2756  case WINDOW_SEEK_TAIL:
2757  update_frametailpos(winobj, slot);
2758  abs_pos = winstate->frametailpos + relpos;
2759  break;
2760  default:
2761  elog(ERROR, "unrecognized window seek type: %d", seektype);
2762  abs_pos = 0; /* keep compiler quiet */
2763  break;
2764  }
2765 
2766  gottuple = window_gettupleslot(winobj, abs_pos, slot);
2767  if (gottuple)
2768  gottuple = row_is_in_frame(winstate, abs_pos, slot);
2769 
2770  if (!gottuple)
2771  {
2772  if (isout)
2773  *isout = true;
2774  *isnull = true;
2775  return (Datum) 0;
2776  }
2777  else
2778  {
2779  if (isout)
2780  *isout = false;
2781  if (set_mark)
2782  {
2783  int frameOptions = winstate->frameOptions;
2784  int64 mark_pos = abs_pos;
2785 
2786  /*
2787  * In RANGE mode with a moving frame head, we must not let the
2788  * mark advance past frameheadpos, since that row has to be
2789  * fetchable during future update_frameheadpos calls.
2790  *
2791  * XXX it is very ugly to pollute window functions' marks with
2792  * this consideration; it could for instance mask a logic bug that
2793  * lets a window function fetch rows before what it had claimed
2794  * was its mark. Perhaps use a separate mark for frame head
2795  * probes?
2796  */
2797  if ((frameOptions & FRAMEOPTION_RANGE) &&
2798  !(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING))
2799  {
2800  update_frameheadpos(winobj, winstate->temp_slot_2);
2801  if (mark_pos > winstate->frameheadpos)
2802  mark_pos = winstate->frameheadpos;
2803  }
2804  WinSetMarkPosition(winobj, mark_pos);
2805  }
2806  econtext->ecxt_outertuple = slot;
2807  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2808  econtext, isnull);
2809  }
2810 }
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:1857
ExprContext * ps_ExprContext
Definition: execnodes.h:883
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition: parsenodes.h:508
TupleTableSlot * temp_slot_1
Definition: execnodes.h:1906
#define WINDOW_SEEK_TAIL
Definition: windowapi.h:34
PlanState ps
Definition: execnodes.h:1103
int64 frameheadpos
Definition: execnodes.h:1872
#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:1907
void * list_nth(const List *list, int n)
Definition: list.c:410
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:276
static void update_frameheadpos(WindowObject winobj, TupleTableSlot *slot)
#define WINDOW_SEEK_HEAD
Definition: windowapi.h:33
WindowAggState * winstate
Definition: nodeWindowAgg.c:63
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:1873
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:505
#define Assert(condition)
Definition: c.h:670
int64 currentpos
Definition: execnodes.h:1871
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
#define elog
Definition: elog.h:219

◆ WinGetFuncArgInPartition()

Datum WinGetFuncArgInPartition ( WindowObject  winobj,
int  argno,
int  relpos,
int  seektype,
bool  set_mark,
bool isnull,
bool isout 
)

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

2639 {
2640  WindowAggState *winstate;
2641  ExprContext *econtext;
2642  TupleTableSlot *slot;
2643  bool gottuple;
2644  int64 abs_pos;
2645 
2646  Assert(WindowObjectIsValid(winobj));
2647  winstate = winobj->winstate;
2648  econtext = winstate->ss.ps.ps_ExprContext;
2649  slot = winstate->temp_slot_1;
2650 
2651  switch (seektype)
2652  {
2653  case WINDOW_SEEK_CURRENT:
2654  abs_pos = winstate->currentpos + relpos;
2655  break;
2656  case WINDOW_SEEK_HEAD:
2657  abs_pos = relpos;
2658  break;
2659  case WINDOW_SEEK_TAIL:
2660  spool_tuples(winstate, -1);
2661  abs_pos = winstate->spooled_rows - 1 + relpos;
2662  break;
2663  default:
2664  elog(ERROR, "unrecognized window seek type: %d", seektype);
2665  abs_pos = 0; /* keep compiler quiet */
2666  break;
2667  }
2668 
2669  gottuple = window_gettupleslot(winobj, abs_pos, slot);
2670 
2671  if (!gottuple)
2672  {
2673  if (isout)
2674  *isout = true;
2675  *isnull = true;
2676  return (Datum) 0;
2677  }
2678  else
2679  {
2680  if (isout)
2681  *isout = false;
2682  if (set_mark)
2683  {
2684  int frameOptions = winstate->frameOptions;
2685  int64 mark_pos = abs_pos;
2686 
2687  /*
2688  * In RANGE mode with a moving frame head, we must not let the
2689  * mark advance past frameheadpos, since that row has to be
2690  * fetchable during future update_frameheadpos calls.
2691  *
2692  * XXX it is very ugly to pollute window functions' marks with
2693  * this consideration; it could for instance mask a logic bug that
2694  * lets a window function fetch rows before what it had claimed
2695  * was its mark. Perhaps use a separate mark for frame head
2696  * probes?
2697  */
2698  if ((frameOptions & FRAMEOPTION_RANGE) &&
2699  !(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING))
2700  {
2701  update_frameheadpos(winobj, winstate->temp_slot_2);
2702  if (mark_pos > winstate->frameheadpos)
2703  mark_pos = winstate->frameheadpos;
2704  }
2705  WinSetMarkPosition(winobj, mark_pos);
2706  }
2707  econtext->ecxt_outertuple = slot;
2708  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2709  econtext, isnull);
2710  }
2711 }
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:1857
ExprContext * ps_ExprContext
Definition: execnodes.h:883
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition: parsenodes.h:508
TupleTableSlot * temp_slot_1
Definition: execnodes.h:1906
static void spool_tuples(WindowAggState *winstate, int64 pos)
#define WINDOW_SEEK_TAIL
Definition: windowapi.h:34
PlanState ps
Definition: execnodes.h:1103
int64 frameheadpos
Definition: execnodes.h:1872
#define ERROR
Definition: elog.h:43
TupleTableSlot * temp_slot_2
Definition: execnodes.h:1907
void * list_nth(const List *list, int n)
Definition: list.c:410
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:276
static void update_frameheadpos(WindowObject winobj, TupleTableSlot *slot)
int64 spooled_rows
Definition: execnodes.h:1870
#define WINDOW_SEEK_HEAD
Definition: windowapi.h:33
WindowAggState * winstate
Definition: nodeWindowAgg.c:63
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:670
int64 currentpos
Definition: execnodes.h:1871
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
#define elog
Definition: elog.h:219

◆ WinGetPartitionLocalMemory()

void* WinGetPartitionLocalMemory ( WindowObject  winobj,
Size  sz 
)

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

2501 {
2502  Assert(WindowObjectIsValid(winobj));
2503  if (winobj->localmem == NULL)
2504  winobj->localmem =
2506  return winobj->localmem;
2507 }
WindowAggState * winstate
Definition: nodeWindowAgg.c:63
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:741
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:670
MemoryContext partcontext
Definition: execnodes.h:1885

◆ WinGetPartitionRowCount()

int64 WinGetPartitionRowCount ( WindowObject  winobj)

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

2531 {
2532  Assert(WindowObjectIsValid(winobj));
2533  spool_tuples(winobj->winstate, -1);
2534  return winobj->winstate->spooled_rows;
2535 }
static void spool_tuples(WindowAggState *winstate, int64 pos)
int64 spooled_rows
Definition: execnodes.h:1870
WindowAggState * winstate
Definition: nodeWindowAgg.c:63
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:670

◆ WinRowsArePeers()

bool WinRowsArePeers ( WindowObject  winobj,
int64  pos1,
int64  pos2 
)

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

2584 {
2585  WindowAggState *winstate;
2586  WindowAgg *node;
2587  TupleTableSlot *slot1;
2588  TupleTableSlot *slot2;
2589  bool res;
2590 
2591  Assert(WindowObjectIsValid(winobj));
2592  winstate = winobj->winstate;
2593  node = (WindowAgg *) winstate->ss.ps.plan;
2594 
2595  /* If no ORDER BY, all rows are peers; don't bother to fetch them */
2596  if (node->ordNumCols == 0)
2597  return true;
2598 
2599  slot1 = winstate->temp_slot_1;
2600  slot2 = winstate->temp_slot_2;
2601 
2602  if (!window_gettupleslot(winobj, pos1, slot1))
2603  elog(ERROR, "specified position is out of window: " INT64_FORMAT,
2604  pos1);
2605  if (!window_gettupleslot(winobj, pos2, slot2))
2606  elog(ERROR, "specified position is out of window: " INT64_FORMAT,
2607  pos2);
2608 
2609  res = are_peers(winstate, slot1, slot2);
2610 
2611  ExecClearTuple(slot1);
2612  ExecClearTuple(slot2);
2613 
2614  return res;
2615 }
int ordNumCols
Definition: plannodes.h:806
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:1857
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * temp_slot_1
Definition: execnodes.h:1906
PlanState ps
Definition: execnodes.h:1103
#define ERROR
Definition: elog.h:43
TupleTableSlot * temp_slot_2
Definition: execnodes.h:1907
WindowAggState * winstate
Definition: nodeWindowAgg.c:63
Plan * plan
Definition: execnodes.h:849
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:670
#define INT64_FORMAT
Definition: c.h:338
static bool are_peers(WindowAggState *winstate, TupleTableSlot *slot1, TupleTableSlot *slot2)
#define elog
Definition: elog.h:219

◆ WinSetMarkPosition()

void WinSetMarkPosition ( WindowObject  winobj,
int64  markpos 
)

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

2549 {
2550  WindowAggState *winstate;
2551 
2552  Assert(WindowObjectIsValid(winobj));
2553  winstate = winobj->winstate;
2554 
2555  if (markpos < winobj->markpos)
2556  elog(ERROR, "cannot move WindowObject's mark position backward");
2557  tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
2558  if (markpos > winobj->markpos)
2559  {
2560  tuplestore_skiptuples(winstate->buffer,
2561  markpos - winobj->markpos,
2562  true);
2563  winobj->markpos = markpos;
2564  }
2565  tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
2566  if (markpos > winobj->seekpos)
2567  {
2568  tuplestore_skiptuples(winstate->buffer,
2569  markpos - winobj->seekpos,
2570  true);
2571  winobj->seekpos = markpos;
2572  }
2573 }
#define ERROR
Definition: elog.h:43
Tuplestorestate * buffer
Definition: execnodes.h:1868
WindowAggState * winstate
Definition: nodeWindowAgg.c:63
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:670
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