From f807147752674568ac21acd77b750ee82f5f46e3 Mon Sep 17 00:00:00 2001 From: "Jonathan C. Otsuka" Date: Wed, 9 Nov 2022 15:09:28 -0600 Subject: [PATCH] Add ability to gets all active connections to the database. This adds the ability to get all active connections to the database. Co-authored-by: Matt Hall --- README.md | 42 ++++++++++++++++++++++++++++++++++++++ lib/ecto_psql_extras.ex | 10 ++++++++- lib/queries/connections.ex | 23 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/queries/connections.ex diff --git a/README.md b/README.md index 9848867..dd5b5ea 100644 --- a/README.md +++ b/README.md @@ -555,6 +555,48 @@ EctoPSQLExtras.mandelbrot(YourApp.Repo) This command outputs the Mandelbrot set, calculated through SQL. +### `connections` + +``` + +EctoPSQLExtras.connections(YourApp.Repo) ++----------------------------------------------+ +| Shows all the active database connections | ++----------+----------------+------------------+ +| username | client_address | application_name | ++----------+----------------+------------------+ +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | your_app | +| postgres | 172.20.0.1/32 | psql | ++----------+----------------+------------------+ +``` + +This command returns the list of all active connections to the database. + +To have `application_name` populate in connections output, you need to configure your Phoenix applications' Repo by adding the `parameters` and set `application_name`: + +```elixir +config :your_app, YourApp.Repo, + username: "postgres", + password: "postgres", + hostname: "localhost", + database: "your_app_dev", + stacktrace: true, + show_sensitive_data_on_connection_error: true, + pool_size: 10, + parameters: [ + {:application_name, "your_app"} + ] +``` + ## Query sources - [https://github.com/heroku/heroku-pg-extras](https://github.com/heroku/heroku-pg-extras) diff --git a/lib/ecto_psql_extras.ex b/lib/ecto_psql_extras.ex index 68e2fb9..061985a 100644 --- a/lib/ecto_psql_extras.ex +++ b/lib/ecto_psql_extras.ex @@ -49,7 +49,8 @@ defmodule EctoPSQLExtras do duplicate_indexes: EctoPSQLExtras.DuplicateIndexes, null_indexes: EctoPSQLExtras.NullIndexes, vacuum_stats: EctoPSQLExtras.VacuumStats, - kill_all: EctoPSQLExtras.KillAll + kill_all: EctoPSQLExtras.KillAll, + connections: EctoPSQLExtras.Connections } |> Map.merge(pg_stat_statements_queries(repo)) |> Map.merge(ssl_used_query(repo)) @@ -360,6 +361,13 @@ defmodule EctoPSQLExtras do """ def ssl_used(repo, opts \\ []), do: query(:ssl_used, repo, opts) + @doc """ + Run `connections` query on `repo`, in the given `format`. + + `format` is either `:ascii` or `:raw` + """ + def connections(repo, opts \\ []), do: query(:connections, repo, opts) + defp format(:ascii, info, result) do names = Enum.map(info.columns, & &1.name) types = Enum.map(info.columns, & &1.type) diff --git a/lib/queries/connections.ex b/lib/queries/connections.ex new file mode 100644 index 0000000..8dd55e0 --- /dev/null +++ b/lib/queries/connections.ex @@ -0,0 +1,23 @@ +defmodule EctoPSQLExtras.Connections do + @behaviour EctoPSQLExtras + + def info do + %{ + title: "Returns the list of all active database connections", + columns: [ + %{name: :username, type: :string}, + %{name: :client_address, type: :string}, + %{name: :application_name, type: :string} + ] + } + end + + def query(_args \\ []) do + """ + /* ECTO_PSQL_EXTRAS: Returns the list of all active database connections */ + + SELECT usename as username, client_addr::text as client_address, application_name FROM pg_stat_activity + WHERE datname = current_database(); + """ + end +end