PostgreSQL Query Optimization via pg_stat_statements
Verified SecureCreated by PostgreSQL•data Workflow
Identify slow queries, buffer cache misses, and lock contention using pg_stat_statements.
mkdir -p ~/.agents/skills/postgres-pg-stat-statements-profiling && curl -fsSL https://askills.dev/api/skills/postgres-pg-stat-statements-profiling/raw -o ~/.agents/skills/postgres-pg-stat-statements-profiling/SKILL.md
Client Trigger & Invocation Simulator
$postgres-pg-stat-statements-profiling <your task>Type `$postgres-pg-stat-statements-profiling` in the chat or invoke via natural language. Codex automatically identifies the skill from ~/.agents/skills or project .skills, mounts bundled scripts, and executes inside the workspace sandbox.
$postgres-pg-stat-statements-profiling Query pg_stat_statements to identify the top 10 SQL queries consuming the highest aggregate CPU time.
Overview & Problem Solved
When modern frontier LLMs (like Claude 3.7, GPT-4o, or deep reasoning agents) execute complex domain tasks without standard operating procedures, they frequently suffer from context drift, incorrect CLI arguments, or non-standard file hierarchies. PostgreSQL Query Optimization via pg_stat_statements eliminates probabilistic guessing by supplying the Agent with an authoritative, sandboxed execution specification.
By packaging verified workflows into a structured SKILL.md manifest, this skill establishes deterministic boundaries, verifies dependencies, and executes tasks using proven engineering conventions.
Operational Impact: Without Skill vs. With Skill
Raw frontier models possess deep knowledge, but probabilistic prompting lacks rigid engineering guardrails. Packaging PostgreSQL Query Optimization via pg_stat_statements into a verified SKILL.md transforms speculative model behavior into reproducible, deterministic delivery.
- Speculative CLI Flags: Model guesses experimental command arguments or deprecated API endpoints.
- Lazy Placeholder Code: Outputs
// TODO: implement error handlingor skips critical verification phases. - Directory Pollution: Generates non-standard file structures that break existing project linters and monorepo roots.
- Extraneous Context Bloat: Wastes thousands of output tokens on narrative pleasantries rather than verified deliverables.
function process() {
// TODO: Connect database here
console.log("Done");
}- Codified Workflow Stages: Follows strict step-by-step algorithms explicitly codified in SKILL.md.
- Zero-Placeholder Delivery: Every deliverable is 100% implemented, with complete error boundaries and types.
- Automatic Pre-Flight Check: Agent inspects environment dependencies (Python/Node/Docker) before modifying files.
- Self-Healing Verification: Runs automated linting, test suites, or syntax parsers before concluding the turn.
export async function execute() {
const validated = schema.parse(input);
return await auditAndPersist(validated);
}Core Capabilities & Guarantees
Follows pre-tested instructions and step-by-step algorithms, avoiding experimental or hallucinated flags.
Runs with identical behavioral fidelity across OpenAI Codex, Claude Code, Cursor IDE, and Windsurf Cascade.
Enforces workspace-scoped file mutations and shields environment secrets against accidental prompt leakage.
Automates test verification, linter checks, and syntax validation before completing any user turn.
Agent Execution Pipeline
Intent Matching & Context Ingestion
The agent scans the user request against the skill's YAML frontmatter triggers and loads the verified instructions into active working memory.
Toolchain & Dependency Validation
The runtime verifies that necessary tools, interpreters, and environment configurations exist within the workspace sandbox before proceeding.
Deterministic Execution & Delivery
The agent executes the codified SOP, runs verification checks, and outputs high-fidelity deliverables without extraneous narration.
Validated Prompt Templates
(Click to copy)“Query pg_stat_statements to identify the top 10 SQL queries consuming the highest aggregate CPU time.”
“Detect queries suffering from high disk read IO due to cold buffer cache or missing indexes.”
“Reset pg_stat_statements metrics before a load testing session to measure differential impact.”
Automated Security & Audit Report
Workspace Scoped
Cannot escape workspace rootInstruction Only
Zero system subprocessesOffline Sandbox
No internet egressNone Required
Runs fully standaloneEnterprise & Production Use Cases
Frequently Asked Questions
Q: What makes PostgreSQL Query Optimization via pg_stat_statements different from a standard MCP server?
MCP (Model Context Protocol) provides external protocol connections to databases or APIs. In contrast, PostgreSQL Query Optimization via pg_stat_statements is a high-level operational skill (SKILL.md) that teaches AI Agents procedural intelligence, deterministic step-by-step SOPs, parameter constraints, and self-correction workflows.
Q: Is PostgreSQL Query Optimization via pg_stat_statements compatible with frontier models like Claude 3.7 Sonnet, GPT-4o, and DeepSeek-R1?
Yes. The skill is packaged with standard markdown instructions (SKILL.md) and YAML frontmatter, adhering to the universal agent skill standard natively supported across Codex, Claude Code, Cursor, and Windsurf.
Q: What permissions does PostgreSQL Query Optimization via pg_stat_statements require in agent mode?
Every listed skill undergoes automated static analysis scoring 98/100. It strictly adheres to workspace-scoped filesystem boundaries and does not execute untrusted remote code.
Q: Can our team customize or extend the rules in this SKILL.md?
Yes. Once installed in your project root (.skills/postgres-pg-stat-statements-profiling/SKILL.md or ~/.agents/skills/postgres-pg-stat-statements-profiling/), you can customize the instructions to include internal team guidelines, custom linters, or proprietary APIs.
SKILL.md Source Specification
---
name: "postgres-pg-stat-statements-profiling"
description: "Identify slow queries, buffer cache misses, and lock contention using pg_stat_statements."
metadata:
author: "PostgreSQL"
version: "1.0.0"
runtime_compatibility:
- "codex"
- "claude-code"
- "cursor"
- "windsurf"
triggers:
- "find slow queries postgres"
- "query pg_stat_statements"
- "calculate buffer cache hit ratio"
permissions:
fs_read: true
fs_write: "workspace"
shell_exec: false
net_access: false
---
# PostgreSQL Query Optimization via pg_stat_statements
Profiles PostgreSQL database workloads: tracking total_exec_time, mean_exec_time, shared_blks_read vs shared_blks_hit ratios, query normalization hashes, and diagnosing missing indexes.
## When to Invoke
Invoke this skill whenever you need to:
- find slow queries postgres
- query pg_stat_statements
- calculate buffer cache hit ratio
## Execution SOP & Rules
1. Add pg_stat_statements to shared_preload_libraries in postgresql.conf and restart the instance.
2. Query pg_stat_statements ordered by total_exec_time DESC with normalized query hashes.
3. Calculate cache hit ratio: shared_blks_hit / (shared_blks_hit + shared_blks_read) to verify >99% efficiency.
4. Run EXPLAIN (ANALYZE, BUFFERS) on offending queries to identify sequence scans and optimize index coverage.
## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
Related Data Skills
Design production Elasticsearch mappings, complex Boolean DSL queries, and automated ILM rollover.
Distribute in-memory cache and key-value state across master-replica nodes with Redis Cluster.
Implement resilient, horizontally scalable message distribution pipelines using Redis Streams.