1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.137 2009/04 /27 16:27:36 momjian Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.138 2009/05 /27 01:18:06 tgl Exp $ -->
2
2
3
3
<sect1 id="xfunc">
4
4
<title>User-Defined Functions</title>
@@ -1177,6 +1177,12 @@ CREATE FUNCTION test(int, int) RETURNS int
1177
1177
<literal>timeofday()</>.
1178
1178
</para>
1179
1179
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
+
1180
1186
<para>
1181
1187
There is relatively little difference between <literal>STABLE</> and
1182
1188
<literal>IMMUTABLE</> categories when considering simple interactive
@@ -1192,16 +1198,35 @@ CREATE FUNCTION test(int, int) RETURNS int
1192
1198
</para>
1193
1199
1194
1200
<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,
1196
1223
a function containing only <command>SELECT</> commands can safely be
1197
1224
marked <literal>STABLE</>, even if it selects from tables that might be
1198
1225
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.
1205
1230
</para>
1206
1231
1207
1232
<para>
@@ -1225,14 +1250,14 @@ CREATE FUNCTION test(int, int) RETURNS int
1225
1250
<para>
1226
1251
Before <productname>PostgreSQL</productname> release 8.0, the requirement
1227
1252
that <literal>STABLE</> and <literal>IMMUTABLE</> functions cannot modify
1228
- the database was not enforced by the system. Release 8.0 enforces it
1253
+ the database was not enforced by the system. Releases 8.0 and later enforce it
1229
1254
by requiring SQL functions and procedural language functions of these
1230
1255
categories to contain no SQL commands other than <command>SELECT</>.
1231
1256
(This is not a completely bulletproof test, since such functions could
1232
1257
still call <literal>VOLATILE</> functions that modify the database.
1233
1258
If you do that, you will find that the <literal>STABLE</> or
1234
1259
<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 .)
1236
1261
</para>
1237
1262
</note>
1238
1263
</sect1>
0 commit comments