From 30168be8f75b95183abccf48f0da7a64a0cfbd9f Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 22 May 2021 10:25:36 -0400 Subject: [PATCH] Remove plpgsql's special-case code paths for SET/RESET. In the wake of 84f5c2908, it's no longer necessary for plpgsql to handle SET/RESET specially. The point of that was just to avoid taking a new transaction snapshot prematurely, which the regular code path through _SPI_execute_plan() now does just fine (in fact better, since it now does the right thing for LOCK too). Hence, rip out a few lines of code, going back to the old way of treating SET/RESET as a generic SQL command. This essentially reverts all but the test cases from b981275b6. Discussion: https://postgr.es/m/15990-eee2ac466b11293d@postgresql.org --- .../src/expected/plpgsql_transaction.out | 2 +- src/pl/plpgsql/src/pl_exec.c | 37 ------------------- src/pl/plpgsql/src/pl_funcs.c | 23 ------------ src/pl/plpgsql/src/pl_gram.y | 36 +----------------- src/pl/plpgsql/src/pl_unreserved_kwlist.h | 2 - src/pl/plpgsql/src/plpgsql.h | 14 +------ 6 files changed, 3 insertions(+), 111 deletions(-) diff --git a/src/pl/plpgsql/src/expected/plpgsql_transaction.out b/src/pl/plpgsql/src/expected/plpgsql_transaction.out index e205a1e002..8fceb88c9b 100644 --- a/src/pl/plpgsql/src/expected/plpgsql_transaction.out +++ b/src/pl/plpgsql/src/expected/plpgsql_transaction.out @@ -497,7 +497,7 @@ END; $$; ERROR: SET TRANSACTION ISOLATION LEVEL must be called before any query CONTEXT: SQL statement "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ" -PL/pgSQL function inline_code_block line 3 at SET +PL/pgSQL function inline_code_block line 3 at SQL statement DO LANGUAGE plpgsql $$ BEGIN SAVEPOINT foo; diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index 4e705c162a..78b593d12c 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -317,8 +317,6 @@ static int exec_stmt_commit(PLpgSQL_execstate *estate, PLpgSQL_stmt_commit *stmt); static int exec_stmt_rollback(PLpgSQL_execstate *estate, PLpgSQL_stmt_rollback *stmt); -static int exec_stmt_set(PLpgSQL_execstate *estate, - PLpgSQL_stmt_set *stmt); static void plpgsql_estate_setup(PLpgSQL_execstate *estate, PLpgSQL_function *func, @@ -2088,10 +2086,6 @@ exec_stmts(PLpgSQL_execstate *estate, List *stmts) rc = exec_stmt_rollback(estate, (PLpgSQL_stmt_rollback *) stmt); break; - case PLPGSQL_STMT_SET: - rc = exec_stmt_set(estate, (PLpgSQL_stmt_set *) stmt); - break; - default: /* point err_stmt to parent, since this one seems corrupt */ estate->err_stmt = save_estmt; @@ -4926,37 +4920,6 @@ exec_stmt_rollback(PLpgSQL_execstate *estate, PLpgSQL_stmt_rollback *stmt) return PLPGSQL_RC_OK; } -/* - * exec_stmt_set - * - * Execute SET/RESET statement. - * - * We just parse and execute the statement normally, but we have to do it - * without setting a snapshot, for things like SET TRANSACTION. - * XXX spi.c now handles this correctly, so we no longer need a special case. - */ -static int -exec_stmt_set(PLpgSQL_execstate *estate, PLpgSQL_stmt_set *stmt) -{ - PLpgSQL_expr *expr = stmt->expr; - SPIExecuteOptions options; - int rc; - - if (expr->plan == NULL) - exec_prepare_plan(estate, expr, 0); - - memset(&options, 0, sizeof(options)); - options.read_only = estate->readonly_func; - - rc = SPI_execute_plan_extended(expr->plan, &options); - - if (rc != SPI_OK_UTILITY) - elog(ERROR, "SPI_execute_plan_extended failed executing query \"%s\": %s", - expr->query, SPI_result_code_string(rc)); - - return PLPGSQL_RC_OK; -} - /* ---------- * exec_assign_expr Put an expression's result into a variable. * ---------- diff --git a/src/pl/plpgsql/src/pl_funcs.c b/src/pl/plpgsql/src/pl_funcs.c index 919b968826..e0863acb3d 100644 --- a/src/pl/plpgsql/src/pl_funcs.c +++ b/src/pl/plpgsql/src/pl_funcs.c @@ -288,8 +288,6 @@ plpgsql_stmt_typename(PLpgSQL_stmt *stmt) return "COMMIT"; case PLPGSQL_STMT_ROLLBACK: return "ROLLBACK"; - case PLPGSQL_STMT_SET: - return "SET"; } return "unknown"; @@ -370,7 +368,6 @@ static void free_perform(PLpgSQL_stmt_perform *stmt); static void free_call(PLpgSQL_stmt_call *stmt); static void free_commit(PLpgSQL_stmt_commit *stmt); static void free_rollback(PLpgSQL_stmt_rollback *stmt); -static void free_set(PLpgSQL_stmt_set *stmt); static void free_expr(PLpgSQL_expr *expr); @@ -460,9 +457,6 @@ free_stmt(PLpgSQL_stmt *stmt) case PLPGSQL_STMT_ROLLBACK: free_rollback((PLpgSQL_stmt_rollback *) stmt); break; - case PLPGSQL_STMT_SET: - free_set((PLpgSQL_stmt_set *) stmt); - break; default: elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type); break; @@ -626,12 +620,6 @@ free_rollback(PLpgSQL_stmt_rollback *stmt) { } -static void -free_set(PLpgSQL_stmt_set *stmt) -{ - free_expr(stmt->expr); -} - static void free_exit(PLpgSQL_stmt_exit *stmt) { @@ -825,7 +813,6 @@ static void dump_perform(PLpgSQL_stmt_perform *stmt); static void dump_call(PLpgSQL_stmt_call *stmt); static void dump_commit(PLpgSQL_stmt_commit *stmt); static void dump_rollback(PLpgSQL_stmt_rollback *stmt); -static void dump_set(PLpgSQL_stmt_set *stmt); static void dump_expr(PLpgSQL_expr *expr); @@ -925,9 +912,6 @@ dump_stmt(PLpgSQL_stmt *stmt) case PLPGSQL_STMT_ROLLBACK: dump_rollback((PLpgSQL_stmt_rollback *) stmt); break; - case PLPGSQL_STMT_SET: - dump_set((PLpgSQL_stmt_set *) stmt); - break; default: elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type); break; @@ -1329,13 +1313,6 @@ dump_rollback(PLpgSQL_stmt_rollback *stmt) printf("ROLLBACK\n"); } -static void -dump_set(PLpgSQL_stmt_set *stmt) -{ - dump_ind(); - printf("%s\n", stmt->expr->query); -} - static void dump_exit(PLpgSQL_stmt_exit *stmt) { diff --git a/src/pl/plpgsql/src/pl_gram.y b/src/pl/plpgsql/src/pl_gram.y index 34e0520719..3fcca43b90 100644 --- a/src/pl/plpgsql/src/pl_gram.y +++ b/src/pl/plpgsql/src/pl_gram.y @@ -197,7 +197,7 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt); %type stmt_return stmt_raise stmt_assert stmt_execsql %type stmt_dynexecute stmt_for stmt_perform stmt_call stmt_getdiag %type stmt_open stmt_fetch stmt_move stmt_close stmt_null -%type stmt_commit stmt_rollback stmt_set +%type stmt_commit stmt_rollback %type stmt_case stmt_foreach_a %type proc_exceptions @@ -328,7 +328,6 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt); %token K_QUERY %token K_RAISE %token K_RELATIVE -%token K_RESET %token K_RETURN %token K_RETURNED_SQLSTATE %token K_REVERSE @@ -338,7 +337,6 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt); %token K_SCHEMA %token K_SCHEMA_NAME %token K_SCROLL -%token K_SET %token K_SLICE %token K_SQLSTATE %token K_STACKED @@ -899,8 +897,6 @@ proc_stmt : pl_block ';' { $$ = $1; } | stmt_rollback { $$ = $1; } - | stmt_set - { $$ = $1; } ; stmt_perform : K_PERFORM @@ -2273,34 +2269,6 @@ opt_transaction_chain: | /* EMPTY */ { $$ = false; } ; -stmt_set : K_SET - { - PLpgSQL_stmt_set *new; - - new = palloc0(sizeof(PLpgSQL_stmt_set)); - new->cmd_type = PLPGSQL_STMT_SET; - new->lineno = plpgsql_location_to_lineno(@1); - new->stmtid = ++plpgsql_curr_compile->nstatements; - plpgsql_push_back_token(K_SET); - new->expr = read_sql_stmt(); - - $$ = (PLpgSQL_stmt *)new; - } - | K_RESET - { - PLpgSQL_stmt_set *new; - - new = palloc0(sizeof(PLpgSQL_stmt_set)); - new->cmd_type = PLPGSQL_STMT_SET; - new->lineno = plpgsql_location_to_lineno(@1); - new->stmtid = ++plpgsql_curr_compile->nstatements; - plpgsql_push_back_token(K_RESET); - new->expr = read_sql_stmt(); - - $$ = (PLpgSQL_stmt *)new; - } - ; - cursor_variable : T_DATUM { @@ -2588,7 +2556,6 @@ unreserved_keyword : | K_QUERY | K_RAISE | K_RELATIVE - | K_RESET | K_RETURN | K_RETURNED_SQLSTATE | K_REVERSE @@ -2598,7 +2565,6 @@ unreserved_keyword : | K_SCHEMA | K_SCHEMA_NAME | K_SCROLL - | K_SET | K_SLICE | K_SQLSTATE | K_STACKED diff --git a/src/pl/plpgsql/src/pl_unreserved_kwlist.h b/src/pl/plpgsql/src/pl_unreserved_kwlist.h index 44c8b68bfb..fcb34f7c7f 100644 --- a/src/pl/plpgsql/src/pl_unreserved_kwlist.h +++ b/src/pl/plpgsql/src/pl_unreserved_kwlist.h @@ -89,7 +89,6 @@ PG_KEYWORD("prior", K_PRIOR) PG_KEYWORD("query", K_QUERY) PG_KEYWORD("raise", K_RAISE) PG_KEYWORD("relative", K_RELATIVE) -PG_KEYWORD("reset", K_RESET) PG_KEYWORD("return", K_RETURN) PG_KEYWORD("returned_sqlstate", K_RETURNED_SQLSTATE) PG_KEYWORD("reverse", K_REVERSE) @@ -99,7 +98,6 @@ PG_KEYWORD("rowtype", K_ROWTYPE) PG_KEYWORD("schema", K_SCHEMA) PG_KEYWORD("schema_name", K_SCHEMA_NAME) PG_KEYWORD("scroll", K_SCROLL) -PG_KEYWORD("set", K_SET) PG_KEYWORD("slice", K_SLICE) PG_KEYWORD("sqlstate", K_SQLSTATE) PG_KEYWORD("stacked", K_STACKED) diff --git a/src/pl/plpgsql/src/plpgsql.h b/src/pl/plpgsql/src/plpgsql.h index d5010862a5..fcbfcd678b 100644 --- a/src/pl/plpgsql/src/plpgsql.h +++ b/src/pl/plpgsql/src/plpgsql.h @@ -127,8 +127,7 @@ typedef enum PLpgSQL_stmt_type PLPGSQL_STMT_PERFORM, PLPGSQL_STMT_CALL, PLPGSQL_STMT_COMMIT, - PLPGSQL_STMT_ROLLBACK, - PLPGSQL_STMT_SET + PLPGSQL_STMT_ROLLBACK } PLpgSQL_stmt_type; /* @@ -566,17 +565,6 @@ typedef struct PLpgSQL_stmt_rollback bool chain; } PLpgSQL_stmt_rollback; -/* - * SET statement - */ -typedef struct PLpgSQL_stmt_set -{ - PLpgSQL_stmt_type cmd_type; - int lineno; - unsigned int stmtid; - PLpgSQL_expr *expr; -} PLpgSQL_stmt_set; - /* * GET DIAGNOSTICS item */