rails-pg-extras 1.2.4 → 1.5.1

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: 808e77df9b7f529b420f7a88775e90e031b055dd26804fd61a80b6fc2e8d96b7
4
- data.tar.gz: '01853117599237d182f2edb901550cf52ba46691a98e1f8a1f2594f139b90e4b'
3
+ metadata.gz: 0322aaf3e072e39ad3174eb39bc66711c7738ad0e943aaac6056a4a42efa7368
4
+ data.tar.gz: 898d8e611d025487cf03c9ead7271427af08fb2604b35eab1c90e44b820e2769
5
5
  SHA512:
6
- metadata.gz: e2d7d8a9558dea4637026b9c165942816c77527229172ca398c81ee0badc940e0c970f4049b2464d2aa667cef83ec43f11d8afd0c253203fda98b47f6703e87f
7
- data.tar.gz: 7f4975baacf9d2822eeca20f260f03debec3dabb1e11544c48bf4b0a323393c8966a792b64e91777e95758c29cc69a1295660174195ddb66310c9601fd66d148
6
+ metadata.gz: 49e97a75d7af406f40f070b97890d2c9eedc1c759c9941991b545596d344b3af9a1adfa23daf86998992fb409618160474e6075b7d158a83ff9b2d3b37dc659d
7
+ data.tar.gz: fb1549df9972842ef573ca47d4eaf0b17722b054be82693110afdc0cb0972b59ba25148794c1f743fc1e26fb8abe65c70f135c2339a99ab4495a5680550544cb
data/.circleci/config.yml CHANGED
@@ -16,6 +16,7 @@ jobs:
16
16
  - run: gem update --system
17
17
  - run: gem install bundler
18
18
  - run: bundle install --path vendor/bundle
19
+ - run: sudo apt-get update
19
20
  - run: sudo apt install postgresql-client
20
21
  - run: dockerize -wait tcp://localhost:5432 -timeout 1m
21
22
  - run:
data/README.md CHANGED
@@ -16,6 +16,8 @@ Alternative versions:
16
16
 
