CREATE_PROCEDURE(7pg16)

๐Ÿ“ฆ version 16.15, in opensuse-leap 16.0

๐Ÿ’… CREATE_PROCEDURE ๐Ÿ’…

okay so bestie, you're on the PostgreSQL 16.15 Documentation page for CREATE PROCEDURE(7) and honestly? you've come to the right place. this is the whole glow-up guide to making a brand new procedure. let's go ๐ŸŽ€


โœจ 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. just look at her. iconic. ๐ŸŒˆ


๐Ÿ’– DESCRIPTION ๐Ÿ’–

CREATE PROCEDURE defines a new procedure โ€” that's the whole vibe. and CREATE OR REPLACE PROCEDURE will either create a fresh new procedure or replace an existing definition, your call, main character energy only โœจ

so to even be allowed to define a procedure, you need 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. if you don't, it lands in the current schema. simple as that ๐Ÿซถ

the rule: the name of your new procedure must not match any existing procedure or function with the same input argument types in the same schema. but โ€” and this is the fun part โ€” procedures and functions of different argument types can share a name. that's called overloading, and it's literally so cool.

wanna replace the current definition of an existing procedure? use CREATE OR REPLACE PROCEDURE. but ngl, you cannot change the name or the argument types of a procedure this way. if you tried, you'd actually be creating a new, distinct procedure. sneaky, but that's the truth ๐Ÿ’€

when CREATE OR REPLACE PROCEDURE is used to replace an existing procedure, the ownership and permissions do not change. everything else about the procedure gets the values specified or implied in the command. and you must own the procedure to replace it (yes, that includes being a member of the owning role, bestie).

the user that creates the procedure becomes the owner of the procedure. ๐Ÿ‘‘

and to be able to create a procedure, you must have USAGE privilege on the argument types.

refer to Section 38.4 for the full tea on writing procedures ๐Ÿ“–

๐Ÿฆ‹ PARAMETERS ๐Ÿฆ‹

  • name โ€” the name of the procedure you're creating (optionally schema-qualified, obviously ๐Ÿ’…)
  • argmode โ€” the mode of an argument: IN, OUT, INOUT, or VARIADIC. if you just... don't say anything? the default is IN. easy.
  • argname โ€” the name of an argument. that's it, that's the whole thing ๐ŸŽ€
  • argtype โ€” the data type(s) of the procedure's arguments (optionally schema-qualified), if any. these can be base, composite, or domain types, or they can reference the type of a table column.

depending on the implementation language, you might also be allowed to specify โ€œpseudo-typesโ€ such as cstring. pseudo-types mean the actual argument type is either incompletely specified, or outside the set of ordinary SQL data types. mysterious. love that for them ๐ŸŒธ

a column's type is referenced by writing table_name.column_name%TYPE. and using this feature can sometimes help make a procedure independent of changes to the definition of a table. so handy. obsessed.

  • default_expr โ€” an expression used as the default value if the parameter isn't specified. it has to be coercible to the argument type of the parameter. important: all input parameters following a parameter with a default value must have default values as well. them's the rules ๐Ÿซถ
  • lang_name โ€” the name of the language the procedure is implemented in. it can be sql, c, internal, or the name of a user-defined procedural language, e.g., plpgsql. the default is sql if sql_body is specified.
โš ๏ธ enclosing the name in single quotes is deprecated and requires matching case. so like... don't. ๐Ÿ™ˆ
  • 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; see CREATE 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 doesn't know how to handle a type and no transform is supplied, it will fall back to a default behavior for converting data types โ€” but that depends on the implementation, so iykyk.
  • [EXTERNAL] SECURITY INVOKER / [EXTERNAL] SECURITY DEFINER โ€” SECURITY INVOKER indicates that the procedure is to be executed with the privileges of the user that calls it. that's the default, bb. SECURITY DEFINER specifies that the procedure is executed with the privileges of the user that owns it. spicy ๐Ÿ‘€

the key word EXTERNAL is allowed for SQL conformance, but it's optional since โ€” unlike in SQL โ€” this feature applies to all procedures, not only external ones.

๐Ÿšจ a SECURITY DEFINER procedure cannot execute transaction control statements (for example, COMMIT and ROLLBACK, depending on the language). no take-backs allowed.
  • configuration_parameter / value โ€” the SET 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. SET FROM CURRENT saves the value of the parameter that is current when CREATE PROCEDURE is executed as the value to be applied when the procedure is entered.

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. literally the plot twist of the century ๐Ÿ˜ญ

and 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 the full scoop on 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.

pro tip from me to you: it's 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. ๐Ÿ˜ฎโ€๐Ÿ’จ do yourself a favor.

  • obj_file, link_symbol โ€” this form of the AS 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 obj_file is the name of the shared library file containing the compiled C procedure, and is interpreted as for the LOAD command. the string link_symbol is 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's 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, we've all been there), start a new session. ๐Ÿ”„

  • sql_body โ€” the body of a LANGUAGE SQL procedure. it 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, and bestie they matter:

  • this form only works for LANGUAGE SQL, whereas the string constant form works for all languages.
  • this form is parsed at procedure definition time, while 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. ๐Ÿ’€ not the dangling procedures!
  • finally, this form is more compatible with the SQL standard and other SQL implementations. slay.

๐ŸŒŸ NOTES ๐ŸŒŸ

see CREATE FUNCTION (CREATE_FUNCTION(7)) for more details on function creation that also apply to procedures โ€” the lore is shared, bestie.

and use CALL(7) to actually execute a procedure. ๐Ÿƒโ€โ™€๏ธ


๐ŸŒธ EXAMPLES ๐ŸŒธ

first up, the string-constant way:

CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
AS $$
INSERT INTO tbl VALUES (a);
INSERT INTO tbl VALUES (b);
$$;

or โ€” and this is the BEGIN ATOMIC girlie:

CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
BEGIN ATOMIC
  INSERT INTO tbl VALUES (a);
  INSERT INTO tbl VALUES (b);
END;

and then you call it like this:

CALL insert_data(1, 2);

ate. no crumbs. ๐Ÿ”ฅ


๐Ÿ‘‘ COMPATIBILITY ๐Ÿ‘‘

a CREATE PROCEDURE command is defined in the SQL standard. the PostgreSQL implementation can be used in a compatible way, but it has many extensions. for details see also CREATE FUNCTION (CREATE_FUNCTION(7)). we're compatible and extra, it's giving range ๐ŸŒˆ


๐Ÿงš SEE ALSO ๐Ÿงš


okay that's the whole page, bestie!! ๐Ÿ“ this was CREATE PROCEDURE(7) from the PostgreSQL 16.15 Documentation, dated 2026, and i'm signing off โ€” go write some procedures, you absolute icon ๐Ÿ’œโœจ๐Ÿซถ

๐Ÿ“ฆ where this page comes from