跳到正文
Skillpedia
数据库18k · #10698

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/postgres-expert/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 WITH for readability but be aware that prior to PostgreSQL 12 they act as optimization barriers; use MATERIALIZED/NOT MATERIALIZED hints 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 RANGE on timestamp columns for time-series data; combine with partition pruning for fast queries
  • Run VACUUM (VERBOSE) and ANALYZE after bulk operations; configure autovacuum_vacuum_scale_factor per-table for heavy-write tables
  • Use pgbouncer in 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; use NOT EXISTS instead
  • Do not set work_mem globally 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能