postgres icon indicating copy to clipboard operation
postgres copied to clipboard

allow to rollback a transaction gracefully

Open Newbie012 opened this issue 3 years ago • 3 comments

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()

Newbie012 avatar Oct 08 '22 17:10 Newbie012

That's a great idea!

porsager avatar Oct 12 '22 19:10 porsager

@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.

apjoseph avatar Oct 13 '22 15:10 apjoseph

Thanks @apjoseph

I'm not in a hurry, so I guess I'll wait for official implementation. Thanks again for the workaround! 🙂

Newbie012 avatar Oct 13 '22 16:10 Newbie012