2 .\" Title: CREATE PROCEDURE
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE PROCEDURE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_PROCEDURE \- define a new procedure
35 CREATE [ OR REPLACE ] PROCEDURE
36 \fIname\fR ( [ [ \fIargmode\fR ] [ \fIargname\fR ] \fIargtype\fR [ { DEFAULT | = } \fIdefault_expr\fR ] [, \&.\&.\&.] ] )
37 { LANGUAGE \fIlang_name\fR
38 | TRANSFORM { FOR TYPE \fItype_name\fR } [, \&.\&.\&. ]
39 | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
40 | SET \fIconfiguration_parameter\fR { TO \fIvalue\fR | = \fIvalue\fR | FROM CURRENT }
41 | AS \*(Aq\fIdefinition\fR\*(Aq
42 | AS \*(Aq\fIobj_file\fR\*(Aq, \*(Aq\fIlink_symbol\fR\*(Aq
48 \fBCREATE PROCEDURE\fR
49 defines a new procedure\&.
50 \fBCREATE OR REPLACE PROCEDURE\fR
51 will either create a new procedure, or replace an existing definition\&. To be able to define a procedure, the user must have the
53 privilege on the language\&.
55 If a schema name is included, then the procedure is created in the specified schema\&. Otherwise it is created in the current schema\&. 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, procedures and functions of different argument types can share a name (this is called
58 To replace the current definition of an existing procedure, use
59 \fBCREATE OR REPLACE PROCEDURE\fR\&. It is not possible to change the name or argument types of a procedure this way (if you tried, you would actually be creating a new, distinct procedure)\&.
62 \fBCREATE OR REPLACE PROCEDURE\fR
63 is used to replace an existing procedure, the ownership and permissions of the procedure do not change\&. All other procedure properties are assigned the values specified or implied in the command\&. You must own the procedure to replace it (this includes being a member of the owning role)\&.
65 The user that creates the procedure becomes the owner of the procedure\&.
67 To be able to create a procedure, you must have
69 privilege on the argument types\&.
73 for further information on writing procedures\&.
78 The name (optionally schema\-qualified) of the procedure to create\&.
83 The mode of an argument:
87 VARIADIC\&. If omitted, the default is
93 The name of an argument\&.
98 The data type(s) of the procedure\*(Aqs 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\&.
100 Depending on the implementation language it might also be allowed to specify
101 \(lqpseudo\-types\(rq
103 cstring\&. Pseudo\-types indicate that the actual argument type is either incompletely specified, or outside the set of ordinary SQL data types\&.
105 The type of a column is referenced by writing
106 \fItable_name\fR\&.\fIcolumn_name\fR%TYPE\&. Using this feature can sometimes help make a procedure independent of changes to the definition of a table\&.
111 An expression to be used as default value if the parameter is not specified\&. The expression has to be coercible to the argument type of the parameter\&. All input parameters following a parameter with a default value must have default values as well\&.
116 The name of the language that the procedure is implemented in\&. It can be
119 internal, or the name of a user\-defined procedural language, e\&.g\&.,
120 plpgsql\&. The default is
124 is specified\&. Enclosing the name in single quotes is deprecated and requires matching case\&.
127 TRANSFORM { FOR TYPE \fItype_name\fR } [, \&.\&.\&. ] }
129 Lists which transforms a call to the procedure should apply\&. Transforms convert between SQL types and language\-specific data types; see
130 CREATE TRANSFORM (\fBCREATE_TRANSFORM\fR(7))\&. Procedural language implementations usually have hardcoded knowledge of the built\-in types, so those don\*(Aqt 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\&.
133 [EXTERNAL] SECURITY INVOKER
135 [EXTERNAL] SECURITY DEFINER
138 indicates that the procedure is to be executed with the privileges of the user that calls it\&. That is the default\&.
140 specifies that the procedure is to be executed with the privileges of the user that owns it\&.
144 is allowed for SQL conformance, but it is optional since, unlike in SQL, this feature applies to all procedures not only external ones\&.
148 procedure cannot execute transaction control statements (for example,
151 \fBROLLBACK\fR, depending on the language)\&.
154 \fIconfiguration_parameter\fR
160 clause 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\&.
162 saves the value of the parameter that is current when
163 \fBCREATE PROCEDURE\fR
164 is executed as the value to be applied when the procedure is entered\&.
168 clause is attached to a procedure, then the effects of a
170 command executed inside the procedure for the same variable are restricted to the procedure: the configuration parameter\*(Aqs prior value is still restored at procedure exit\&. However, an ordinary
175 clause, much as it would do for a previous
177 command: the effects of such a command will persist after procedure exit, unless the current transaction is rolled back\&.
181 clause is attached to a procedure, then that procedure cannot execute transaction control statements (for example,
184 \fBROLLBACK\fR, depending on the language)\&.
190 for more information about allowed parameter names and values\&.
195 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\&.
197 It is often helpful to use dollar quoting (see
198 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\&.
201 \fIobj_file\fR, \fIlink_symbol\fR
205 clause 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 string
207 is the name of the shared library file containing the compiled C procedure, and is interpreted as for the
209 command\&. The string
211 is the procedure\*(Aqs 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\&.
214 \fBCREATE PROCEDURE\fR
215 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\&.
222 procedure\&. This should be a block
239 This is similar to writing the text of the procedure body as a string constant (see
241 above), but there are some differences: This form only works for
242 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
243 DROP \&.\&.\&. CASCADE
244 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\&.
249 CREATE FUNCTION (\fBCREATE_FUNCTION\fR(7))
250 for more details on function creation that also apply to procedures\&.
254 to execute a procedure\&.
261 CREATE PROCEDURE insert_data(a integer, b integer)
264 INSERT INTO tbl VALUES (a);
265 INSERT INTO tbl VALUES (b);
278 CREATE PROCEDURE insert_data(a integer, b integer)
281 INSERT INTO tbl VALUES (a);
282 INSERT INTO tbl VALUES (b);
295 CALL insert_data(1, 2);
303 \fBCREATE PROCEDURE\fR
304 command is defined in the SQL standard\&. The
306 implementation can be used in a compatible way but has many extensions\&. For details see also
307 CREATE FUNCTION (\fBCREATE_FUNCTION\fR(7))\&.
309 ALTER PROCEDURE (\fBALTER_PROCEDURE\fR(7)), DROP PROCEDURE (\fBDROP_PROCEDURE\fR(7)), \fBCALL\fR(7), CREATE FUNCTION (\fBCREATE_FUNCTION\fR(7))