Postgres indexes every SaaS app needs (and how to find missing ones)
The indexes multi-tenant SaaS apps almost always need, how to find slow queries and missing indexes in Postgres, and the mistakes that make indexes useless.
Samed Škulj
· 3 min read
Many SaaS apps run fine for the first year, then slow down page by page as the biggest customers' data grows. Often the cause is simple: queries that scan whole tables because the right index doesn't exist. Here are the indexes most SaaS apps need, and how to find what's missing in yours.
The indexes almost every SaaS app needs
Tenant column, first
In a multi-tenant app, nearly every query filters by organization_id (or account_id, workspace_id). Index it on every tenant-scoped table. Better still, make it the first column of composite indexes for the queries you actually run:
create index bookings_org_starts_idx
on bookings (organization_id, starts_at desc);
This one index serves "this organization's bookings, newest first" without sorting.
Foreign keys
Postgres does not automatically index foreign key columns. Without an index on bookings.customer_id, loading a customer's bookings scans the table, and deleting a customer checks every booking row. Index the foreign keys you join or filter on.
Lookups by external IDs and unique fields
Email addresses, Stripe customer IDs, external sync IDs, slugs. These are looked up constantly and should be unique indexes:
create unique index users_email_idx on users (lower(email));
Note the lower(): if your query uses lower(email), the index must too.
Status queues
Background jobs and workflows often query "pending items, oldest first". A partial index keeps this small and fast:
create index jobs_pending_idx
on jobs (created_at)
where status = 'pending';
How to find slow queries
pg_stat_statements
Enable the pg_stat_statements extension (most managed Postgres providers support it). Then:
select query, calls, mean_exec_time, total_exec_time
from pg_stat_statements
order by total_exec_time desc
limit 20;
Sort by total time, not mean time. A 20 ms query called a million times matters more than a 2 s query called twice.
EXPLAIN ANALYZE
For each suspect query, run it with real parameters:
explain (analyze, buffers)
select * from bookings
where organization_id = '...'
order by starts_at desc
limit 50;
Look for Seq Scan on large tables, a Sort step on many rows, and big gaps between estimated and actual row counts.
Tables that are scanned too often
select relname, seq_scan, idx_scan, n_live_tup
from pg_stat_user_tables
order by seq_scan desc;
A large table with many sequential scans and few index scans is missing an index.
Mistakes that make indexes useless
- Wrong column order. An index on
(starts_at, organization_id)doesn't help a query that filters by organization and sorts by date. Put equality filters first, then range or sort columns. - Functions on the column.
where date(created_at) = ...can't use an index oncreated_at. Rewrite as a range. - Leading wildcards.
like '%term'can't use a normal index. Use full-text search or a trigram index. - Too many indexes. Each one slows writes and takes space. Remove unused ones;
pg_stat_user_indexesshows which are never scanned.
Creating indexes safely in production
On a large, live table, use create index concurrently so writes aren't blocked while it builds. It takes longer, can't run inside a transaction, and needs checking afterwards, but it avoids downtime.
Indexes fix slow individual queries; fixing N+1 patterns fixes too many queries. Most slow SaaS pages need both. A performance and code audit covers the database, the API and the frontend, with a ranked fix list.

Written by Samed Škulj
Senior Full-Stack Engineer since 2021. I have shipped with teams at Deel, HTEC Group (for Zoox), Ministry of Programming, and now help SaaS teams add AI features and get AI-built apps ready for production. LinkedIn