* Fetch some more rows from the node's cursor. */
| 3790 | * Fetch some more rows from the node's cursor. |
| 3791 | */ |
| 3792 | static void |
| 3793 | fetch_more_data(ForeignScanState *node) |
| 3794 | { |
| 3795 | PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state; |
| 3796 | PGresult *volatile res = NULL; |
| 3797 | MemoryContext oldcontext; |
| 3798 | |
| 3799 | /* |
| 3800 | * We'll store the tuples in the batch_cxt. First, flush the previous |
| 3801 | * batch. |
| 3802 | */ |
| 3803 | fsstate->tuples = NULL; |
| 3804 | MemoryContextReset(fsstate->batch_cxt); |
| 3805 | oldcontext = MemoryContextSwitchTo(fsstate->batch_cxt); |
| 3806 | |
| 3807 | /* PGresult must be released before leaving this function. */ |
| 3808 | PG_TRY(); |
| 3809 | { |
| 3810 | PGconn *conn = fsstate->conn; |
| 3811 | int numrows; |
| 3812 | int i; |
| 3813 | |
| 3814 | if (fsstate->async_capable) |
| 3815 | { |
| 3816 | Assert(fsstate->conn_state->pendingAreq); |
| 3817 | |
| 3818 | /* |
| 3819 | * The query was already sent by an earlier call to |
| 3820 | * fetch_more_data_begin. So now we just fetch the result. |
| 3821 | */ |
| 3822 | res = pgfdw_get_result(conn, fsstate->query); |
| 3823 | /* On error, report the original query, not the FETCH. */ |
| 3824 | if (PQresultStatus(res) != PGRES_TUPLES_OK) |
| 3825 | pgfdw_report_error(ERROR, res, conn, false, fsstate->query); |
| 3826 | |
| 3827 | /* Reset per-connection state */ |
| 3828 | fsstate->conn_state->pendingAreq = NULL; |
| 3829 | } |
| 3830 | else |
| 3831 | { |
| 3832 | char sql[64]; |
| 3833 | |
| 3834 | /* This is a regular synchronous fetch. */ |
| 3835 | snprintf(sql, sizeof(sql), "FETCH %d FROM c%u", |
| 3836 | fsstate->fetch_size, fsstate->cursor_number); |
| 3837 | |
| 3838 | res = pgfdw_exec_query(conn, sql, fsstate->conn_state); |
| 3839 | /* On error, report the original query, not the FETCH. */ |
| 3840 | if (PQresultStatus(res) != PGRES_TUPLES_OK) |
| 3841 | pgfdw_report_error(ERROR, res, conn, false, fsstate->query); |
| 3842 | } |
| 3843 | |
| 3844 | /* Convert the data into HeapTuples */ |
| 3845 | numrows = PQntuples(res); |
| 3846 | fsstate->tuples = (HeapTuple *) palloc0(numrows * sizeof(HeapTuple)); |
| 3847 | fsstate->num_tuples = numrows; |
| 3848 | fsstate->next_tuple = 0; |
| 3849 |
no test coverage detected