[ PROMPT_NODE_23490 ]
Supabase Postgres Best Practices 智能体
[ SKILL_DOCUMENTATION ]
# Postgres 最佳实践
**版本 1.0.0**
Supabase
2026 年 1 月
> 本文档针对 AI 智能体和 LLM 进行了优化。规则按性能影响程度排序。
---
## 摘要
为使用 Supabase 和 Postgres 的开发者提供的全面 Postgres 性能优化指南。包含 8 个类别的性能规则,按影响程度从关键(查询性能、连接管理)到增量(高级特性)排序。每条规则均包含详细解释、错误与正确 SQL 示例、查询计划分析以及具体的性能指标,以指导自动化优化和代码生成。
---
## 目录
1. [查询性能](#query-performance) - **关键**
- 1.1 [在 WHERE 和 JOIN 列上添加索引](#11-add-indexes-on-where-and-join-columns)
- 1.2 [为数据选择正确的索引类型](#12-choose-the-right-index-type-for-your-data)
- 1.3 [为多列查询创建复合索引](#13-create-composite-indexes-for-multi-column-queries)
- 1.4 [使用覆盖索引避免表查找](#14-use-covering-indexes-to-avoid-table-lookups)
- 1.5 [使用部分索引进行过滤查询](#15-use-partial-indexes-for-filtered-queries)
2. [连接管理](#connection-management) - **关键**
- 2.1 [配置空闲连接超时](#21-configure-idle-connection-timeouts)
- 2.2 [设置适当的连接限制](#22-set-appropriate-connection-limits)
- 2.3 [为所有应用使用连接池](#23-use-connection-pooling-for-all-applications)
- 2.4 [在连接池中正确使用预处理语句](#24-use-prepared-statements-correctly-with-pooling)
3. [安全与 RLS](#security-rls) - **关键**
- 3.1 [应用最小权限原则](#31-apply-principle-of-least-privilege)
- 3.2 [为多租户数据启用行级安全性 (RLS)](#32-enable-row-level-security-for-multi-tenant-data)
- 3.3 [优化 RLS 策略以提升性能](#33-optimize-rls-policies-for-performance)
4. [模式设计](#schema-design) - **高**
- 4.1 [选择适当的数据类型](#41-choose-appropriate-data-types)
- 4.2 [为外键列建立索引](#42-index-foreign-key-columns)
- 4.3 [对大表进行分区以提升性能](#43-partition-large-tables-for-better-performance)
- 4.4 [选择最优主键策略](#44-select-optimal-primary-key-strategy)
- 4.5 [使用小写标识符以确保兼容性](#45-use-lowercase-identifiers-for-compa