正确查询以获取 PostgreSQL 数据库中的当前连接数
以下两个哪个更准确?
select numbackends from pg_stat_database;
select count(*) from pg_stat_activity;
Which of the following two is more accurate?
select numbackends from pg_stat_database;
select count(*) from pg_stat_activity;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这两个查询并不等同。第一个版本的等效版本是:
在这种情况下,我希望该版本比第二个版本稍快,因为它需要计算的行数较少。但您不太可能能够衡量差异。
两个查询都基于完全相同的数据,因此它们同样准确。
Those two queries aren't equivalent. The equivalent version of the first one would be:
In that case, I would expect that version to be slightly faster than the second one, simply because it has fewer rows to count. But you are not likely going to be able to measure a difference.
Both queries are based on exactly the same data, so they will be equally accurate.
以下查询非常有帮助
The following query is very helpful
他们肯定会给出不同的结果。更好的是
因为它包含与 WAL 发送进程的连接,这些连接被视为常规连接并计入
max_connections
。请参阅 max_wal_senders
They definitely may give different results. The better one is
It's because it includes connections to WAL sender processes which are treated as regular connections and count towards
max_connections
.See max_wal_senders
根据状态聚合所有 postgres 会话(有多少处于空闲状态、有多少在做某事...)
Aggregation of all postgres sessions per their status (how many are idle, how many doing something...)
从源代码来看,pg_stat_database 查询似乎为您提供了所有用户与当前数据库的连接数。另一方面,pg_stat_activity 查询仅为查询用户提供当前数据库的连接数。
From looking at the source code, it seems like the pg_stat_database query gives you the number of connections to the current database for all users. On the other hand, the pg_stat_activity query gives the number of connections to the current database for the querying user only.