Skip to content

Commit 227338b

Browse files
committed
Doc: improve introductory information about procedures.
Clarify the discussion in "User-Defined Procedures", by laying out the key differences between functions and procedures in a bulleted list. Notably, this avoids burying the lede about procedures being able to do transaction control. Make the back-link in the CREATE FUNCTION reference page more prominent, and add one in CREATE PROCEDURE. Per gripe from Guyren Howe. Thanks to David Johnston for discussion. Discussion: https://postgr.es/m/BYAPR03MB4903C53A8BB7EFF5EA289674A6949@BYAPR03MB4903.namprd03.prod.outlook.com
1 parent 3ebc6d2 commit 227338b

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

doc/src/sgml/ref/create_function.sgml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ CREATE [ OR REPLACE ] FUNCTION
100100
To be able to create a function, you must have <literal>USAGE</literal>
101101
privilege on the argument types and the return type.
102102
</para>
103+
104+
<para>
105+
Refer to <xref linkend="xfunc"/> for further information on writing
106+
functions.
107+
</para>
103108
</refsect1>
104109

105110
<refsect1>
@@ -578,12 +583,6 @@ CREATE [ OR REPLACE ] FUNCTION
578583
</varlistentry>
579584

580585
</variablelist>
581-
582-
<para>
583-
Refer to <xref linkend="xfunc"/> for further information on writing
584-
functions.
585-
</para>
586-
587586
</refsect1>
588587

589588
<refsect1 id="sql-createfunction-overloading">
@@ -661,8 +660,7 @@ CREATE FUNCTION foo(int, int default 42) ...
661660
<title>Examples</title>
662661

663662
<para>
664-
Here are some trivial examples to help you get started. For more
665-
information and examples, see <xref linkend="xfunc"/>.
663+
Add two integers using a SQL function:
666664
<programlisting>
667665
CREATE FUNCTION add(integer, integer) RETURNS integer
668666
AS 'select $1 + $2;'

doc/src/sgml/ref/create_procedure.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ CREATE [ OR REPLACE ] PROCEDURE
7676
To be able to create a procedure, you must have <literal>USAGE</literal>
7777
privilege on the argument types.
7878
</para>
79+
80+
<para>
81+
Refer to <xref linkend="xproc"/> for further information on writing
82+
procedures.
83+
</para>
7984
</refsect1>
8085

8186
<refsect1>

doc/src/sgml/xfunc.sgml

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363

6464
<para>
6565
Throughout this chapter, it can be useful to look at the reference
66-
page of the <xref linkend="sql-createfunction"/> command to
66+
page of the <link linkend="sql-createfunction"><command>CREATE
67+
FUNCTION</command></link> command to
6768
understand the examples better. Some examples from this chapter
6869
can be found in <filename>funcs.sql</filename> and
6970
<filename>funcs.c</filename> in the <filename>src/tutorial</filename>
@@ -81,21 +82,55 @@
8182
</indexterm>
8283

8384
<para>
84-
A procedure is a database object similar to a function. The difference is
85-
that a procedure does not return a value, so there is no return type
86-
declaration. While a function is called as part of a query or DML
87-
command, a procedure is called in isolation using
88-
the <link linkend="sql-call"><command>CALL</command></link> command. If the <command>CALL</command> command is not
89-
part of an explicit transaction, a procedure in many server-side
90-
languages can commit, rollback, and begin new transactions during
91-
its execution, which is not possible in functions.
85+
A procedure is a database object similar to a function.
86+
The key differences are:
87+
88+
<itemizedlist>
89+
<listitem>
90+
<para>
91+
Procedures are defined with
92+
the <link linkend="sql-createprocedure"><command>CREATE
93+
PROCEDURE</command></link> command, not <command>CREATE
94+
FUNCTION</command>.
95+
</para>
96+
</listitem>
97+
<listitem>
98+
<para>
99+
Procedures do not return a function value; hence <command>CREATE
100+
PROCEDURE</command> lacks a <literal>RETURNS</literal> clause.
101+
However, procedures can instead return data to their callers via
102+
output parameters.
103+
</para>
104+
</listitem>
105+
<listitem>
106+
<para>
107+
While a function is called as part of a query or DML command, a
108+
procedure is called in isolation using
109+
the <link linkend="sql-call"><command>CALL</command></link> command.
110+
</para>
111+
</listitem>
112+
<listitem>
113+
<para>
114+
A procedure can commit or roll back transactions during its
115+
execution (then automatically beginning a new transaction), so long
116+
as the invoking <command>CALL</command> command is not part of an
117+
explicit transaction block. A function cannot do that.
118+
</para>
119+
</listitem>
120+
<listitem>
121+
<para>
122+
Certain function attributes, such as strictness, don't apply to
123+
procedures. Those attributes control how the function is
124+
used in a query, which isn't relevant to procedures.
125+
</para>
126+
</listitem>
127+
</itemizedlist>
92128
</para>
93129

94130
<para>
95-
The explanations on how to define user-defined functions in the rest of
96-
this chapter apply to procedures as well, except that
97-
the <link linkend="sql-createprocedure"><command>CREATE PROCEDURE</command></link> command is used instead, there is
98-
no return type, and some other features such as strictness don't apply.
131+
The explanations in the following sections about how to define
132+
user-defined functions apply to procedures as well, except for the
133+
points made above.
99134
</para>
100135

101136
<para>

0 commit comments

Comments
 (0)