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
This commit is contained in:
Tom Lane 2021-05-22 10:25:36 -04:00
parent 9e215378d7
commit 30168be8f7
6 changed files with 3 additions and 111 deletions

View File

@ -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;

View File

@ -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.
* ----------

View File

@ -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)
{

View File

@ -197,7 +197,7 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt);
%type <stmt> stmt_return stmt_raise stmt_assert stmt_execsql
%type <stmt> stmt_dynexecute stmt_for stmt_perform stmt_call stmt_getdiag
%type <stmt> stmt_open stmt_fetch stmt_move stmt_close stmt_null
%type <stmt> stmt_commit stmt_rollback stmt_set
%type <stmt> stmt_commit stmt_rollback
%type <stmt> stmt_case stmt_foreach_a
%type <list> proc_exceptions
@ -328,7 +328,6 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt);
%token <keyword> K_QUERY
%token <keyword> K_RAISE
%token <keyword> K_RELATIVE
%token <keyword> K_RESET
%token <keyword> K_RETURN
%token <keyword> K_RETURNED_SQLSTATE
%token <keyword> K_REVERSE
@ -338,7 +337,6 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt);
%token <keyword> K_SCHEMA
%token <keyword> K_SCHEMA_NAME
%token <keyword> K_SCROLL
%token <keyword> K_SET
%token <keyword> K_SLICE
%token <keyword> K_SQLSTATE
%token <keyword> 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

View File

@ -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)

View File

@ -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
*/