gendesign/tradein-mvp/backend/data/sql/086_deals_address_trgm_index.sql
bot-backend dffe1ccc18 fix(tradein): data-layer tail A2+B+C — trgm index, FDW options, matview refresh (Refs #769)
Хвост #769 (A1+D смержены #798; E отложен — geo-radius client-risk).

A2 (#10 index): 086 — pg_trgm GIN индекс на deals.address (был seq-scan на
street-match). Идемпот (IF NOT EXISTS), plain CREATE INDEX (txn-safe).

B (#13 FDW): 087 — ALTER SERVER gendesign_remote OPTIONS fetch_size=10000 +
use_remote_estimate=true (лучше планируется 6.8M-row FDW import). DO-block
читает srvoptions → ADD-or-SET, идемпотентно.

C (#14 matview): listings_search_mv активно читается search_query.py но НИКОГДА
не рефрешился (refresh_search_matview.py существует, в beat/scheduler не было).
scheduler.py: + trigger_refresh_search_matview_run (по образцу asking_to_sold,
через _claim_run advisory-lock #750, run_in_executor — refresh CONCURRENTLY с
autocommit). 088 — seed scrape_schedules (03:00-04:00 UTC ночь, ON CONFLICT DO
NOTHING). ruff clean.

Post-deploy QA: EXPLAIN deals street-query → Bitmap Index Scan; FDW remote-estimate.
2026-05-30 20:38:32 +03:00

34 lines
1.4 KiB
PL/PgSQL

-- 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;