Refactor to rollback before doing anything, and use a manual commit.
This avoids warnings in the PostgreSQL log about a transaction already in progress.