PostgreSQL Performance & Query Optimization · 课时

使用 pg_stat_activity 诊断实时活动

学习使用 pg_stat_activity 视图查看每个连接当前正在执行的操作,查找长时间运行和被阻塞的查询,并安全地取消或终止问题会话

第 4 / 4 课13 个步骤

使用 pg_stat_activity 诊断实时活动 是 CoddyKit 上的免费 PostgreSQL Performance & Query Optimization 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 PostgreSQL Performance & Query Optimization 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Your Window into Live Sessions

The pg_stat_activity view has one row per server connection. It is the first place to look when the database feels slow or stuck, showing what each session is doing this instant.

The Key Columns

The most useful columns are:

  • pid: the backend process id
  • state: active, idle, idle in transaction
  • query: the current or last SQL text
  • wait_event: what the session is waiting on

A Basic Look

Select the essentials for all active sessions.

SELECT pid, usename, state, wait_event, query
FROM pg_stat_activity
WHERE state <> 'idle';

Finding Long-Running Queries

Compute how long each active query has been running by subtracting query_start from now.

SELECT pid, now() - query_start AS runtime, query
FROM pg_stat_activity
WHERE state = 'active'
ORDER BY runtime DESC;

Idle in Transaction Danger

A session in idle in transaction holds locks and pins the oldest XID without doing work. These can block vacuum and other sessions. Hunt them down.

SELECT pid, now() - xact_start AS tx_age, query
FROM pg_stat_activity
WHERE state = 'idle in transaction'
ORDER BY tx_age DESC;

Seeing What a Session Waits On

The wait_event_type and wait_event columns reveal whether a session is waiting on a lock, on I/O, or on a client. This pinpoints the bottleneck.

SELECT pid, wait_event_type, wait_event, query
FROM pg_stat_activity
WHERE wait_event IS NOT NULL;

Finding Who Blocks Whom

Combine activity with pg_blocking_pids to see which sessions are blocking others.

SELECT pid, pg_blocking_pids(pid) AS blocked_by, query
FROM pg_stat_activity
WHERE cardinality(pg_blocking_pids(pid)) > 0;

Cancelling a Query

pg_cancel_backend stops the current query in a session but leaves the connection open. Try this first — it is the gentler option.

SELECT pg_cancel_backend(12345);

Terminating a Connection

If cancelling is not enough, pg_terminate_backend closes the whole connection, rolling back its transaction. Use it for stuck idle-in-transaction sessions.

SELECT pg_terminate_backend(12345);

Building a Monitoring Habit

Good practice:

  • Set idle_in_transaction_session_timeout to auto-kill stragglers
  • Set statement_timeout to bound runaway queries
  • Watch pg_stat_activity during incidents before reaching for the kill switch

Counting Connections by State

To gauge overall pressure, summarize how many connections sit in each state. A pile of idle-in-transaction or active sessions hints at pooling or query problems.

SELECT state, count(*)
FROM pg_stat_activity
GROUP BY state
ORDER BY count(*) DESC;

Quick Check

Test your live-monitoring knowledge.

Recap

You learned live diagnosis with pg_stat_activity:

  • One row per connection with state, query, and wait info
  • Find long queries via now() - query_start
  • Hunt idle-in-transaction sessions that block vacuum
  • pg_blocking_pids reveals who blocks whom
  • Cancel a query or terminate a backend when needed
免费开始

用 AI 导师学习 SQL — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
22
课程
88

常见问题解答

「使用 pg_stat_activity 诊断实时活动」课时是免费的吗?

是的 — 「使用 pg_stat_activity 诊断实时活动」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 PostgreSQL Performance & Query Optimization 课程的其余内容,请升级到 CoddyKit PRO。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。

「使用 pg_stat_activity 诊断实时活动」这节课中我会学到什么?

学习使用 pg_stat_activity 视图查看每个连接当前正在执行的操作,查找长时间运行和被阻塞的查询,并安全地取消或终止问题会话 你通过在浏览器中直接运行的动手代码来练习 PostgreSQL Performance & Query Optimization,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 PostgreSQL Performance & Query Optimization 需要有经验吗?

无需任何先前经验。CoddyKit 上的 PostgreSQL Performance & Query Optimization 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 pg_stat_activity 诊断实时活动」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 PostgreSQL Performance & Query Optimization 课中编写并运行代码吗?

能。每节 PostgreSQL Performance & Query Optimization 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 pg_stat_statements 和 pg_buffercache
  2. 用于分析的日志配置
  3. 集成外部监控工具
  4. 使用 pg_stat_activity 诊断实时活动
← 返回 PostgreSQL Performance & Query Optimization