Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.0 KB
Raw Blame
name: backend_cpu
description: >
  Per-backend CPU, page faults and I/O from /proc via pg_proctab, joined to
  pg_stat_activity — attributes host CPU to users/queries (what pg_stat_kcache
  would give). Cumulative per (pid, backend_start); rss is a gauge. The active
  statement's query_id and query tags are sampled at the tick so CPU can be
  grouped by query and by code path; idle backends carry neither.
interval: 60s
scope: cluster
kind: cumulative
requires: { extension: pg_proctab }
key: [pid, backend_start]
passthrough: [rss_kb, num_threads, query_id]
tags:
  from: query_tags_raw
query: |
  SELECT a.pid, a.backend_start, a.datname, a.usename, a.application_name, a.backend_type, a.state,
         NULL::bigint AS query_id,
         CASE WHEN a.state = 'active' THEN a.query END AS query_tags_raw,
         p.utime AS utime_jiffies, p.stime AS stime_jiffies,
         p.minflt, p.majflt,
         p.rchar AS read_chars, p.wchar AS write_chars, p.reads AS read_bytes, p.writes AS write_bytes,
         (p.rss * 4)::bigint AS rss_kb, p.num_threads
  FROM pg_proctab() p
  JOIN pg_stat_activity a ON a.pid = p.pid
  WHERE a.backend_type IN ('client backend', 'autovacuum worker', 'parallel worker', 'checkpointer', 'background writer', 'walwriter')
variants:
  - min_pg_version: 140000
    query: |
      SELECT a.pid, a.backend_start, a.datname, a.usename, a.application_name, a.backend_type, a.state,
             CASE WHEN a.state = 'active' THEN a.query_id END AS query_id,
             CASE WHEN a.state = 'active' THEN a.query END AS query_tags_raw,
             p.utime AS utime_jiffies, p.stime AS stime_jiffies,
             p.minflt, p.majflt,
             p.rchar AS read_chars, p.wchar AS write_chars, p.reads AS read_bytes, p.writes AS write_bytes,
             (p.rss * 4)::bigint AS rss_kb, p.num_threads
      FROM pg_proctab() p
      JOIN pg_stat_activity a ON a.pid = p.pid
      WHERE a.backend_type IN ('client backend', 'autovacuum worker', 'parallel worker', 'checkpointer', 'background writer', 'walwriter')