postgres
postgres copied to clipboard
allow to rollback a transaction gracefully
Currently, there's no way to roll back a transaction without throwing an exception inside of it.
I was hoping for something like sql.rollback()
That's a great idea!
@Newbie012 To achieve this effect with current functionality, you can just execute
tx`ROLLBACK;`
inside the transaction.
You'll see this warning when the transaction attempts to commit:
{
severity_local: 'WARNING',
severity: 'WARNING',
code: '25P01',
message: 'there is no transaction in progress',
file: 'xact.c',
line: '3898',
routine: 'EndTransactionBlock'
}
but it's harmless.
To avoid this message, if you are using Postgres 12+, you can simply run:
tx`ROLLBACK AND CHAIN;`
Which will immediately start a new transaction after the other is rolled back.
Thanks @apjoseph
I'm not in a hurry, so I guess I'll wait for official implementation. Thanks again for the workaround! 🙂