174 words, 1 min read

When working with AshPostgres and extensions like vectors, you’ll need to tell Postgrex about custom types. Fortunately, this only requires a small configuration update.

First, create a new file at lib/postgrex_types.ex and define a custom type module:

# lib/postgrex_types.ex
Postgrex.Types.define(
MyApp.PostgrexTypes,
[AshPostgres.Extensions.Vector] ++ Ecto.Adapters.Postgres.extensions(),
[]
)

Here we extend the default Postgres types with AshPostgres.Extensions.Vector.

Next, add this type definition to your repo configuration.

config/dev.exs

config :my_app, MyApp.Repo,
types: MyApp.PostgrexTypes, # <--- Add this
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "my_app_dev",
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10

config/runtime.exs

config :my_app, MyApp.Repo,
# ssl: true,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
# For machines with several cores, consider starting multiple pools of `pool_size`
# pool_count: 4,
socket_options: maybe_ipv6,
types: MyApp.PostgrexTypes # <--- Add this

config/test.exs

config :my_app, MyApp.Repo,
types: MyApp.PostgrexTypes, # <--- Add this
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "my_app_test#{System.get_env("MIX_TEST_PARTITION")}",
pool: Ecto.Adapters.SQL.Sandbox,
pool_size: System.schedulers_online() * 2

By defining MyApp.PostgrexTypes and updating your repo configuration in dev, test, and runtime, your application will be able to handle custom Postgres types like vectors seamlessly.