Pg stat statements observability

From CarlosCaPe/octorato · View interactive version

Pg stat statements observability is an AI agent skill from the GitHub repository CarlosCaPe/octorato. pg_stat_statements Query Observability

How to use this skill

Skill file

pg_stat_statements Query Observability

Source: PostgreSQL Best Practices

-- "Server configuration",

What

Enabling and using the pg_stat_statements extension to capture per-query

performance metrics (calls, total time, mean time, rows, shared blocks).

This is the primary tool for identifying slow queries, regression, and

optimization opportunities.

Why

Without query-level metrics:

pg_stat_statements is lightweight (< 1% overhead), built into PostgreSQL,

and available on Azure Flexible Server as a loadable module.

How

Enable the extension (Azure Flexible Server)

  1. Azure Portal > Server Parameters:

```text

shared_preload_libraries = pg_stat_statements -- requires restart

pg_stat_statements.track = all -- track all statements

pg_stat_statements.max = 5000 -- max tracked queries

```

  1. Restart the server (required for shared_preload_libraries).
  2. Create the extension in each database:

```sql

CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

```

Top 10 queries by total execution time

SELECT
    calls,
    ROUND(total_exec_time::numeric, 2)       AS total_ms,
    ROUND(mean_exec_time::numeric, 2)        AS mean_ms,
    ROUND(min_exec_time::numeric, 2)         AS min_ms,
    ROUND(max_exec_time::numeric, 2)         AS max_ms,
    rows,
    LEFT(query, 120)                         AS query_preview
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;

Top 10 queries by mean execution time (slow outliers)

SELECT
    calls,
    ROUND(mean_exec_time::numeric, 2)        AS mean_ms,
    ROUND(total_exec_time::numeric, 2)       AS total_ms,
    rows,
    LEFT(query, 120)                         AS query_preview
FROM pg_stat_statements
WHERE calls > 10                             -- ignore one-off admin queries
ORDER BY mean_exec_time DESC
LIMIT 10;

Top 10 queries by shared buffer reads (I/O heavy)

SELECT
    calls,
    shared_blks_read + shared_blks_hit       AS total_blks,
    ROUND(
        100.0 * shared_blks_hit /
        NULLIF(shared_blks_read + shared_blks_hit, 0), 2
    )                                        AS cache_hit_pct,
    ROUND(mean_exec_time::numeric, 2)        AS mean_ms,
    LEFT(query, 120)                         AS query_preview
FROM pg_stat_statements
ORDER BY shared_blks_read DESC
LIMIT 10;

Reset statistics (after optimization to re-baseline)

-- Reset ALL query stats (use sparingly)
SELECT pg_stat_statements_reset();

Check if extension is active

SELECT * FROM pg_available_extensions
WHERE name = 'pg_stat_statements';

-- If installed, verify it is tracking
SELECT COUNT(*) AS tracked_queries
FROM pg_stat_statements;

Combine with slow query logging

-- Server parameter (Azure Portal)
log_min_duration_statement = 250    -- log queries > 250ms

-- This gives you two layers:
-- 1. pg_stat_statements: aggregated stats for ALL queries
-- 2. PostgreSQL log: individual slow query events with parameters

Key Metrics to Monitor

| Metric | What It Tells You | Action Threshold |

|--------|------------------|-----------------|

| total_exec_time | Cumulative CPU cost | Top query > 50% of total |

| mean_exec_time | Per-call latency | > 100ms for OLTP queries |

| calls | Query frequency | Top query > 10K calls/hour |

| rows | Rows returned per call | > 10K rows (missing pagination?) |

| shared_blks_read | Disk I/O | High reads = missing index or seq scan |

| cache_hit_pct | Buffer cache effectiveness | < 95% = under-provisioned memory |

When to Use

Where We Used It

Related Skills

References

-- "Server configuration" section

-- "Query-level observability" requirement

Gotchas

Flexible Server -- coordinate with ops

unless pg_stat_statements.save = on (default on Azure)

reset; use it only after a known baseline change

cannot see actual parameter values (by design, for security)

the least-used entry -- increase if your app has many dynamic queries


Category: Tooling | Origin:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.