Prompt
Building search over Postgres records with fast indexing
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
If you want fast search over Postgres records, the usual answer is: use the right indexes for the search type rather than trying to “index everything.”
Main options
1) Exact match / filtering
Use standard B-tree indexes.
CREATE INDEX idx_users_email ON users (email);
CREATE INDEX idx_orders_status_created_at ON orders (status, created_at);
Good for:
=<,>BETWEENORDER BYon indexed columns
2) Case-insensitive search
Use lower() or citext.
CREATE INDEX idx_users_email_lower ON users (lower(email));
Query:
SELECT * FROM users
WHERE lower(email) = lower('Test@Example.com');
Or use citext:
CREATE EXTENSION IF NOT EXISTS citext;
ALTER TABLE users ALTER COLUMN email TYPE citext;
3) Partial text search with LIKE
For prefix search:
CREATE INDEX idx_products_name_prefix ON products (name text_pattern_ops);
Query:
SELECT * FROM products
WHERE name LIKE 'iph%';
For anywhere-substring search (%iph%), use trigram indexes.
4) Fuzzy / substring search
Use trigram indexes via pg_trgm.
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX idx_products_name_trgm ON products USING gin (name gin_trgm_ops);
Query:
SELECT * FROM products
WHERE name ILIKE '%iphone%';
This is often the best option for “search box” style lookup.
5) Full-text search
If you need linguistic search over text content, use Postgres full-text search.
ALTER TABLE articles
ADD COLUMN search_vector tsvector
GENERATED ALWAYS AS (
to_tsvector('english', coalesce(title, '') || ' ' || coalesce(body, ''))
) STORED;
CREATE INDEX idx_articles_search_vector ON articles USING gin (search_vector);
Query:
SELECT *
FROM articles
WHERE search_vector @@ plainto_tsquery('english', 'fast indexing');
Good for:
- documents
- articles
- search by words, stemming, ranking
Fast indexing strategy
A practical pattern is:
- Use B-tree for structured fields.
- Use GIN + pg_trgm for user-facing text search.
- Use full-text search for content search.
- Avoid scanning large text columns without indexes.
- Only index columns you actually query.
If you need “Google-like” search
Postgres can handle a lot, but if you need:
- autocomplete
- typo tolerance
- ranking
- faceting
- massive scale
you may eventually want a dedicated search engine like:
- Elasticsearch / OpenSearch
- Meilisearch
- Typesense
- Algolia
Good default for most apps
If you’re building a basic search over records, I’d usually recommend:
- B-tree indexes for exact filters
pg_trgmGIN indexes for text search- full-text search for article/document content
If you want, I can give you:
- a schema design for searchable Postgres records,
- example indexes for your specific table, or
- a Django / Rails / Node / SQLAlchemy implementation.