-- 0057_search_fts_indexes.sql
-- ----------------------------------------------------------------------------
-- Backing GIN indexes for the to_tsvector expressions used by
-- src/lib/services/search.service.ts. Without these the full-text
-- predicates (`to_tsvector('simple', col) @@ to_tsquery('simple', $1)`)
-- sequential-scan every row in clients / yachts / companies / interests
-- on each search, which becomes painful at scale.
--
-- Built with CREATE INDEX CONCURRENTLY so the index build doesn't lock
-- the table for new writes. That means each statement must run OUTSIDE
-- a transaction — the custom `scripts/db-migrate.ts` runner detects
-- CONCURRENTLY and runs the statement standalone.

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_clients_fulltext
  ON clients USING gin (to_tsvector('simple', coalesce(full_name, '')));
--> statement-breakpoint

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_residential_clients_fulltext
  ON residential_clients USING gin (to_tsvector('simple', coalesce(full_name, '')));
--> statement-breakpoint

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_yachts_fulltext
  ON yachts USING gin (to_tsvector('simple', coalesce(name, '') || ' ' || coalesce(builder, '')));

-- companies search uses plain ILIKE only (no to_tsvector); index omitted.
