简体   繁体   English

使用 pg_stat_statements 加入 pg_stat_activity?

[英]Join pg_stat_activity with pg_stat_statements?

I am researching heavy loads within the database when a user runs heavy queries.当用户运行大量查询时,我正在研究数据库中的大量负载。 I got to pg_stat_statements, which shows me the id of the user, the query, and the time it takes to run.我到了 pg_stat_statements,它显示了用户的 id、查询以及运行所需的时间。 On the other hand I found pg_stat_activity that shows me the active users to the database with their pid, query_start, etc. I joined the 2 as they have similarities in user id.另一方面,我发现 pg_stat_activity 向我显示了数据库的活动用户及其 pid、query_start 等。我加入了这 2 个,因为它们在用户 ID 上有相似之处。

SELECT a.usesysid,a.pid, a.client_addr, a.query_start, st.query, st.total_time
FROM pg_stat_activity as a
JOIN pg_stat_statements as st
ON a.usesysid = st.userid

this way I get the active user and where he used the query, like its load, etc. But the problem is when it is the same user connected with a different pid, the querys found are duplicated to each user pid that is connected at that moment.这样我就得到了活跃用户以及他在哪里使用查询,比如它的负载等。 但问题是当它是同一个用户连接到不同的 pid 时,找到的查询被复制到每个连接的用户 pid片刻。

In pg_stat_activity, it shows the user's data being active, therefore, if he disconnects his data is lost (I solved it with an ETL).在 pg_stat_activity 中,它显示用户的数据处于活动状态,因此,如果他断开连接,他的数据就会丢失(我用 ETL 解决了这个问题)。 but being active, how could it prevent them from being duplicated and taking their respective query used by the user?但是处于活动状态,如何防止它们被复制并接受用户使用的各自查询?

any suggestion or documentation is welcome.欢迎任何建议或文档。

Don't join them.不要加入他们。 While you can , it is entirely unclear what you hope to accomplish by doing so.虽然您可以,但完全不清楚您希望通过这样做来完成什么。 If you want to know what is loading the system right now, use pg_stat_activity.如果您想知道现在正在加载系统的内容,请使用 pg_stat_activity。 If you want to know in general what causes load averaged over time, use pg_stat_statements.如果您想大致了解导致负载随时间平均的原因,请使用 pg_stat_statements。 And if you want to know what specific query executions took a long time historically, set up auto_explain with log_min_duration and then pull them out of the log file.如果您想知道历史上哪些特定查询执行花费了很长时间,请使用 log_min_duration 设置 auto_explain,然后将它们从日志文件中拉出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM