SPI: Correct way to rollback a subtransaction?

From: "Marko Kreen" <markokr(at)gmail(dot)com>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: SPI: Correct way to rollback a subtransaction?
Date: 2006-02-20 14:46:30
Message-ID: e51f66da0602200646q4ae9cc3rfac81cc4c6f8a5b4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I want to update one row from possibly several backends. In case
of SERIALIZABLE transactions, the update command may fail. To
hide it from calling transaction, I use a subtransaction and try
to catch and hide the error.

With help of plpgsql source, I wrote following code that _seems_
to work. But I have no idea if it's the correct way to do it:

/* store old state */
MemoryContext oldcontext = CurrentMemoryContext;
ResourceOwner oldowner = CurrentResourceOwner;

BeginInternalSubTransaction(NULL);
res = SPI_connect();
if (res < 0)
elog(ERROR, "cannot connect to SPI");

PG_TRY();
{
res = SPI_execute("update one row", false, 0);
SPI_finish();
ReleaseCurrentSubTransaction();
}
PG_CATCH();
{
SPI_finish();
RollbackAndReleaseCurrentSubTransaction();
FlushErrorState();
res = -1; /* remember failure */
}
PG_END_TRY();

/* restore old state */
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;

I am suspicious about the ..SubTransaction and SPI_* nesting
and resetting the error state. Can anyone look if it's correct?

Goal of the exercise is to have 8-byte transaction ID's and snapshots
externally available. (Port of xxid module from Slony). Above code
does the update of the 'epoch' table that has only one row.

--
marko

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2006-02-20 14:54:16 Re: new feature: LDAP database name resolution
Previous Message Tom Lane 2006-02-20 14:45:39 Re: AC_REPLACE_FUNCS([getaddrinfo]) in 8.1.3