Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested transactions in postgres #17

Open
bjg2 opened this issue May 27, 2020 · 1 comment
Open

Nested transactions in postgres #17

bjg2 opened this issue May 27, 2020 · 1 comment

Comments

@bjg2
Copy link

bjg2 commented May 27, 2020

Hi, thanks for this lib as this is really needed.

Looking at the code, I'm not sure I understand how nested transactions work... As far as I understand, Beginx() uses the same Node object and just does a savepoint and overrides the last savepoint that happened?

If I understand code correctly, below code with 2 savepoints would not work?

create table testtable (
	field1 text,
	field2 int
)

begin transaction

insert into testtable (field1, field2) values ('edsa', 1)

SAVEPOINT sp_1;

insert into testtable (field1, field2) values ('eds22a', 13)

SAVEPOINT sp_2;

insert into testtable (field1, field2) values ('eds22eea', 123)

-- 3 rows
select * from testtable

ROLLBACK TO SAVEPOINT sp_2;

-- 2 rows
select * from testtable

ROLLBACK TO SAVEPOINT sp_1;

-- 1 row
select * from testtable

rollback transaction
@skateinmars
Copy link
Member

skateinmars commented Feb 23, 2022

Hello, and sorry for the very late reply!

Indeed, the savepoint ID is overridden

sqalx/sqalx.go

Line 147 in bc03be6

n.savePointID = "sp_" + strings.Replace(uuids.Hex128(), "-", "_", -1)

So multiple savepoints will not work, the second time Rollback will get called it will roll back the entire transaction instead of rollbacking to the previous savepoint.

I think this could be solved by using a slice of savepoints?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants