Ecto.Adapters.Postgres — Ecto SQL v3.12.1 (original) (raw)

Adapter module for PostgreSQL.

It uses Postgrex for communicating to the database.

Features

Options

Postgres options split in different categories described below. All options can be given via the repository configuration:

config :your_app, YourApp.Repo,
  ...

The :prepare option may be specified per operation:

YourApp.Repo.all(Queryable, prepare: :unnamed)

Migration options

When using the :pg_advisory_lock migration lock strategy and Ecto cannot obtain the lock due to another instance occupying the lock, Ecto will wait for 5 seconds and then retry infinity times. This is configurable on the repo with keys:migration_advisory_lock_retry_interval_ms and :migration_advisory_lock_max_tries. If the retries are exhausted, the migration will fail.

Some downsides to using advisory locks is that some Postgres-compatible systems or plugins may not support session level locks well and therefore result in inconsistent behavior. For example, PgBouncer when using pool_modes other than session won't work well with advisory locks. CockroachDB is another system that is designed in a way that advisory locks don't make sense for their distributed database.

Connection options

The :socket_options are particularly useful when configuring the size of both send and receive buffers. For example, when Ecto starts with a pool of 20 connections, the memory usage may quickly grow from 20MB to 50MB based on the operating system default values for TCP buffers. It is advised to stick with the operating system defaults but they can be tweaked if desired:

socket_options: [recbuf: 8192, sndbuf: 8192]

We also recommend developers to consult the Postgrex.start_link/1documentation for a complete listing of all supported options.

Storage options

After connect callback

If you want to execute a callback as soon as connection is established to the database, you can use the :after_connect configuration. For example, in your repository configuration you can add:

after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]}

You can also specify your own module that will receive the Postgrex connection as argument.

Extensions

Both PostgreSQL and its adapter for Elixir, Postgrex, support an extension system. If you want to use custom extensions for Postgrex alongside Ecto, you must define a type module with your extensions. Create a new file anywhere in your application with the following:

Postgrex.Types.define(MyApp.PostgresTypes,
                      [MyExtension.Foo, MyExtensionBar] ++ Ecto.Adapters.Postgres.extensions())

Once your type module is defined, you can configure the repository to use it:

config :my_app, MyApp.Repo, types: MyApp.PostgresTypes