P PasteCode
Playbook

Prompt-to-PR: Add PostgreSQL Full-Text Search

SOP for adding native PostgreSQL full-text search with tsvector, GIN index, ts_rank, and a Next.js search API — no third-party search service needed.

CursorClaude CodeCodexWindsurf Next.jsPostgreSQLTypeScript
.md .json Difficulty: Hard Updated Jun 8, 2026

PostgreSQL’s built-in full-text search handles most product search needs without Elasticsearch or Algolia. This playbook adds a tsvector column, a GIN index, and a ranked search query behind a Next.js API route.

1. Requirement

Add full-text search over a posts table (columns: title, body, tags). Search should return results ranked by relevance with snippet highlights. The implementation must use a generated tsvector column (not computed at query time) so the GIN index is used.

2. First Prompt

First Prompt
Add native PostgreSQL full-text search to the posts table in this project.
Database: PostgreSQL. ORM/query builder: [Drizzle / postgres.js — use whichever
is already in src/db/].
Step 1 — migration:
Create a migration file (or Drizzle schema change) that:
a. Adds a generated column:
search_vector tsvector GENERATED ALWAYS AS (
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(body, '')), 'B') ||
setweight(to_tsvector('english', coalesce(tags, '')), 'C')
) STORED;
b. Creates a GIN index on search_vector:
CREATE INDEX posts_search_idx ON posts USING gin(search_vector);
Step 2 — query function:
Create `src/lib/queries/search.ts` exporting `searchPosts(q: string, limit = 10)`.
The query must:
- Convert the user query to a tsquery: `websearch_to_tsquery('english', $1)`.
- Filter: `search_vector @@ query`.
- Rank: `ts_rank(search_vector, query) DESC`.
- Return: id, title, ts_headline('english', body, query,
'MaxWords=30, MinWords=15, ShortWord=3, HighlightAll=false') AS snippet.
- Use parameterized query (no string interpolation of the user input).
Step 3 — API route:
Create `src/app/api/search/route.ts` (GET). Read `q` from URL search params.
Return 400 if q is empty or shorter than 2 chars. Return JSON array of results.
Add cache-control: public, max-age=60.
Step 4 — do not touch any UI components.

3. Expected File Changes

src/db/migrations/<timestamp>_add_search_vector.sql (new — or Drizzle migration)
src/db/schema.ts (search_vector column if Drizzle)
src/lib/queries/search.ts (new — searchPosts function)
src/app/api/search/route.ts (new — GET endpoint)

4. Review Checklist

  • GENERATED ALWAYS AS ... STORED — column is stored (not virtual), so the GIN index can be used. Confirm STORED keyword is present.
  • websearch_to_tsquery is used (not to_tsquery) — handles multi-word and phrase queries from untrusted input without syntax errors.
  • The user-supplied query is passed as a SQL parameter, never interpolated.
  • ts_rank orders results — not alphabetical or insertion order.
  • ts_headline length is capped (MaxWords=30) to avoid returning huge snippets.
  • GIN index name is explicit — easier to drop/recreate if needed.
  • API route validates minimum query length (2 chars) to avoid full-table-scan tsqueries like 'a':*.
  • Cache-Control: public, max-age=60 is set on the response — search results can be short-lived cached.

5. Test Commands

Terminal window
# Run the migration
npx drizzle-kit migrate
# or: psql $DATABASE_URL < migration.sql
# Verify the generated column and index exist
psql $DATABASE_URL -c "\d posts" | grep search_vector
psql $DATABASE_URL -c "\di posts_search_idx"
# Query performance — confirm index scan, not seq scan
psql $DATABASE_URL -c "
EXPLAIN ANALYZE
SELECT id FROM posts
WHERE search_vector @@ websearch_to_tsquery('english', 'your test query')
LIMIT 10;
" | grep "Index Scan"
# Test the API endpoint
bun dev &
curl "http://localhost:3000/api/search?q=your+test+query" | jq .
# Confirm empty query returns 400
curl -o /dev/null -w "%{http_code}" "http://localhost:3000/api/search?q="
# Expect: 400

6. Common Failures

  • Seq scan instead of index scanVIRTUAL column (not STORED). Only STORED generated columns can be indexed in PostgreSQL. Confirm the migration uses STORED.
  • to_tsquery throws on multi-word inputto_tsquery('english', 'quick brown') is a syntax error. Always use websearch_to_tsquery for user-supplied strings.
  • ts_headline returns entire bodyHighlightAll=true accidentally set, or MaxWords not set. Check the options string.
  • GIN index not used for LIKE queries — ensure the WHERE clause uses @@ operator, not LIKE or ILIKE. Full-text search and LIKE are separate.
  • Migration fails on existing data — the GENERATED column populates on creation; on large tables this can be slow. Run in a transaction with a progress check.

7. Fix Prompt

Fix Prompt
EXPLAIN ANALYZE shows a sequential scan instead of an index scan on the
posts table. The search_vector column was added as VIRTUAL (or without
the STORED keyword) so PostgreSQL cannot create a GIN index on it.
Fix the migration:
ALTER TABLE posts DROP COLUMN search_vector;
ALTER TABLE posts ADD COLUMN search_vector tsvector
GENERATED ALWAYS AS (
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(body, '')), 'B')
) STORED;
CREATE INDEX posts_search_idx ON posts USING gin(search_vector);
Confirm the word STORED appears in the column definition.

8. PR Description

PR description
## Feature: PostgreSQL native full-text search on posts
- Generated `tsvector` column (`STORED`) with weighted fields:
title (A), body (B), tags (C)
- GIN index `posts_search_idx` — index scans confirmed via `EXPLAIN ANALYZE`
- `searchPosts(q, limit)` uses `websearch_to_tsquery` (safe for user input)
and `ts_rank` for relevance ordering
- `ts_headline` snippets (max 30 words) with matched terms highlighted
- GET `/api/search?q=` — validates query length, returns ranked JSON,
`Cache-Control: public, max-age=60`
No third-party search dependency added.