CREATE_PROCEDURE(7pg15)
โจ CREATE PROCEDURE โจ
okay bestie, gather round ๐
you're reading the CREATE PROCEDURE page from the PostgreSQL 15.19 docs (section 7, we're fancy like that) โ this is literally the whole glow-up guide for defining a brand new procedure. let's get into it ๐
๐ธ NAME ๐ธ
CREATE_PROCEDURE โ define a new procedure. that's it. that's the whole vibe. โจ
๐ฆ SYNOPSIS ๐ฆ
CREATE [ OR REPLACE ] PROCEDURE
name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] )
{ LANGUAGE lang_name
| TRANSFORM { FOR TYPE type_name } [, ... ]
| [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
| SET configuration_parameter { TO value | = value | FROM CURRENT }
| AS 'definition'
| AS 'obj_file', 'link_symbol'
| sql_body
} ...
like, read it out loud, it's giving incantation ๐ฎ and honestly? it is one. one typo and the whole thing ghosts you ๐
๐ DESCRIPTION ๐
so CREATE PROCEDURE defines a new procedure. and CREATE OR REPLACE PROCEDURE? it'll either create a fresh new one or replace an existing definition. two for the price of one, slay ๐ซถ
quick permission check though โ to even be able to define a procedure, the user must have the USAGE privilege on the language. no privilege, no procedure, no cap ๐ญ
if you include a schema name, the procedure gets created in that specified schema. otherwise it lands in the current schema. โจ but here's the rule: the name of the new procedure must not match any existing procedure or function with the same input argument types in the same schema. However (and this is the fun part), procedures and functions of different argument types can share a name โ that's called overloading ๐ iconic behavior honestly.
to replace the current definition of an existing procedure, use CREATE OR REPLACE PROCEDURE. but โ and this is a big but โ it is not possible to change the name or argument types of a procedure this way. if you tried, you'd actually be creating a new, distinct procedure ๐ like ma'am that's a different person.
when CREATE OR REPLACE PROCEDURE is used to replace an existing procedure, the ownership and permissions of the procedure do not change. every other procedure property gets assigned the values specified or implied in the command. and you must own the procedure to replace it โ that includes being a member of the owning role ๐
the user that creates the procedure becomes the owner of the procedure. periodt.
also: to be able to create a procedure, you must have USAGE privilege on the argument types. yes, both. we check everything around here ๐ฅ
refer to Section 38.4 for further information on writing procedures โ that's your homework, bestie ๐
๐ PARAMETERS ๐
okay this is the part where we go one by one, no skipping, no summarizing, we're thorough in this house ๐
nameโ the name of the procedure to create, optionally schema-qualified. simple. cute.argmodeโ the mode of an argument:IN,OUT,INOUT, orVARIADIC. if you omit it, the default isIN. (default king behavior ๐)argnameโ the name of an argument.argtypeโ the data type(s) of the procedure's arguments, optionally schema-qualified, if any. the argument types can be base, composite, or domain types, or can reference the type of a table column.
depending on the implementation language, it might also be allowed to specify "pseudo-types" such as cstring. pseudo-types indicate that the actual argument type is either incompletely specified, or outside the set of ordinary SQL data types.
the type of a column is referenced by writing table_name.column_name%TYPE. using this feature can sometimes help make a procedure independent of changes to the definition of a table. honestly? a lifesaver. a vibe. use it ๐ฆ
default_exprโ an expression to be used as the default value if the parameter is not specified. the expression has to be coercible to the argument type of the parameter. and all input parameters following a parameter with a default value must have default values as well. no mixing, no chaos, we keep it orderly โจlang_nameโ the name of the language that the procedure is implemented in. it can besql,c,internal, or the name of a user-defined procedural language, e.g.,plpgsql. the default issqlifsql_bodyis specified. enclosing the name in single quotes is deprecated and requires matching case โ ๏ธ like, don't. it's giving 2003.TRANSFORM { FOR TYPE type_name } [, ... ] }โ lists which transforms a call to the procedure should apply. transforms convert between SQL types and language-specific data types; seeCREATE TRANSFORM(CREATE_TRANSFORM(7)). procedural language implementations usually have hardcoded knowledge of the built-in types, so those don't need to be listed here. if a procedural language implementation does not know how to handle a type and no transform is supplied, it will fall back to a default behavior for converting data types, but this depends on the implementation. ymmv, as always ๐ง[EXTERNAL] SECURITY INVOKERand[EXTERNAL] SECURITY DEFINERโSECURITY INVOKERindicates that the procedure is to be executed with the privileges of the user that calls it. that is the default.SECURITY DEFINERspecifies that the procedure is to be executed with the privileges of the user that owns it.
the key word EXTERNAL is allowed for SQL conformance, but it is optional since, unlike in SQL, this feature applies to all procedures not only external ones. (so it's decorative. cute but optional ๐
)
โ ๏ธ lowkey important: aSECURITY DEFINERprocedure cannot execute transaction control statements (for example,COMMITandROLLBACK, depending on the language). not me finding out the hard way ๐ญ
configuration_parameter,valueโ theSETclause causes the specified configuration parameter to be set to the specified value when the procedure is entered, and then restored to its prior value when the procedure exits.SET FROM CURRENTsaves the value of the parameter that is current whenCREATE PROCEDUREis executed as the value to be applied when the procedure is entered. so it snapshots the now-now and replays it later ๐ธ
if a SET clause is attached to a procedure, then the effects of a SET LOCAL command executed inside the procedure for the same variable are restricted to the procedure: the configuration parameter's prior value is still restored at procedure exit. however, an ordinary SET command (without LOCAL) overrides the SET clause, much as it would do for a previous SET LOCAL command: the effects of such a command will persist after procedure exit, unless the current transaction is rolled back. yes, this is the tricky bit. read it twice, i did ๐ซ
if a SET clause is attached to a procedure, then that procedure cannot execute transaction control statements (for example, COMMIT and ROLLBACK, depending on the language).
see SET(7) and Chapter 20 for more information about allowed parameter names and values.
definitionโ a string constant defining the procedure; the meaning depends on the language. it can be an internal procedure name, the path to an object file, an SQL command, or text in a procedural language.
it is often helpful to use dollar quoting (see Section 4.1.2.4) to write the procedure definition string, rather than the normal single quote syntax. without dollar quoting, any single quotes or backslashes in the procedure definition must be escaped by doubling them. doubling. them. every. time. do yourself a favor and use the dollars ๐ธ
obj_file,link_symbolโ this form of theASclause is used for dynamically loadable C language procedures when the procedure name in the C language source code is not the same as the name of the SQL procedure. the stringobj_fileis the name of the shared library file containing the compiled C procedure, and is interpreted as for theLOADcommand. the stringlink_symbolis the procedure's link symbol, that is, the name of the procedure in the C language source code. if the link symbol is omitted, it is assumed to be the same as the name of the SQL procedure being defined.
when repeated CREATE PROCEDURE calls refer to the same object file, the file is only loaded once per session. to unload and reload the file (perhaps during development), start a new session. no shortcut, just new session energy ๐
sql_bodyโ the body of aLANGUAGE SQLprocedure. this should be a block:
BEGIN ATOMIC
statement;
statement;
...
statement;
END
this is similar to writing the text of the procedure body as a string constant (see definition above), but there are some differences: this form only works for LANGUAGE SQL, the string constant form works for all languages. this form is parsed at procedure definition time, the string constant form is parsed at execution time; therefore this form cannot support polymorphic argument types and other constructs that are not resolvable at procedure definition time. this form tracks dependencies between the procedure and objects used in the procedure body, so DROP ... CASCADE will work correctly, whereas the form using string literals may leave dangling procedures. finally, this form is more compatible with the SQL standard and other SQL implementations. honestly? this form ate ๐
๐ NOTES ๐
see CREATE FUNCTION (CREATE_FUNCTION(7)) for more details on function creation that also apply to procedures. shared lore, iykyk ๐
use CALL(7) to execute a procedure. that's the whole point, bestie โจ
๐ EXAMPLES ๐
first way:
CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
AS $$
INSERT INTO tbl VALUES (a);
INSERT INTO tbl VALUES (b);
$$;
or โ the other flavor:
CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
BEGIN ATOMIC
INSERT INTO tbl VALUES (a);
INSERT INTO tbl VALUES (b);
END;
and call like this:
CALL insert_data(1, 2);
look at that. gorgeous. no notes ๐ฅ
๐ COMPATIBILITY ๐
a CREATE PROCEDURE command is defined in the SQL standard. the PostgreSQL implementation can be used in a compatible way but has many extensions. for details see also CREATE FUNCTION (CREATE_FUNCTION(7)). we're compatible and extra, it's a whole personality ๐
๐ซถ SEE ALSO ๐ซถ
ALTER PROCEDURE(ALTER_PROCEDURE(7))DROP PROCEDURE(DROP_PROCEDURE(7))CALL(7)CREATE FUNCTION(CREATE_FUNCTION(7))
and that's the page!! ๐ this was PostgreSQL 15.19 documentation, dated 2026, and yes the footer says the same thing โ go forth and define your procedures, you absolute icon ๐๐
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms