pg_monitor 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/Dockerfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +114 -0
- data/Rakefile +10 -0
- data/docker-compose.yml +27 -0
- data/lib/pg_monitor/db_connection.rb +15 -0
- data/lib/pg_monitor/index_usage.rb +22 -0
- data/lib/pg_monitor/slow_queries.rb +22 -0
- data/lib/pg_monitor/version.rb +5 -0
- data/lib/pg_monitor.rb +10 -0
- data/postgresql.conf +5 -0
- data/sig/pg_monitor.rbs +4 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aa26f1ee15de860372c7ec2ad7304a2fe5d01fb5a934e76b192d301faef0657f
|
4
|
+
data.tar.gz: 2f1e2bba02213cd53e49502c2b5056963d0634e74125e342c8b1cd8e45a27a53
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40a373281b77a31c5551ea8ec7b1cfc78e26d191ffc8f6730dec67d5e0bb964c4c933ac97497f29a3b9c8ceb1b93b1761511e71c2b817e204b9f53cc48be626f
|
7
|
+
data.tar.gz: eb46b021df9b08fc7ff91663150173b476c9fa881a41e8a954e73617f965476b64db70d32cf1d6680abfc691fb3a4243cd24086a044b81320489bed1965e5d1e
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/Dockerfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Hassan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# PgMonitor
|
2
|
+
|
3
|
+
PgMonitor is a Ruby gem that provides insights into PostgreSQL database.
|
4
|
+
It would integrate seamlessly with a Rails application in the future, and would expose statistics via a mounted route.
|
5
|
+
|
6
|
+
## Features
|
7
|
+
|
8
|
+
- View index usage statistics (last used, scan count, etc.)
|
9
|
+
- Identify slow queries using `pg_stat_statements`
|
10
|
+
- [TBD] Works with the existing Rails database configuration
|
11
|
+
- [TBD] Provides a simple HTML interface for monitoring
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'pg_monitor'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
```sh
|
24
|
+
bundle install
|
25
|
+
```
|
26
|
+
|
27
|
+
Or install it manually:
|
28
|
+
|
29
|
+
```sh
|
30
|
+
gem install pg_monitor
|
31
|
+
```
|
32
|
+
|
33
|
+
## Configuration
|
34
|
+
|
35
|
+
### Enable `pg_stat_statements`
|
36
|
+
|
37
|
+
Ensure that `pg_stat_statements` is enabled in your **PostgreSQL configuration**.
|
38
|
+
|
39
|
+
Modify `postgresql.conf`:
|
40
|
+
|
41
|
+
```conf
|
42
|
+
shared_preload_libraries = 'pg_stat_statements'
|
43
|
+
pg_stat_statements.track = all
|
44
|
+
track_activity_query_size = 2048
|
45
|
+
```
|
46
|
+
|
47
|
+
Restart PostgreSQL after making the changes:
|
48
|
+
|
49
|
+
```sh
|
50
|
+
sudo systemctl restart postgresql
|
51
|
+
```
|
52
|
+
|
53
|
+
## Usage
|
54
|
+
|
55
|
+
Mount the engine in `config/routes.rb`:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
mount PgMonitor::Engine, at: '/pg_monitor'
|
59
|
+
```
|
60
|
+
|
61
|
+
Then, navigate to:
|
62
|
+
|
63
|
+
```
|
64
|
+
http://localhost:3000/pg_monitor
|
65
|
+
```
|
66
|
+
|
67
|
+
### Programmatic Access
|
68
|
+
|
69
|
+
#### **Index Usage**
|
70
|
+
```ruby
|
71
|
+
PgMonitor::IndexUsage.fetch
|
72
|
+
```
|
73
|
+
|
74
|
+
#### **Slow Queries**
|
75
|
+
```ruby
|
76
|
+
PgMonitor::SlowQueries.fetch(limit: 10)
|
77
|
+
```
|
78
|
+
|
79
|
+
## Running Tests
|
80
|
+
|
81
|
+
### **1. Start PostgreSQL in Docker**
|
82
|
+
|
83
|
+
Ensure you have Docker installed, then start the database:
|
84
|
+
|
85
|
+
```sh
|
86
|
+
docker-compose up -d postgres
|
87
|
+
```
|
88
|
+
|
89
|
+
### **2. Run Tests**
|
90
|
+
|
91
|
+
```sh
|
92
|
+
docker-compose run --rm test-runner
|
93
|
+
```
|
94
|
+
|
95
|
+
Or directly using RSpec:
|
96
|
+
|
97
|
+
```sh
|
98
|
+
bundle exec rspec
|
99
|
+
```
|
100
|
+
|
101
|
+
## Contributing
|
102
|
+
|
103
|
+
1. Fork the repo
|
104
|
+
2. Create a feature branch (`git checkout -b my-feature`)
|
105
|
+
3. Commit your changes (`git commit -m 'Add new feature'`)
|
106
|
+
4. Push to the branch (`git push origin my-feature`)
|
107
|
+
5. Open a Pull Request
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
This project is licensed under the MIT License.
|
112
|
+
|
113
|
+
## Author
|
114
|
+
Created by [Hassan Murtaza](https://github.com/hmurtaza7).
|
data/Rakefile
ADDED
data/docker-compose.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
services:
|
2
|
+
postgres:
|
3
|
+
image: postgres:17
|
4
|
+
container_name: pg_monitor_test_db
|
5
|
+
environment:
|
6
|
+
POSTGRES_USER: postgres
|
7
|
+
POSTGRES_PASSWORD: password
|
8
|
+
POSTGRES_DB: pg_monitor_test
|
9
|
+
ports:
|
10
|
+
- "5432:5432"
|
11
|
+
volumes:
|
12
|
+
- ./postgresql.conf:/etc/postgresql.conf
|
13
|
+
command: ["postgres", "-c", "config_file=/etc/postgresql.conf"]
|
14
|
+
|
15
|
+
test-runner:
|
16
|
+
build: .
|
17
|
+
depends_on:
|
18
|
+
- postgres
|
19
|
+
environment:
|
20
|
+
PG_DATABASE: pg_monitor_test
|
21
|
+
PG_USER: postgres
|
22
|
+
PG_PASSWORD: password
|
23
|
+
PG_HOST: postgres
|
24
|
+
PG_PORT: 5432
|
25
|
+
volumes:
|
26
|
+
- .:/app
|
27
|
+
entrypoint: ["bash", "-c", "bundle exec rspec"]
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "pg"
|
2
|
+
|
3
|
+
module PgMonitor
|
4
|
+
class DBConnection
|
5
|
+
def self.connection
|
6
|
+
@connection ||= PG.connect(
|
7
|
+
dbname: ENV.fetch("PG_DATABASE", "pg_monitor_test"),
|
8
|
+
user: ENV.fetch("PG_USER", "postgres"),
|
9
|
+
password: ENV.fetch("PG_PASSWORD", "password"),
|
10
|
+
host: ENV.fetch("PG_HOST", "localhost"),
|
11
|
+
port: ENV.fetch("PG_PORT", "5432")
|
12
|
+
)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "db_connection"
|
2
|
+
|
3
|
+
module PgMonitor
|
4
|
+
class IndexUsage
|
5
|
+
def fetch
|
6
|
+
query = <<~SQL
|
7
|
+
SELECT
|
8
|
+
pg_class.relname AS table_name,
|
9
|
+
indexrelname AS index_name,
|
10
|
+
idx_scan AS index_scan_count,
|
11
|
+
idx_tup_read AS tuples_read,
|
12
|
+
idx_tup_fetch AS tuples_fetched
|
13
|
+
FROM pg_stat_all_indexes
|
14
|
+
JOIN pg_class ON pg_stat_all_indexes.relid = pg_class.oid
|
15
|
+
ORDER BY idx_scan DESC;
|
16
|
+
SQL
|
17
|
+
|
18
|
+
result = DBConnection.connection.exec(query)
|
19
|
+
result.map { |row| row }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "db_connection"
|
2
|
+
|
3
|
+
module PgMonitor
|
4
|
+
class SlowQueries
|
5
|
+
def fetch(limit: 10)
|
6
|
+
query = <<~SQL
|
7
|
+
SELECT
|
8
|
+
query,
|
9
|
+
calls,
|
10
|
+
total_exec_time AS total_time,
|
11
|
+
mean_exec_time AS mean_time,
|
12
|
+
max_exec_time AS max_time
|
13
|
+
FROM pg_stat_statements
|
14
|
+
ORDER BY mean_exec_time DESC
|
15
|
+
LIMIT $1;
|
16
|
+
SQL
|
17
|
+
|
18
|
+
result = DBConnection.connection.exec_params(query, [limit])
|
19
|
+
result.map { |row| row }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/pg_monitor.rb
ADDED
data/postgresql.conf
ADDED
data/sig/pg_monitor.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_monitor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hassan Murtaza
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Connects to PostgreSQL and retrieves few statistics such as index stats
|
28
|
+
and table bloats.
|
29
|
+
email:
|
30
|
+
- "...@gmail.com"
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".rspec"
|
36
|
+
- ".standard.yml"
|
37
|
+
- CHANGELOG.md
|
38
|
+
- Dockerfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- docker-compose.yml
|
43
|
+
- lib/pg_monitor.rb
|
44
|
+
- lib/pg_monitor/db_connection.rb
|
45
|
+
- lib/pg_monitor/index_usage.rb
|
46
|
+
- lib/pg_monitor/slow_queries.rb
|
47
|
+
- lib/pg_monitor/version.rb
|
48
|
+
- postgresql.conf
|
49
|
+
- sig/pg_monitor.rbs
|
50
|
+
homepage: https://github.com/hmurtaza7/pg_monitor
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
homepage_uri: https://github.com/hmurtaza7/pg_monitor
|
55
|
+
source_code_uri: https://github.com/hmurtaza7/pg_monitor
|
56
|
+
changelog_uri: https://github.com/hmurtaza7/pg_monitor/blob/main/CHANGELOG.md
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 3.0.0
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.5.22
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: A gem to view PostgreSQL statistics.
|
76
|
+
test_files: []
|