---------------------------------------------------------------- * CteScanNext * * This is a workhorse for ExecCteScan * ---------------------------------------------------------------- */
| 28 | * ---------------------------------------------------------------- |
| 29 | */ |
| 30 | static TupleTableSlot * |
| 31 | CteScanNext(CteScanState *node) |
| 32 | { |
| 33 | EState *estate; |
| 34 | ScanDirection dir; |
| 35 | bool forward; |
| 36 | Tuplestorestate *tuplestorestate; |
| 37 | bool eof_tuplestore; |
| 38 | TupleTableSlot *slot; |
| 39 | |
| 40 | /* |
| 41 | * get state info from node |
| 42 | */ |
| 43 | estate = node->ss.ps.state; |
| 44 | dir = estate->es_direction; |
| 45 | forward = ScanDirectionIsForward(dir); |
| 46 | tuplestorestate = node->leader->cte_table; |
| 47 | tuplestore_select_read_pointer(tuplestorestate, node->readptr); |
| 48 | slot = node->ss.ss_ScanTupleSlot; |
| 49 | |
| 50 | /* |
| 51 | * If we are not at the end of the tuplestore, or are going backwards, try |
| 52 | * to fetch a tuple from tuplestore. |
| 53 | */ |
| 54 | eof_tuplestore = tuplestore_ateof(tuplestorestate); |
| 55 | |
| 56 | if (!forward && eof_tuplestore) |
| 57 | { |
| 58 | if (!node->leader->eof_cte) |
| 59 | { |
| 60 | /* |
| 61 | * When reversing direction at tuplestore EOF, the first |
| 62 | * gettupleslot call will fetch the last-added tuple; but we want |
| 63 | * to return the one before that, if possible. So do an extra |
| 64 | * fetch. |
| 65 | */ |
| 66 | if (!tuplestore_advance(tuplestorestate, forward)) |
| 67 | return NULL; /* the tuplestore must be empty */ |
| 68 | } |
| 69 | eof_tuplestore = false; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * If we can fetch another tuple from the tuplestore, return it. |
| 74 | * |
| 75 | * Note: we have to use copy=true in the tuplestore_gettupleslot call, |
| 76 | * because we are sharing the tuplestore with other nodes that might write |
| 77 | * into the tuplestore before we get called again. |
| 78 | */ |
| 79 | if (!eof_tuplestore) |
| 80 | { |
| 81 | if (tuplestore_gettupleslot(tuplestorestate, forward, true, slot)) |
| 82 | return slot; |
| 83 | if (forward) |
| 84 | eof_tuplestore = true; |
| 85 | } |
| 86 | |
| 87 | /* |
nothing calls this directly
no test coverage detected