rails-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: 506d95562fb9dcf353e233962941de468492a429cf1f4c32aedbc12c8ad61ad8
4
- data.tar.gz: a26cdce454bf3a379e2cefd6219cecb3bf12aae7b9d6014b717eec9685cc98af
3
+ metadata.gz: 5cf36789c8d7210dd1512d3d7ef8de0d0bcf88ca3d339e1ee5e89daf82264c32
4
+ data.tar.gz: 5054854e791db2e53408c53fd30d11e907b7caed8549fccaecedd98b10469ce4
5
5
  SHA512:
6
- metadata.gz: a660057f6a220a65acf624754273fa62fe16459092b81ee5ba61c1b4a2a38f47c8204b233faf91f4a5ff9ab2bda48efdb784cc1d027f792497caba1c9c994727
7
- data.tar.gz: d2bd6dd7bae2885b36f779063e1b59ab3c402caa630f86e5d6dca83c45454c5194e3f421a532c307b06594edba4270f86c92510ee531515b053a8682427811a0
6
+ metadata.gz: 3fe6766ddb5896202a408ae95e6a4fc91edf42149d15b348c731bfdda153225ae7949cbc6eab2cb266ffa0ebf4daf1eb3d497cb06807b64fe66c0fad28eb17d1
7
+ data.tar.gz: 3c8d26e07807c15df4af9a8fb785bc9cc78fc52f55baae6c46db0911edc2e13a7cdd0b882560ba363c9c89b46a8fb7b48d350ec63bb3b1258fd335fe8816f1ad
data/README.md CHANGED
@@ -1,20 +1,43 @@
1
1
  # Rails PG Extras [![Gem Version](https://badge.fury.io/rb/rails-pg-extras.svg)](https://badge.fury.io/rb/rails-pg-extras) [![CircleCI](https://circleci.com/gh/pawurb/rails-pg-extras.svg?style=svg)](https://circleci.com/gh/pawurb/rails-pg-extras)
2
2
 
3
- Rails 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 on Rails apps that are not using the Heroku PostgreSQL plugin.
3
+ Rails 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 on Rails apps that are not using the Heroku PostgreSQL plugin.
4
4
 
5
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.
6
6
 
