Skip to content

Commit

Permalink
Add ability to gets all active connections to the database.
Browse files Browse the repository at this point in the history
This adds the ability to get all active connections to the database.

Co-authored-by: Matt Hall <matt.hall@binarynoggin.com>
  • Loading branch information
djgoku and Matt Hall committed Nov 17, 2022
1 parent 76c1937 commit 395bf97
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,32 @@ 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 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | phoenix_1_6_15 |
| postgres | 172.20.0.1/32 | psql |
+----------+----------------+------------------+
```

This command returns the list of all active connections to the database.

## Query sources

- [https://github.com/heroku/heroku-pg-extras](https://github.com/heroku/heroku-pg-extras)
Expand Down
10 changes: 9 additions & 1 deletion lib/ecto_psql_extras.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions lib/queries/connections.ex
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/ecto_psql_extras_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule EctoPSQLExtrasTest do
outliers_legacy: EctoPSQLExtras.OutliersLegacy
}

@skip_queries [:kill_all]
@skip_queries [:kill_all, :connections]

test "all queries define info" do
for pair <- Map.merge(queries(), @optional_queries) do
Expand Down

0 comments on commit 395bf97

Please sign in to comment.