Behaviour of sql language function

Started by Marcelo Lacerdaalmost 14 years ago3 messagesdocs
Jump to latest
#1Marcelo Lacerda
marceloslacerda@gmail.com

Hi all I noticed the following beaviour, if a command in a sql function
fails the other statements fail like if the entire function where
surrounded by a begin-commit block. While I find this behaviour nice, as
far as I could find, this behaviour is not documented[1]http://www.postgresql.org/docs/9.1/static/sql-createfunction.html. I wished to
create a function that depends on such way of working but I cannot trust
that sql functions will continue to work that way on the next postgresql
versions.

So, is it documented somewhere?

I'm linking to a pastebin file that exemplifies the behaviour.

[1]: http://www.postgresql.org/docs/9.1/static/sql-createfunction.html
[2]: http://pastebin.com/LWfFgH7L -- Marcelo Lacerda
--
Marcelo Lacerda

#2Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: Marcelo Lacerda (#1)
Re: Behaviour of sql language function

Marcelo Sena <marceloslacerda@gmail.com> wrote:

So, is it documented somewhere?

http://www.postgresql.org/docs/current/static/tutorial-transactions.html

| PostgreSQL actually treats every SQL statement as being executed
| within a transaction. If you do not issue a BEGIN command, then
| each individual statement has an implicit BEGIN and (if
| successful) COMMIT wrapped around it.

Combine this with the fact that a function can only run in the
context of a command, like:

SELECT func_name();

You have your guarantee -- as long as you understand the possible
action of subtransactions (like savepoints).

-Kevin

#3Marcelo Lacerda
marceloslacerda@gmail.com
In reply to: Kevin Grittner (#2)
Re: Behaviour of sql language function

Oh, nice, I haven't noticed that fact. Thanks!

--
Marcelo Lacerda

On Thu, Apr 26, 2012 at 5:53 PM, Kevin Grittner <Kevin.Grittner@wicourts.gov

Show quoted text

wrote:

Marcelo Sena <marceloslacerda@gmail.com> wrote:

So, is it documented somewhere?

http://www.postgresql.org/docs/current/static/tutorial-transactions.html

| PostgreSQL actually treats every SQL statement as being executed
| within a transaction. If you do not issue a BEGIN command, then
| each individual statement has an implicit BEGIN and (if
| successful) COMMIT wrapped around it.

Combine this with the fact that a function can only run in the
context of a command, like:

SELECT func_name();

You have your guarantee -- as long as you understand the possible
action of subtransactions (like savepoints).

-Kevin