How to check for pending operations in a PostgreSQL transaction

Viewed 41528

I have a session (SQLAlchemy) on PostgreSQL, with an active uncommitted transaction. I have just passed the session to some call tree that may or may not have issued SQL INSERT/UPDATE/DELETE statements, through sqlalchemy.orm or directly through the underlying connection.

Is there a way to check whether there are any pending data-modifying statements in this transaction? I.e. whether commit would be a no-op or not, and whether rollback would discard something or not?

I've seen people point out v$transaction in Oracle for the same thing (see this SO question). I'm looking for something similar to use on PostgreSQL.

4 Answers

Since Postgres 10:

select txid_current_if_assigned();

will return null if there is no current transaction.

If a Start Transaction has been issued, it will still return null if there have been no updates.

Related