- Home
- Skills
- Data & Databases
- Postgres Performance Playbook
Works with the AI Agents you already use
Postgres Performance Playbook
Turn slow Supabase queries around: measure real plans, fix indexes, tame RLS, and stop connection exhaustion with a tested order of operations.
Free
Postgres Performance Playbook
Example session with this skill installed
Our Supabase dashboard is crawling. The posts feed endpoint takes over 4 seconds once users scroll deep, and we started seeing 'remaining connection slots are reserved' errors at around 300 concurrent users. The main table is posts (~2M rows), filtered by author_id, sorted by created_at, with soft deletes via deleted_at. RLS checks visibility = 'public' OR author_id = auth.uid(). Please profile the queries, propose the right composite and partial indexes as CONCURRENTLY migrations, rewrite our offset-based PostgREST paging into keyset pagination, and give me the pooler URL options and timeout settings to fix connection exhaustion.
- Read your context and instructions
- Compiled the postgres performance playbook
Summary
Profiled the two slowest PostgREST endpoints and cut p95 latency from 4.2s to 210ms by replacing a sequential scan and offset paging with a composite partial index and a keyset cursor.
| Metric | Before | After |
|---|---|---|
| p95 feed latency | 4,200 ms | 210 ms |
| Author query plan | Seq Scan + Sort (412k rows) | Index Scan on idx_posts_author_created |
| Page ~5,000 fetch | 1.9 s (OFFSET 100000) | 12 ms (keyset cursor) |
| Pooler timeouts | 34/min | 0 |
Next steps
- Deploy
CREATE INDEX CONCURRENTLY idx_posts_author_created ON posts (author_id, created_at DESC) WHERE deleted_at IS NULL;in a low-traffic window. - Set
statement_timeout=2sandidle_in_transaction_session_timeout=30son the pooler connection string. - Add
post_members (post_id, user_id)index before shipping team-based RLS. - Re-run EXPLAIN (ANALYZE, BUFFERS) once the table passes 1M rows to confirm the plan holds.
Connects securely to your tools. The creator never sees your data.
About this skill
Supabase applications often suffer from high latency and connection exhaustion as data volume scales. This skill provides a systematic order of operations to diagnose bottlenecks, optimize query execution, and stabilize database performance. It focuses on concrete fixes for indexing, RLS policy efficiency, and connection management.
What it does
- Query Profiling provides specific patterns for
EXPLAIN (ANALYZE, BUFFERS)to identify sequential scans and costly sort nodes. - Index Optimization defines rules for composite, partial, and covering indexes based on query selectivity and predicates.
- RLS Tuning simplifies row-level security policies to prevent recursive lookups and expensive subqueries from slowing down auth-scoped requests.
- Connection Management configures transaction pooling settings and timeouts to prevent pool exhaustion in high-concurrency environments.
- Pagination Strategy implements keyset pagination patterns to replace slow
OFFSETandLIMITqueries.
How it works
- Use the measurement commands to generate real query plans and identify high-impact bottlenecks.
- Apply specific index patterns or schema changes based on the identified scan types.
- Refactor RLS policies and application-side
SELECTstatements to reduce I/O and network overhead.
Frameworks & tools
PostgreSQL, Supabase, PostgREST, and PgBouncer.
Why this beats prompting it yourself
Generic AI advice often suggests adding indexes to every column, which destroys write performance. This skill enforces a "read-heavy vs. write-heavy" balance and provides Supabase-specific patterns for RLS and connection pooling that general models miss.
Use cases
- Reducing API latency in a Supabase-backed dashboard with millions of rows.
- Fixing "too many connections" errors during traffic spikes.
- Speeding up slow authenticated queries caused by complex RLS policies.
- Optimizing search and filtering performance for large JSONB datasets.
Known limitations
Does not cover hardware-level scaling or vertical instance upgrades. Requires direct database access for some diagnostic commands.
Assumes Supabase-style transaction poolers and auth.uid(); self-hosted Postgres with different proxies needs the pooling and RLS sections adapted. It cannot fix bottlenecks outside the database, such as app serialization or network transfer.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
1 use
Times used to date
Free to use
No account required to browse
Trust & safety
Security scan
Security scan passed 1 month ago
- Free with an Agensi account
Permissions
File Scopes
Terminal runs read-only EXPLAIN and pg_indexes diagnostics, file reads cover migrations and query code, and environment access is needed to locate pooler connection settings.