diff options
| author | Peter Eisentraut | 2021-04-07 19:30:08 +0000 |
|---|---|---|
| committer | Peter Eisentraut | 2021-04-07 19:47:55 +0000 |
| commit | e717a9a18b2e34c9c40e5259ad4d31cd7e420750 (patch) | |
| tree | 6eda5b4cf6468d599efc0da4628bec53d35484af /src/include/nodes | |
| parent | 1e55e7d1755cefbb44982fbacc7da461fa8684e6 (diff) | |
SQL-standard function body
This adds support for writing CREATE FUNCTION and CREATE PROCEDURE
statements for language SQL with a function body that conforms to the
SQL standard and is portable to other implementations.
Instead of the PostgreSQL-specific AS $$ string literal $$ syntax,
this allows writing out the SQL statements making up the body
unquoted, either as a single statement:
CREATE FUNCTION add(a integer, b integer) RETURNS integer
LANGUAGE SQL
RETURN a + b;
or as a block
CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
BEGIN ATOMIC
INSERT INTO tbl VALUES (a);
INSERT INTO tbl VALUES (b);
END;
The function body is parsed at function definition time and stored as
expression nodes in a new pg_proc column prosqlbody. So at run time,
no further parsing is required.
However, this form does not support polymorphic arguments, because
there is no more parse analysis done at call time.
Dependencies between the function and the objects it uses are fully
tracked.
A new RETURN statement is introduced. This can only be used inside
function bodies. Internally, it is treated much like a SELECT
statement.
psql needs some new intelligence to keep track of function body
boundaries so that it doesn't send off statements when it sees
semicolons that are inside a function body.
Tested-by: Jaime Casanova <jcasanov@systemguards.com.ec>
Reviewed-by: Julien Rouhaud <rjuju123@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/1c11f1eb-f00c-43b7-799d-2d44132c02d7@2ndquadrant.com
Diffstat (limited to 'src/include/nodes')
| -rw-r--r-- | src/include/nodes/nodes.h | 1 | ||||
| -rw-r--r-- | src/include/nodes/parsenodes.h | 13 |
2 files changed, 14 insertions, 0 deletions
diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 2051abbbf92..d9e417bcd70 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -321,6 +321,7 @@ typedef enum NodeTag T_DeleteStmt, T_UpdateStmt, T_SelectStmt, + T_ReturnStmt, T_PLAssignStmt, T_AlterTableStmt, T_AlterTableCmd, diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index acc093d6e0f..7a44bccdd3b 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -140,6 +140,8 @@ typedef struct Query bool hasForUpdate; /* FOR [KEY] UPDATE/SHARE was specified */ bool hasRowSecurity; /* rewriter has applied some RLS policy */ + bool isReturn; /* is a RETURN statement */ + List *cteList; /* WITH list (of CommonTableExpr's) */ List *rtable; /* list of range table entries */ @@ -1721,6 +1723,16 @@ typedef struct SetOperationStmt } SetOperationStmt; +/* + * RETURN statement (inside SQL function body) + */ +typedef struct ReturnStmt +{ + NodeTag type; + Node *returnval; +} ReturnStmt; + + /* ---------------------- * PL/pgSQL Assignment Statement * @@ -2924,6 +2936,7 @@ typedef struct CreateFunctionStmt List *parameters; /* a list of FunctionParameter */ TypeName *returnType; /* the return type */ List *options; /* a list of DefElem */ + Node *sql_body; } CreateFunctionStmt; typedef enum FunctionParameterMode |
