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 ability to gets all active connections to the database. #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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