handle postgresql transaction errors

The current DatabaseError handling does not work well with postgresql since a database error on the main transaction will fail all consequent database queries.
Executing the query in it's own transaction and eventually rolling it back solves this issue.