Project: YugabyteDB (App Definition and Development / Database)
File: ddl_create_function.md

title: Create function statement [YSQL] headerTitle: Create function linkTitle: CREATE FUNCTION description: Use the CREATE FUNCTION statement to create a function in a database. menu: v2.18: identifier: ddlcreatefunction parent: statements type: docs {{< tip title="See the dedicated 'User-defined subprograms and anonymous blocks' section." >}} User-defined functions are part of a larger area of functionality. See this major section: {{< /tip >}} Use the `CREATE FUNCTION` statement to create a function in a database. {{%ebnf%}} create_function, argdeclwith_dflt, arg_decl, subprogram_signature, unalterablefnattribute, lang_name, subprogram_implementation, sqlstmtlist, alterablefnandprocattribute, alterablefnonly_attribute, volatility, onnullinput, parallel_mode {{%/ebnf%}} <a name="make-function-returns-mandatory"></a> {{< tip title="'Regard the 'RETURNS' clause as mandatory." >}} The to the topic of user-defined subprograms explains that, for historical reasons, you can create a function, when a creating a procedure is the proper choice, by omitting the `RETURNS` clause and by giving it `OUT` or `INOUT` arguments. Yugabyte recommends that you don't exploit this freedom. For this reason, the `RETURNS` clause is shown as mandatory in the createfunction_ syntax rule (though the PostgreSQL documentation shows it as optional). Yugabyte further recommends that you avoid declaring `out` or `inout` arguments for a function. When the purpose is to return more than just a single scalar value, you should create a dedicated composite type for the purpose and use this to declare the function's return value. {{< /tip >}} {{< tip title="'create function' and the 'subprogram_signature' rule." >}} When you write a `CREATE FUNCTION` statement, you will already have decided what formal arguments it will havei.e. for each, what will be its name, mode, data type, and optionally its default value. When, later, you alter or drop a function, you must identify it. You do this, in and , typically by specifying just its . You are allowed to use the full subprogramsignature. But this is unconventional. Notice that the subprogramsignature does not include the optional specification of default values; and you cannot mention these when you alter or drop a function. The distinction between the subprogramsignature and the subprogramcallsignature_ is discussed carefully in the section . {{< /tip >}} The meanings of the various function attributes are explained in the section . A function, like other schema objects such as a table, inevitably has an owner. You cannot specify the owner explicitly when a function is created. Rather, it's defined implicitly as what the built-in currentuser function returns when it's invoked in the session that creates the function. This user must have the usage privilege on the function's schema, its argument data types, and its return data

You (optionally) specify the function's schema and (mandatorily) its name within its schema as the argument of the _ rule. If a function with the given name, schema, and argument types already exists then `CREATE FUNCTION` will draw an error unless the `CREATE OR REPLACE FUNCTION` variant is used. Functions with different can share the same . (The same holds for procedures.) See section . `CREATE OR REPLACE FUNCTION` doesn't change permissions that have been granted on an existing function. To use this statement, the current user must own the function, or be a member of the role that owns it. In contrast, if you drop and then recreate a function, the new function is not the same entity as the old one. So you will have to drop existing objects that depend upon the old function. (Dropping the function `CASCADE` achieves this.) Alternatively, `ALTER FUNCTION` can be used to change most of the attributes of an existing function. The languages supported by default are `SQL`, `PLPGSQL` and `C`. <a name="create-function-grants-execute-to-public"></a> {{< tip title="'Create function' grants 'execute' to 'public'." >}} Execute is granted automatically to public when you create a new function. This is very unlikely to be want you wantand so this behavior presents a disguised security risk. Yugabyte recommends that your standard practice be to revoke this privilege immediately after creating a function. {{< /tip >}} {{< tip title="You cannot set the 'depends on extension' attribute with 'create function'." >}} A function's depends on extension attribute cannot be set using `CREATE to set it. {{< /tip >}} Use the appropriate variant of the `RETURN` clause to create either a scalar function or a table function. Notice that scalar can denote not just an atomic value of data types like int, numeric, text, and so on (and domains based on those data types); it can also denote a single composite value like that of a user-defined type. Try this: ```plpgsql create schema s; create type s.x as (i int, t text); create function s.f(i in int, t in text) returns x security invoker set searchpath = pgcatalog, pg_temp language plpgsql as $body$ begin return (i*2, t||t)::s.x; end; $body$; WITH c as (select s.f(42, 'dog') as v) select (v).i, (v).t FROM c; ``` This is the result: ```output i | t -+-- 84 | dogdog ``` Try this ```plpgsql create table s.t(k serial primary key, v text); insert into s.t(v) values ('dog'), ('cat'), ('frog'); create function

returns table(z text) security definer set searchpath = pgcatalog, pg_temp language plpgsql as $body$ begin z := 'Starting content of t '; return next; z := '-'; return next; for z in (select v from s.t order by k) loop return next; end loop; begin insert into s.t(v) values ('mouse'); exception when stringdataright_truncation then z := ''; return next; z := 'stringdataright_truncation caught'; return next; end; insert into s.t(v) values ('bird'); z := ''; return next; z := 'Finishing content of t'; return next; z := '-'; return next; for z in (select v from s.t order by k) loop return next; end loop; end; $body$; \t on select z from s.f(); \t off ``` This is the result: ```output Starting content of t dog cat frog stringdataright_truncation caught Finishing content of t dog cat frog bird ``` This kind of table function provides a convenient way to produce an arbitrarily formatted report that can easily be spooled to a file. This is because the select output is easily accessible (in ysqlsh) on stdoutand it's correspondingly easily accessible in client-side programming languages that do SQL like say, Python. In contrast, the output from raise info is tricky to capture (and definitely very hard to interleave in proper sequence with select results) because it comes on stderr. {{< tip title="Always name the formal arguments and write the function's body last." >}} YSQL inherits from PostgreSQL the ability to specify the arguments only by listing their data types and to reference them in the body using the positional notation `$1`, `$2`, and so on. The earliest versions of PostgreSQL didn't allow named parameters. But the version that YSQL is based on does allow this. Your code will be very much easier to understand if you use named arguments like the example does. The syntax rules allow you to write the alterable and unalterable attributes in any order, like this: ```plpgsql create function s.f(i in int, t in text) returns s.x security invoker as $body$ begin return (i*2, t||t)::s.x; end; $body$ set searchpath = pgcatalog, pg_temp language plpgsql; ``` Yugabyte recommends that you avoid exploiting this freedom and choose a standard order where, especially, you write the body last. For example, it helps readability to specify the language immediately before the body. (Try the \\sf meta-command for a function that you created. It always shows the source text last, no matter what order your create [or replace] used.) Following this practice will allow you to review, and discuss, your code in a natural way by distinguishing, informally, between the function header (i.e. everything that comes before the body) and the implementation (i.e. the body). {{< /tip >}}