V
V
Vadim2021-05-17 15:58:48
PostgreSQL
Vadim, 2021-05-17 15:58:48

What is better to use to find the number of active connections to the database?

Hey everyone

I saw this

postgres=> SELECT numbackends FROM pg_stat_database WHERE datname = 'my_super_db';


and this
postgres=> SELECT count(*) FROM pg_stat_activity WHERE datname = 'my_super_db;


how about your experience? PostgreSQL 12

all the best,
Vadim

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2021-05-17
@Viji

No difference. They have the same data source. You can also call pg_stat_get_db_numbackends(oid) directly.
In production, pg_stat_activity is usually used for monitoring to immediately pick up the grouping by backend statuses, the duration of active transactions. Well, most likely only the client backend will be interesting on this chart (well, maybe with parallel workers). For autovacuum processes, it is more convenient to have a separate schedule.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question