-- 086_deals_address_trgm_index.sql
-- Issue #769 Part A2 (finding #10) — GIN trigram index on deals.address.
--
-- Context:
--   The deals table (002_core_tables.sql line 84) has an `address text` column used
--   in street-match queries (import_rosreestr_dkp in scheduler.py builds ILIKE / text
--   predicates). Without a trigram index the planner falls back to seq-scan on the
--   full deals table. pg_trgm is already enabled in 050_search_optimization.sql but
--   only for the listings table — not for deals.
--
-- Idempotency:
--   CREATE EXTENSION IF NOT EXISTS   — safe on re-run (ext already active = no-op).
--   CREATE INDEX IF NOT EXISTS       — safe on re-run.
--   Both inside a single BEGIN/COMMIT — plain CREATE INDEX (not CONCURRENTLY) is
--   required to stay transaction-safe (CONCURRENTLY cannot run inside a txn block).
--   Acceptable here because deals is populated incrementally via FDW import batches
--   and the table starts small.
--
-- Dependencies:
--   002_core_tables.sql (deals table with address column).
--
-- Deploy order:
--   Apply after 002_core_tables.sql. No application code changes required.
--   Post-deploy QA: EXPLAIN (ANALYZE, BUFFERS) SELECT ... FROM deals WHERE address
--   ILIKE '%улица%'; should show Bitmap Index Scan on deals_address_trgm_idx.

BEGIN;

CREATE EXTENSION IF NOT EXISTS pg_trgm;

CREATE INDEX IF NOT EXISTS deals_address_trgm_idx
    ON deals USING gin (address gin_trgm_ops);

COMMIT;
