supabase-cost-tuning
Optimize Supabase costs through plan selection, database tuning, storage cleanup, connection pooling, and Edge Function optimization. Use when analyzing Supabase billing, reducing costs, right-sizing compute, or implementing usage tracking and budget alerts. Trigger with phrases like "supabase cost", "supabase billing", "reduce supabase costs", "supabase pricing", "supabase expensive", "supabase budget".
What this skill does
# Supabase Cost Tuning
## Overview
Reduce Supabase spend by auditing usage against plan limits, eliminating database and storage waste, and right-sizing compute resources. The three biggest levers: database optimization (vacuum, index cleanup, archival), storage lifecycle management (compress before upload, orphan cleanup), and connection pooling to reduce compute add-on requirements.
## Prerequisites
- Supabase project with Dashboard access (Settings > Billing)
- `@supabase/supabase-js` installed: `npm install @supabase/supabase-js`
- Service role key for admin operations (storage audit, cleanup scripts)
- SQL editor access (Dashboard > SQL Editor or `psql` connection)
## Pricing Reference
| Resource | Free Tier | Pro ($25/mo) | Team ($599/mo) |
|----------|-----------|--------------|----------------|
| Database | 500 MB | 8 GB included, $0.125/GB extra | 8 GB included |
| Storage | 1 GB | 100 GB included, $0.021/GB extra | 100 GB included |
| Bandwidth | 5 GB | 250 GB included, $0.09/GB extra | 250 GB included |
| Edge Functions | 500K invocations | 2M invocations, $2/million extra | 2M invocations |
| Realtime | 200 concurrent | 500 concurrent | 500 concurrent |
| Auth MAU | 50,000 | 100,000 | 100,000 |
**Compute add-ons** (Pro and above):
| Instance | vCPUs | RAM | Price |
|----------|-------|-----|-------|
| Micro | 2 | 1 GB | Included with Pro |
| Small | 2 | 2 GB | $25/mo |
| Medium | 2 | 4 GB | $50/mo |
| Large | 4 | 8 GB | $100/mo |
| XL | 8 | 16 GB | $200/mo |
| 2XL | 16 | 32 GB | $400/mo |
**Decision framework:** Read replicas ($25/mo each) beat scaling up when reads dominate and you need geographic distribution. Connection pooling (Supavisor, free) reduces compute pressure from idle connections.
## Instructions
### Step 1: Audit Current Usage and Identify Cost Drivers
Run these queries in the SQL Editor to understand where your database budget is going:
```sql
-- Total database size
select pg_size_pretty(pg_database_size(current_database())) as total_db_size;
-- Database size by table (find the biggest offenders)
select
relname as table_name,
pg_size_pretty(pg_total_relation_size(relid)) as total_size,
pg_size_pretty(pg_relation_size(relid)) as table_size,
pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as index_size,
n_live_tup as row_count
from pg_stat_user_tables
order by pg_total_relation_size(relid) desc
limit 20;
-- Find unused indexes consuming space (zero scans since last stats reset)
select
schemaname || '.' || indexrelname as index_name,
pg_size_pretty(pg_relation_size(indexrelid)) as size,
idx_scan as scans_since_reset
from pg_stat_user_indexes
where idx_scan = 0
and schemaname = 'public'
order by pg_relation_size(indexrelid) desc
limit 10;
-- Check dead tuple bloat (high ratio means VACUUM is needed)
select
relname,
n_dead_tup,
n_live_tup,
round(n_dead_tup::numeric / greatest(n_live_tup, 1) * 100, 1) as dead_pct
from pg_stat_user_tables
where n_dead_tup > 1000
order by n_dead_tup desc;
-- Connection count (high count may indicate pooling issues)
select count(*) as active_connections,
max_conn as max_allowed
from pg_stat_activity,
(select setting::int as max_conn from pg_settings where name = 'max_connections') mc
group by max_conn;
```
Audit storage usage programmatically:
```typescript
import { createClient } from '@supabase/supabase-js'
const supabaseAdmin = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!
)
// List storage usage per bucket
const { data: buckets } = await supabaseAdmin.storage.listBuckets()
for (const bucket of buckets ?? []) {
const { data: files } = await supabaseAdmin.storage
.from(bucket.name)
.list('', { limit: 1000 })
const totalSize = files?.reduce((sum, f) => sum + (f.metadata?.size || 0), 0) ?? 0
console.log(`${bucket.name}: ${(totalSize / 1024 / 1024).toFixed(1)} MB`)
}
```
Check your current spend: **Dashboard > Settings > Billing** shows usage against plan limits with a breakdown by resource category.
### Step 2: Optimize Database, Storage, and Bandwidth
**Database optimization — reclaim space and reduce bloat:**
```sql
-- Archive old data before deleting (preserve for compliance/analytics)
create table if not exists public.events_archive (like public.events including all);
insert into public.events_archive
select * from public.events
where created_at < now() - interval '6 months';
delete from public.events
where created_at < now() - interval '6 months';
-- Run VACUUM ANALYZE to reclaim space and update query planner stats
vacuum (verbose, analyze) public.events;
-- Drop confirmed-unused indexes (verify idx_scan = 0 from Step 1)
-- WARNING: always confirm the index is unused before dropping
drop index if exists idx_events_legacy_status;
-- Remove soft-deleted records past retention period
delete from public.orders
where deleted_at is not null
and deleted_at < now() - interval '90 days';
vacuum (analyze) public.orders;
```
**Storage optimization — compress before upload, clean orphans:**
```typescript
// Compress images before upload (reduces storage + bandwidth)
async function uploadCompressed(
bucket: string,
path: string,
file: File
): Promise<string> {
// Use client-side compression before uploading
const compressed = await compressImage(file, { maxWidth: 1920, quality: 0.8 })
const { data, error } = await supabaseAdmin.storage
.from(bucket)
.upload(path, compressed, {
contentType: file.type,
upsert: true,
})
if (error) throw error
return data.path
}
// Clean orphaned files older than 30 days
async function cleanOrphanedUploads() {
const cutoff = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString()
const { data: orphans } = await supabaseAdmin
.from('storage.objects')
.select('name, created_at')
.eq('bucket_id', 'uploads')
.lt('created_at', cutoff)
if (orphans?.length) {
const paths = orphans.map(o => o.name)
// Delete in batches of 100
for (let i = 0; i < paths.length; i += 100) {
await supabaseAdmin.storage
.from('uploads')
.remove(paths.slice(i, i + 100))
}
console.log(`Cleaned ${orphans.length} orphaned files`)
}
}
```
**Bandwidth reduction — select only what you need:**
```typescript
// BAD: transfers entire row (wastes bandwidth)
const { data } = await supabase.from('products').select('*')
// GOOD: request only needed columns
const { data } = await supabase.from('products').select('id, name, price')
// Use count queries for totals (head: true = zero data transferred)
const { count } = await supabase
.from('orders')
.select('*', { count: 'exact', head: true })
// Paginate large result sets
const { data } = await supabase
.from('logs')
.select('id, message, created_at')
.order('created_at', { ascending: false })
.range(0, 49) // 50 rows per page
```
### Step 3: Right-Size Compute and Reduce Edge Function Costs
**Connection pooling with Supavisor** (reduces need for compute upgrades):
```typescript
// Use the pooler connection string instead of direct connection
// Dashboard > Settings > Database > Connection string > Mode: Transaction
// In your app, use the pooled connection URL (port 6543)
// Direct: postgresql://postgres:[email protected]:5432/postgres
// Pooled: postgresql://postgres:[email protected]:6543/postgres
// For @supabase/supabase-js, connection pooling is handled automatically
// For direct pg connections (migrations, ORMs), use pooled URL:
const pool = new Pool({
connectionString: process.env.DATABASE_URL, // Use pooler URL
max: 10, // Limit client-side pool size too
})
```
**Edge Function cold start reduction:**
```typescript
// Minimize cold starts — keep imports lightweight
// BAD: importing heavy libraries unconditionally
import { parse } from 'some-huge-csv-library'
// GOOD: dynamic import only when needed
Deno.serve(async (req) => {
const { action } = await req.jsonRelated in Data & Analytics
clawarr-suite
IncludedComprehensive management for self-hosted media stacks (Sonarr, Radarr, Lidarr, Readarr, Prowlarr, Bazarr, Overseerr, Plex, Tautulli, SABnzbd, Recyclarr, Unpackerr, Notifiarr, Maintainerr, Kometa, FlareSolverr). Deep library exploration, analytics, dashboard generation, content management, request handling, subtitle management, indexer control, download monitoring, quality profile sync, library cleanup automation, notification routing, collection/overlay management, and media tracker integration (Trakt, Letterboxd, Simkl).
querying-soql
IncludedSOQL query generation, optimization, and analysis with 100-point scoring. Use this skill when the user needs SOQL/SOSL authoring or optimization: natural-language-to-query generation, relationship queries, aggregates, query-plan analysis, and performance or safety improvements for Salesforce queries. TRIGGER when: user writes, optimizes, or debugs SOQL/SOSL queries, touches .soql files, or asks about relationship queries, aggregates, or query performance. DO NOT TRIGGER when: bulk data operations (use handling-sf-data), Apex DML logic (use generating-apex), or report/dashboard queries.
app-store-optimization
IncludedApp Store Optimization (ASO) toolkit for researching keywords, analyzing competitor rankings, generating metadata suggestions, and improving app visibility on Apple App Store and Google Play Store. Use when the user asks about ASO, app store rankings, app metadata, app titles and descriptions, app store listings, app visibility, or mobile app marketing on iOS or Android. Supports keyword research and scoring, competitor keyword analysis, metadata optimization, A/B test planning, launch checklists, and tracking ranking changes.
habit-flow
IncludedAI-powered atomic habit tracker with natural language logging, streak tracking, smart reminders, and coaching. Use for creating habits, logging completions naturally ("I meditated today"), viewing progress, and getting personalized coaching.
app-store-optimization
IncludedApp Store Optimization (ASO) toolkit for researching keywords, analyzing competitor rankings, generating metadata suggestions, and improving app visibility on Apple App Store and Google Play Store. Use when the user asks about ASO, app store rankings, app metadata, app titles and descriptions, app store listings, app visibility, or mobile app marketing on iOS or Android. Supports keyword research and scoring, competitor keyword analysis, metadata optimization, A/B test planning, launch checklists, and tracking ranking changes.
visualizing-data
IncludedBuilds dashboards, reports, and data-driven interfaces requiring charts, graphs, or visual analytics. Provides systematic framework for selecting appropriate visualizations based on data characteristics and analytical purpose. Includes 24+ visualization types organized by purpose (trends, comparisons, distributions, relationships, flows, hierarchies, geospatial), accessibility patterns (WCAG 2.1 AA compliance), colorblind-safe palettes, and performance optimization strategies. Use when creating visualizations, choosing chart types, displaying data graphically, or designing data interfaces.