Хвост #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.
69 lines
2.8 KiB
PL/PgSQL
69 lines
2.8 KiB
PL/PgSQL
-- 087_fdw_server_options.sql
|
|
-- Issue #769 Part B (finding #13) — FDW server performance options.
|
|
--
|
|
-- Context:
|
|
-- SERVER gendesign_remote was created by 060_postgres_fdw_extension.sql.
|
|
-- The current options are: host, dbname, port, extensions, connect_timeout.
|
|
-- Two planning options are missing:
|
|
-- fetch_size — rows fetched per round-trip (default 100, raise to 10000
|
|
-- for bulk cursor-based pagination in import_rosreestr_dkp).
|
|
-- use_remote_estimate — let planner query ANALYZE stats from the remote server
|
|
-- to produce accurate cost estimates for the 6.8M-row
|
|
-- rosreestr_deals foreign table and quarter_price_index MV.
|
|
--
|
|
-- Idempotency:
|
|
-- ALTER SERVER ... OPTIONS (ADD ...) raises ERROR if the option already exists.
|
|
-- ALTER SERVER ... OPTIONS (SET ...) raises ERROR if the option does not exist yet.
|
|
-- Therefore a DO $$ block checks pg_foreign_server.srvoptions for each option:
|
|
-- - If present → SET (update the value in case it was previously set differently).
|
|
-- - If absent → ADD (insert for the first time).
|
|
-- This is safe to run repeatedly regardless of prior state.
|
|
--
|
|
-- Dependencies:
|
|
-- 060_postgres_fdw_extension.sql (gendesign_remote server must exist).
|
|
--
|
|
-- Deploy order:
|
|
-- Apply after 060_postgres_fdw_extension.sql. No FDW USER MAPPING changes required.
|
|
-- Effect is immediate — next FDW query uses the new planner options.
|
|
|
|
BEGIN;
|
|
|
|
DO $$
|
|
DECLARE
|
|
v_opts text[];
|
|
BEGIN
|
|
-- Read current srvoptions array for gendesign_remote.
|
|
SELECT COALESCE(srvoptions, ARRAY[]::text[])
|
|
INTO v_opts
|
|
FROM pg_foreign_server
|
|
WHERE srvname = 'gendesign_remote';
|
|
|
|
IF NOT FOUND THEN
|
|
RAISE EXCEPTION 'FDW server gendesign_remote not found — apply 060_postgres_fdw_extension.sql first';
|
|
END IF;
|
|
|
|
-- fetch_size: rows per FDW cursor round-trip (default 100 → 10000)
|
|
IF v_opts @> ARRAY['fetch_size=10000'] THEN
|
|
NULL; -- already correct value, no-op
|
|
ELSIF EXISTS (
|
|
SELECT 1 FROM unnest(v_opts) AS opt WHERE opt LIKE 'fetch_size=%'
|
|
) THEN
|
|
ALTER SERVER gendesign_remote OPTIONS (SET fetch_size '10000');
|
|
ELSE
|
|
ALTER SERVER gendesign_remote OPTIONS (ADD fetch_size '10000');
|
|
END IF;
|
|
|
|
-- use_remote_estimate: push ANALYZE stats collection to remote for better plans
|
|
IF v_opts @> ARRAY['use_remote_estimate=true'] THEN
|
|
NULL; -- already correct value, no-op
|
|
ELSIF EXISTS (
|
|
SELECT 1 FROM unnest(v_opts) AS opt WHERE opt LIKE 'use_remote_estimate=%'
|
|
) THEN
|
|
ALTER SERVER gendesign_remote OPTIONS (SET use_remote_estimate 'true');
|
|
ELSE
|
|
ALTER SERVER gendesign_remote OPTIONS (ADD use_remote_estimate 'true');
|
|
END IF;
|
|
END
|
|
$$;
|
|
|
|
COMMIT;
|