17
17
  - [Python](https://github.com/pawurb/ecto_psql_extras)
18
18
 
19
+ - [Haskell](https://github.com/pawurb/haskell-pg-extras)
20
+
19
21
  ## Installation
20
22
 
21
23
  In your Gemfile
@@ -76,11 +78,20 @@ RailsPGExtras.cache_hit(in_format: :raw) =>
76
78
  #<PG::Result:0x00007f75777f7328 status=PGRES_TUPLES_OK ntuples=2 nfields=2 cmd_tuples=2>
77
79
  ```
78
80
 
81
+ Some methods accept an optional `args` param allowing you to customize queries:
82
+
83
+ ```ruby
84
+ RailsPGExtras.long_running_queries(args: { threshold: "200 milliseconds" })
85
+
86
+ ```
87
+
79
88
  ## Available methods
80
89
 
81
90
  ### `cache_hit`
82
91
 
83
92
  ```
93
+ RailsPGExtras.cache_hit
94
+
84
95
  $ rake pg_extras:cache_hit
85
96
 
86
97
  name | ratio
@@ -96,6 +107,8 @@ This command provides information on the efficiency of the buffer cache, for bot
96
107
 
97
108
  ```
98
109
 
110
+ RailsPGExtras.index_cache_hit
111
+
99
112
  $ rake pg_extras:index_cache_hit
100
113
 
101
114
  | name | buffer_hits | block_reads | total_read | ratio |
@@ -106,12 +119,14 @@ $ rake pg_extras:index_cache_hit
106
119
  (truncated results for brevity)
107
120
  ```
108
121
 
109
- The same as `cache_hit` with each table's indexes cache hit info displayed seperately.
122
+ The same as `cache_hit` with each table's indexes cache hit info displayed separately.
110
123
 
111
124
  ### `table_cache_hit`
112
125
 
113
126
  ```
114
127
 
128
+ RailsPGExtras.table_cache_hit
129
+
115
130
  $ rake pg_extras:table_cache_hit
116
131
 
117
132
  | name | buffer_hits | block_reads | total_read | ratio |
@@ -127,6 +142,8 @@ The same as `cache_hit` with each table's cache hit info displayed seperately.
127
142
  ### `index_usage`
128
143
 
129
144
  ```
145
+ RailsPGExtras.index_usage
146
+
130
147
  $ rake pg_extras:index_usage
131
148
 
132
149
  relname | percent_of_times_index_used | rows_in_table
@@ -144,6 +161,8 @@ This command provides information on the efficiency of indexes, represented as w
144
161
  ### `locks`
145
162
 
146
163
  ```
164
+ RailsPGExtras.locks
165
+
147
166
  $ rake pg_extras:locks
148
167
 
149
168
  procpid | relname | transactionid | granted | query_snippet | mode | age
@@ -157,11 +176,13 @@ $ rake pg_extras:locks
157
176
  (4 rows)
158
177
  ```
159
178
 
160
- This command displays queries that have taken out an exlusive lock on a relation. Exclusive locks typically prevent other operations on that relation from taking place, and can be a cause of "hung" queries that are waiting for a lock to be granted.
179
+ This command displays queries that have taken out an exclusive lock on a relation. Exclusive locks typically prevent other operations on that relation from taking place, and can be a cause of "hung" queries that are waiting for a lock to be granted.
161
180
 
162
181
  ### `all_locks`
163
182
 
164
183
  ```
184
+ RailsPGExtras.all_locks
185
+
165
186
  $ rake pg_extras:all_locks
166
187
  ```
167
188
 
@@ -170,6 +191,8 @@ This command displays all the current locks, regardless of their type.
170
191
  ### `outliers`
171
192
 
172
193
  ```
194
+ RubyPGExtras.outliers(args: { limit: 20 })
195
+
173
196
  $ rake pg_extras:outliers
174
197
 
175
198
  qry | exec_time | prop_exec_time | ncalls | sync_io_time
@@ -183,13 +206,15 @@ $ rake pg_extras:outliers
183
206
  (truncated results for brevity)
184
207
  ```
185
208
 
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).
209
+ 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 file system).
187
210
 
188
211
  Typically, an efficient query will have an appropriate ratio of calls to total execution time, with as little time spent on I/O as possible. Queries that have a high total execution time but low call count should be investigated to improve their performance. Queries that have a high proportion of execution time being spent on synchronous I/O should also be investigated.
189
212
 
190
213
  ### `calls`
191
214
 
192
215
  ```
216
+ RubyPGExtras.calls(args: { limit: 10 })
217
+
193
218
  $ rake pg_extras:calls
194
219
 
195
220
  qry | exec_time | prop_exec_time | ncalls | sync_io_time
@@ -207,6 +232,8 @@ This command is much like `pg:outliers`, but ordered by the number of times a st
207
232
  ### `blocking`
208
233
 
209
234
  ```
235
+ RubyPGExtras.blocking
236
+
210
237
  $ rake pg_extras:blocking
211
238
 
212
239
  blocked_pid | blocking_statement | blocking_duration | blocking_pid | blocked_statement | blocked_duration
@@ -220,6 +247,8 @@ This command displays statements that are currently holding locks that other sta
220
247
  #### `total_index_size`
221
248
 
222
249
  ```
250
+ RubyPGExtras.total_index_size
251
+
223
252
  $ rake pg_extras:total_index_size
224
253
 
225
254
  size
@@ -233,6 +262,8 @@ This command displays the total size of all indexes on the database, in MB. It i
233
262
  ### `index_size`
234
263
 
235
264
  ```
265
+ RubyPGExtras.index_size
266
+
236
267
  $ rake pg_extras:index_size
237
268
  name | size
