PostgreSQL, get statements of a running transaction If I execute a transaction like:

begin;
update test set a=2;                                                                                           
select pg_sleep(9999);


I will get a RowExclusiveLock on test.

If I try to see which query is in that pid pg_sleep will be shown (because it is the currently running query).

Is there any option to get the query that actually create the lock?

Or maybe a query to get all the statements of the running transaction?
No, you can't do that.  Unless you have very verbose logging turned on (like log_statement=all) and can mine that data out of the server log file.

I think it would be a nice feature to optionally store some limited amount of data (like 4096 bytes, but configurable) of "prior transaction context" for each backend and then exposing it through pg_stat_actvity, and maybe logging it for log_lock_waits or deadlock errors.  But as far as I know there is no active work going on to add such a feature.