Reputation: 1776
I have situation in which I have to commit a portion of code as transaction of its own.
I have created a table subtransaction_tbl
:
CREATE TABLE subtransaction_tbl
(
entryval integer
)
And a function in language plpython3u:
CREATE FUNCTION subtransaction_nested_test_t() RETURNS void
AS $$
plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
with plpy.subtransaction():
plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)")
$$ LANGUAGE plpython3u;
First situation:
BEGIN TRANSACTION;
INSERT INTO subtransaction_tbl VALUES (4);
select subtransaction_nested_test_t();
COMMIT TRANSACTION;
Entries in table are correct: 1,2,4
Second situation:
BEGIN TRANSACTION;
INSERT INTO subtransaction_tbl VALUES (4);
select subtransaction_nested_test_t();
ROLLBACK TRANSACTION;
Values in the table are not populated
I expected 1
or 2
should be added to table subtransaction_tbl
but to my surprise no value was inserted. I imagined a new subtransaction was opened by the function and it should not depend upon the parent transaction. Please let me know if I am right or not.
Are there autonomous transactions in Postgres? Or do I have to modify my plpython3u function?
Upvotes: 41
Views: 63390
Reputation: 3157
Here is a patch with support of Autonomous transactions; it's a work-in-progress. If you want, you may patch Postgres master source code and compile.
Here's a video at FOSSAsia with the description.
The patch introduces Autonomous Transactions for PL/pgSQL.
It adds pragma AUTONOMOUS_TRANSACTION
to functions.
When one such function is executed all (at the current time not all,
WIP) statements from it are executed in an autonomous session.
Example SQL-request:
CREATE TABLE tbl (a int);
CREATE OR REPLACE FUNCTION func() RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO tbl VALUES (1);
END;
$$;
START TRANSACTION;
SELECT func();
ROLLBACK;
SELECT * FROM tbl;
DROP FUNCTION func;
DROP TABLE tbl;
Output:
a
---
1
(1 row)
For each backend the patch lazily creates a pool of autonomous sessions. When backend calls autonomous function, backend takes one autonomous session from this pool and sends there function's statements for execution. When execution is finished backend returns session to pool. Lazily means that pool is created only when first autonomous session is needed. Backend and autonomous session communicate with the help of Postgres client-server protocol. Messages are sent through dynamic shared memory.
Execution of backend and autonomous session is synchronous: autonomous session waits for messages from backend and backend waits for messages from autonomous session.
Upvotes: 1
Reputation: 25860
Postgres does support nested transactions, but they differ from the conventional SQL, more like transactions with nested partial points.
On the top level you always have your typical BEGIN/COMMIT/ROLLBACK
, and on nested levels you have to use the following commands:
SAVEPOINT name
- creates a new savepoint, with name unique for the transactionRELEASE SAVEPOINT name
- commits the savepoint, though it will only persist if the containing transaction commitsROLLBACK TO SAVEPOINT name
- rolls back the savepointYou would also have to make sure that:
SAVEPOINT
are unique;SAVEPOINT
is propagated upwards to the top level.The last bit is a bit tricky, unless you use a library that can do that for you automatically.
When I wrote pg-promise, I made sure that those two provisions are guaranteed:
sp_xy
, where x
is the current task/transaction depth, and y
is the actual transaction level + 1;ROLLBACK TO SAVEPOINT name
, plus the top-level ROLLBACK
in case a child transaction fails - all built on the standard promise-chaining logic.See also the limitations of the PostgreSQL nested transactions explained...
Upvotes: 44
Reputation: 657052
There are no autonomous transactions in Postgres before Postgres 11, where SQL procedures were added. See:
Everything that's done in a function is committed or rolled back with the transaction.
In Postgres 10 or older a workaround could be to (ab-)use dblink:
There is also the related concept of a SAVEPOINT
. (Not the same thing!):
plpython has subtransactions (with plpy.subtransaction():
), but that's not the same as autonomous transactions. There is no separate COMMIT
. All it does, is bundle a couple of statements together to make them atomic. Without that, if an exception occurs somewhere in the middle, and you catch that exception, only the code up to this exception would be executed. If you wrap it into a subtransaction, it's all or nothing. This is like using a SAVEPOINT
, not an autonomous transaction. The manual:
The subtransaction context manager does not trap errors, it only assures that all database operations executed inside its scope will be atomically committed or rolled back.
Here is a discussion of the feature:
Upvotes: 32