towerops/priv/repo/migrations/20260404000001_add_geoip_gist_index.exs

27 lines
943 B
Elixir

defmodule Towerops.Repo.Migrations.AddGeoipGistIndex do
use Ecto.Migration
@disable_ddl_transaction true
@disable_migration_lock true
def up do
# btree_gist is required to create a GiST index on int8range expressions
execute "CREATE EXTENSION IF NOT EXISTS btree_gist"
# GiST index for IP range containment queries using the @> operator.
# Turns the full sequential scan (530ms, 22K calls) into a fast index scan.
# The companion query change in geoip.ex uses:
# WHERE int8range(start_ip_int, end_ip_int, '[]') @> $1::bigint
execute """
CREATE INDEX CONCURRENTLY IF NOT EXISTS geoip_blocks_ip_range_gist
ON geoip_blocks USING gist (int8range(start_ip_int, end_ip_int, '[]'))
"""
# Update planner statistics so the new index is used immediately
execute "ANALYZE geoip_blocks"
end
def down do
execute "DROP INDEX CONCURRENTLY IF EXISTS geoip_blocks_ip_range_gist"
end
end