MCPcopy Create free account
hub / github.com/apache/cloudberry / exec_run_select

Function exec_run_select

src/pl/plpgsql/src/pl_exec.c:5698–5774  ·  view source on GitHub ↗

---------- * exec_run_select Execute a select query * ---------- */

Source from the content-addressed store, hash-verified

5696 * ----------
5697 */
5698static int
5699exec_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 */

Callers 3

exec_stmt_performFunction · 0.85
exec_stmt_forsFunction · 0.85
exec_eval_exprFunction · 0.85

Calls 8

exec_prepare_planFunction · 0.85
setup_param_listFunction · 0.85
SPI_result_code_stringFunction · 0.85
exec_eval_cleanupFunction · 0.85
errcodeFunction · 0.50
errmsgFunction · 0.50

Tested by

no test coverage detected