---
name: postgres-deep
description: |
  PostgreSQL deep-dive: EXPLAIN ANALYZE interpretation, index types (B-tree, GIN, GiST, BRIN),
  partial/covering/composite indexes, pg_stat_statements slow query analysis, Row Level Security,
  window functions, CTEs, advisory locks, LISTEN/NOTIFY, connection pooling with PgBouncer.
  Use when optimizing slow queries, designing indexing strategy, implementing multi-tenancy.
allowed-tools: Read, Grep, Glob
---

# PostgreSQL Deep Dive

## When to Use This Skill
- Optimizing slow queries with EXPLAIN ANALYZE
- Designing indexes for specific access patterns
- Implementing Row Level Security for multi-tenancy
- Using window functions for analytics queries
- Configuring PgBouncer for connection pooling

## Core Principles

1. **EXPLAIN ANALYZE before optimizing** — never guess; measure first; the plan shows the actual cost
2. **Index the access pattern, not the column** — a composite index `(user_id, created_at DESC)` beats two separate indexes for paginated user queries
3. **Partial indexes reduce size and maintenance cost** — `WHERE status = 'pending'` on a 10M row table with 1000 pending rows = tiny, fast index
4. **RLS is enforced at the DB layer** — even if application code has a bug, unauthorized rows are invisible
5. **pg_stat_statements is your slow query log** — enable it; it shows cumulative query costs across all calls

## References available
- `references/query-optimization.md` — EXPLAIN ANALYZE interpretation, node types, warning signs, stale statistics, work_mem, CTEs
- `references/query-anti-patterns.md` — N+1 queries, missing FK indexes, over-fetching, OFFSET pagination, NOT IN gotcha, DISTINCT misuse
- `references/indexing-strategies.md` — B-tree, GIN, GiST, BRIN index types; composite and covering indexes; index type decision tree
- `references/index-tuning.md` — partial indexes, expression indexes, index maintenance, unused/bloated index detection, CONCURRENTLY
- `references/partitioning.md` — range/list/hash partitioning, when to partition, partition pruning, partitioned table constraints
- `references/partition-management.md` — partition maintenance, DETACH/DROP/ATTACH, pg_partman automation, anti-patterns
- `references/connection-pooling.md` — PgBouncer setup, pool modes (session/transaction/statement), pool sizing, application config
- `references/connection-diagnostics.md` — SHOW POOLS, pg_stat_activity monitoring, connection troubleshooting runbook, Kubernetes deploy

## Scripts available
- `scripts/analyze-slow-queries.sh` — query pg_stat_statements for top consumers and low cache-hit queries


---
Source attribution: rnavarych
Original project: https://github.com/rnavarych/alpha-engineer/blob/c5b74102c7fd584af3d344ba7c331a7bbec3c729/plugins/billy-milligan/skills/shared/postgres-deep/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Dave AI.

## Upstream license notice

MIT License

Copyright (c) 2024-2026 Raman Navarych

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Dave AI resource metadata

Creator / publisher: rnavarych
Original project: https://github.com/rnavarych/alpha-engineer/blob/c5b74102c7fd584af3d344ba7c331a7bbec3c729/plugins/billy-milligan/skills/shared/postgres-deep/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering
Roles: Backend Developer, Data Analyst, Data Scientist, Database Administrator, ML / AI Engineer
Specialties: Database
Tools: PostgreSQL
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: a3f856f3d3ed5113a85c328c11bd7ebe0416050dadfcce6305146da9afbbdbf1
