summaryrefslogtreecommitdiff
path: root/src/bin/psql/describe.c
diff options
context:
space:
mode:
authorPeter Eisentraut2021-04-07 19:30:08 +0000
committerPeter Eisentraut2021-04-07 19:47:55 +0000
commite717a9a18b2e34c9c40e5259ad4d31cd7e420750 (patch)
tree6eda5b4cf6468d599efc0da4628bec53d35484af /src/bin/psql/describe.c
parent1e55e7d1755cefbb44982fbacc7da461fa8684e6 (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 <[email protected]> Reviewed-by: Julien Rouhaud <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
Diffstat (limited to 'src/bin/psql/describe.c')
-rw-r--r--src/bin/psql/describe.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 440249ff69d..52f7b2ce78c 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -505,11 +505,18 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
appendPQExpBufferStr(&buf, ",\n ");
printACLColumn(&buf, "p.proacl");
appendPQExpBuffer(&buf,
- ",\n l.lanname as \"%s\""
- ",\n p.prosrc as \"%s\""
+ ",\n l.lanname as \"%s\"",
+ gettext_noop("Language"));
+ if (pset.sversion >= 140000)
+ appendPQExpBuffer(&buf,
+ ",\n COALESCE(p.prosrc, pg_catalog.pg_get_function_sqlbody(p.oid)) as \"%s\"",
+ gettext_noop("Source code"));
+ else
+ appendPQExpBuffer(&buf,
+ ",\n p.prosrc as \"%s\"",
+ gettext_noop("Source code"));
+ appendPQExpBuffer(&buf,
",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
- gettext_noop("Language"),
- gettext_noop("Source code"),
gettext_noop("Description"));
}