I am creating a Postgres function where I want to insert data into multiple tables and if any of them get failed then I want to rollback the transaction. But while writing the code begin transaction; in my function it says below error: ERROR: syntax error at or near "transaction"
1 Answer
PL/PgSQL functions cannot begin or commit transactions.
You appear to want autonomous transactions, but they are not supported.
Perhaps you can do what you need with a PL/PgSQL BEGIN ... EXCEPTION ... block. You can abort the (sub)transaction with a RAISE command to raise an error, instead of ROLLBACK.