postgres-expert
Skill này làm gì
Skill này hướng dẫn làm việc với PostgreSQL, gồm tối ưu truy vấn, lập chỉ mục, thiết kế schema, giao dịch và giám sát. Nó dành cho nhà phát triển và quản trị cơ sở dữ liệu vận hành PostgreSQL trong sản xuất.
Khi nào nên dùng
- ✓Khi một truy vấn PostgreSQL chậm và cần phân tích hoặc một chỉ mục.
- ✓Khi bạn thiết kế một schema mới hoặc quyết định cách chuẩn hóa bảng.
- ✓Khi bạn muốn tìm các truy vấn chậm hoặc bảng có nhiều lần quét tuần tự.
Liên kết skill
https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/postgres-expert/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Truy vấn này trên bảng đơn hàng của chúng tôi mất tám giây. Kiểm tra query plan và đề xuất một chỉ mục khắc phục nó.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill yêu cầu agent đọc query plan trước và sau mọi thay đổi, chọn đúng loại chỉ mục và giữ giao dịch ngắn.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
Câu hỏi thường gặp
EXPLAIN ANALYZE là gì?
Nó cho thấy PostgreSQL thực sự chạy truy vấn thế nào, gồm thời gian và mức dùng buffer, để bạn thấy thời gian đi đâu.
Nên dùng loại chỉ mục nào?
Skill gợi ý B-tree cho so sánh bằng và khoảng, GIN cho tìm kiếm toàn văn và JSONB, còn BRIN cho bảng lớn có thứ tự.
Làm sao tìm truy vấn chậm?
Skill khuyên dùng extension pg_stat_statements để xác định các truy vấn chậm.
Chi tiết
Mô tả gốc của tác giả
PostgreSQL expert for query optimization, indexing, extensions, and database administration
repo: RightNow-AI/openfang
Độ phổ biến
- Sao GitHub
- 18k
- Vị trí xếp hạng
- #10698
- Giấy phép
- Apache-2.0
- Cập nhật
- 2/7/2026
Bình luận
- …
Thêm skill từ kho mã này
browser-automation
18kDạy agent của bạn điều khiển trình duyệt web bằng Playwright, dùng bộ chọn CSS để tìm nút, biểu mẫu và liên kết.
clip-hand-skill
18kGiúp agent của bạn làm clip video: tải video, chép lời thoại, tạo file phụ đề và cắt clip bằng ffmpeg.
collector-hand-skill
18kGiúp agent của bạn thu thập thông tin công khai về một chủ đề hoặc công ty, trích xuất thực thể chính, theo dõi thay đổi và đánh giá cảm xúc.
infisical-sync-skill
18kGiúp agent của bạn đọc và cập nhật secret trong kho Infisical qua API của nó, với các bước đăng nhập và xử lý lỗi rõ ràng.
Skill tương tự
discrawl
394kCho phép agent của bạn tìm kiếm và tóm tắt kho lưu trữ tin nhắn Discord trên máy, kiểm tra độ mới của dữ liệu và đọc tin nhắn trực tiếp.
clickhouse-io
280kGiúp agent của bạn tối ưu truy vấn và schema trong ClickHouse theo thực hành tốt nhất cho phân tích dữ liệu hiệu năng cao khi truy vấn chậm.
scientific-db-pubmed-database
280kGiúp agent của bạn tìm bài báo khoa học trong lĩnh vực y sinh. Hữu ích cho nghiên cứu cần thông tin y khoa cụ thể.