Skip to content
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

Add get_os_pid/1 function #270

Merged
merged 2 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/epgsql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
get_parameter/2,
set_notice_receiver/2,
get_cmd_status/1,
get_os_pid/1,
squery/2,
equery/2, equery/3, equery/4,
prepared_query/3,
Expand Down Expand Up @@ -260,6 +261,13 @@ set_notice_receiver(C, PidOrName) ->
get_cmd_status(C) ->
epgsql_sock:get_cmd_status(C).

%% @doc Returns the OS pid of PostgreSQL backend OS process that serves this connection.
%%
%% Similar to `SELECT pg_get_pid()', but does not need network roundtrips.
-spec get_os_pid(connection()) -> integer().
get_os_pid(C) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it confusing the terminology around os since this call will be executed from a process within the BEAM that is potentially living in another machine compared to the PG server.
How about renaming it to pg_backend_pid?

Copy link
Member Author

@seriyps seriyps Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'd prefer get_backend_pid though, because epgsql is already about postgres. And in Postgres's SQL it's called pg_backend_pid to show that it is not from SQL standard, but Postgres vendor-specific I believe.

epgsql_sock:get_os_pid(C).

-spec squery(connection(), sql_query()) -> epgsql_cmd_squery:response() | epgsql_sock:error().
%% @doc runs simple `SqlQuery' via given `Connection'
%% @see epgsql_cmd_squery
Expand Down
9 changes: 8 additions & 1 deletion src/epgsql_sock.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
get_cmd_status/1,
cancel/1,
copy_send_rows/3,
standby_status_update/3]).
standby_status_update/3,
get_os_pid/1]).

-export([handle_call/3, handle_cast/2, handle_info/2, format_status/2]).
-export([init/1, code_change/3, terminate/2]).
Expand Down Expand Up @@ -151,6 +152,9 @@ copy_send_rows(C, Rows, Timeout) ->
standby_status_update(C, FlushedLSN, AppliedLSN) ->
gen_server:call(C, {standby_status_update, FlushedLSN, AppliedLSN}).

-spec get_os_pid(epgsql:connection()) -> integer().
get_os_pid(C) ->
gen_server:call(C, get_os_pid).

%% -- command APIs --

Expand Down Expand Up @@ -230,6 +234,9 @@ handle_call({set_async_receiver, PidOrName}, _From, #state{async = Previous} = S
handle_call(get_cmd_status, _From, #state{complete_status = Status} = State) ->
{reply, {ok, Status}, State};

handle_call(get_os_pid, _From, #state{backend = {Pid, _Key}} = State) ->
{reply, Pid, State};

handle_call({standby_status_update, FlushedLSN, AppliedLSN}, _From,
#state{handler = on_replication,
subproto_state = #repl{last_received_lsn = ReceivedLSN} = Repl} = State) ->
Expand Down
5 changes: 5 additions & 0 deletions src/epgsqla.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
get_parameter/2,
set_notice_receiver/2,
get_cmd_status/1,
get_os_pid/1,
squery/2,
equery/2, equery/3,
prepared_query/3,
Expand Down Expand Up @@ -84,6 +85,10 @@ set_notice_receiver(C, PidOrName) ->
get_cmd_status(C) ->
epgsql_sock:get_cmd_status(C).

-spec get_os_pid(epgsql:connection()) -> integer().
get_os_pid(C) ->
epgsql_sock:get_os_pid(C).

-spec squery(epgsql:connection(), epgsql:sql_query()) -> reference().
squery(C, Sql) ->
cast(C, epgsql_cmd_squery, Sql).
Expand Down
5 changes: 5 additions & 0 deletions src/epgsqli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
get_parameter/2,
set_notice_receiver/2,
get_cmd_status/1,
get_os_pid/1,
squery/2,
equery/2, equery/3,
prepared_query/3,
Expand Down Expand Up @@ -81,6 +82,10 @@ set_notice_receiver(C, PidOrName) ->
get_cmd_status(C) ->
epgsql_sock:get_cmd_status(C).

-spec get_os_pid(epgsql:connection()) -> integer().
get_os_pid(C) ->
epgsql_sock:get_os_pid(C).

-spec squery(epgsql:connection(), epgsql:sql_query()) -> reference().
squery(C, Sql) ->
incremental(C, epgsql_cmd_squery, Sql).
Expand Down
11 changes: 10 additions & 1 deletion test/epgsql_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ groups() ->
listen_notify,
listen_notify_payload,
set_notice_receiver,
get_cmd_status
get_cmd_status,
get_os_pid
],
SubGroups ++
[{epgsql, [], [{group, generic} | Tests]},
Expand Down Expand Up @@ -1419,6 +1420,14 @@ get_cmd_status(Config) ->
?assertEqual({ok, 'commit'}, Module:get_cmd_status(C))
end).

get_os_pid(Config) ->
Module = ?config(module, Config),
epgsql_ct:with_connection(Config, fun(C) ->
{ok, [#column{}], [{PidBin}]} = Module:squery(C, "SELECT pg_backend_pid()"),
Pid = Module:get_os_pid(C),
?assertEqual(PidBin, integer_to_binary(Pid))
end).

range_type(Config) ->
epgsql_ct:with_min_version(Config, [9, 2], fun(_C) ->
check_type(Config, int4range, "int4range(10, 20)", {10, 20}, [
Expand Down
6 changes: 5 additions & 1 deletion test/epgsql_cast.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
-module(epgsql_cast).

-export([connect/1, connect/2, connect/3, connect/4, close/1]).
-export([get_parameter/2, set_notice_receiver/2, get_cmd_status/1, squery/2, equery/2, equery/3]).
-export([get_parameter/2, set_notice_receiver/2, get_cmd_status/1, get_os_pid/1,
squery/2, equery/2, equery/3]).
-export([prepared_query/3]).
-export([parse/2, parse/3, parse/4, describe/2, describe/3]).
-export([bind/3, bind/4, execute/2, execute/3, execute/4, execute_batch/2, execute_batch/3]).
Expand Down Expand Up @@ -58,6 +59,9 @@ set_notice_receiver(C, PidOrName) ->
get_cmd_status(C) ->
epgsqla:get_cmd_status(C).

get_os_pid(C) ->
epgsqla:get_os_pid(C).

squery(C, Sql) ->
Ref = epgsqla:squery(C, Sql),
receive_result(C, Ref).
Expand Down
6 changes: 5 additions & 1 deletion test/epgsql_incremental.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
-module(epgsql_incremental).

-export([connect/1, connect/2, connect/3, connect/4, close/1]).
-export([get_parameter/2, set_notice_receiver/2, get_cmd_status/1, squery/2, equery/2, equery/3]).
-export([get_parameter/2, set_notice_receiver/2, get_cmd_status/1, get_os_pid/1,
squery/2, equery/2, equery/3]).
-export([prepared_query/3]).
-export([parse/2, parse/3, parse/4, describe/2, describe/3]).
-export([bind/3, bind/4, execute/2, execute/3, execute/4, execute_batch/2, execute_batch/3]).
Expand Down Expand Up @@ -56,6 +57,9 @@ set_notice_receiver(C, PidOrName) ->
get_cmd_status(C) ->
epgsqli:get_cmd_status(C).

get_os_pid(C) ->
epgsqli:get_os_pid(C).

squery(C, Sql) ->
Ref = epgsqli:squery(C, Sql),
case receive_results(C, Ref, []) of
Expand Down