AGENT SKILL

postgres-database-expert

Design and manage PostgreSQL databases with proper schema design, query optimization, GORM/pgx patterns, and Redis caching. Use when designing schemas, writing or optimizing SQL queries, analyzing EXPLAIN plans, managing indexes, implementing connection pooling, or setting up Redis caching layers. For zero-downtime migration strategies (expand-contract, backfills, rollbacks), consult the postgres references directory for migration guidance. Trigger: PostgreSQL, SQL, migration, query optimization, pgx, GORM, EXPLAIN ANALYZE. Do NOT trigger for: NoSQL databases, MongoDB, DynamoDB design, application-level caching strategies, N+1 query optimization in application code (use performance-expert).

License 许可
MIT
最近更新
2026-08-27
Stars
★ 1
Forks
0

01 前言 Front Matter

name
postgres-database-expert
license
MIT
metadata
{ "author": "Community", "status": "stable", "version": "1.1", "category": "data" }
description
Design and manage PostgreSQL databases with proper schema design, query optimization, GORM/pgx patterns, and Redis caching. Use when designing schemas, writing or optimizing SQL queries, analyzing EXPLAIN plans, managing indexes, implementing connection pooling, or setting up Redis caching layers. For zero-downtime migration strategies (expand-contract, backfills, rollbacks), consult the postgres references directory for migration guidance. Trigger: PostgreSQL, SQL, migration, query optimization, pgx, GORM, EXPLAIN ANALYZE. Do NOT trigger for: NoSQL databases, MongoDB, DynamoDB design, application-level caching strategies, N+1 query optimization in application code (use performance-expert).

02 资源文件 10

03 SKILL.md 正文

PostgreSQL Database Expert

Design and manage PostgreSQL databases with proper schema, migrations, query optimization, and caching.

See references/overview.md

Core Stack

See references/stack.md

  • Database: PostgreSQL 16
  • Go: GORM + pgx/v5
  • Migrations: golang-migrate
  • Caching: Redis

Naming Conventions

See references/naming.md

  • Tables: snake_case, plural
  • Columns: snake_case
  • PKs: id (UUID)
  • FKs: {table}_id
  • Indexes: idx_{table}_{columns}

Schema Rules

  • UUIDs for PKs | BIGINT for value amounts (NEVER FLOAT) | ALWAYS TIMESTAMPTZ
  • Soft deletes with deleted_at | NOT NULL by default

→ references/schema-design.md | references/naming.md

Migrations

  • Safe: ADD COLUMN, CREATE INDEX CONCURRENTLY
  • Dangerous: ALTER TYPE, CREATE INDEX(without CONCURRENTLY)
  • EXPLAIN red flags: Seq Scan | high cost Sort | Nested Loop

→ references/migrations.md | references/performance.md

Workflow

See references/workflow.md

  1. Schema design (UUIDs, BIGINT, TIMESTAMPTZ)
  2. Safe migrations (up + down, CONCURRENTLY)
  3. Index strategy (B-tree, composite, GIN)
  4. Query optimization (EXPLAIN ANALYZE)
  5. ORM patterns (GORM, pgx)
  6. Redis caching (cache-aside, TTL)

Security

Parameterized queries only | NEVER plaintext passwords | dedicated roles per service Parameterized queries prevent SQL injection (OWASP 2025 A05) — see the OWASP SQL Injection Prevention Cheat Sheet → references/security.md | references/code-patterns.md | https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html

Constraints

  • NEVER FLOAT for value → BIGINT in units
  • NEVER TIMESTAMP without zone → TIMESTAMPTZ
  • NEVER OFFSET on large tables → cursor-based pagination
  • NEVER SELECT * in production → explicit columns
  • NEVER CREATE INDEX without CONCURRENTLY on production
  • ALWAYS write up + down migrations
  • ALWAYS EXPLAIN ANALYZE before optimizing
  • ALWAYS parameterized queries (NEVER string concat)
  • ALWAYS set connection pool limits
  • ALWAYS invalidate cache on data mutation

Overview

PostgreSQL 16 schema design with UUID primary keys, BIGINT for value amounts, TIMESTAMPTZ for temporal data, and snake_case naming conventions. Paired with Redis cache-aside patterns and safe migrations using CREATE INDEX CONCURRENTLY and expand-contract for breaking changes.

Quick Reference

Concept Rule Example
PKs UUID (not serial) id UUID PRIMARY KEY DEFAULT gen_random_uuid()
Value BIGINT in units value BIGINT NOT NULL
Timestamps TIMESTAMPTZ always created_at TIMESTAMPTZ NOT NULL DEFAULT now()
Indexes CONCURRENTLY on production CREATE INDEX CONCURRENTLY idx_items_status ON items(status)
Migrations Up + Down always golang-migrate with separate up/down files
Pagination Cursor-based (no OFFSET) WHERE id > $1 ORDER BY id LIMIT 50
Soft Deletes deleted_at TIMESTAMPTZ WHERE deleted_at IS NULL