238
269
  ---------------------------------------------------------------+---------
@@ -254,6 +285,8 @@ This command displays the size of each each index in the database, in MB. It is
254
285
  ### `table_size`
255
286
 
256
287
  ```
288
+ RubyPGExtras.table_size
289
+
257
290
  $ rake pg_extras:table_size
258
291
 
259
292
  name | size
@@ -271,6 +304,8 @@ This command displays the size of each table and materialized view in the databa
271
304
  ### `table_indexes_size`
272
305
 
273
306
  ```
307
+ RubyPGExtras.table_indexes_size
308
+
274
309
  $ rake pg_extras:table_indexes_size
275
310
 
276
311
  table | indexes_size
@@ -283,11 +318,13 @@ $ rake pg_extras:table_indexes_size
283
318
  (truncated results for brevity)
284
319
  ```
285
320
 
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()`.
321
+ This command displays the total size of indexes for each table and materialized view, in MB. It is calculated by using the system administration function `pg_indexes_size()`.
287
322
 
288
323
  ### `total_table_size`
289
324
 
290
325
  ```
326
+ RubyPGExtras.total_table_size
327
+
291
328
  $ rake pg_extras:total_table_size
292
329
 
293
330
  name | size
@@ -305,6 +342,8 @@ This command displays the total size of each table and materialized view in the
305
342
  ### `unused_indexes`
306
343
 
307
344
  ```
345
+ RubyPGExtras.unused_indexes(args: { min_scans: 20 })
346
+
308
347
  $ rake pg_extras:unused_indexes
309
348
 
310
349
  table | index | index_size | index_scans
@@ -317,9 +356,29 @@ $ rake pg_extras:unused_indexes
317
356
 
318
357
  This command displays indexes that have < 50 scans recorded against them, and are greater than 5 pages in size, ordered by size relative to the number of index scans. This command is generally useful for eliminating indexes that are unused, which can impact write performance, as well as read performance should they occupy space in memory.
319
358
 
