Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Postgres, what does pg_stat_database.xact_commit actually mean?

Tags:

postgresql

I'm trying to understand SELECT xact_commit FROM pg_stat_database; According to docs, it is "Number of transactions in this database that have been committed". But I turned on logging all queries (log_min_duration = 0) and it seems there are other things besides that can affect xact_commit than just a query. For example, connecting a psql client or typing BEGIN; will increase it by various values. There is a step in my application that runs a single query (as confirmed by the log), but consistently increases the counter by 15-20. Does anyone know anything more specific about what is counted in xact_commit, or if there is a way to count only actual queries?

like image 355
Jonathan Crosmer Avatar asked Jun 26 '26 03:06

Jonathan Crosmer


1 Answers

pg_stat_database.xact_commit really is the number of commits in the database (remember that every statement that is not run in a transaction block actually runs in its own little transaction, so it will cause a commit).

The mystery that remains to be solved is why you see more commits than statements, which seems quite impossible (For example, BEGIN starts a transaction, so by definition it cannot increase xact_commit).

The solution is probably that database activity statistics are collected asynchronously: they are sent to the statistics collector process via an UDP socket, and the statistics collector eventually updates the statistics.

So my guess is that the increased transaction count you see is actually from earlier activities.

Try keeping the database absolutely idle for a while and then try again, then you should see a slower increase.

like image 148
Laurenz Albe Avatar answered Jun 28 '26 18:06

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!