Re: can you do rollback in a trigger function?

From: Shaun Thomas <sthomas(at)optionshouse(dot)com>
To: Quang Thoi <Quang_Thoi(at)symantec(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: can you do rollback in a trigger function?
Date: 2013-10-15 19:18:29
Message-ID: 525D9505.1030209@optionshouse.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/15/2013 01:38 PM, Quang Thoi wrote:

> I want to roll back deletion if there is a reference (FK) in another
> table.
>
> Can I explicitly call a rollback inside a function?

No. Transactions are controlled outside the body of any executing
function. You do have have a couple other options, though. If you can't
use a basic foreign key which does this automatically, you can have your
trigger function return NULL. From the documentation:

"Row-level triggers fired BEFORE can return null to signal the trigger
manager to skip the rest of the operation for this row (i.e., subsequent
triggers are not fired, and the INSERT/UPDATE/DELETE does not occur for
this row)"

You could also raise an explicit error so the user sees something. To
fake a foreign key violation, you'd do:

RAISE EXCEPTION foreign_key_violation;

So you don't need a rollback anyway.

--
Shaun Thomas
OptionsHouse | 141 W. Jackson Blvd. | Suite 500 | Chicago IL, 60604
312-676-8870
sthomas(at)optionshouse(dot)com

______________________________________________

See http://www.peak6.com/email_disclaimer/ for terms and conditions related to this email

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Carlo Curatolo 2013-10-15 20:21:32 Re: Invalid Page Header Error
Previous Message Vick Khera 2013-10-15 19:00:10 Re: can you do rollback in a trigger function?