how to use SAVEPOINT in stored function

From: Frank(dot)Motzkat(at)ic3s(dot)de
To: pgsql-general(at)postgresql(dot)org
Subject: how to use SAVEPOINT in stored function
Date: 2005-12-05 14:39:59
Message-ID: OFF0C71065.258CBBBC-ONC12570CE.004FC312-C12570CE.00508C96@notes.ic3s.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


<br><font size=2 face="sans-serif">Hi community,</font>
<br>
<br><font size=2 face="sans-serif">I would like using savepoints in my
stored functions but I always get the error</font>
<br>
<br><font size=2 face="sans-serif"><b><i>ERROR: &nbsp;SPI_execute_plan
failed executing query &quot;SAVEPOINT my_savepoint&quot;: SPI_ERROR_TRANSACTION</i></b></font>
<br><font size=2 face="sans-serif"><b><i>CONTEXT: &nbsp;PL/pgSQL function
&quot;savepoint_test&quot; line 3 at SQL statement</i></b></font>
<br>
<br><font size=2 face="sans-serif">My test function can be found below.
I would be very grateful for any hint which brings progress to my developments
...</font>
<br>
<br><font size=2 face="sans-serif">-- create table</font>
<br><font size=2 face="sans-serif">CREATE TABLE testtable</font>
<br><font size=2 face="sans-serif">(</font>
<br><font size=2 face="sans-serif">&nbsp; name varchar(256),</font>
<br><font size=2 face="sans-serif">&nbsp; number int4 DEFAULT 1,</font>
<br><font size=2 face="sans-serif">&nbsp; id varchar(64) NOT NULL,</font>
<br><font size=2 face="sans-serif">&nbsp; CONSTRAINT pk_id PRIMARY KEY
(id)</font>
<br><font size=2 face="sans-serif">) </font>
<br><font size=2 face="sans-serif">WITHOUT OIDS;</font>
<br><font size=2 face="sans-serif">ALTER TABLE testtable OWNER TO postgres;</font>
<br>
<br><font size=2 face="sans-serif">-- insert dummy record</font>
<br><font size=2 face="sans-serif">insert into testtable (id,number) values
('id_1', 1);</font>
<br>
<br><font size=2 face="sans-serif">-- create test function</font>
<br><font size=2 face="sans-serif">CREATE OR REPLACE FUNCTION savepoint_test</font>
<br><font size=2 face="sans-serif">(</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; in_no integer, </font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; in_name varchar,</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; in_id varchar</font>
<br><font size=2 face="sans-serif">) RETURNS void</font>
<br><font size=2 face="sans-serif">AS $$</font>
<br><font size=2 face="sans-serif">BEGIN</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; BEGIN</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; SAVEPOINT my_savepoint;</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; DELETE FROM testtable WHERE number = in_no;</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; insert into testtable (id,number) values ('id_2',
2);</font>
<br><font size=2 face="sans-serif">-- &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;COMMIT;</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; RELEASE SAVEPOINT my_savepoint;</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; EXCEPTION</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; WHEN unique_violation &nbsp;THEN</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ROLLBACK TO my_savepoint;</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; END;</font>
<br><font size=2 face="sans-serif">END</font>
<br><font size=2 face="sans-serif">$$ LANGUAGE plpgsql;</font>
<br>
<br><font size=2 face="sans-serif">-- call test function</font>
<br><font size=2 face="sans-serif">select * from savepoint_test(1, CAST('test-1'
AS VARCHAR), CAST('id_1' AS VARCHAR));</font>
<br>
<br>
<br><font size=2 face="sans-serif">regards,</font>
<br>
<br><font size=2 face="sans-serif">frank</font>
<br>
<br>

Attachment Content-Type Size
unknown_filename text/html 3.7 KB

Browse pgsql-general by date

  From Date Subject
Next Message Timothy Perrigo 2005-12-05 14:42:47 unsubscribe
Previous Message Rich Doughty 2005-12-05 14:21:47 massive performance hit when using "Limit 1"