---
name: rls-performance-fb765d0f460e
description: >-
  RLS 效能診斷：EXPLAIN ANALYZE、PGRST003 pool timeout、connection pool 耗盡、API 變慢。NOT for RLS policy 的正確性設計（走 supabase-rls）。
---


# RLS Performance Playbook

RLS 效能問題的診斷操作手冊：新增 policy join 或改 policy WHERE、新增含 pagination／filter 的 endpoint、`PGRST003`、pool 耗盡、特定頁面變慢。量測模板、plan 判讀、index、延遲基準在 `db-schema/<variant>/query-optimization.md` 與 `rls-policy.md`，連線池預算與 PGRST003 處置在 `db-runtime/<variant>/database.md`；本 skill 只串起診斷流程並補規約沒有的判讀訊號。

# SOP

## Phase 1 -- 收斂症狀

1. READ 讀取症狀描述與 `log.error`：是否有 `PGRST003`（pool 耗盡，REST 504）、哪個頁面或 endpoint 變慢、最近是否新增或修改 policy join／WHERE、是否新增含分頁的 endpoint。

## Phase 2 -- 量測與判讀

1. DELEGATE 在目標主機量測：慢 query 依 `query-optimization.md` § 效能測量（`pg_stat_statements`）找出 total／mean 最高者，再依 § EXPLAIN ANALYZE 模板以**目標角色**（`set local role`，superuser 會 bypass RLS）跑 `EXPLAIN ANALYZE`；self-hosted 連線問題依 db-runtime `database.md` § Connection Pool Runbook 的 `pg_stat_activity` 查詢按 `usename`／`state` 分組。
2. THINK 讀取 `rules/連線池與RLS診斷判準.md`，搭配 `query-optimization.md` § 讀 plan 的重點，判定是 policy 寫法、缺 index、N+1 還是 pool 問題。

## Phase 3 -- 修正與驗證

1. WRITE 依判定修正：改 policy 寫法（`rls-policy.md` § RLS 效能）、加 index（`query-optimization.md` § Index 設計原則）、N+1 改 `select(*, related(*))` embed 或 RPC，或縮小交易批次（db-runtime `database.md` § Transaction 與批次寫入安全）。pool 問題照 Runbook 處置，**NEVER** 對 pool timeout 盲目 retry。
2. DELEGATE 以相同角色重跑 `EXPLAIN ANALYZE`，比較修正前後與有／無 policy 的 Execution Time 與 `Seq Scan`，依已載入規則判定是否仍超標；仍超標就回 Phase 2。


---
Source attribution: YuDefine
Original project: https://github.com/YuDefine/nuxt-supabase-starter/blob/2987d936c8a6ea7f676c42528192416901bb3ee3/template/.claude/skills/rls-performance/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2025-present YuDefine - 域定資訊工作室

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: YuDefine
Original project: https://github.com/YuDefine/nuxt-supabase-starter/blob/2987d936c8a6ea7f676c42528192416901bb3ee3/template/.claude/skills/rls-performance/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering, Product Management
Roles: Data Analyst, Backend Developer, Database Administrator, ML / AI Engineer, Product Manager
Specialties: Database
Tools: Supabase, PostgreSQL
Country applicability: Country not specified
Classification basis: Source category, role and specialization labels
Instruction SHA-256: 196b14e85befbc5d22fd004741e911df1753151e2b21a107b68617bc58ffda05
