postgresql 如何使用 pg_stat_activity?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17654033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:17:16  来源:igfitidea点击:

How to use pg_stat_activity?

debuggingpostgresql

提问by Richard

I'd like to see which queries are being executed on a live Django application, and how much memory they are taking up. I have read that pg_stat_activitycan be useful to monitor a Postgres database.

我想看看在实时 Django 应用程序上正在执行哪些查询,以及它们占用了多少内存。我读过这pg_stat_activity对监视 Postgres 数据库很有用。

I have looked at the Postgres documentation, but I have a very simple question that doesn't seem to be answered there.

查看了 Postgres 文档,但我有一个非常简单的问题,似乎没有在那里得到解答。

How do I actually get started with pg_stat_activity? What do I type to use it, and where do I type it?

我实际上如何开始使用pg_stat_activity?我输入什么来使用它,我在哪里输入它?

回答by Craig Ringer

See this closely related answer.

看到这个密切相关的答案

pg_stat_activityis a view in the pg_catalogschema.

pg_stat_activitypg_catalog模式中的一个视图。

You can query it by SELECTing from it like any other table, eg SELECT * FROM pg_stat_activity. The manual page you linked to explains its columns.

您可以SELECT像任何其他表一样通过ing查询它,例如SELECT * FROM pg_stat_activity. 您链接到的手册页解释了其列。

You'll sometimes find yourself wanting to join on other tables like pg_class(tables), pg_namespace(schemas), etc.

您有时会发现自己想要加入其他表,如pg_class(表)、pg_namespace(模式)等。

pg_stat_activitydoes notexpose information about back-end memory use. You need to use operating-system level facilities for that. It doestell you the process ID, active user, currently running query, activity status, time the last query started, etc. It's good for identifying long-running idle in transactionsessions, very long running queries, etc.

pg_stat_activity没有公开有关后端的内存使用信息。为此,您需要使用操作系统级别的工具。它确实会告诉您进程 ID、活动用户、当前正在运行的查询、活动状态、上次查询开始的时间等。它有助于识别长时间运行的idle in transaction会话、非常长时间运行的查询等。

Frankly, PostgreSQL's built-in monitoring is rather rudimentary. It's one of the areas that's not that exciting to work on, and commercial clients aren't often willing to fund it. Most people couple tools like check_postgreswith Icinga and Munin, or use Zabbix or other external monitoring agents.

坦率地说,PostgreSQL 的内置监控相当初级。这是一个不那么令人兴奋的领域之一,商业客户通常不愿意为其提供资金。大多数人将工具check_postgres与 Icinga 和 Munin 结合使用,或者使用 Zabbix 或其他外部监控代理。

In your case it sounds like you really want pg_stat_statements, and/or PgBadger log analysis with suitable logging settings and possibly the auto_explainmodule.

在您的情况下,听起来您确实想要pg_stat_statements和/或 PgBadger 日志分析与合适的日志记录设置和可能的auto_explain模块。