A PostgreSQL client with a pool of connections.

Start a pool from client options and run statements on it:

{ok, ClientOptions} = pgc_client_options:from_uri(~"postgresql://postgres:postgres@localhost/postgres"),
{ok, Pool} = pgc:start_link(ClientOptions, #{max_size => 10}),
{ok, #{command := ~"select", rows := 1}, [#{~"answer" := 42}]} =
    pgc:execute(Pool, {~"select $1::int4 as answer", [42]}).

A transaction holds one connection. Its statements run on the transaction reference, and the fun returns {commit, Result} or {rollback, Result}:

Name = pgc:transaction(Pool, fun (Transaction) ->
    {ok, _, [#{~"name" := Name}]} = pgc:execute(Transaction, {~"select $1::text as name", [~"pgc"]}),
    {commit, Name}
end).

Under a supervisor, start the pool with a child specification:

pgc:child_spec(my_pool, {local, my_pool}, ClientOptions, #{max_size => 10}).

Statements can also be templates, see pgc_statement. For a single connection, see pgc_client.

Summary

Types

execute_options()

-type execute_options() :: pgc_client:execute_options().

pool_name()

-type pool_name() :: supervisor:sup_name().

pool_options()

-type pool_options() :: #{max_size => pos_integer()}.

pool_ref()

-type pool_ref() :: supervisor:sup_ref().

request_error()

-type request_error() :: pgc_client:request_error().

result_metadata()

-type result_metadata() :: pgc_client:result_metadata().

transaction_options()

-type transaction_options() ::
          #{access => read_write | read_only | default,
            isolation => serializable | repeatable_read | read_committed | read_uncommitted | default,
            deferrable => boolean() | default}.

transaction_ref()

-type transaction_ref() :: reference().

Functions

child_spec(Id, ClientOptions, PoolOptions)

-spec child_spec(Id, pgc_client_options:t(), pool_options()) -> supervisor:child_spec()
                    when Id :: term().

child_spec(Id, Name, ClientOptions, PoolOptions)

-spec child_spec(Id, pool_name(), pgc_client_options:t(), pool_options()) -> supervisor:child_spec()
                    when Id :: term().

execute(Client, Statement)

-spec execute(TransactionRef | PoolRef, Statement) -> {ok, Metadata, Rows} | {error, Error}
                 when
                     TransactionRef :: transaction_ref(),
                     PoolRef :: pool_ref(),
                     Statement ::
                         unicode:chardata() |
                         {unicode:chardata(), Parameters} |
                         pgc_statement:template(),
                     Parameters :: [dynamic()],
                     Metadata :: pgc_client:result_metadata(),
                     Rows :: [dynamic()],
                     Error :: pgc_client:request_error().

execute/3

-spec execute(TransactionRef | PoolRef, Statement, Options) -> {ok, Metadata, Rows} | {error, Error}
                 when
                     TransactionRef :: transaction_ref(),
                     PoolRef :: pool_ref(),
                     Statement ::
                         unicode:chardata() |
                         {unicode:chardata(), Parameters} |
                         pgc_statement:t() |
                         pgc_statement:template(),
                     Parameters :: [dynamic()],
                     Options :: pgc_client:execute_options(),
                     Metadata :: pgc_client:result_metadata(),
                     Rows :: [dynamic()],
                     Error :: pgc_client:request_error().

start_link(ClientOptions, PoolOptions)

-spec start_link(pgc_client_options:t(), pool_options()) -> {ok, pid()}.

start_link(Name, ClientOptions, PoolOptions)

-spec start_link(pool_name(), pgc_client_options:t(), pool_options()) -> {ok, pid()}.

stop(PoolRef)

-spec stop(pool_ref()) -> ok.

transaction(PoolRef, Transaction)

-spec transaction(PoolRef, Transaction) -> Result
                     when
                         PoolRef :: pool_ref(),
                         Transaction :: fun((transaction_ref()) -> {commit | rollback, Result}).

transaction(PoolRef, Transaction, Options)

-spec transaction(PoolRef, Transaction, Options) -> Result
                     when
                         PoolRef :: pool_ref(),
                         Transaction :: fun((transaction_ref()) -> {commit | rollback, Result}),
                         Options :: transaction_options().