From 1d95b38b74959899e6b22831b4af905357f04de0 Mon Sep 17 00:00:00 2001 From: "okbob@github.com" Date: Mon, 24 Nov 2025 18:05:03 +0100 Subject: [PATCH 6/7] LET command - assign a result of expression to the session variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The value is assigned to session variables usually by SET command. Unfortunately there are two reasons why SET should not be used for this purpose in Postgres. 1. Using a_expr inside generic_set ram rule produces reduce conflicts, so it needs    total reimplementation of related gram rules. 2. SET is no plan command - so it doesn't support usage of parameters. 3. Excepting implementation issues, there is fact, so if we use SET command    for assigning values to session variables, then there can be collisions    between session variables and GUC, and then we need some concepts, how    these collisions should be solved, or how to protect self against these    collisions. With the dedicated command, the collisions between GUC and session    variables are not possible. The command LET is executed as usual query execution. The result is stored to the target session variable (resultVariable) by using VariableDestReceiver. Implementations of EXPLAIN LET and PREPARE LET statements are not supported now. Postponed to next step due reducing patch size. --- doc/src/sgml/ddl.sgml | 27 ++++ doc/src/sgml/ref/allfiles.sgml | 1 + doc/src/sgml/ref/create_variable.sgml | 3 + doc/src/sgml/ref/drop_variable.sgml | 1 + doc/src/sgml/ref/let.sgml | 95 ++++++++++++++ doc/src/sgml/reference.sgml | 1 + src/backend/commands/session_variable.c | 86 ++++++++++++ src/backend/nodes/nodeFuncs.c | 8 ++ src/backend/optimizer/plan/planner.c | 1 + src/backend/parser/analyze.c | 103 ++++++++++++--- src/backend/parser/gram.y | 38 +++++- src/backend/parser/parse_agg.c | 7 + src/backend/parser/parse_expr.c | 9 ++ src/backend/parser/parse_func.c | 3 + src/backend/tcop/utility.c | 15 +++ src/bin/psql/tab-complete.in.c | 9 +- src/include/commands/session_variable.h | 5 + src/include/nodes/parsenodes.h | 15 +++ src/include/nodes/pathnodes.h | 3 + src/include/parser/kwlist.h | 1 + src/include/parser/parse_node.h | 1 + src/include/tcop/cmdtaglist.h | 1 + .../expected/session_variables_dml.out | 123 ++++++++++++++++++ .../regress/sql/session_variables_dml.sql | 87 +++++++++++++ src/tools/pgindent/typedefs.list | 1 + 25 files changed, 624 insertions(+), 20 deletions(-) create mode 100644 doc/src/sgml/ref/let.sgml diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml index 150b04f1d4b..92163d0a74a 100644 --- a/doc/src/sgml/ddl.sgml +++ b/doc/src/sgml/ddl.sgml @@ -5683,10 +5683,37 @@ EXPLAIN SELECT count(*) FROM measurement WHERE logdate >= DATE '2008-01-01'; session variable identifier, and can be used only for session variable identifier. The special syntax for accessing session variables removes risk of collisions between variable identifiers and column names. + + + + The value of a session variable is set with the SQL statement + LET. The value of a session variable can be retrieved + with the SQL statement SELECT. +CREATE VARIABLE var1 AS date; +LET var1 = current_date; +SELECT VARIABLE(var1); + + + or + + +CREATE VARIABLE public.current_user_id AS integer; +GRANT SELECT ON VARIABLE public.current_user_id TO PUBLIC; +LET current_user_id = (SELECT id FROM users WHERE usename = session_user); SELECT VARIABLE(current_user_id); + + + The value of a session variable is local to the current session. Retrieving + a variable's value returns a NULL, unless its value has + been set to something else in the current session using the + LET command. Session variables are not transactional: + any changes made to the value of a session variable in a transaction won't + be undone if the transaction is rolled back (just like variables in + procedural languages). + diff --git a/doc/src/sgml/ref/allfiles.sgml b/doc/src/sgml/ref/allfiles.sgml index a7349919658..cd3faa667f0 100644 --- a/doc/src/sgml/ref/allfiles.sgml +++ b/doc/src/sgml/ref/allfiles.sgml @@ -157,6 +157,7 @@ Complete list of usable sgml source files in this directory. + diff --git a/doc/src/sgml/ref/create_variable.sgml b/doc/src/sgml/ref/create_variable.sgml index 5ed74c5c173..11b1ef74212 100644 --- a/doc/src/sgml/ref/create_variable.sgml +++ b/doc/src/sgml/ref/create_variable.sgml @@ -107,6 +107,8 @@ CREATE { TEMP | TEMPORAL } VARIABLE namevar1: CREATE TEMPORARY VARIABLE var1 AS date; +LET var1 = current_date; +SELECT VARIABLE(var1); @@ -126,6 +128,7 @@ CREATE TEMPORARY VARIABLE var1 AS date; + diff --git a/doc/src/sgml/ref/drop_variable.sgml b/doc/src/sgml/ref/drop_variable.sgml index e8517a78200..dede42e4ffb 100644 --- a/doc/src/sgml/ref/drop_variable.sgml +++ b/doc/src/sgml/ref/drop_variable.sgml @@ -78,6 +78,7 @@ DROP VARIABLE var1; + diff --git a/doc/src/sgml/ref/let.sgml b/doc/src/sgml/ref/let.sgml new file mode 100644 index 00000000000..5909d203e94 --- /dev/null +++ b/doc/src/sgml/ref/let.sgml @@ -0,0 +1,95 @@ + + + + + LET + + + + session variable + changing + + + + LET + 7 + SQL - Language Statements + + + + LET + change a session variable's value + + + + +LET session_variable = sql_expression + + + + + Description + + + The LET command assigns a value to the specified session + variable. + + + + + + Parameters + + + + session_variable + + + The name of the session variable. + + + + + + sql_expression + + + An arbitrary SQL expression. The result must be of a data type that can + be cast to the type of the session variable in an assignment. + + + + + + + + + Examples + +CREATE VARIABLE myvar AS integer; +LET myvar = 10; +LET myvar = (SELECT sum(val) FROM tab); + + + + + Compatibility + + + The LET is a PostgreSQL + extension. + + + + + See Also + + + + + + + diff --git a/doc/src/sgml/reference.sgml b/doc/src/sgml/reference.sgml index c03e7692c7a..6fcd7a81321 100644 --- a/doc/src/sgml/reference.sgml +++ b/doc/src/sgml/reference.sgml @@ -185,6 +185,7 @@ &grant; &importForeignSchema; &insert; + &let; &listen; &load; &lock; diff --git a/src/backend/commands/session_variable.c b/src/backend/commands/session_variable.c index 95d70911bcc..36490387da0 100644 --- a/src/backend/commands/session_variable.c +++ b/src/backend/commands/session_variable.c @@ -19,14 +19,19 @@ #include "catalog/pg_proc.h" #include "catalog/pg_type.h" #include "commands/session_variable.h" +#include "executor/executor.h" +#include "executor/svariableReceiver.h" #include "miscadmin.h" #include "parser/parse_type.h" +#include "rewrite/rewriteHandler.h" #include "storage/proc.h" +#include "tcop/tcopprot.h" #include "utils/acl.h" #include "utils/builtins.h" #include "utils/datum.h" #include "utils/lsyscache.h" #include "utils/memutils.h" +#include "utils/snapmgr.h" #include "utils/syscache.h" /* @@ -326,3 +331,84 @@ DropVariableByName(char *varname) HASH_REMOVE, NULL); } + +/* + * Assign the result of the evaluated expression to the session variable + */ +void +ExecuteLetStmt(ParseState *pstate, + LetStmt *stmt, + ParamListInfo params, + QueryEnvironment *queryEnv, + QueryCompletion *qc) +{ + Query *query = castNode(Query, stmt->query); + List *rewritten; + DestReceiver *dest; + PlannedStmt *plan; + QueryDesc *queryDesc; + char *varname = query->resultVariable; + SVariable svar; + + svar = search_variable(varname); + + /* only owner can set content of variable */ + if (svar->varowner != GetUserId() && !superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + errmsg("permission denied for session variable %s", + varname))); + + /* create a dest receiver for LET */ + dest = CreateVariableDestReceiver(varname); + + /* run the query rewriter */ + query = copyObject(query); + + rewritten = QueryRewrite(query); + + Assert(list_length(rewritten) == 1); + + query = linitial_node(Query, rewritten); + Assert(query->commandType == CMD_SELECT); + + /* plan the query */ + plan = pg_plan_query(query, pstate->p_sourcetext, + CURSOR_OPT_PARALLEL_OK, params, NULL); + + /* + * Use a snapshot with an updated command ID to ensure this query sees the + * results of any previously executed queries. (This could only matter if + * the planner executed an allegedly-stable function that changed the + * database contents, but let's do it anyway to be parallel to the EXPLAIN + * code path.) + */ + PushCopiedSnapshot(GetActiveSnapshot()); + UpdateActiveSnapshotCommandId(); + + /* create a QueryDesc, redirecting output to our tuple receiver */ + queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext, + GetActiveSnapshot(), InvalidSnapshot, + dest, params, queryEnv, 0); + + /* call ExecutorStart to prepare the plan for execution */ + ExecutorStart(queryDesc, 0); + + /* + * Run the plan to completion. The result should be only one row. To + * check if there are too many result rows, we try to fetch two. + */ + ExecutorRun(queryDesc, ForwardScanDirection, 2L); + + /* save the rowcount if we're given a QueryCompletion to fill */ + if (qc) + SetQueryCompletion(qc, CMDTAG_LET, queryDesc->estate->es_processed); + + /* and clean up */ + ExecutorFinish(queryDesc); + ExecutorEnd(queryDesc); + + FreeQueryDesc(queryDesc); + + PopActiveSnapshot(); +} diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index c1eb81f79e5..fdb13ada75d 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -4374,6 +4374,14 @@ raw_expression_tree_walker_impl(Node *node, return true; } break; + case T_LetStmt: + { + LetStmt *stmt = (LetStmt *) node; + + if (WALK(stmt->query)) + return true; + } + break; case T_PLAssignStmt: { PLAssignStmt *stmt = (PLAssignStmt *) node; diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index c00331be6b8..6dc63954a61 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -375,6 +375,7 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions, glob->partition_directory = NULL; glob->rel_notnullatts_hash = NULL; glob->sessionVariables = NIL; + glob->resultVariable = parse->resultVariable; /* * Assess whether it's feasible to use parallel mode for this query. We diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 885106922d0..cd6aeff3040 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -53,15 +53,18 @@ #include "utils/builtins.h" #include "utils/guc.h" #include "utils/rel.h" +#include "utils/lsyscache.h" #include "utils/syscache.h" -/* Passthrough data for transformPLAssignStmtTarget */ +/* Passthrough data for transformAssignTarget */ typedef struct SelectStmtPassthrough { - PLAssignStmt *stmt; /* the assignment statement */ + Node *stmt; /* the assignment statement */ Node *target; /* node representing the target variable */ + char *target_name; /* the name used by err */ List *indirection; /* indirection yet to be applied to target */ + CoercionContext ccontext; /* context indicators to control coercions */ } SelectStmtPassthrough; /* Hook for plugins to get control at end of parse analysis */ @@ -85,7 +88,7 @@ static Query *transformReturnStmt(ParseState *pstate, ReturnStmt *stmt); static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt); static Query *transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt); -static List *transformPLAssignStmtTarget(ParseState *pstate, List *tlist, +static List *transformAssignTarget(ParseState *pstate, List *tlist, SelectStmtPassthrough *passthru); static Query *transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt); @@ -95,6 +98,8 @@ static Query *transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt); static Query *transformCallStmt(ParseState *pstate, CallStmt *stmt); +static Query *transformLetStmt(ParseState *pstate, + LetStmt *stmt); static void transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc, bool pushedDown); #ifdef DEBUG_NODE_TESTS_ENABLED @@ -342,6 +347,7 @@ transformStmt(ParseState *pstate, Node *parseTree) case T_UpdateStmt: case T_DeleteStmt: case T_MergeStmt: + case T_LetStmt: (void) test_raw_expression_coverage(parseTree, NULL); break; default: @@ -421,6 +427,11 @@ transformStmt(ParseState *pstate, Node *parseTree) (CallStmt *) parseTree); break; + case T_LetStmt: + result = transformLetStmt(pstate, + (LetStmt *) parseTree); + break; + default: /* @@ -482,6 +493,7 @@ stmt_requires_parse_analysis(RawStmt *parseTree) case T_ExplainStmt: case T_CreateTableAsStmt: case T_CallStmt: + case T_LetStmt: result = true; break; @@ -547,6 +559,7 @@ query_requires_rewrite_plan(Query *query) case T_ExplainStmt: case T_CreateTableAsStmt: case T_CallStmt: + case T_LetStmt: result = true; break; default: @@ -1390,7 +1403,7 @@ count_rowexpr_columns(ParseState *pstate, Node *expr) * * This function is also used to transform the source expression of a * PLAssignStmt. In that usage, passthru is non-NULL and we need to - * call transformPLAssignStmtTarget after the initial transformation of the + * call transformAssignTarget after the initial transformation of the * SELECT's targetlist. (We could generalize this into an arbitrary callback * function, but for now that would just be more notation with no benefit.) * All the rest is the same as a regular SelectStmt. @@ -1443,8 +1456,8 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt, * Otherwise, mark column origins (which are useless in a PLAssignStmt). */ if (passthru) - qry->targetList = transformPLAssignStmtTarget(pstate, qry->targetList, - passthru); + qry->targetList = transformAssignTarget(pstate, qry->targetList, + passthru); else markTargetListOrigins(pstate, qry->targetList); @@ -2842,9 +2855,11 @@ transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt) EXPR_KIND_UPDATE_TARGET); /* Set up passthrough data for transformPLAssignStmtTarget */ - passthru.stmt = stmt; + passthru.stmt = (Node *) stmt; passthru.target = target; + passthru.target_name = stmt->name; passthru.indirection = indirection; + passthru.ccontext = COERCION_PLPGSQL; /* * To avoid duplicating a lot of code, we use transformSelectStmt to do @@ -2867,18 +2882,21 @@ transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt) /* * Callback function to adjust a SELECT's tlist to make the output suitable - * for assignment to a PLAssignStmt's target variable. + * for assignment to a PLAssignStmt's target variable pr LET's target + * session variable. * * Note: we actually modify the tle->expr in-place, but the function's API * is set up to not presume that. */ static List * -transformPLAssignStmtTarget(ParseState *pstate, List *tlist, - SelectStmtPassthrough *passthru) +transformAssignTarget(ParseState *pstate, List *tlist, + SelectStmtPassthrough *passthru) { - PLAssignStmt *stmt = passthru->stmt; + Node *stmt = passthru->stmt; Node *target = passthru->target; + char *target_name = passthru->target_name; List *indirection = passthru->indirection; + CoercionContext ccontext = passthru->ccontext; Oid targettype; int32 targettypmod; Oid targetcollation; @@ -2913,7 +2931,7 @@ transformPLAssignStmtTarget(ParseState *pstate, List *tlist, tle->expr = (Expr *) transformAssignmentIndirection(pstate, target, - stmt->name, + target_name, false, targettype, targettypmod, @@ -2921,10 +2939,10 @@ transformPLAssignStmtTarget(ParseState *pstate, List *tlist, indirection, list_head(indirection), (Node *) tle->expr, - COERCION_PLPGSQL, + ccontext, exprLocation(target)); } - else if (targettype != type_id && + else if (IsA(stmt, PLAssignStmt) && targettype != type_id && (targettype == RECORDOID || ISCOMPLEX(targettype)) && (type_id == RECORDOID || ISCOMPLEX(type_id))) { @@ -2947,7 +2965,7 @@ transformPLAssignStmtTarget(ParseState *pstate, List *tlist, coerce_to_target_type(pstate, orig_expr, type_id, targettype, targettypmod, - COERCION_PLPGSQL, + ccontext, COERCE_IMPLICIT_CAST, -1); /* With COERCION_PLPGSQL, this error is probably unreachable */ @@ -2956,7 +2974,7 @@ transformPLAssignStmtTarget(ParseState *pstate, List *tlist, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("variable \"%s\" is of type %s" " but expression is of type %s", - stmt->name, + target_name, format_type_be(targettype), format_type_be(type_id)), errhint("You will need to rewrite or cast the expression."), @@ -3324,6 +3342,59 @@ transformCallStmt(ParseState *pstate, CallStmt *stmt) return result; } +/* + * transformLetStmt - + * transform an Let Statement + */ +static Query * +transformLetStmt(ParseState *pstate, LetStmt *stmt) +{ + Query *qry; + Query *result; + Node *target; + VariableFence *vf; + SelectStmtPassthrough passthru; + Param *paramvar; + + /* gram allows only SELECT */ + Assert(IsA(stmt->query, SelectStmt)); + + /* Use implicit VariableFence for forcing session variables */ + vf = makeNode(VariableFence); + vf->varname = stmt->target; + vf->location = stmt->location; + + target = transformExpr(pstate, (Node *) vf, EXPR_KIND_LET_TARGET); + + paramvar = castNode(Param, target); + + Assert(paramvar->paramkind == PARAM_VARIABLE); + + /* Set up passthrough data for transformAssignTarget */ + passthru.stmt = (Node *) stmt; + passthru.target = (Node *) paramvar; + passthru.target_name = paramvar->paramvarname; + passthru.indirection = NIL; + passthru.ccontext = COERCION_ASSIGNMENT; + + /* we need to postpone conversion of "unknown" to text */ + pstate->p_resolve_unknowns = false; + + qry = transformSelectStmt(pstate, (SelectStmt *) stmt->query, &passthru); + + qry->resultVariable = paramvar->paramvarname; + qry->canSetTag = true; + + stmt->query = (Node *) qry; + + /* represent the command as a utility Query */ + result = makeNode(Query); + result->commandType = CMD_UTILITY; + result->utilityStmt = (Node *) stmt; + + return result; +} + /* * Produce a string representation of a LockClauseStrength value. * This should only be applied to valid values (not LCS_NONE). diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 837330a807f..327ecf88712 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -301,7 +301,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); DropTransformStmt DropUserMappingStmt ExplainStmt FetchStmt GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt - ListenStmt LoadStmt LockStmt MergeStmt NotifyStmt ExplainableStmt PreparableStmt + LetStmt ListenStmt LoadStmt LockStmt MergeStmt NotifyStmt ExplainableStmt PreparableStmt CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt RemoveFuncStmt RemoveOperStmt RenameStmt ReturnStmt RevokeStmt RevokeRoleStmt RuleActionStmt RuleActionStmtOrEmpty RuleStmt @@ -747,7 +747,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); KEEP KEY KEYS LABEL LANGUAGE LARGE_P LAST_P LATERAL_P - LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL + LEADING LEAKPROOF LEAST LET LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED LSN_P MAPPING MATCH MATCHED MATERIALIZED MAXVALUE MERGE MERGE_ACTION METHOD @@ -1095,6 +1095,7 @@ stmt: | ImportForeignSchemaStmt | IndexStmt | InsertStmt + | LetStmt | ListenStmt | RefreshMatViewStmt | LoadStmt @@ -12932,6 +12933,37 @@ opt_hold: /* EMPTY */ { $$ = 0; } | WITHOUT HOLD { $$ = 0; } ; +/***************************************************************************** + * + * QUERY: + * LET STATEMENT + * + *****************************************************************************/ +LetStmt: LET ColId '=' a_expr + { + LetStmt *n = makeNode(LetStmt); + SelectStmt *select; + ResTarget *res; + + n->target = $2; + + select = makeNode(SelectStmt); + res = makeNode(ResTarget); + + /* create target list for implicit query */ + res->name = NULL; + res->indirection = NIL; + res->val = (Node *) $4; + res->location = @4; + + select->targetList = list_make1(res); + n->query = (Node *) select; + + n->location = @2; + $$ = (Node *) n; + } + ; + /***************************************************************************** * * QUERY: @@ -18037,6 +18069,7 @@ unreserved_keyword: | LARGE_P | LAST_P | LEAKPROOF + | LET | LEVEL | LISTEN | LOAD @@ -18653,6 +18686,7 @@ bare_label_keyword: | LEAKPROOF | LEAST | LEFT + | LET | LEVEL | LIKE | LISTEN diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index b8340557b34..a9bd4e68154 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -584,6 +584,10 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) errkind = true; break; + case EXPR_KIND_LET_TARGET: + errkind = true; + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -1023,6 +1027,9 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_LET_TARGET: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index aa2483c96a6..bc6aab75cca 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -591,6 +591,9 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_PARTITION_BOUND: err = _("cannot use column reference in partition bound expression"); break; + case EXPR_KIND_LET_TARGET: + err = _("cannot use column reference as target of LET command"); + break; /* * There is intentionally no default: case here, so that the @@ -961,6 +964,7 @@ expr_kind_allows_session_variables(ParseExprKind p_expr_kind) case EXPR_KIND_RETURNING: case EXPR_KIND_VALUES: case EXPR_KIND_VALUES_SINGLE: + case EXPR_KIND_LET_TARGET: result = true; break; @@ -1980,6 +1984,9 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_GENERATED_COLUMN: err = _("cannot use subquery in column generation expression"); break; + case EXPR_KIND_LET_TARGET: + err = _("cannot use subquery as a target of LET command"); + break; /* * There is intentionally no default: case here, so that the @@ -3339,6 +3346,8 @@ ParseExprKindName(ParseExprKind exprKind) return "GENERATED AS"; case EXPR_KIND_CYCLE_MARK: return "CYCLE"; + case EXPR_KIND_LET_TARGET: + return "LET"; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index 778d69c6f3c..13616c9b3c2 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2783,6 +2783,9 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_LET_TARGET: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 7dc9c0a6c80..3f1c8cb5e89 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -237,6 +237,7 @@ ClassifyUtilityCommandAsReadOnly(Node *parsetree) case T_CallStmt: case T_DoStmt: + case T_LetStmt: { /* * Commands inside the DO block or the called procedure might @@ -1075,6 +1076,11 @@ standard_ProcessUtility(PlannedStmt *pstmt, DropVariableByName(((DropSessionVarStmt *) parsetree)->name); break; + case T_LetStmt: + ExecuteLetStmt(pstate, (LetStmt *) parsetree, params, + queryEnv, qc); + break; + default: /* All other statement types have event trigger support */ ProcessUtilitySlow(pstate, pstmt, queryString, @@ -2219,6 +2225,10 @@ UtilityContainsQuery(Node *parsetree) return UtilityContainsQuery(qry->utilityStmt); return qry; + case T_LetStmt: + qry = castNode(Query, ((LetStmt *) parsetree)->query); + return qry; + default: return NULL; } @@ -2414,6 +2424,10 @@ CreateCommandTag(Node *parsetree) tag = CMDTAG_SELECT; break; + case T_LetStmt: + tag = CMDTAG_LET; + break; + /* utility statements --- same whether raw or cooked */ case T_TransactionStmt: { @@ -3304,6 +3318,7 @@ GetCommandLogLevel(Node *parsetree) break; case T_PLAssignStmt: + case T_LetStmt: lev = LOGSTMT_ALL; break; diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c index d257b4fc37a..4da36144145 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -1265,8 +1265,8 @@ static const char *const sql_commands[] = { "ABORT", "ALTER", "ANALYZE", "BEGIN", "CALL", "CHECKPOINT", "CLOSE", "CLUSTER", "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE", "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", - "FETCH", "GRANT", "IMPORT FOREIGN SCHEMA", "INSERT INTO", "LISTEN", "LOAD", "LOCK", - "MERGE INTO", "MOVE", "NOTIFY", "PREPARE", + "FETCH", "GRANT", "IMPORT FOREIGN SCHEMA", "INSERT INTO", "LET", + "LISTEN", "LOAD", "LOCK", "MERGE INTO", "MOVE", "NOTIFY", "PREPARE", "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", "RESET", "REVOKE", "ROLLBACK", "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START", @@ -4798,6 +4798,11 @@ match_previous_words(int pattern_id, else if (TailMatches("VALUES") && !TailMatches("DEFAULT", "VALUES")) COMPLETE_WITH("("); +/* LET */ + /* Complete LET with "=" */ + else if (TailMatches("LET", MatchAny)) + COMPLETE_WITH("="); + /* LOCK */ /* Complete LOCK [TABLE] [ONLY] with a list of tables */ else if (Matches("LOCK")) diff --git a/src/include/commands/session_variable.h b/src/include/commands/session_variable.h index 610b757899e..c4b4d9e6832 100644 --- a/src/include/commands/session_variable.h +++ b/src/include/commands/session_variable.h @@ -16,8 +16,10 @@ #define SESSIONVARIABLE_H #include "catalog/objectaddress.h" +#include "nodes/params.h" #include "parser/parse_node.h" #include "nodes/parsenodes.h" +#include "tcop/cmdtag.h" extern void CreateVariable(ParseState *pstate, CreateSessionVarStmt *stmt); extern void DropVariableByName(char *varname); @@ -32,4 +34,7 @@ extern void get_session_variable_type_typmod_collid(char *varname, int32 *typmod, Oid *collid); +extern void ExecuteLetStmt(ParseState *pstate, LetStmt *stmt, ParamListInfo params, + QueryEnvironment *queryEnv, QueryCompletion *qc); + #endif diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 487464b14b1..3decc639d81 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -147,6 +147,9 @@ typedef struct Query */ int resultRelation pg_node_attr(query_jumble_ignore); + /* target variable of LET statement */ + char *resultVariable; + /* has aggregates in tlist or havingQual */ bool hasAggs pg_node_attr(query_jumble_ignore); /* has window functions in tlist */ @@ -2170,6 +2173,18 @@ typedef struct MergeStmt WithClause *withClause; /* WITH clause */ } MergeStmt; +/* ---------------------- + * Let Statement + * ---------------------- + */ +typedef struct LetStmt +{ + NodeTag type; + char *target; /* target variable */ + Node *query; /* source expression */ + ParseLoc location; +} LetStmt; + /* ---------------------- * Select Statement * diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index 97a00df8d1f..4aa0a07f44a 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -192,6 +192,9 @@ typedef struct PlannerGlobal /* list of used session variables */ List *sessionVariables; + + /* name of session variable used like target of LET command */ + char *resultVariable; } PlannerGlobal; /* macro for fetching the Plan associated with a SubPlan node */ diff --git a/src/include/parser/kwlist.h b/src/include/parser/kwlist.h index 8b1f2bd3b68..2181927682c 100644 --- a/src/include/parser/kwlist.h +++ b/src/include/parser/kwlist.h @@ -258,6 +258,7 @@ PG_KEYWORD("leading", LEADING, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("leakproof", LEAKPROOF, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("least", LEAST, COL_NAME_KEYWORD, BARE_LABEL) PG_KEYWORD("left", LEFT, TYPE_FUNC_NAME_KEYWORD, BARE_LABEL) +PG_KEYWORD("let", LET, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("level", LEVEL, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("like", LIKE, TYPE_FUNC_NAME_KEYWORD, BARE_LABEL) PG_KEYWORD("limit", LIMIT, RESERVED_KEYWORD, AS_LABEL) diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index 84e886940d8..026743b7337 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -82,6 +82,7 @@ typedef enum ParseExprKind EXPR_KIND_COPY_WHERE, /* WHERE condition in COPY FROM */ EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */ EXPR_KIND_CYCLE_MARK, /* cycle mark value */ + EXPR_KIND_LET_TARGET, /* only session variables */ } ParseExprKind; diff --git a/src/include/tcop/cmdtaglist.h b/src/include/tcop/cmdtaglist.h index 7e59d0a5275..fd212257167 100644 --- a/src/include/tcop/cmdtaglist.h +++ b/src/include/tcop/cmdtaglist.h @@ -185,6 +185,7 @@ PG_CMDTAG(CMDTAG_GRANT, "GRANT", true, false, false) PG_CMDTAG(CMDTAG_GRANT_ROLE, "GRANT ROLE", false, false, false) PG_CMDTAG(CMDTAG_IMPORT_FOREIGN_SCHEMA, "IMPORT FOREIGN SCHEMA", true, false, false) PG_CMDTAG(CMDTAG_INSERT, "INSERT", false, false, true) +PG_CMDTAG(CMDTAG_LET, "LET", false, false, false) PG_CMDTAG(CMDTAG_LISTEN, "LISTEN", false, false, false) PG_CMDTAG(CMDTAG_LOAD, "LOAD", false, false, false) PG_CMDTAG(CMDTAG_LOCK_TABLE, "LOCK TABLE", false, false, false) diff --git a/src/test/regress/expected/session_variables_dml.out b/src/test/regress/expected/session_variables_dml.out index 01efa71c8f0..79a062f6efa 100644 --- a/src/test/regress/expected/session_variables_dml.out +++ b/src/test/regress/expected/session_variables_dml.out @@ -133,3 +133,126 @@ RESET min_parallel_table_scan_size; RESET max_parallel_workers_per_gather; DROP TABLE testvar_testtab; DROP VARIABLE temp_var02; +CREATE TEMP VARIABLE temp_var03 AS numeric; +-- LET stmt is not allowed inside CTE +WITH x AS (LET temp_var03 = 3.14) SELECT * FROM x; +ERROR: syntax error at or near "LET" +LINE 1: WITH x AS (LET temp_var03 = 3.14) SELECT * FROM x; + ^ +-- LET stmt requires result with exactly one row +LET temp_var03 = generate_series(1,1); +SELECT VARIABLE(temp_var03); + temp_var03 +------------ + 1 +(1 row) + +-- should fail +LET temp_var03 = generate_series(1,2); +ERROR: expression returned more than one row +LET temp_var03 = generate_series(1,0); +ERROR: expression returned no rows +CREATE OR REPLACE FUNCTION testvar_sql01(numeric) +RETURNS void AS $$ +LET temp_var03 = $1; +$$ LANGUAGE sql; +CREATE OR REPLACE FUNCTION testvar_sql02() +RETURNS numeric AS $$ +SELECT VARIABLE(temp_var03); +$$ LANGUAGE sql; +SELECT testvar_sql01(3.14); + testvar_sql01 +--------------- + +(1 row) + +SELECT testvar_sql02(), VARIABLE(temp_var03); + testvar_sql02 | temp_var03 +---------------+------------ + 3.14 | 3.14 +(1 row) + +CREATE OR REPLACE FUNCTION testvar_pl(varchar) +RETURNS varchar AS $$ +BEGIN + LET temp_var03 = $1::numeric; + RETURN VARIABLE(temp_var03); +END +$$ LANGUAGE plpgsql SECURITY DEFINER; +SELECT testvar_pl('3.14'); + testvar_pl +------------ + 3.14 +(1 row) + +DROP VARIABLE temp_var03; +SET plan_cache_mode to force_generic_plan; +-- should not crash +SELECT testvar_sql01(3.14); +ERROR: session variable "temp_var03" doesn't exist +CONTEXT: SQL function "testvar_sql01" during inlining +SELECT testvar_sql02(), VARIABLE(temp_var03); +ERROR: session variable "temp_var03" doesn't exist +SELECT testvar_pl('3.141592'); +ERROR: session variable "temp_var03" doesn't exist +CONTEXT: SQL statement "LET temp_var03 = $1::numeric" +PL/pgSQL function testvar_pl(character varying) line 3 at SQL statement +-- can work again if we create variable +CREATE TEMP VARIABLE temp_var03 AS numeric; +SELECT testvar_sql01(3.14); + testvar_sql01 +--------------- + +(1 row) + +SELECT testvar_sql02(), VARIABLE(temp_var03); + testvar_sql02 | temp_var03 +---------------+------------ + 3.14 | 3.14 +(1 row) + +SELECT testvar_pl('3.141592'); + testvar_pl +------------ + 3.141592 +(1 row) + +CREATE ROLE regress_session_variable_test_role_04; +SET ROLE regress_session_variable_test_role_04; +-- should fail +SELECT testvar_sql01(3.14); +ERROR: permission denied for session variable temp_var03 +CONTEXT: SQL function "testvar_sql01" statement 1 +-- should be ok (security definer) +SELECT testvar_pl('3.141592'); + testvar_pl +------------ + 3.141592 +(1 row) + +SET ROLE TO DEFAULT; +DROP FUNCTION testvar_sql01(numeric); +DROP FUNCTION testvar_sql02(); +DROP FUNCTION testvar_pl(varchar); +DROP ROLE regress_session_variable_test_role_04; +DROP VARIABLE temp_var03; +SET plan_cache_mode TO DEFAULT; +-- test extended query protocol +CREATE TEMP VARIABLE temp_var04 AS int; +LET temp_var04 = $1 \bind 10 \g +SELECT VARIABLE(temp_var04); + temp_var04 +------------ + 10 +(1 row) + +LET temp_var04 = $1 \parse letps +\bind_named letps 100 \g +SELECT VARIABLE(temp_var04); + temp_var04 +------------ + 100 +(1 row) + +\close_prepared letps +DROP VARIABLE temp_var04; diff --git a/src/test/regress/sql/session_variables_dml.sql b/src/test/regress/sql/session_variables_dml.sql index bf56b19467b..b8408c97cad 100644 --- a/src/test/regress/sql/session_variables_dml.sql +++ b/src/test/regress/sql/session_variables_dml.sql @@ -118,3 +118,90 @@ RESET max_parallel_workers_per_gather; DROP TABLE testvar_testtab; DROP VARIABLE temp_var02; + +CREATE TEMP VARIABLE temp_var03 AS numeric; + +-- LET stmt is not allowed inside CTE +WITH x AS (LET temp_var03 = 3.14) SELECT * FROM x; + +-- LET stmt requires result with exactly one row +LET temp_var03 = generate_series(1,1); +SELECT VARIABLE(temp_var03); + +-- should fail +LET temp_var03 = generate_series(1,2); +LET temp_var03 = generate_series(1,0); + +CREATE OR REPLACE FUNCTION testvar_sql01(numeric) +RETURNS void AS $$ +LET temp_var03 = $1; +$$ LANGUAGE sql; + +CREATE OR REPLACE FUNCTION testvar_sql02() +RETURNS numeric AS $$ +SELECT VARIABLE(temp_var03); +$$ LANGUAGE sql; + +SELECT testvar_sql01(3.14); +SELECT testvar_sql02(), VARIABLE(temp_var03); + +CREATE OR REPLACE FUNCTION testvar_pl(varchar) +RETURNS varchar AS $$ +BEGIN + LET temp_var03 = $1::numeric; + RETURN VARIABLE(temp_var03); +END +$$ LANGUAGE plpgsql SECURITY DEFINER; + +SELECT testvar_pl('3.14'); + +DROP VARIABLE temp_var03; + +SET plan_cache_mode to force_generic_plan; + +-- should not crash +SELECT testvar_sql01(3.14); +SELECT testvar_sql02(), VARIABLE(temp_var03); +SELECT testvar_pl('3.141592'); + +-- can work again if we create variable +CREATE TEMP VARIABLE temp_var03 AS numeric; +SELECT testvar_sql01(3.14); +SELECT testvar_sql02(), VARIABLE(temp_var03); +SELECT testvar_pl('3.141592'); + +CREATE ROLE regress_session_variable_test_role_04; + +SET ROLE regress_session_variable_test_role_04; + +-- should fail +SELECT testvar_sql01(3.14); + +-- should be ok (security definer) +SELECT testvar_pl('3.141592'); + +SET ROLE TO DEFAULT; + +DROP FUNCTION testvar_sql01(numeric); +DROP FUNCTION testvar_sql02(); +DROP FUNCTION testvar_pl(varchar); + +DROP ROLE regress_session_variable_test_role_04; + +DROP VARIABLE temp_var03; + +SET plan_cache_mode TO DEFAULT; + +-- test extended query protocol +CREATE TEMP VARIABLE temp_var04 AS int; + +LET temp_var04 = $1 \bind 10 \g +SELECT VARIABLE(temp_var04); + +LET temp_var04 = $1 \parse letps +\bind_named letps 100 \g +SELECT VARIABLE(temp_var04); + +\close_prepared letps + +DROP VARIABLE temp_var04; diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index 088429dd424..7fbc5c8834d 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -1550,6 +1550,7 @@ LargeObjectDesc Latch LauncherLastStartTimesEntry LerpFunc +LetStmt LexDescr LexemeEntry LexemeHashKey -- 2.52.0