Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgREST Transactions

Tags:

postgrest

I am using PostgREST to expose DB entities to a Springboot app which consumes those.

I have two entities inside my DB which are Person and City.

I would like to save the Person entity and the City at the same time, if any of those two fails I would like the other one to not persist on the PostgREST.

I would like to achieve Transactional behaviour but on PostgREST. Is there any chance to achieve this natively from the tool or without programmatically delete the just created record on exception?

like image 664
OEH Avatar asked Mar 21 '26 05:03

OEH


1 Answers

You could create a PL/pgSQL procedure that receives data from City and Person, and insert on both tables, and calls a ROLLBACK if anything fails. Here are the docs with some examples: https://www.postgresql.org/docs/11/plpgsql-transactions.html

Postgrest will expose the procedure in the /rpc/{function_name} endpoint. Here are the docs: https://postgrest.org/en/stable/api.html#stored-procedures

like image 65
Rodrigo Avatar answered Mar 23 '26 23:03

Rodrigo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!