From 52f6acc22b7d63458033e4137715f3f3fd4c79d5 Mon Sep 17 00:00:00 2001 From: Nathan Long Date: Thu, 12 Mar 2026 16:33:57 -0400 Subject: [PATCH] Set some process labels This makes it easier to understand the process tree in tools like Observer and Phoenix LiveDashboard and gives clearer errors for crashes. --- lib/postgrex/protocol.ex | 8 +++++++- lib/postgrex/replication_connection.ex | 2 ++ lib/postgrex/simple_connection.ex | 2 ++ lib/postgrex/type_server.ex | 1 + lib/postgrex/utils.ex | 11 +++++++++++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/postgrex/protocol.ex b/lib/postgrex/protocol.ex index 7d815471..773be678 100644 --- a/lib/postgrex/protocol.ex +++ b/lib/postgrex/protocol.ex @@ -229,6 +229,7 @@ defmodule Postgrex.Protocol do with {:ok, database} <- fetch_database(opts), status = %{status | types_key: if(types_mod, do: {host, port, database})}, {:ok, ret} <- connect_and_handshake(host, port, sock_opts, timeout, s, status) do + Postgrex.Utils.set_label({Postgrex.Protocol, database}) {:ok, ret} else {:error, err} -> @@ -1907,7 +1908,12 @@ defmodule Postgrex.Protocol do end) ref = make_ref() - {_, mon} = spawn_monitor(fn -> reload_init(s, status, oids, ref, buffer) end) + + {_, mon} = + spawn_monitor(fn -> + Postgrex.Utils.set_label({Postgrex.Protocol, :fetch_type_info}) + reload_init(s, status, oids, ref, buffer) + end) receive do {:DOWN, ^mon, _, _, {^ref, s, buffer}} -> diff --git a/lib/postgrex/replication_connection.ex b/lib/postgrex/replication_connection.ex index afa47aa3..e9966f2d 100644 --- a/lib/postgrex/replication_connection.ex +++ b/lib/postgrex/replication_connection.ex @@ -466,6 +466,8 @@ defmodule Postgrex.ReplicationConnection do @doc false @impl :gen_statem def init({mod, arg, opts}) do + Postgrex.Utils.set_label({Postgrex.ReplicationConnection, mod}) + case mod.init(arg) do {:ok, mod_state} -> opts = diff --git a/lib/postgrex/simple_connection.ex b/lib/postgrex/simple_connection.ex index eaef1f8e..448c26a0 100644 --- a/lib/postgrex/simple_connection.ex +++ b/lib/postgrex/simple_connection.ex @@ -299,6 +299,8 @@ defmodule Postgrex.SimpleConnection do @doc false @impl :gen_statem def init({mod, args, opts}) do + Postgrex.Utils.set_label({Postgrex.SimpleConnection, mod}) + case mod.init(args) do {:ok, mod_state} -> idle_timeout = opts[:idle_timeout] diff --git a/lib/postgrex/type_server.ex b/lib/postgrex/type_server.ex index 65c4662e..5190c30e 100644 --- a/lib/postgrex/type_server.ex +++ b/lib/postgrex/type_server.ex @@ -57,6 +57,7 @@ defmodule Postgrex.TypeServer do ## Callbacks def init({module, starter}) do + Postgrex.Utils.set_label({Postgrex.TypeServer, module}) _ = Process.flag(:trap_exit, true) Process.link(starter) diff --git a/lib/postgrex/utils.ex b/lib/postgrex/utils.ex index 73b067b6..4e4844aa 100644 --- a/lib/postgrex/utils.ex +++ b/lib/postgrex/utils.ex @@ -170,6 +170,17 @@ defmodule Postgrex.Utils do ## Helpers + @doc """ + Set a process label if `Process.set_label/1` is available. + """ + def set_label(label) do + if function_exported?(Process, :set_label, 1) do + apply(Process, :set_label, [label]) + else + :ok + end + end + defp parse_version_bit(bit) do {int, _} = Integer.parse(bit) int