PostgreSQL – How to Ensure pg_stat_activity Shows All Queries

postgresqlstatistics

I am working with pg_stat_activity in postgres. It is working and I can see queries in this view after clicking the play button in datagrip. However, some queries I definitely executed (see screenshot, and look at the first query) are not shown at all. I need statistics of them (so really all my queries). How to ensure pg_stat_activity shows all my queries? Do I need to add additional settings?queries in datagrip

Best Answer

pg_stat_activity is not an auditing facility. It only shows one query per connected session. Either the query which is currently executing, or if the connection is idle than the immediately prior query.

If you want to see historical queries, use a different tool. Maybe pg_stat_statements, or log_statement=all. Or any of a large number of third-party tools.