Thread: Transaction manipulation commands in user defined c functions?

Transaction manipulation commands in user defined c functions?

From
Michael Hadjivassiliou
Date:
I've been searching for a way to do transaction blocks and savepoints within a user defined c function. Apparently the docs state that if an SPI function throws an error, control goes out of the function and the current transaction is rolled back, yet it hints that you can do subtransactions within a c function.

a. Is that possible?
b. I couldn't find any doc or reference to such a feature.

Has anyone got an example doing this? Can you please just point me to the right direction?

Michael.
#avg_ls_inline_popup { position:absolute; z-index:9999; padding: 0px 0px; margin-left: 0px; margin-top: 0px; width: 240px; overflow: hidden; word-wrap: break-word; color: black; font-size: 10px; text-align: left; line-height: 13px;}

Re: Transaction manipulation commands in user defined c functions?

From
Tom Lane
Date:
Michael Hadjivassiliou <sporegnosis@gmail.com> writes:
> I've been searching for a way to do transaction blocks and savepoints within
> a user defined c function. Apparently the docs state that if an SPI function
> throws an error, control goes out of the function and the current
> transaction is rolled back, yet it hints that you can do subtransactions
> within a c function.

Well, sure.  Look at the way that plpgsql implements a BEGIN/EXCEPTION
block, for example.  You need to establish a subtransaction and have
a TRY/CATCH block to catch the error and clean up.

But having said that ... it's pretty messy and I wonder why you are
wanting to do this in a C function.  Your code would be a lot more
future-proof in plpgsql, say.  And if you're invoking operations as
heavyweight as a subtransaction, you're certainly not going to get any
noticeable performance win from using C.

            regards, tom lane

Re: Transaction manipulation commands in user defined c functions?

From
Michael Hadjivassiliou
Date:
Unfortunately part of the code must be in c. I could follow a hybrid approach
by coding the stuff surrounded by the transaction blocks in c functions and the rest in pl/pgsql,
however that approach is not elegant imho.

I can understand that the c api for managing transactions might change in the future. r there plans for it to become "standardized" at some point?


>Well, sure.  Look at the way that plpgsql implements a BEGIN/EXCEPTION
>block, for example.  You need to establish a subtransaction and have
>a TRY/CATCH block to catch the error and clean up.

can you suggest some .c files or links to have a look?

Thanks.

On Wed, Jun 9, 2010 at 7:14 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Michael Hadjivassiliou <sporegnosis@gmail.com> writes:
> I've been searching for a way to do transaction blocks and savepoints within
> a user defined c function. Apparently the docs state that if an SPI function
> throws an error, control goes out of the function and the current
> transaction is rolled back, yet it hints that you can do subtransactions
> within a c function.

Well, sure.  Look at the way that plpgsql implements a BEGIN/EXCEPTION
block, for example.  You need to establish a subtransaction and have
a TRY/CATCH block to catch the error and clean up.

But having said that ... it's pretty messy and I wonder why you are
wanting to do this in a C function.  Your code would be a lot more
future-proof in plpgsql, say.  And if you're invoking operations as
heavyweight as a subtransaction, you're certainly not going to get any
noticeable performance win from using C.

                       regards, tom lane

#avg_ls_inline_popup { position:absolute; z-index:9999; padding: 0px 0px; margin-left: 0px; margin-top: 0px; width: 240px; overflow: hidden; word-wrap: break-word; color: black; font-size: 10px; text-align: left; line-height: 13px;}