---------- * exec_run_select Execute a select query * ---------- */
| 5696 | * ---------- |
| 5697 | */ |
| 5698 | static int |
| 5699 | exec_run_select(PLpgSQL_execstate *estate, |
| 5700 | PLpgSQL_expr *expr, long maxtuples, Portal *portalP) |
| 5701 | { |
| 5702 | ParamListInfo paramLI; |
| 5703 | int rc; |
| 5704 | |
| 5705 | /* |
| 5706 | * On the first call for this expression generate the plan. |
| 5707 | * |
| 5708 | * If we don't need to return a portal, then we're just going to execute |
| 5709 | * the query immediately, which means it's OK to use a parallel plan, even |
| 5710 | * if the number of rows being fetched is limited. If we do need to |
| 5711 | * return a portal (i.e., this is for a FOR loop), the user's code might |
| 5712 | * invoke additional operations inside the FOR loop, making parallel query |
| 5713 | * unsafe. In any case, we don't expect any cursor operations to be done, |
| 5714 | * so specify NO_SCROLL for efficiency and semantic safety. |
| 5715 | */ |
| 5716 | if (expr->plan == NULL) |
| 5717 | { |
| 5718 | int cursorOptions = CURSOR_OPT_NO_SCROLL; |
| 5719 | |
| 5720 | if (portalP == NULL) |
| 5721 | cursorOptions |= CURSOR_OPT_PARALLEL_OK; |
| 5722 | exec_prepare_plan(estate, expr, cursorOptions); |
| 5723 | } |
| 5724 | |
| 5725 | /* |
| 5726 | * Set up ParamListInfo to pass to executor |
| 5727 | */ |
| 5728 | paramLI = setup_param_list(estate, expr); |
| 5729 | |
| 5730 | /* |
| 5731 | * If a portal was requested, put the query and paramlist into the portal |
| 5732 | */ |
| 5733 | if (portalP != NULL) |
| 5734 | { |
| 5735 | *portalP = SPI_cursor_open_with_paramlist(NULL, expr->plan, |
| 5736 | paramLI, |
| 5737 | estate->readonly_func); |
| 5738 | if (*portalP == NULL) |
| 5739 | elog(ERROR, "could not open implicit cursor for query \"%s\": %s", |
| 5740 | expr->query, SPI_result_code_string(SPI_result)); |
| 5741 | exec_eval_cleanup(estate); |
| 5742 | return SPI_OK_CURSOR; |
| 5743 | } |
| 5744 | |
| 5745 | /* |
| 5746 | * Execute the query |
| 5747 | */ |
| 5748 | rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI, |
| 5749 | estate->readonly_func, maxtuples); |
| 5750 | if (rc != SPI_OK_SELECT) |
| 5751 | { |
| 5752 | /* |
| 5753 | * SELECT INTO deserves a special error message, because "query is not |
| 5754 | * a SELECT" is not very helpful in that case. |
| 5755 | */ |
no test coverage detected