ruby-pg-extras 0.7.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '05768abc8a8d84bf67f2b5421f687044aab327521e9206cb58356d6c0905ec68'
4
- data.tar.gz: 864923ef0192c664b46dce8ecc0b126a5d6699319fda1f101d9d36c5b1e73758
3
+ metadata.gz: 159b413f611699a0be3d00f9c15efee109a910a28fe047d9c9f2a33b477a500b
4
+ data.tar.gz: 9a7ff1ad3c60a82fd7396f557833c66eb536d7d7c0f0fab8eeb0db7803603a5b
5
5
  SHA512:
6
- metadata.gz: 113c943c72a86772077079886dc6ea0b23b82651e00dad201921b91caa40900802a5efe832f9e3945012ba23ae17c5c8044d8e18a1325ff16df965843aeb6c15
7
- data.tar.gz: 15e13d0547b587ddb7fc3fb26bfbde451b7a9e47615631cfa379bb0fc6b69dd96a95624814e4ebec1d876861685528028b467325725815cd78d8d14a06fcdd9f
6
+ metadata.gz: db53acf068d87f8d2947af7128acaa13ec5d4bd402626b18152d0131baafac4395dded2f4e1003272ecae9a5b6f1fd4aef8a9fb589ba96d9cafb1f3b252f832c
7
+ data.tar.gz: 5357e82fa106c7511f15b731899228bb2e045cf22a597f588eb6244e537104627e3b142752ffe63aa61bacab0215ebc425642b6d48c8e928593a23ef387f34f3
data/README.md CHANGED
@@ -1,20 +1,43 @@
1
1
  # Ruby PG Extras [![Gem Version](https://badge.fury.io/rb/ruby-pg-extras.svg)](https://badge.fury.io/rb/ruby-pg-extras) [![CircleCI](https://circleci.com/gh/pawurb/ruby-pg-extras.svg?style=svg)](https://circleci.com/gh/pawurb/ruby-pg-extras)
2
2
 
3
- Ruby port of [Heroku PG Extras](https://github.com/heroku/heroku-pg-extras). The goal of this project is to provide powerful insights into the PostgreSQL database for Ruby apps that are not using the Heroku PostgreSQL plugin.
3
+ Ruby 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 Ruby apps that are not using the Heroku PostgreSQL plugin.
4
4
 
5
- Included rake tasks and Ruby methods 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. Ruby API enables developers to easily integrate the tool into e.g. automatic monitoring tasks.
5
+ 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. Ruby API enables developers to easily integrate the tool into e.g. automatic monitoring tasks.
6
6
 
7
- Are you riding on Rails? Check out the [Rails version](https://github.com/pawurb/rails-pg-extras).
7
+ 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).
8
8
 
9
- ### Installation
9
+ Alternative versions:
10
+
11
+ - [Ruby on Rails](https://github.com/pawurb/rails-pg-extras)
12
+
13
+ - [NodeJS](https://github.com/pawurb/node-postgres-extras)
14
+
15
+ - [Elixir](https://github.com/pawurb/ecto_psql_extras)
16
+
17
+ - [Python](https://github.com/pawurb/python-pg-extras)
18
+
19
+ ## Installation
10
20
 
11
21
  In your Gemfile
12
22
 
13
23
  ```ruby
14
- gem 'ruby-pg-extras'
24
+ gem "ruby-pg-extras"
25
+ ```
26
+
27
+ Some of the queries (e.g., `calls` and `outliers`) require [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) extension enabled.
28
+
29
+ You can check if it is enabled in your database by running:
30
+
31
+ ```ruby
32
+ RubyPGExtras.extensions
15
33
  ```
34
+ You should see the similar line in the output:
16
35
 
17
- ### Usage
36
+ ```bash
37
+ | pg_stat_statements | 1.7 | 1.7 | track execution statistics of all SQL statements executed |
38
+ ```
39
+
40
+ ## Usage
18
41
 
19
42
  Gem expects the `ENV['DATABASE_URL']` value in the following format:
20
43
 
@@ -60,9 +83,9 @@ RubyPGExtras.cache_hit(in_format: :raw) =>
60
83
  #<PG::Result:0x00007f75777f7328 status=PGRES_TUPLES_OK ntuples=2 nfields=2 cmd_tuples=2>
61
84
  ```
62
85
 
63
- ### Available methods
86
+ ## Available methods
64
87
 
65
- #### `cache_hit`
88
+ ### `cache_hit`
66
89
 
67
90
  ```ruby
68
91
 
@@ -77,7 +100,39 @@ RubyPGExtras.cache_hit
77
100
 
78
101
  This command provides information on the efficiency of the buffer cache, for both index reads (`index hit rate`) as well as table reads (`table hit rate`). A low buffer cache hit ratio can be a sign that the Postgres instance is too small for the workload.
79
102
 
80
- #### `index_usage`
103
+ ### `index_cache_hit`
104
+
105
+ ```ruby
106
+
107
+ RubyPGExtras.index_cache_hit
108
+
109
+ | name | buffer_hits | block_reads | total_read | ratio |
110
+ +-----------------------+-------------+-------------+------------+-------------------+
111
+ | teams | 187665 | 109 | 187774 | 0.999419514948821 |
112
+ | subscriptions | 5160 | 6 | 5166 | 0.99883855981417 |
113
+ | plans | 5718 | 9 | 5727 | 0.998428496595076 |
114
+ (truncated results for brevity)
115
+ ```
116
+
117
+ The same as `cache_hit` with each table's indexes cache hit info displayed seperately.
118
+
119
+ ### `table_cache_hit`
120
+
121
+ ```ruby
122
+
123
+ RubyPGExtras.table_cache_hit
124
+
125
+ | name | buffer_hits | block_reads | total_read | ratio |
126
+ +-----------------------+-------------+-------------+------------+-------------------+
127
+ | plans | 32123 | 2 | 32125 | 0.999937743190662 |
128
+ | subscriptions | 95021 | 8 | 95029 | 0.999915815172211 |
129
+ | teams | 171637 | 200 | 171837 | 0.99883610631005 |
130
+ (truncated results for brevity)
131
+ ```
132
+
133
+ The same as `cache_hit` with each table's cache hit info displayed seperately.
134
+
135
+ ### `index_usage`
81
136
 
82
137
  ```ruby
83
138
 
@@ -139,10 +194,7 @@ RubyPGExtras.outliers
139
194
  SELECT * FROM usage_events WHERE (alp.. | 01:18:10.754354 | 0.6% | 102,114,301 | 00:00:00
140
195
  UPDATE usage_events SET reporter_id =.. | 00:52:35.683254 | 0.4% | 23,786,348 | 00:00:00
141
196
  INSERT INTO usage_events (id, retaine.. | 00:49:24.952561 | 0.4% | 21,988,201 | 00:00:00
142
- COPY public.app_ownership_events (id,.. | 00:37:14.31082 | 0.3% | 13 | 00:12:32.584754
143
- INSERT INTO app_ownership_events (id,.. | 00:26:59.808212 | 0.2% | 383,109 | 00:00:00
144
- SELECT * FROM app_ownership_events .. | 00:19:06.021846 | 0.1% | 744,879 | 00:00:00
145
- (10 rows)
197
+ (truncated results for brevity)
146
198
  ```
147
199
 
148
200
  This command displays statements, obtained from `pg_stat_statements`, ordered by the amount of time to execute in aggregate. This includes the statement itself, the total execution time for that statement, the proportion of total execution time for all statements that statement has taken up, the number of times that statement has been called, and the amount of time that statement spent on synchronous I/O (reading/writing from the filesystem).
@@ -164,10 +216,7 @@ RubyPGExtras.calls
164
216
  UPDATE usage_events SET reporter_id =.. | 00:52:35.986167 | 0.4% | 23,788,388 | 00:00:00
165
217
  INSERT INTO usage_events (id, retaine.. | 00:49:25.260245 | 0.4% | 21,990,326 | 00:00:00
166
218
  INSERT INTO usage_events (id, retaine.. | 01:42:59.436532 | 0.8% | 12,328,187 | 00:00:00
167
- SELECT * FROM app_ownership_events .. | 00:19:06.289521 | 0.1% | 744,976 | 00:00:00
168
- INSERT INTO app_ownership_events(id, .. | 00:26:59.885631 | 0.2% | 383,153 | 00:00:00
169
- UPDATE app_ownership_events SET app_i.. | 00:01:22.282337 | 0.0% | 359,741 | 00:00:00
170
- (10 rows)
219
+ (truncated results for brevity)
171
220
  ```
172
221
 
173
222
  This command is much like `pg:outliers`, but ordered by the number of times a statement has been called.
@@ -186,7 +235,7 @@ RubyPGExtras.blocking
186
235
 
187
236
  This command displays statements that are currently holding locks that other statements are waiting to be released. This can be used in conjunction with `pg:locks` to determine which statements need to be terminated in order to resolve lock contention.
188
237
 
189
- #### `total_index_size`
238
+ ### `total_index_size`
190
239
 
191
240
  ```ruby
192
241
 
@@ -218,11 +267,6 @@ RubyPGExtras.index_size
218
267
  index_attempts_on_enrollment_id | 1957 MB
219
268
  index_enrollment_attemptables_by_enrollment_activity_id | 1789 MB
220
269
  enrollment_activities_pkey | 458 MB
221
- index_enrollment_activities_by_lesson_enrollment_and_activity | 402 MB
222
- index_placement_attempts_on_response_id | 109 MB
223
- index_placement_attempts_on_placement_test_id | 108 MB
224
- index_placement_attempts_on_grade_level_id | 97 MB
225
- index_lesson_enrollments_on_lesson_id | 93 MB
226
270
  (truncated results for brevity)
227
271
  ```
228
272
 
@@ -244,7 +288,7 @@ RubyPGExtras.table_size
244
288
  (truncated results for brevity)
245
289
  ```
246
290
 
247
- This command displays the size of each table in the database, in MB. It is calculated by using the system administration function `pg_table_size()`, which includes the size of the main data fork, free space map, visibility map and TOAST data.
291
+ This command displays the size of each table and materialized view in the database, in MB. It is calculated by using the system administration function `pg_table_size()`, which includes the size of the main data fork, free space map, visibility map and TOAST data.
248
292
 
249
293
  ### `table_indexes_size`
250
294
 
@@ -262,7 +306,7 @@ RubyPGExtras.table_indexes_size
262
306
  (truncated results for brevity)
263
307
  ```
264
308
 
265
- This command displays the total size of indexes for each table, in MB. It is calcualtes by using the system administration function `pg_indexes_size()`.
309
+ This command displays the total size of indexes for each table and materialized view, in MB. It is calcualtes by using the system administration function `pg_indexes_size()`.
266
310
 
267
311
  ### `total_table_size`
268
312
 
@@ -280,7 +324,7 @@ RubyPGExtras.total_table_size
280
324
  (truncated results for brevity)
281
325
  ```
282
326
 
283
- This command displays the total size of each table in the database, in MB. It is calculated by using the system administration function `pg_total_relation_size()`, which includes table size, total index size and TOAST data.
327
+ This command displays the total size of each table and materialized view in the database, in MB. It is calculated by using the system administration function `pg_total_relation_size()`, which includes table size, total index size and TOAST data.
284
328
 
285
329
  ### `unused_indexes`
286
330
 
@@ -315,20 +359,12 @@ RubyPGExtras.seq_scans
315
359
  messages | 3922247
316
360
  contests_customers | 2915972
317
361
  classroom_goals | 2142014
318
- contests | 1370267
319
- goals | 1112659
320
- districts | 158995
321
- rollup_reports | 115942
322
- customers | 93847
323
- schools | 92984
324
- classrooms | 92982
325
- customer_settings | 91226
326
362
  (truncated results for brevity)
327
363
  ```
328
364
 
329
365
  This command displays the number of sequential scans recorded against all tables, descending by count of sequential scans. Tables that have very high numbers of sequential scans may be underindexed, and it may be worth investigating queries that read from these tables.
330
366
 
331
- ### long_running_queries
367
+ ### `long_running_queries`
332
368
 
333
369
  ```ruby
334
370
 
@@ -345,7 +381,7 @@ RubyPGExtras.long_running_queries
345
381
 
346
382
  This command displays currently running queries, that have been running for longer than 5 minutes, descending by duration. Very long running queries can be a source of multiple issues, such as preventing DDL statements completing or vacuum being unable to update `relfrozenxid`.
347
383
 
348
- ### records_rank
384
+ ### `records_rank`
349
385
 
350
386
  ```ruby
351
387
 
@@ -364,7 +400,7 @@ RubyPGExtras.records_rank
364
400
 
365
401
  This command displays an estimated count of rows per table, descending by estimated count. The estimated count is derived from `n_live_tup`, which is updated by vacuum operations. Due to the way `n_live_tup` is populated, sparse vs. dense pages can result in estimations that are significantly out from the real count of rows.
366
402
 
367
- ### bloat
403
+ ### `bloat`
368
404
 
369
405
  ```ruby
370
406
 
@@ -378,11 +414,12 @@ RubyPGExtras.bloat
378
414
  index | public | bloated_table::bloated_index | 3.7 | 34 MB
379
415
  table | public | clean_table | 0.2 | 3808 kB
380
416
  table | public | other_clean_table | 0.3 | 1576 kB
417
+ (truncated results for brevity)
381
418
  ```
382
419
 
383
420
  This command displays an estimation of table "bloat" – space allocated to a relation that is full of dead tuples, that has yet to be reclaimed. Tables that have a high bloat ratio, typically 10 or greater, should be investigated to see if vacuuming is aggressive enough, and can be a sign of high table churn.
384
421
 
385
- ### vacuum_stats
422
+ ### `vacuum_stats`
386
423
 
387
424
  ```ruby
388
425
 
@@ -395,26 +432,37 @@ RubyPGExtras.vacuum_stats
395
432
  public | other_table | | 2013-04-26 11:41 | 41 | 47 | 58 |
396
433
  public | queue_table | | 2013-04-26 17:39 | 12 | 8,228 | 52 | yes
397
434
  public | picnic_table | | | 13 | 0 | 53 |
435
+ (truncated results for brevity)
398
436
  ```
399
437
 
400
438
  This command displays statistics related to vacuum operations for each table, including an estiamtion of dead rows, last autovacuum and the current autovacuum threshold. This command can be useful when determining if current vacuum thresholds require adjustments, and to determine when the table was last vacuumed.
401
439
 
402
- ### mandelbrot
440
+ ### `kill_all`
403
441
 
404
442
  ```ruby
405
443
 
406
- RubyPGExtras.mandelbrot
444
+ RubyPGExtras.kill_all
407
445
 
408
446
  ```
409
447
 
410
- This command outputs the Mandelbrot set, calculated through SQL.
448
+ This commands kills all the currently active connections to the database. It can be useful as a last resort when your database is stuck in a deadlock.
411
449
 
412
- ## FAQ
450
+ ### `extensions`
413
451
 
414
- * Does is not violate the Heroku PG Extras license?
452
+ ```ruby
453
+
454
+ RubyPGExtras.extensions
455
+
456
+ ```
457
+
458
+ This command lists all the currently installed and available PostgreSQL extensions.
415
459
 
416
- The original plugin is MIT based so it means that copying and redistribution in any format is permitted.
460
+ ### `mandelbrot`
417
461
 
418
- ## Disclaimer
462
+ ```ruby
419
463
 
420
- This tool is in beta state.
464
+ RubyPGExtras.mandelbrot
465
+
466
+ ```
467
+
468
+ This command outputs the Mandelbrot set, calculated through SQL.
@@ -14,7 +14,7 @@ module RubyPGExtras
14
14
  long_running_queries mandelbrot outliers
15
15
  records_rank seq_scans table_indexes_size
16
16
  table_size total_index_size total_table_size
17
- unused_indexes vacuum_stats
17
+ unused_indexes vacuum_stats kill_all
18
18
  )
19
19
 
20
20
  QUERIES.each do |query_name|
@@ -0,0 +1,6 @@
1
+ /* Kill all the active database connections */
2
+
3
+ SELECT pg_terminate_backend(pid) FROM pg_stat_activity
4
+ WHERE pid <> pg_backend_pid()
5
+ AND query <> '<insufficient privilege>'
6
+ AND datname = current_database();
@@ -6,5 +6,5 @@ FROM pg_class c
6
6
  LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
7
7
  WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
8
8
  AND n.nspname !~ '^pg_toast'
9
- AND c.relkind='r'
9
+ AND c.relkind IN ('r', 'm')
10
10
  ORDER BY pg_indexes_size(c.oid) DESC;
@@ -6,5 +6,5 @@ FROM pg_class c
6
6
  LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
7
7
  WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
8
8
  AND n.nspname !~ '^pg_toast'
9
- AND c.relkind='r'
9
+ AND c.relkind IN ('r', 'm')
10
10
  ORDER BY pg_table_size(c.oid) DESC;
@@ -6,5 +6,5 @@ FROM pg_class c
6
6
  LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
7
7
  WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
8
8
  AND n.nspname !~ '^pg_toast'
9
- AND c.relkind='r'
9
+ AND c.relkind IN ('r', 'm')
10
10
  ORDER BY pg_total_relation_size(c.oid) DESC;
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RubyPGExtras
2
- VERSION = "0.7.0"
4
+ VERSION = "1.2.2"
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-pg-extras
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-11 00:00:00.000000000 Z
11
+ date: 2020-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -92,6 +92,7 @@ files:
92
92
  - lib/ruby-pg-extras/queries/index_cache_hit.sql
93
93
  - lib/ruby-pg-extras/queries/index_size.sql
94
94
  - lib/ruby-pg-extras/queries/index_usage.sql
95
+ - lib/ruby-pg-extras/queries/kill_all.sql
95
96
  - lib/ruby-pg-extras/queries/locks.sql
96
97
  - lib/ruby-pg-extras/queries/long_running_queries.sql
97
98
  - lib/ruby-pg-extras/queries/mandelbrot.sql
@@ -113,7 +114,7 @@ homepage: http://github.com/pawurb/ruby-pg-extras
113
114
  licenses:
114
115
  - MIT
115
116
  metadata: {}
116
- post_install_message:
117
+ post_install_message:
117
118
  rdoc_options: []
118
119
  require_paths:
119
120
  - lib
@@ -128,8 +129,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
129
  - !ruby/object:Gem::Version
129
130
  version: '0'
130
131
  requirements: []
131
- rubygems_version: 3.0.6
132
- signing_key:
132
+ rubygems_version: 3.1.2
133
+ signing_key:
133
134
  specification_version: 4
134
135
  summary: Ruby PostgreSQL performance database insights
135
136
  test_files: