Doing something at commit/rollback

As of this writing, there is no possibility to define a trigger function which is executed ON COMMIT or ON ROLLBACK. However, if you really need to have some code executed on these database events, you have a possibility to register a C-language function to be called on these events. Unfortunately, this registration cannot be done in a permanent way like triggers, but the registration function has to be called each time a new connection starts:

RegisterXactCallback(my_xact_callback, NULL); 

Use grep -r RegisterXactCallback in the contrib/ directory of PostgreSQL's source code to find files with examples of actual callback functions.

Get PostgreSQL Server Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.