7
- Not using Rails? Check out the core dependency [pure Ruby version](https://github.com/pawurb/ruby-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
+ - Core dependency - [Ruby](https://github.com/pawurb/ruby-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/ecto_psql_extras)
18
+
19
+ ## Installation
10
20
 
11
21
  In your Gemfile
12
22
 
13
23
  ```ruby
14
- gem 'rails-pg-extras'
24
+ gem "rails-pg-extras"
15
25
  ```
16
26
 
17
- ### Usage
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
+ RailsPGExtras.extensions
33
+ ```
34
+ You should see the similar line in the output:
35
+
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
  Each command can be used as a rake task, or a directly from the Ruby code.
20
43
 
@@ -53,12 +76,13 @@ RailsPGExtras.cache_hit(in_format: :raw) =>
53
76
  #<PG::Result:0x00007f75777f7328 status=PGRES_TUPLES_OK ntuples=2 nfields=2 cmd_tuples=2>
54
77
  ```
55
78
 
56
- ### Available methods
79
+ ## Available methods
57
80
 
58
- #### `cache_hit`
81
+ ### `cache_hit`
59
82
 
60
- ```bash
83
+ ```
61
84
  $ rake pg_extras:cache_hit
85
+
62
86
  name | ratio
63
87
  ----------------+------------------------
64
88
  index hit rate | 0.99957765013541945832
@@ -68,10 +92,43 @@ $ rake pg_extras:cache_hit
68
92
 
69
93
  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.
70
94
 
71
- #### `index_usage`
95
+ ### `index_cache_hit`
96
+
97
+ ```
98
+
99
+ $ rake pg_extras:index_cache_hit
100
+
101
+ | name | buffer_hits | block_reads | total_read | ratio |
102
+ +-----------------------+-------------+-------------+------------+-------------------+
103
+ | teams | 187665 | 109 | 187774 | 0.999419514948821 |
104
+ | subscriptions | 5160 | 6 | 5166 | 0.99883855981417 |
105
+ | plans | 5718 | 9 | 5727 | 0.998428496595076 |
106
+ (truncated results for brevity)
107
+ ```
108
+
109
+ The same as `cache_hit` with each table's indexes cache hit info displayed seperately.
110
+
111
+ ### `table_cache_hit`
112
+
113
+ ```
114
+
115
+ $ rake pg_extras:table_cache_hit
116
+
117
+ | name | buffer_hits | block_reads | total_read | ratio |
118
+ +-----------------------+-------------+-------------+------------+-------------------+
119
+ | plans | 32123 | 2 | 32125 | 0.999937743190662 |
120
+ | subscriptions | 95021 | 8 | 95029 | 0.999915815172211 |
121
+ | teams | 171637 | 200 | 171837 | 0.99883610631005 |
122
+ (truncated results for brevity)
123
+ ```
124
+
125
+ The same as `cache_hit` with each table's cache hit info displayed seperately.
126
+
127
+ ### `index_usage`
72
128
 
73
129
  ```
74
130
  $ rake pg_extras:index_usage
131
+
75
132
  relname | percent_of_times_index_used | rows_in_table
76
133
  ---------------------+-----------------------------+---------------
77
134
  events | 65 | 1217347
@@ -114,6 +171,7 @@ This command displays all the current locks, regardless of their type.
114
171
 
115
172
  ```
116
173
  $ rake pg_extras:outliers
174
+
117
175
  qry | exec_time | prop_exec_time | ncalls | sync_io_time
118
176
  -----------------------------------------+------------------+----------------+-------------+--------------
119
177
  SELECT * FROM archivable_usage_events.. | 154:39:26.431466 | 72.2% | 34,211,877 | 00:00:00
@@ -122,11 +180,7 @@ $ rake pg_extras:outliers
122
180
  INSERT INTO usage_events (id, retaine.. | 01:42:59.436532 | 0.8% | 12,328,187 | 00:00:00
123
181
  SELECT * FROM usage_events WHERE (alp.. | 01:18:10.754354 | 0.6% | 102,114,301 | 00:00:00
124
182
  UPDATE usage_events SET reporter_id =.. | 00:52:35.683254 | 0.4% | 23,786,348 | 00:00:00
125
- INSERT INTO usage_events (id, retaine.. | 00:49:24.952561 | 0.4% | 21,988,201 | 00:00:00
126
- COPY public.app_ownership_events (id,.. | 00:37:14.31082 | 0.3% | 13 | 00:12:32.584754
127
- INSERT INTO app_ownership_events (id,.. | 00:26:59.808212 | 0.2% | 383,109 | 00:00:00
128
- SELECT * FROM app_ownership_events .. | 00:19:06.021846 | 0.1% | 744,879 | 00:00:00
129
- (10 rows)
183
+ (truncated results for brevity)
130
184
  ```
131
185
 
132
186
  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).
@@ -137,6 +191,7 @@ Typically, an efficient query will have an appropriate ratio of calls to total e
137
191
 
138
192
  ```
139
193
  $ rake pg_extras:calls
194
+
140
195
  qry | exec_time | prop_exec_time | ncalls | sync_io_time
141
196
  -----------------------------------------+------------------+----------------+-------------+--------------
142
197
  SELECT * FROM usage_events WHERE (alp.. | 01:18:11.073333 | 0.6% | 102,120,780 | 00:00:00
@@ -144,12 +199,7 @@ $ rake pg_extras:calls
144
199
  COMMIT | 00:00:52.31724 | 0.0% | 47,288,615 | 00:00:00
145
200
  SELECT * FROM archivable_usage_event.. | 154:39:26.431466 | 72.2% | 34,211,877 | 00:00:00
146
201
  UPDATE usage_events SET reporter_id =.. | 00:52:35.986167 | 0.4% | 23,788,388 | 00:00:00
147
- INSERT INTO usage_events (id, retaine.. | 00:49:25.260245 | 0.4% | 21,990,326 | 00:00:00
148
- INSERT INTO usage_events (id, retaine.. | 01:42:59.436532 | 0.8% | 12,328,187 | 00:00:00
149
- SELECT * FROM app_ownership_events .. | 00:19:06.289521 | 0.1% | 744,976 | 00:00:00
150
- INSERT INTO app_ownership_events(id, .. | 00:26:59.885631 | 0.2% | 383,153 | 00:00:00
151
- UPDATE app_ownership_events SET app_i.. | 00:01:22.282337 | 0.0% | 359,741 | 00:00:00
152
- (10 rows)
202
+ (truncated results for brevity)
153
203
  ```
154
204
 
155
205
  This command is much like `pg:outliers`, but ordered by the number of times a statement has been called.
@@ -158,6 +208,7 @@ This command is much like `pg:outliers`, but ordered by the number of times a st
158
208
 
159
209
  ```
160
210
  $ rake pg_extras:blocking
211
+
161
212
  blocked_pid | blocking_statement | blocking_duration | blocking_pid | blocked_statement | blocked_duration
162
213
  -------------+--------------------------+-------------------+--------------+------------------------------------------------------------------------------------+------------------
163
214
  461 | select count(*) from app | 00:00:03.838314 | 15682 | UPDATE "app" SET "updated_at" = '2013-03-04 15:07:04.746688' WHERE "id" = 12823149 | 00:00:03.821826
@@ -170,6 +221,7 @@ This command displays statements that are currently holding locks that other sta
170
221
 
171
222
  ```
172
223
  $ rake pg_extras:total_index_size
224
+
173
225
  size
174
226
  -------
175
227
  28194 MB
@@ -194,11 +246,6 @@ $ rake pg_extras:index_size
194
246
  index_attempts_on_enrollment_id | 1957 MB
195
247
  index_enrollment_attemptables_by_enrollment_activity_id | 1789 MB
196
248
  enrollment_activities_pkey | 458 MB
197
- index_enrollment_activities_by_lesson_enrollment_and_activity | 402 MB
198
- index_placement_attempts_on_response_id | 109 MB
199
- index_placement_attempts_on_placement_test_id | 108 MB
200
- index_placement_attempts_on_grade_level_id | 97 MB
201
- index_lesson_enrollments_on_lesson_id | 93 MB
202
249
  (truncated results for brevity)
203
250
  ```
204
251
 
@@ -208,6 +255,7 @@ This command displays the size of each each index in the database, in MB. It is
208
255
 
209
256
  ```
210
257
  $ rake pg_extras:table_size
258
+
211
259
  name | size
212
260
  ---------------------------------------------------------------+---------
213
261
  learning_coaches | 196 MB
@@ -218,12 +266,13 @@ $ rake pg_extras:table_size
218
266
  (truncated results for brevity)
219
267
  ```
220
268
 
221
- 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.
269
+ 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.
222
270
 
223
271
  ### `table_indexes_size`
224
272
 
225
273
  ```
226
- $ rake pg_extras:table-indexes-size
274
+ $ rake pg_extras:table_indexes_size
275
+
227
276
  table | indexes_size
228
277
  ---------------------------------------------------------------+--------------
229
278
  learning_coaches | 153 MB
@@ -234,12 +283,13 @@ $ rake pg_extras:table-indexes-size
234
283
  (truncated results for brevity)
235
284
  ```
236
285
 
237
- 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()`.
286
+ 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()`.
238
287
 
239
288
  ### `total_table_size`
240
289
 
241
290
  ```
242
291
  $ rake pg_extras:total_table_size
292
+
243
293
  name | size
244
294
  ---------------------------------------------------------------+---------
245
295
  learning_coaches | 349 MB
@@ -250,12 +300,13 @@ $ rake pg_extras:total_table_size
250
300
  (truncated results for brevity)
251
301
  ```
252
302
 
253
- 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.
303
+ 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.
254
304
 
255
305
  ### `unused_indexes`
256
306
 
257
307
  ```
258
308
  $ rake pg_extras:unused_indexes
309
+
259
310
  table | index | index_size | index_scans
260
311
  ---------------------+--------------------------------------------+------------+-------------
261
312
  public.grade_levels | index_placement_attempts_on_grade_level_id | 97 MB | 0
@@ -294,7 +345,7 @@ $ rake pg_extras:seq_scans
294
345
 
295
346
  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.
296
347
 
297
- ### long_running_queries
348
+ ### `long_running_queries`
298
349
 
299
350
  ```
300
351
  $ rake pg_extras:long_running_queries
@@ -309,10 +360,11 @@ $ rake pg_extras:long_running_queries
309
360
 
310
361
  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`.
311
362
 
312
- ### records_rank
363
+ ### `records_rank`
313
364
 
314
365
  ```
315
366
  $ rake pg_extras:records_rank
367
+
316
368
  name | estimated_count
317
369
  -----------------------------------+-----------------
318
370
  tastypie_apiaccess | 568891
@@ -326,7 +378,7 @@ $ rake pg_extras:records_rank
326
378
 
327
379
  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.
328
380
 
329
- ### bloat
381
+ ### `bloat`
330
382
 
331
383
  ```
332
384
  $ rake pg_extras:bloat
@@ -338,39 +390,51 @@ $ rake pg_extras:bloat
338
390
  index | public | bloated_table::bloated_index | 3.7 | 34 MB
339
391
  table | public | clean_table | 0.2 | 3808 kB
340
392
  table | public | other_clean_table | 0.3 | 1576 kB
393
+ (truncated results for brevity)
341
394
  ```
342
395
 
343
396
  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.
344
397
 
345
- ### vacuum_stats
398
+ ### `vacuum_stats`
346
399
 
347
400
  ```
348
401
  $ rake pg_extras:vacuum_stats
402
+
349
403
  schema | table | last_vacuum | last_autovacuum | rowcount | dead_rowcount | autovacuum_threshold | expect_autovacuum
350
404
  --------+-----------------------+-------------+------------------+----------------+----------------+----------------------+-------------------
351
405
  public | log_table | | 2013-04-26 17:37 | 18,030 | 0 | 3,656 |
352
406
  public | data_table | | 2013-04-26 13:09 | 79 | 28 | 66 |
353
407
  public | other_table | | 2013-04-26 11:41 | 41 | 47 | 58 |
354
408
  public | queue_table | | 2013-04-26 17:39 | 12 | 8,228 | 52 | yes
355
- public | picnic_table | | | 13 | 0 | 53 |
409
+ (truncated results for brevity)
356
410
  ```
357
411
 
358
412
  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.
359
413
 
360
- ### mandelbrot
414
+ ### `kill_all`
415
+
416
+ ```ruby
417
+
418
+ RailsPGExtras.kill_all
361
419
 
362
- ```
363
- $ rake pg_extras:mandelbrot
364
420
  ```
365
421
 
366
- This command outputs the Mandelbrot set, calculated through SQL.
422
+ 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.
423
+
424
+ ### `extensions`
425
+
426
+ ```ruby
427
+
428
+ RailsPGExtras.extensions
367
429
 
368
- ## FAQ
430
+ ```
369
431
 
370
- * Does is not violate the Heroku PG Extras license?
432
+ This command lists all the currently installed and available PostgreSQL extensions.
371
433
 
372
- The original plugin is MIT based so it means that copying and redistribution in any format is permitted.
434
+ ### mandelbrot
373
435
 
374
- ## Disclaimer
436
+ ```
437
+ $ rake pg_extras:mandelbrot
438
+ ```
375
439
 
376
- This tool is in beta state.
440
+ This command outputs the Mandelbrot set, calculated through SQL.
@@ -3,9 +3,16 @@
3
3
  require 'rails-pg-extras'
4
4
 
5
5
  namespace :pg_extras do
6
+ desc "Establish connection"
7
+ task :establish_connection do
8
+ db_config_file = File.open('config/database.yml')
9
+ db_config = YAML::load(db_config_file)
10
+ ActiveRecord::Base.establish_connection(db_config[Rails.env])
11
+ end
12
+
6
13
  RailsPGExtras::QUERIES.each do |query_name|
7
14
  desc RubyPGExtras.description_for(query_name: query_name)
8
- task query_name.to_sym => :environment do
15
+ task query_name.to_sym => :establish_connection do
9
16
  RailsPGExtras.public_send(query_name)
10
17
  end
11
18
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsPGExtras
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: rails-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: ruby-pg-extras
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.7.0
19
+ version: 1.2.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.7.0
26
+ version: 1.2.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -93,7 +93,7 @@ homepage: http://github.com/pawurb/rails-pg-extras
93
93
  licenses:
94
94
  - MIT
95
95
  metadata: {}
96
- post_install_message:
96
+ post_install_message:
97
97
  rdoc_options: []
98
98
  require_paths:
99
99
  - lib
@@ -108,8 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.0.6
112
- signing_key:
111
+ rubygems_version: 3.1.2
112
+ signing_key:
113
113
  specification_version: 4
114
114
  summary: Rails PostgreSQL performance database insights
115
115
  test_files: