Skip site navigation (1) Skip section navigation (2)

Re: Transactions within a function body

From: "Bob Henkel" <bob(dot)henkel(at)gmail(dot)com>
To: "Alvaro Herrera" <alvherre(at)commandprompt(dot)com>
Cc: "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>, "PGSQL Hackers" <pgsql-hackers(at)postgresql(dot)org>, "Reg Me Please" <regmeplease(at)gmail(dot)com>, "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>, "Dennis Brakhane" <brakhane(at)googlemail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Transactions within a function body
Date: 2008-10-02 15:46:25
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-generalpgsql-hackers
Have you looked at creating a function in perl and creating a new
connection? Or using a dblink query which can create a new connection?
These two methods work. I have used them to insert to a log table regardless
of the parent transaction being commited or rolled back.

A old example I posted of using pl/perl can be found here ->

The key is opening a new session which using dblink or pl/perl dbi
connection will do. This is not ideal or efficient.  It would be nice if you
could just do autonomous transactions natively in pl/pgsql, but I find this
method works for the cases where you need it(logging, huge batch processing
tasks where it's not ideal to process everything in one transaction).


"Hi all.
Is there a way to have (sub)transactions within a function body?
I'd like to execute some code (a transaction!) inside a function and later
decide whether that transaction is to be committed or not.

On Thu, Oct 2, 2008 at 10:40 AM, Alvaro Herrera

> Gurjeet Singh escribió:
> > I have seen this feature being asked for, and this work-around suggested
> so
> > many times. If plpgql does it internally, why not provide a clean
> interface
> > for this? Is there some road-block, or that nobody has ever tried it?
> Initially we aimed at just exposing SAVEPOINT and ROLLBACK TO in
> functions, but ran into the problem that the SPI stack needs to be dealt
> with appropriately and you can't do it if the user is able to modify it
> arbitrarily by calling transaction-modifying commands.  That's when the
> EXCEPTION idea came up.  We never went back and studied whether we could
> have fixed the SPI limitation, but it's not trivial.
> --
> Alvaro Herrera
> <>
> The PostgreSQL Company - Command Prompt, Inc.
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:

In response to


pgsql-hackers by date

Next:From: Robert TreatDate: 2008-10-02 15:57:30
Subject: Re: Block-level CRC checks
Previous:From: Alvaro HerreraDate: 2008-10-02 15:40:22
Subject: Re: Transactions within a function body

pgsql-general by date

Next:From: Tom LaneDate: 2008-10-02 15:56:40
Subject: Re: tsearch 2 query
Previous:From: Alvaro HerreraDate: 2008-10-02 15:40:22
Subject: Re: Transactions within a function body

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group