PgMonitor is a Ruby gem that provides insights into PostgreSQL database. It would integrate seamlessly with a Rails application in the future, and would expose statistics via a mounted route.
- View index usage statistics (last used, scan count, etc.)
- Identify slow queries using
pg_stat_statements
- Works with existing Rails database configuration, if present
- Provides a simple HTML interface for monitoring
Add this line to your application's Gemfile:
gem 'pg_monitor'
And then execute:
bundle install
Or install it manually:
gem install pg_monitor
Ensure that pg_stat_statements
is enabled in your PostgreSQL configuration.
Modify postgresql.conf
:
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = all
track_activity_query_size = 2048
Restart PostgreSQL after making the changes:
sudo systemctl restart postgresql
Ensure that the extension is enabled in your database:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
Execute the above SQL query by connecting to your database using psql
or any other client.
To run the PgMonitor, follow these steps:
-
Ensure you have all the dependencies installed:
bundle install
-
Run the server using the Rake task:
rake server
-
The application should now be running. You can access it in your web browser at
http://localhost:4567
.
PgMonitor::IndexUsage.fetch
PgMonitor::SlowQueries.fetch(limit: 10)
Ensure you have Docker installed, then start the database:
docker-compose up -d postgres
docker-compose run --rm test-runner
Or directly using RSpec:
bundle exec rspec
- Fork the repo
- Create a feature branch (
git checkout -b my-feature
) - Commit your changes (
git commit -m 'Add new feature'
) - Push to the branch (
git push origin my-feature
) - Open a Pull Request
This project is licensed under the MIT License.
Created by Hassan Murtaza.