Functions, transactions and RETURN

From: "Stewart Ben (RBAU/EQS4) *" <Ben(dot)Stewart(at)au(dot)bosch(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Functions, transactions and RETURN
Date: 2005-09-23 01:50:49
Message-ID: E253BDD7F008244585AEE87AF8F0224F116C7A54@cl-mail01.au.bosch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

After reading up on Postgres documentation, it seems that transactions
and savepoints are not available to functions, and savepoints are
implemented via BEGIN.. EXCEPTION.. END blocks.

I have a function returning an int4 with the following proposed
structure:

----------------------------------------------------------------------
BEGIN
-- Start the transaction, lock tables
SAVEPOINT start;

LOCK TABLE backend.table IN ACCESS EXCLUSIVE MODE;
LOCK TABLE backend.table2 IN SHARE MODE;

-- Check data in another table.
IF NOT tt_check_table2(var1) THEN
ROLLBACK TO SAVEPOINT start;
RETURN -1; -- E_NO_ACCESS
END IF;

-- Check data in this table.
IF tt_check_table(var2) THEN
ROLLBACK TO SAVEPOINT start;
RETURN -2000; -- E_DUP_COURSE
END IF;

-- <snipped more checks>

-- Insert the row
BEGIN
INSERT INTO backend.table
(foo, bar, baz)
VALUES (1, 2, 3);

-- Success
RETURN 0;

EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT start;
RETURN -32767; -- E_UNKNOWN
END;

ROLLBACK TO SAVEPOINT start;

END;
----------------------------------------------------------------------

I can't see how to implement both savepoints and returning distinct
values with the BEGIN.. RAISE EXCEPTION.. EXCEPTION.. END method
proposed in previous mailing list posts.

Are there any suggestions on how to implement this?

Best regards,

Ben Stewart

--
Robert Bosch (Australia) Pty. Ltd.
Engineering Quality Services, Software Engineer (RBAU/EQS4)
Locked Bag 66 - Clayton South, VIC 3169 - AUSTRALIA
mailto:ben(dot)stewart(at)au(dot)bosch(dot)com
http://www.bosch.com.au/

Browse pgsql-sql by date

  From Date Subject
Next Message Stewart Ben (RBAU/EQS4) * 2005-09-23 05:55:54 Re: Functions, transactions and RETURN
Previous Message Russell Simpkins 2005-09-23 01:34:11 Re: Primary and Foreign Key?