# rust-pg-extras [](https://crates.io/crates/pg-extras) [](https://github.com/pawurb/rust-pg-extras/actions)
Rust port of [Heroku PG Extras](https://github.com/heroku/heroku-pg-extras) with several additions and improvements. The goal of this project is to provide powerful insights into the PostgreSQL database for Rust apps that are not using the Heroku PostgreSQL plugin.
Queries can be used to obtain information about a Postgres instance, that may be useful when analyzing performance issues. This includes information about locks, index usage, buffer cache hit ratios and vacuum statistics. Rust API enables developers to easily integrate the tool into e.g. automatic monitoring tasks.
You can check out this blog post for detailed step by step tutorial on how to [optimize PostgreSQL using PG Extras library](https://pawelurbanek.com/postgresql-fix-performance).
Alternative versions:
- [Ruby on Rails](https://github.com/pawurb/rails-pg-extras)
- [NodeJS](https://github.com/pawurb/node-postgres-extras)
- [Elixir](https://github.com/pawurb/ecto_psql_extras)
- [Python](https://github.com/pawurb/python-pg-extras)
- [Haskell](https://github.com/pawurb/haskell-pg-extras)
## Installation
In your Cargo.toml
```rust
pg-extras = "0.3"
```
`calls` and `outliers` queries require [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) extension.
You can check if it is enabled in your database by running:
```rust
use pg_extras::{render_table, extensions}
render_table(extensions()?);
```
You should see the similar line in the output:
```bash
`ssl_used` requires `sslinfo` extension, and `buffercache_usage`/`buffercache_usage` queries need `pg_buffercache`. You can enable them all by running this SQL:
```sql
CREATE EXTENSION IF NOT EXISTS sslinfo;
CREATE EXTENSION IF NOT EXISTS pg_buffercache;
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
```
## Usage
Package expects the `ENV['PG_EXTRAS_DATABASE_URL']` or `ENV['DATABASE_URL']` value in the following format:
```rust
ENV["DATABASE_URL"] = "postgresql://postgres:secret@localhost:5432/database_name"
```
You can run queries using a Rust API to display an ASCCI table with results:
```rust
use pg_extras::{render_table, cache_hit}
render_table(cache_hit(None).await?);
```
```bash
+----------------+------------------------+
| name | ratio |
+----------------+------------------------+
| index hit rate | 0.97796610169491525424 |
| table hit rate | 0.96724294813466787989 |
+----------------+------------------------+
```
Alternatively you can work directly with returned structs:
```rust
use pg_extras::{render_table, cache_hit, CacheHit}
let cache_hit_res: Vec<CacheHit> = cache_hit(None).await?;
println!("{:?}", cache_hit_res);
// [CacheHit { name: "index hit rate", ratio: 0.9779... }, CacheHit { name: "table hit rate", ratio: 0.9672... }]
```
Some methods accept params allowing you to customize queries:
```rust
cache_hit(Some("other_schema".to_string)).await?;
```
You can customize the default `public` schema by setting `ENV['PG_EXTRAS_SCHEMA']` value.
## Command line
After running `cargo install pg-extras` you can use `pg_extras` shell command:
```bash
$ pg_extras cache_hit
+----------------+------------------------+