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
-type execute_options() :: pgc_client:execute_options().
-type pool_name() :: supervisor:sup_name().
-type pool_options() :: #{max_size => pos_integer()}.
-type pool_ref() :: supervisor:sup_ref().
-type request_error() :: pgc_client:request_error().
-type result_metadata() :: pgc_client:result_metadata().
-type transaction_options() :: #{access => read_write | read_only | default, isolation => serializable | repeatable_read | read_committed | read_uncommitted | default, deferrable => boolean() | default}.
-type transaction_ref() :: reference().
Functions
-spec child_spec(Id, pgc_client_options:t(), pool_options()) -> supervisor:child_spec() when Id :: term().
-spec child_spec(Id, pool_name(), pgc_client_options:t(), pool_options()) -> supervisor:child_spec() when Id :: term().
-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().
-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().
-spec start_link(pgc_client_options:t(), pool_options()) -> {ok, pid()}.
-spec start_link(pool_name(), pgc_client_options:t(), pool_options()) -> {ok, pid()}.
-spec stop(pool_ref()) -> ok.
-spec transaction(PoolRef, Transaction) -> Result when PoolRef :: pool_ref(), Transaction :: fun((transaction_ref()) -> {commit | rollback, Result}).
-spec transaction(PoolRef, Transaction, Options) -> Result when PoolRef :: pool_ref(), Transaction :: fun((transaction_ref()) -> {commit | rollback, Result}), Options :: transaction_options().