1- <!-- $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.137 2009/04 /2716:27:36 momjian Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.138 2009/05 /2701:18:06 tgl Exp $ -->
22
33 <sect1 id="xfunc">
44 <title>User-Defined Functions</title>
@@ -1177,6 +1177,12 @@ CREATE FUNCTION test(int, int) RETURNS int
11771177 <literal>timeofday()</>.
11781178 </para>
11791179
1180+ <para>
1181+ Another important example is that the <function>current_timestamp</>
1182+ family of functions qualify as <literal>STABLE</>, since their values do
1183+ not change within a transaction.
1184+ </para>
1185+
11801186 <para>
11811187 There is relatively little difference between <literal>STABLE</> and
11821188 <literal>IMMUTABLE</> categories when considering simple interactive
@@ -1192,16 +1198,35 @@ CREATE FUNCTION test(int, int) RETURNS int
11921198 </para>
11931199
11941200 <para>
1195- Because of the snapshotting behavior of MVCC (see <xref linkend="mvcc">)
1201+ For functions written in SQL or in any of the standard procedural
1202+ languages, there is a second important property determined by the
1203+ volatility category, namely the visibility of any data changes that have
1204+ been made by the SQL command that is calling the function. A
1205+ <literal>VOLATILE</> function will see such changes, a <literal>STABLE</>
1206+ or <literal>IMMUTABLE</> function will not. This behavior is implemented
1207+ using the snapshotting behavior of MVCC (see <xref linkend="mvcc">):
1208+ <literal>STABLE</> and <literal>IMMUTABLE</> functions use a snapshot
1209+ established as of the start of the calling query, whereas
1210+ <literal>VOLATILE</> functions obtain a fresh snapshot at the start of
1211+ each query they execute.
1212+ </para>
1213+
1214+ <note>
1215+ <para>
1216+ Functions written in C can manage snapshots however they want, but it's
1217+ usually a good idea to make C functions work this way too.
1218+ </para>
1219+ </note>
1220+
1221+ <para>
1222+ Because of this snapshotting behavior,
11961223 a function containing only <command>SELECT</> commands can safely be
11971224 marked <literal>STABLE</>, even if it selects from tables that might be
11981225 undergoing modifications by concurrent queries.
1199- <productname>PostgreSQL</productname> will execute a <literal>STABLE</>
1200- function using the snapshot established for the calling query, and so it
1201- will see a fixed view of the database throughout that query.
1202- Also note
1203- that the <function>current_timestamp</> family of functions qualify
1204- as stable, since their values do not change within a transaction.
1226+ <productname>PostgreSQL</productname> will execute all commands of a
1227+ <literal>STABLE</> function using the snapshot established for the
1228+ calling query, and so it will see a fixed view of the database throughout
1229+ that query.
12051230 </para>
12061231
12071232 <para>
@@ -1225,14 +1250,14 @@ CREATE FUNCTION test(int, int) RETURNS int
12251250 <para>
12261251 Before <productname>PostgreSQL</productname> release 8.0, the requirement
12271252 that <literal>STABLE</> and <literal>IMMUTABLE</> functions cannot modify
1228- the database was not enforced by the system.Release 8.0enforces it
1253+ the database was not enforced by the system.Releases 8.0and later enforce it
12291254 by requiring SQL functions and procedural language functions of these
12301255 categories to contain no SQL commands other than <command>SELECT</>.
12311256 (This is not a completely bulletproof test, since such functions could
12321257 still call <literal>VOLATILE</> functions that modify the database.
12331258 If you do that, you will find that the <literal>STABLE</> or
12341259 <literal>IMMUTABLE</> function does not notice the database changes
1235- applied by the called function.)
1260+ applied by the called function, since they are hidden from its snapshot .)
12361261 </para>
12371262 </note>
12381263 </sect1>