Workflow

  1. Design schema: UUID PKs, BIGINT for value, TIMESTAMPTZ, snake_case naming, NOT NULL by default
  2. Write safe migrations: ADD COLUMN is safe, ALTER TYPE needs caution — always write up + down
  3. Index strategy: B-tree for equality, composite for multi-column filters, GIN for full-text search; always CONCURRENTLY on production
  4. Optimize queries: run EXPLAIN ANALYZE, watch for Seq Scan (needs index), high-cost Sort, or Nested Loop on large tables
  5. Implement caching: cache-aside pattern with Redis, 5min default TTL, invalidate on mutation
  6. Connection pooling: pgx pool with MaxConns: 20 and statement timeout

Anti-patterns

FAIL: Using FLOAT for value

-- BAD
amount DECIMAL(10,2) -- rounding errors

PASS: Use BIGINT in units

-- GOOD
value BIGINT NOT NULL CHECK (value >= 0)

FAIL: TIMESTAMP without timezone

-- BAD
deleted_at TIMESTAMP -- ambiguous across timezones

PASS: Always use TIMESTAMPTZ

-- GOOD
deleted_at TIMESTAMPTZ

FAIL: OFFSET pagination on large tables

-- BAD — scans all skipped rows
SELECT * FROM items ORDER BY id LIMIT 50 OFFSET 10000;

PASS: Cursor-based pagination

-- GOOD — constant time per page
SELECT * FROM items WHERE id > $1 ORDER BY id LIMIT 50;

FAIL: SELECT * in production code

-- BAD — fetches unused columns, breaks if schema changes
SELECT * FROM items WHERE id = $1;

PASS: Explicit column list

-- GOOD
SELECT id, value, status, created_at FROM items WHERE id = $1;

FAIL: Creating index without CONCURRENTLY

-- BAD — blocks writes
CREATE INDEX idx_items_tenant ON items(tenant_id);

PASS: Safe with CONCURRENTLY

-- GOOD — non-blocking
CREATE INDEX CONCURRENTLY idx_items_tenant ON items(tenant_id);

FAIL: N+1 query — fetching related entities in a loop

items, _ := db.Items.FindAll()
for _, item := range items {
    subitems, _ := db.SubItems.FindByItemID(item.ID)  // N queries!
}

PASS: Eager loading with Preload

items, _ := db.Items.Preload("SubItems").FindAll()  // 2 queries total
-- Single query with JOIN
SELECT i.*, s.* FROM items i
LEFT JOIN item_subitems s ON s.item_id = i.id
WHERE i.id = ANY($1);

FAIL: ORDER BY on unindexed column causing Seq Scan + Sort

-- BAD: Seq Scan + Sort on 1M rows
SELECT id, value, created_at
FROM items
WHERE tenant_id = $1
ORDER BY created_at DESC;

PASS: Add composite index matching ORDER BY

CREATE INDEX CONCURRENTLY idx_items_tenant_created
ON items(tenant_id, created_at DESC);
-- NOW: Index Scan Backward — constant-time

References

Verification Checklist

  • [ ] All value columns use BIGINT in units (never FLOAT/DECIMAL)
  • [ ] All timestamp columns use TIMESTAMPTZ (never TIMESTAMP without zone)
  • [ ] Production indexes created with CONCURRENTLY (never blocking writes)
  • [ ] Migrations have both up and down files
  • [ ] Pagination uses cursor-based pattern, not OFFSET
  • [ ] Queries use parameterized inputs (no string concatenation)
  • [ ] EXPLAIN ANALYZE run on new/modified query paths
  • [ ] Parameterized queries verified against SQL Injection Prevention Cheat Sheet (no string concat)

Troubleshooting

