diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c index 55f97b14e6..1f6e669327 100644 --- a/src/backend/executor/spi.c +++ b/src/backend/executor/spi.c @@ -314,7 +314,7 @@ SPI_execute(const char *src, bool read_only, long tcount) memset(&plan, 0, sizeof(_SPI_plan)); plan.magic = _SPI_PLAN_MAGIC; - plan.cursor_options = 0; + plan.cursor_options = CURSOR_OPT_PARALLEL_OK; _SPI_prepare_oneshot_plan(src, &plan); @@ -458,7 +458,7 @@ SPI_execute_with_args(const char *src, memset(&plan, 0, sizeof(_SPI_plan)); plan.magic = _SPI_PLAN_MAGIC; - plan.cursor_options = 0; + plan.cursor_options = CURSOR_OPT_PARALLEL_OK; plan.nargs = nargs; plan.argtypes = argtypes; plan.parserSetup = NULL; diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index 6fc3db07fe..be30a5b744 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -3625,7 +3625,12 @@ exec_stmt_execsql(PLpgSQL_execstate *estate, { ListCell *l; - exec_prepare_plan(estate, expr, 0); + /* Allow parallelism if the function is not trigger type. */ + if (estate->func->fn_is_trigger == PLPGSQL_NOT_TRIGGER) + exec_prepare_plan(estate, expr, CURSOR_OPT_PARALLEL_OK); + else + exec_prepare_plan(estate, expr, 0); + stmt->mod_stmt = false; foreach(l, SPI_plan_get_plan_sources(expr->plan)) { @@ -5172,7 +5177,7 @@ exec_eval_expr(PLpgSQL_execstate *estate, * If first time through, create a plan for this expression. */ if (expr->plan == NULL) - exec_prepare_plan(estate, expr, 0); + exec_prepare_plan(estate, expr, CURSOR_OPT_PARALLEL_OK); /* * If this is a simple expression, bypass SPI and use the executor @@ -5183,9 +5188,14 @@ exec_eval_expr(PLpgSQL_execstate *estate, return result; /* - * Else do it the hard way via exec_run_select + * Else do it the hard way via exec_run_select. Enable parallelism if + * function is not trigger type. */ - rc = exec_run_select(estate, expr, 2, NULL, false); + if (estate->func->fn_is_trigger == PLPGSQL_NOT_TRIGGER) + rc = exec_run_select(estate, expr, 2, NULL, true); + else + rc = exec_run_select(estate, expr, 2, NULL, false); + if (rc != SPI_OK_SELECT) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE),