home / skills / yuniorglez / gemini-elite-core / postgres-tuning

postgres-tuning skill

/skills/postgres-tuning

This skill optimizes PostgreSQL 18+ performance by tuning AIO, forensic plan analysis, and vector indexing for high-throughput workloads.

npx playbooks add skill yuniorglez/gemini-elite-core --skill postgres-tuning

Review the files below or copy the command above to add this skill to your agents.

Files (4)
SKILL.md
4.1 KB
---
name: postgres-tuning
id: postgres-tuning
version: 1.1.0
description: "Senior Database Optimizer for PostgreSQL 17/18+, specialized in Asynchronous I/O (AIO), Query Plan Forensic, and Vector Index optimization."
last_updated: "2026-01-22"
---

# Skill: PostgreSQL Tuning (Standard 2026)

**Role:** The PostgreSQL Tuning Specialist is responsible for ensuring the data layer is never the bottleneck. In 2026, this role masters the native AIO subsystem of PostgreSQL 18, high-density vector indexing (pgvector), and forensic query analysis using the enhanced EXPLAIN BUFFERS output.

## 🎯 Primary Objectives
1.  **I/O Performance:** Optimizing the Asynchronous I/O subsystem for 2-3x throughput gains on NVMe storage.
2.  **Query Forensic:** Mastering the "Execution Plan" to identify seq-scans, nested loops, and memory spills.
3.  **Indexing Excellence:** Leveraging B-tree Skip Scans and native UUIDv7 for high-insertion workloads.
4.  **Bloat & Vacuum Control:** Tuning autovacuum for high-churn tables to prevent performance degradation.

---

## 🏗️ The 2026 Database Stack

### 1. Engine & Extensions
- **PostgreSQL 18:** Featuring native AIO and parallel `COPY FROM`.
- **pgvector 0.8+:** For high-speed semantic search.
- **pg_stat_statements:** Mandatory for identifying slow query patterns.

### 2. Monitoring Tools
- **pg_aios:** For monitoring the asynchronous I/O workers.
- **pg_stat_io:** For granular I/O analysis across tables and indexes.
- **pganalyze / pgMustard:** For visual execution plan analysis.

---

## 🛠️ Implementation Patterns

### 1. Asynchronous I/O Configuration (PG 18)
Unlocking the full potential of modern SSDs.

```ini
# postgresql.conf 2026 Standard
io_method = worker             # Or io_uring on Linux
io_workers = 4                 # Tune based on CPU/Storage concurrency
max_async_ios = 1024           # Depth of the AIO queue
```

### 2. UUIDv7 Migration
Replacing random UUIDv4 with sequential v7 to reduce index fragmentation.

```sql
-- PostgreSQL 18 Native UUIDv7
CREATE TABLE transactions (
    id uuid DEFAULT uuid_generate_v7() PRIMARY KEY,
    created_at timestamptz DEFAULT now()
);
-- Benefit: 30% faster insertions and better cache locality
```

### 3. Forensic Plan Analysis
Using the enhanced EXPLAIN output to find silent I/O costs.

```sql
EXPLAIN (ANALYZE, BUFFERS, SETTINGS, WAL)
SELECT * FROM orders WHERE status = 'pending';
-- Goal: BUFFERS Shared Hit > 95%
```

---

## 🚫 The "Do Not List" (Anti-Patterns)
1.  **NEVER** run `SELECT *` in high-frequency queries. Fetch only needed columns to save I/O.
2.  **NEVER** ignore "Seq Scans" on tables over 10,000 rows. Add an index.
3.  **NEVER** use random UUIDs as primary keys for high-write tables (Causes B-tree page splits).
4.  **NEVER** set `shared_buffers` over 40% of total RAM without extensive testing.

---

## 🛠️ Troubleshooting & Latency Audit

| Issue | Likely Cause | 2026 Corrective Action |
| :--- | :--- | :--- |
| **High I/O Wait** | Synchrounous I/O bottleneck | Enable `io_method = worker` (PG 18). |
| **Index Bloat** | High UPDATE/DELETE volume | Tune `autovacuum_vacuum_scale_factor` to 0.01. |
| **Memory Spills** | Low `work_mem` for large sorts | Increase `work_mem` for specific heavy sessions. |
| **Slow Vector Search** | Unoptimized HNSW index | Rebuild index with higher `m` and `ef_construction` values. |

---

## 📚 Reference Library
- **[AIO & I/O Tuning](./references/1-aio-and-io-tuning.md):** Mastering the storage engine.
- **[Query Plan Analysis](./references/2-query-plan-analysis.md):** The forensic guide.
- **[Indexing & Bloat](./references/3-indexing-and-bloat.md):** Maintaining data density.

---

## 📊 Performance Metrics
- **Cache Hit Ratio:** > 99% for Shared Buffers.
- **Vacuum Latency:** < 60s for standard tables.
- **Median Query Latency:** < 10ms for OLTP workloads.

---

## 🔄 Evolution of PG Performance
- **v15-16:** Improved aggregation and parallelization.
- **v17:** Incremental backups and memory-efficient vacuum.
- **v18:** Native Asynchronous I/O (AIO), Parallel Copy, and B-tree Skip Scans.

---

**End of PostgreSQL Tuning Standard (v1.1.0)**

Overview

This skill is a senior PostgreSQL tuning specialist focused on PostgreSQL 17/18+ with expertise in native Asynchronous I/O, forensic query-plan analysis, and high-density vector indexing. It provides targeted recommendations to remove storage and planner bottlenecks, reduce insert latency, and keep autovacuum and bloat under control. The goal is measurable throughput and latency improvements for OLTP and vector-search workloads.

How this skill works

The skill inspects configuration, runtime statistics, and execution plans to identify I/O and planner inefficiencies. It analyzes EXPLAIN (ANALYZE, BUFFERS, SETTINGS, WAL) output, pg_stat_statements, and AIO metrics to surface seq-scans, nested loops, memory spills, and silent I/O costs. It recommends tuned postgresql.conf settings (io_method, io_workers, max_async_ios), index strategies (UUIDv7, B-tree skip scans, pgvector HNSW tuning), and autovacuum adjustments for high-churn tables.

When to use it

  • When NVMe-backed databases show high I/O wait or low throughput
  • When slow queries exhibit unexpected I/O in EXPLAIN BUFFERS output
  • Before and after migrating high-write tables to UUIDv7
  • When pgvector searches or HNSW indexes underperform
  • When autovacuum lag or index bloat affects production latency

Best practices

  • Enable and monitor native AIO (io_method=worker or io_uring) and tune io_workers/max_async_ios to match CPU/storage concurrency
  • Use EXPLAIN ANALYZE with BUFFERS and WAL to reveal hidden I/O; aim for Shared Hit >95% for hot tables
  • Replace random UUIDv4 with UUIDv7 for high-insert primary keys to reduce B-tree fragmentation
  • Tune autovacuum aggressively on high-churn tables (lower scale_factor, increase worker concurrency)
  • Optimize work_mem per-query for large sorts and joins rather than raising global settings blindly

Example use cases

  • Improve insert throughput 2–3x on NVMe by enabling AIO and raising async queue depth
  • Diagnose and eliminate a nightly slow-report job by finding a memory spill and increasing session work_mem
  • Reduce index bloat on a billing table by tuning autovacuum and migrating to UUIDv7 primary keys
  • Rebuild a slow pgvector HNSW index with higher m and ef_construction to speed semantic search
  • Convert frequent seq-scans into index seeks by adding targeted indexes and adjusting planner_cost settings

FAQ

Will enabling io_method=worker or io_uring always improve performance?

No. It often improves throughput on NVMe with proper tuning, but you must profile AIO metrics and adjust io_workers and max_async_ios for your CPU and storage concurrency.

When should I prefer UUIDv7 over integer sequences?

Use UUIDv7 when you need globally unique IDs with high write concurrency and distributed generation; prefer sequences for simplest, smallest primary keys when distribution is not required.