-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add prepared statement support (prepare/query_prepared) #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,10 @@ | |
| query/2, | ||
| query/3, | ||
| query/4, | ||
| prepare/2, | ||
| prepare/3, | ||
| query_prepared/3, | ||
| query_prepared/4, | ||
| transaction/1, | ||
| transaction/2, | ||
| transaction/3, | ||
|
|
@@ -148,6 +152,89 @@ query(Query, Params, Options, Conn=#conn{trace=TraceDefault, | |
| #{queue_time => undefined}) | ||
| end). | ||
|
|
||
| %% @doc Prepare a named statement on the given pool. | ||
| %% Returns {ok, Name, ParameterOIDs} which can be passed to query_prepared/3,4. | ||
| -spec prepare(iodata(), iodata()) -> {ok, iodata(), [pg_types:oid()]} | {error, term()}. | ||
| prepare(Name, Query) -> | ||
| prepare(Name, Query, #{}). | ||
|
|
||
| -spec prepare(iodata(), iodata(), options()) -> {ok, iodata(), [pg_types:oid()]} | {error, term()}. | ||
| prepare(Name, Query, Options) -> | ||
| pgo_prepared_cache:init(), | ||
| Pool = maps:get(pool, Options, default), | ||
| PoolOptions = maps:get(pool_options, Options, []), | ||
| case checkout(Pool, PoolOptions) of | ||
| {ok, Ref, Conn} -> | ||
| try | ||
| case pgo_handler:prepare(Conn, Name, Query) of | ||
| {ok, N, OIDs} = Result -> | ||
| pgo_prepared_cache:store(N, Query, OIDs), | ||
| Result; | ||
| Error -> | ||
| Error | ||
| end | ||
| after | ||
| checkin(Ref, Conn) | ||
| end; | ||
| {error, _} = E -> | ||
| E | ||
| end. | ||
|
|
||
| %% @doc Execute a previously prepared named statement. | ||
| %% ParameterOIDs is the list returned by prepare/2,3. | ||
| -spec query_prepared(iodata(), list(), [pg_types:oid()]) -> result(). | ||
| query_prepared(Name, Params, ParameterOIDs) -> | ||
| query_prepared(Name, Params, ParameterOIDs, #{}). | ||
|
|
||
| -spec query_prepared(iodata(), list(), [pg_types:oid()], options()) -> result(). | ||
| query_prepared(Name, Params, ParameterOIDs, Options) -> | ||
| pgo_prepared_cache:init(), | ||
| Pool = maps:get(pool, Options, default), | ||
| PoolOptions = maps:get(pool_options, Options, []), | ||
| DecodeOptions = maps:get(decode_opts, Options, []), | ||
| case checkout(Pool, PoolOptions) of | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any existing transaction is ignored? |
||
| {ok, Ref={_, _, _, Holder}, Conn=#conn{owner=Owner, decode_opts=DefaultDecodeOpts}} -> | ||
| try | ||
| NameBin = iolist_to_binary(Name), | ||
| _ = maybe_prepare_on_conn(Owner, NameBin, Conn), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this mean if preparing fails it'll continue on and ultimately return the wrong error message when it should tell the user why the preparation failed? |
||
| pgo_handler:prepared_query(Conn, Name, Params, ParameterOIDs, | ||
| DecodeOptions ++ DefaultDecodeOpts) | ||
| of | ||
| {error, closed} -> | ||
| maybe_timeout_error(Holder); | ||
| {error, einval} -> | ||
| maybe_timeout_error(Holder); | ||
| Result -> | ||
| Result | ||
| after | ||
| checkin(Ref, Conn) | ||
| end; | ||
| {error, _} = E -> | ||
| E | ||
| end. | ||
|
|
||
| maybe_prepare_on_conn(Owner, NameBin, Conn) -> | ||
| Key = {Owner, NameBin}, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| case pgo_prepared_cache:is_conn_prepared(Key) of | ||
| true -> | ||
| ok; | ||
| false -> | ||
| case pgo_prepared_cache:lookup(NameBin) of | ||
| {ok, Query, _OIDs} -> | ||
| case pgo_handler:prepare(Conn, NameBin, Query) of | ||
| {ok, _, _} -> | ||
| pgo_prepared_cache:mark_conn_prepared(Key); | ||
| {error, {pgsql_error, #{code := <<"42P05">>}}} -> | ||
| %% Already prepared (e.g. from a previous session) | ||
| pgo_prepared_cache:mark_conn_prepared(Key); | ||
| Error -> | ||
| Error | ||
| end; | ||
| not_found -> | ||
| ok | ||
| end | ||
| end. | ||
|
|
||
| %% @equiv transaction(default, Fun, []) | ||
| -spec transaction(fun(() -> any())) -> any() | {error, any()}. | ||
| transaction(Fun) -> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| %% @doc ETS-based cache for prepared statement metadata. | ||
| %% | ||
| %% Stores statement name to {query, parameter OIDs} mappings, and tracks | ||
| %% which connections have each statement prepared. | ||
| -module(pgo_prepared_cache). | ||
|
|
||
| -export([init/0, store/3, lookup/1, is_conn_prepared/1, mark_conn_prepared/1]). | ||
|
|
||
| -define(TABLE, pgo_prepared_cache). | ||
| -define(CONN_TABLE, pgo_prepared_conn_cache). | ||
|
|
||
| %% @doc Initialize cache tables. Safe to call multiple times. | ||
| init() -> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a process so it can be supervised and the tables aren't owned by whoever called |
||
| init_table(?TABLE), | ||
| init_table(?CONN_TABLE). | ||
|
|
||
| %% @doc Store a prepared statement's query and parameter OIDs. | ||
| -spec store(iodata(), iodata(), [pg_types:oid()]) -> ok. | ||
| store(Name, Query, OIDs) -> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be per-pool? Or maybe the whole cache should be per-pool and started in the supervisor with the pool? |
||
| ets:insert(?TABLE, {iolist_to_binary(Name), iolist_to_binary(Query), OIDs}), | ||
| ok. | ||
|
|
||
| %% @doc Look up a prepared statement's query and OIDs by name. | ||
| -spec lookup(iodata()) -> {ok, binary(), [pg_types:oid()]} | not_found. | ||
| lookup(Name) -> | ||
| case ets:lookup(?TABLE, iolist_to_binary(Name)) of | ||
| [{_, Query, OIDs}] -> {ok, Query, OIDs}; | ||
| [] -> not_found | ||
| end. | ||
|
|
||
| %% @doc Check if a statement has been prepared on a specific connection. | ||
| -spec is_conn_prepared({pid(), binary()}) -> boolean(). | ||
| is_conn_prepared(Key) -> | ||
| ets:member(?CONN_TABLE, Key). | ||
|
|
||
| %% @doc Mark a statement as prepared on a specific connection. | ||
| -spec mark_conn_prepared({pid(), binary()}) -> ok. | ||
| mark_conn_prepared(Key) -> | ||
| ets:insert(?CONN_TABLE, {Key}), | ||
| ok. | ||
|
|
||
| init_table(Name) -> | ||
| case ets:whereis(Name) of | ||
| undefined -> | ||
| ets:new(Name, [named_table, public, set, {read_concurrency, true}]); | ||
| _ -> | ||
| ok | ||
| end. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why setup caches here like this?