Symptom Likely cause Fix
Query slow despite index Wrong index type or column order Run EXPLAIN ANALYZE; verify index matches filter + sort columns
Migration blocks writes on production CREATE INDEX without CONCURRENTLY Drop index; re-create with CREATE INDEX CONCURRENTLY
Connection pool exhausted MaxConns too low or slow queries holding connections Increase pool size; add statement timeout; check for long-running queries
Cache returns stale data Cache not invalidated on write path Add cache invalidation in the mutation code path
Index created CONCURRENTLY but not used by query planner (known issue: statistics stale) Table statistics not updated after index creation Run ANALYZE after CREATE INDEX CONCURRENTLY to refresh planner statistics
MVCC bloat causes table size to grow unbounded (autovacuum can't keep up) Long-running transactions prevent vacuum from reclaiming dead tuples Set idle_in_transaction_session_timeout; monitor pg_stat_user_tables.n_dead_tup; tune autovacuum scale_factor
Parameter sniffing: same plan used for different parameter values, causing slow queries on some inputs PostgreSQL caches plan from first execution with specific params Use SET plan_cache_mode = 'force_custom_plan' for skewed data; or use dynamic SQL with literals

| [WARN] VACUUM FULL blocks writes and causes replication lag | VACUUM FULL takes ACCESS EXCLUSIVE lock; triggers WAL burst on replicas | Use pg_repack instead of VACUUM FULL; schedule during maintenance window with application drain | | GORM AutoMigrate creates a constraint with a name that conflicts with an existing FK | AutoMigrate generates constraint names dynamically; name collision on concurrent migration | Disable AutoMigrate in production; use explicit migration with named ALTER TABLE ADD CONSTRAINT | | Limitation: pg_stat_statements resets on server restart, losing long-term query performance history | pg_stat_statements is in-memory; restart or failover resets all accumulated statistics | Snapshot pg_stat_statements to a logging table every hour via pg_cron; monitor trends not absolute values |

04 同仓库更多 Skills

ronsilver
ronsilver/agent-covenant

web-browsing-agent-expert

Web browsing for AI agents: driving a headless browser (Playwright, Puppeteer) from agent tooling, navigation and page-state grounding, DOM extraction and structured data capture, form interaction, session and cookie management, anti-bot mitigation awareness, and safe browsing rules (no PII exfiltration, respect robots and terms). Use when building a browsing tool or MCP server for an agent, teaching an agent to navigate and read web pages, extracting structured content from dynamic sites, or handling login walls and pagination in agent workflows. Trigger: web browsing agent, headless browser, Playwright MCP, Puppeteer, browser automation for agents, DOM extraction, page navigation, session management. Do NOT trigger for: end-to-end UI testing of an application (use playwright-expert); writing standalone scraping scripts for data pipelines (use python-expert); fetching a single public URL (use the fetch tool).

★ 1
ronsilver
ronsilver/agent-covenant

vector-databases

Design and operate vector databases for cloud-native AI features. Use when selecting between pgvector, Pinecone, or Weaviate for AI context layer or integration-hub; designing embedding schemas and index parameters; implementing ANN search with metadata filtering; managing embedding model versions; optimizing query latency; or debugging low retrieval quality. Pairs with rag-architecture for pipeline design and postgres-database-expert for pgvector specifics. Trigger: vector database, pgvector, Pinecone, Weaviate, embedding, ANN search, HNSW, cosine similarity. Do NOT trigger for: relational SQL optimization, standard PostgreSQL schema design, Redis caching.

★ 1
ronsilver
ronsilver/agent-covenant

typescript-expert

Full-stack TypeScript stack for modern projects: frontend SSR/CSR with Next.js 14 App Router and React, backend APIs with Node.js (Express/Fastify), state management with Zustand/Redux Toolkit, unit and integration testing with Vitest/Jest + React Testing Library + MSW, and static analysis with ESLint/Prettier/tsc. Use when building React components or hooks, implementing Next.js App Router or Server Components, writing Node.js REST API handlers, implementing middleware (auth, rate-limit, logging), managing client state (Zustand/RTK), writing frontend/backend tests, or running TypeScript linting/type checks. Trigger: TypeScript, Next.js, React, Zustand, Node.js, Express, Fastify, MSW, Vitest. Do NOT trigger for: Python backend development, Go microservices, iOS/mobile development.

★ 1
ronsilver
ronsilver/agent-covenant

tool-usage

Guide correct tool selection, design, and orchestration for AI agents. Use when choosing between dedicated vs Bash tools, designing MCP tool interfaces, naming tool parameters, handling tool response limits, orchestrating parallel vs sequential calls, evaluating tool accuracy, applying ACI design principles, selecting workflow patterns, or reading/filtering/transforming/generating structured data (JSON, YAML, CSV, TOML, XML). Trigger: tool selection, MCP tool design, orchestration, ACI, parallel execution, composite tool, structured data, jq, yq, dasel, mlr, csvkit. Do NOT trigger for: writing application business logic, domain-specific development (route to domain skill), database migrations.

★ 1
ronsilver
ronsilver/agent-covenant

token-efficiency

Enforce token-efficient output, compression, caching, and model routing. Use when calibrating response verbosity, applying compression strategies to logs or RAG chunks, routing tasks to the right model tier, setting thinking budgets, enforcing word limits, compressing context before reinitiation, applying KV-cache ordering, masking tool output observations, or applying clarification-first protocol before irreversible actions. Trigger: token efficiency, compression, model routing, thinking budget, KV-cache, clarification protocol. Do NOT trigger for: non-agent tooling, CI/CD pipeline scripting, infrastructure provisioning.

★ 1
ronsilver
ronsilver/agent-covenant

time-series-db

Time-series database patterns for cloud-native metrics and analytics: TimescaleDB, InfluxDB, and Amazon Timestream selection; continuous aggregates, retention policies, downsampling, and integration with Prometheus/Grafana. Use when storing business metrics over time, building custom business dashboards, designing high-frequency event storage, or querying time-bucketed aggregations. Trigger: time-series, TimescaleDB, InfluxDB, Prometheus, Grafana, hypertable, continuous aggregate, retention policy. Do NOT trigger for: relational PostgreSQL schema design, NoSQL document stores, Kafka event streaming.

★ 1