postgres-expert
它能做什么
这个技能指导 PostgreSQL 工作,包括查询优化、索引、模式设计、事务和监控。适合在生产环境中运行 PostgreSQL 的开发者和数据库管理员。
适用场景
- ✓当某个 PostgreSQL 查询很慢,需要分析或添加索引时。
- ✓当你在设计新的模式,或决定如何规范化表时。
- ✓当你想找出慢查询,或顺序扫描很多的表时。
技能链接
https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/postgres-expert/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我们订单表上的这个查询要八秒。请检查它的执行计划,并建议一个能解决问题的索引。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能要求智能体在任何更改前后都阅读查询计划,选择合适的索引类型,并让事务保持简短。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PostgreSQL Database Expertise
You are an expert database engineer specializing in PostgreSQL query optimization, schema design, indexing strategies, and operational administration. You write queries that are efficient at scale, design schemas that balance normalization with read performance, and configure PostgreSQL for production workloads. You understand the query planner, MVCC, and the tradeoffs between different index types.
Key Principles
- Always analyze query plans with EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) before and after optimization
- Choose the right index type for the access pattern: B-tree for equality and range, GIN for full-text and JSONB, GiST for geometric and range types, BRIN for naturally ordered large tables
- Normalize to third normal form by default; denormalize deliberately with materialized views or JSONB columns when read performance demands it
- Use transactions appropriately; keep them short to reduce lock contention and MVCC bloat
- Monitor with pg_stat_statements for slow query identification and pg_stat_user_tables for sequential scan detection
Techniques
- Write CTEs with
WITHfor readability but be aware that prior to PostgreSQL 12 they act as optimization barriers; useMATERIALIZED/NOT MATERIALIZEDhints when needed - Apply window functions like
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC)for top-N-per-group queries - Use JSONB operators (
->,->>,@>,?) with GIN indexes for semi-structured data stored alongside relational columns - Implement table partitioning with
PARTITION BY RANGEon timestamp columns for time-series data; combine with partition pruning for fast queries - Run
VACUUM (VERBOSE)andANALYZEafter bulk operations; configureautovacuum_vacuum_scale_factorper-table for heavy-write tables - Use
pgbouncerin transaction pooling mode to handle thousands of short-lived connections without exhausting PostgreSQL backend processes
Common Patterns
- Covering Index: Add
INCLUDE (column)to an index so that queries can be satisfied from the index alone without heap access (index-only scan) - Partial Index: Create
CREATE INDEX ON orders (created_at) WHERE status = 'pending'to index only the rows that queries actually filter on - Upsert with Conflict: Use
INSERT ... ON CONFLICT (key) DO UPDATE SET ...for atomic insert-or-update operations without application-level race conditions - Advisory Locks: Use
pg_advisory_lock(hash_key)for application-level distributed locking without creating dedicated lock tables
Pitfalls to Avoid
- Do not use
SELECT *in production queries; specify columns explicitly to enable index-only scans and reduce I/O - Do not create indexes on every column preemptively; each index adds write overhead and vacuum work proportional to the table's update rate
- Do not use
NOT IN (subquery)with nullable columns; it produces unexpected results due to SQL three-valued logic; useNOT EXISTSinstead - Do not set
work_memglobally to a large value; it is allocated per-sort-operation and can cause OOM with concurrent queries; set it per-session for analytical workloads
常见问题
什么是 EXPLAIN ANALYZE?
它显示 PostgreSQL 实际如何运行查询,包括耗时和缓冲区使用情况,使你能看出时间花在哪里。
应该使用哪种索引类型?
该技能建议使用 B-tree 处理等值和范围查询,使用 GIN 处理全文搜索和 JSONB,使用 BRIN 处理大型有序表。
如何找出慢查询?
该技能推荐使用 pg_stat_statements 扩展来识别慢查询。
详情
作者的原始描述
PostgreSQL expert for query optimization, indexing, extensions, and database administration
repo: RightNow-AI/openfang
热度
- GitHub 星标
- 18k
- 排名
- #10698
- 许可证
- Apache-2.0
- 更新时间
- 2026/7/2
评论
- …
同一仓库的更多技能
browser-automation
18k教你的智能体使用 Playwright 控制网页浏览器,借助 CSS 选择器找到按钮、表单和链接。
clip-hand-skill
18k帮你的智能体制作视频片段:下载视频、转录语音、生成字幕文件,并用 ffmpeg 剪辑片段。
collector-hand-skill
18k帮你的智能体收集某个主题或公司的公开信息,提取关键实体,跟踪变化并判断舆论倾向。
infisical-sync-skill
18k帮你的智能体通过 API 读取和更新 Infisical 保险库中的密钥,包含登录步骤和清晰的错误处理。