0Pricing
PostgreSQL Performance & Query Optimization · บทเรียน

การวินิจฉัยกิจกรรมแบบเรียลไทม์ด้วย pg_stat_activity

เรียนรู้การใช้มุมมอง pg_stat_activity เพื่อดูว่าแต่ละการเชื่อมต่อกำลังทำอะไรอยู่ ค้นหาคำสืบค้นที่ทำงานนานหรือถูกบล็อก และยกเลิกหรือยุติเซสชันที่มีปัญหาอย่างปลอดภัย

การวินิจฉัยกิจกรรมแบบเรียลไทม์ด้วย pg_stat_activity เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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

คำถามที่พบบ่อย

บทเรียน “การวินิจฉัยกิจกรรมแบบเรียลไทม์ด้วย pg_stat_activity” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การวินิจฉัยกิจกรรมแบบเรียลไทม์ด้วย pg_stat_activity” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การวินิจฉัยกิจกรรมแบบเรียลไทม์ด้วย pg_stat_activity”

เรียนรู้การใช้มุมมอง pg_stat_activity เพื่อดูว่าแต่ละการเชื่อมต่อกำลังทำอะไรอยู่ ค้นหาคำสืบค้นที่ทำงานนานหรือถูกบล็อก และยกเลิกหรือยุติเซสชันที่มีปัญหาอย่างปลอดภัย คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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