---------------------------------------------------------------- * ExecLimit * * This is a very simple node which just performs LIMIT/OFFSET * filtering on the stream of tuples returned by a subplan. * ---------------------------------------------------------------- */
| 41 | * ---------------------------------------------------------------- |
| 42 | */ |
| 43 | static TupleTableSlot * /* return: a tuple or NULL */ |
| 44 | ExecLimit_guts(PlanState *pstate) |
| 45 | { |
| 46 | LimitState *node = castNode(LimitState, pstate); |
| 47 | ExprContext *econtext = node->ps.ps_ExprContext; |
| 48 | ScanDirection direction; |
| 49 | TupleTableSlot *slot; |
| 50 | PlanState *outerPlan; |
| 51 | |
| 52 | CHECK_FOR_INTERRUPTS(); |
| 53 | |
| 54 | /* |
| 55 | * get information from the node |
| 56 | */ |
| 57 | direction = node->ps.state->es_direction; |
| 58 | outerPlan = outerPlanState(node); |
| 59 | |
| 60 | /* |
| 61 | * The main logic is a simple state machine. |
| 62 | */ |
| 63 | switch (node->lstate) |
| 64 | { |
| 65 | case LIMIT_INITIAL: |
| 66 | |
| 67 | /* |
| 68 | * First call for this node, so compute limit/offset. (We can't do |
| 69 | * this any earlier, because parameters from upper nodes will not |
| 70 | * be set during ExecInitLimit.) This also sets position = 0 and |
| 71 | * changes the state to LIMIT_RESCAN. |
| 72 | */ |
| 73 | recompute_limits(node); |
| 74 | |
| 75 | /* FALL THRU */ |
| 76 | |
| 77 | case LIMIT_RESCAN: |
| 78 | |
| 79 | /* |
| 80 | * If backwards scan, just return NULL without changing state. |
| 81 | */ |
| 82 | if (!ScanDirectionIsForward(direction)) |
| 83 | return NULL; |
| 84 | |
| 85 | /* |
| 86 | * Check for empty window; if so, treat like empty subplan. |
| 87 | */ |
| 88 | if (node->count <= 0 && !node->noCount) |
| 89 | { |
| 90 | node->lstate = LIMIT_EMPTY; |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Fetch rows from subplan until we reach position > offset. |
| 96 | */ |
| 97 | for (;;) |
| 98 | { |
| 99 | slot = ExecProcNode(outerPlan); |
| 100 | if (TupIsNull(slot)) |
no test coverage detected