359
+ ### `null_indexes`
360
+
361
+ ```ruby
362
+
363
+ RailsPGExtras.null_indexes(args: { min_relation_size_mb: 10 })
364
+
365
+ $ rake pg_extras:null_indexes
366
+
367
+ oid | index | index_size | unique | indexed_column | null_frac | expected_saving
368
+ ---------+--------------------+------------+--------+----------------+-----------+-----------------
369
+ 183764 | users_reset_token | 1445 MB | t | reset_token | 97.00% | 1401 MB
370
+ 88732 | plan_cancelled_at | 539 MB | f | cancelled_at | 8.30% | 44 MB
371
+ 9827345 | users_email | 18 MB | t | email | 28.67% | 5160 kB
372
+
373
+ ```
374
+
375
+ This commands displays indexes that contain `NULL` values. A high ratio of `NULL` values means that using a partial index excluding them will be beneficial in case they are not used for searching. [Source and more info](https://hakibenita.com/postgresql-unused-index-size).
376
+
320
377
  ### `seq_scans`
321
378
 
322
379
  ```
380
+ RubyPGExtras.seq_scans
381
+
323
382
  $ rake pg_extras:seq_scans
324
383
 
325
384
  name | count
@@ -343,11 +402,13 @@ $ rake pg_extras:seq_scans
343
402
  (truncated results for brevity)
344
403
  ```
345
404
 
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.
405
+ 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 under-indexed, and it may be worth investigating queries that read from these tables.
347
406
 
348
407
  ### `long_running_queries`
349
408
 
350
409
  ```
410
+ RubyPGExtras.long_running_queries(args: { threshold: "200 milliseconds" })
411
+
351
412
  $ rake pg_extras:long_running_queries
352
413
 
353
414
  pid | duration | query
@@ -363,6 +424,8 @@ This command displays currently running queries, that have been running for long
363
424
  ### `records_rank`
364
425
 
365
426
  ```
427
+ RubyPGExtras.records_rank
428
+
366
429
  $ rake pg_extras:records_rank
367
430
 
368
431
  name | estimated_count
@@ -381,6 +444,8 @@ This command displays an estimated count of rows per table, descending by estima
381
444
  ### `bloat`
382
445
 
383
446
  ```
447
+ RubyPGExtras.bloat
448
+
384
449
  $ rake pg_extras:bloat
385
450
 
386
451
  type | schemaname | object_name | bloat | waste
@@ -398,6 +463,8 @@ This command displays an estimation of table "bloat" – space allocated to a re
398
463
  ### `vacuum_stats`
399
464
 
400
465
  ```
466
+ RubyPGExtras.vacuum_stats
467
+
401
468
  $ rake pg_extras:vacuum_stats
402
469
 
403
470
  schema | table | last_vacuum | last_autovacuum | rowcount | dead_rowcount | autovacuum_threshold | expect_autovacuum
@@ -409,7 +476,7 @@ $ rake pg_extras:vacuum_stats
409
476
  (truncated results for brevity)
410
477
  ```
411
478
 
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.
479
+ This command displays statistics related to vacuum operations for each table, including an estimation 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.
413
480
 
414
481
  ### `kill_all`
415
482
 
@@ -423,10 +490,15 @@ This commands kills all the currently active connections to the database. It can
423
490
 
424
491
  ### `extensions`
425
492
 
426
- ```ruby
493
+ ```
427
494
 
428
495
  RailsPGExtras.extensions
429
496
 
497
+ $ rake pg_extras:extensions
498
+
499
+ | pg_stat_statements | 1.7 | 1.7 | track execution statistics of all SQL statements executed
500
+ (truncated results for brevity)
501
+
430
502
  ```
431
503
 
432
504
  This command lists all the currently installed and available PostgreSQL extensions.
@@ -434,6 +506,8 @@ This command lists all the currently installed and available PostgreSQL extensio
434
506
  ### mandelbrot
435
507
 
436
508
  ```
509
+ RailsPGExtras.mandelbrot
510
+
437
511
  $ rake pg_extras:mandelbrot
438
512
  ```
439
513
 
@@ -5,20 +5,26 @@ require 'ruby-pg-extras'
5
5
 
6
6
  module RailsPGExtras
7
7
  QUERIES = RubyPGExtras::QUERIES
8
+ DEFAULT_ARGS = RubyPGExtras::DEFAULT_ARGS
8
9
 
9
10
  QUERIES.each do |query_name|
10
- define_singleton_method query_name do |options = { in_format: :display_table }|
11
+ define_singleton_method query_name do |options = {}|
11
12
  run_query(
12
13
  query_name: query_name,
13
- in_format: options.fetch(:in_format)
14
+ in_format: options.fetch(:in_format, :display_table),
15
+ args: options.fetch(:args, {})
14
16
  )
15
17
  end
16
18
  end
17
19
 
18
- def self.run_query(query_name:, in_format:)
19
- result = connection.execute(
20
+ def self.run_query(query_name:, in_format:, args: {})
21
+ sql = if (custom_args = DEFAULT_ARGS[query_name].merge(args)) != {}
22
+ RubyPGExtras.sql_for(query_name: query_name) % custom_args
23
+ else
20
24
  RubyPGExtras.sql_for(query_name: query_name)
21
- )
25
+ end
26
+
27
+ result = connection.execute(sql)
22
28
 
23
29
  RubyPGExtras.display_result(
24
30
  result,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsPGExtras
4
- VERSION = "1.2.4"
4
+ VERSION = "1.5.1"
5
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: 1.2.4
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2021-02-13 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: 1.2.4
19
+ version: 1.5.1
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: 1.2.4
26
+ version: 1.5.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.1.2
111
+ rubygems_version: 3.1.4
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Rails PostgreSQL performance database insights