rails-pg-extras 1.5.0 → 2.0.0

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: 897f027634b0313e42a13d864678eac7f0280c00a9a39ca5d668cabbba970b02
4
- data.tar.gz: d23efda1f8afcbd989278b9362baacb4882796115d7e70263a812787f95de340
3
+ metadata.gz: c107fe728b02513bceaa617c45f5cf5bc4048fb0376ec46a13a70ba86cff4cf9
4
+ data.tar.gz: 07fd71c6c2a1ff119b30845f32967d4d2c49d8ae0eccfbdaf52b228b5c907541
5
5
  SHA512:
6
- metadata.gz: 612408e64b97f03c70dcab7525cce2be8cafa4b9cf5eca27dd88013b5d74ffd08c5e68532599158c697d8754b9fa1ee534bbb698a9603df489d7394b1d036dab
7
- data.tar.gz: f9c66eb8837a6720de96132e0941364aa3e83f627d52a04221efbbe2d361c1c9f2a24635057868c631ecee54fe42a9aa599b9a354f82341f84cd7a0ff2a524b2
6
+ metadata.gz: d9de7a07381f8d203e60f98b750b0791e7be932dfaf8a621106bc69bbe44eb4ce5ca0893e4d8b22905913626f28bedc77da7fad282c4171ea51e3b424c359984
7
+ data.tar.gz: 99872d4b0efcddf6d4a0af106068fc2a83544053c13596c2532320ac746a3ca6174d7ff1fd8e3a8c2533b49acaeaac896bbf6def5bc1f5dc083e6956b9cf52f9
data/.circleci/config.yml CHANGED
@@ -6,6 +6,7 @@ jobs:
6
6
  environment:
7
7
  DATABASE_URL: postgresql://postgres:secret@localhost:5432/rails-pg-extras-test
8
8
  - image: circleci/postgres:11.5
9
+ command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
9
10
  environment:
10
11
  POSTGRES_USER: postgres
11
12
  POSTGRES_DB: rails-pg-extras-test
data/README.md CHANGED
@@ -89,7 +89,9 @@ RailsPGExtras.long_running_queries(args: { threshold: "200 milliseconds" })
89
89
 
90
90
  ### `cache_hit`
91
91
 
92
- ```
92
+ ```ruby
93
+ RailsPGExtras.cache_hit
94
+
93
95
  $ rake pg_extras:cache_hit
94
96
 
95
97
  name | ratio
@@ -101,9 +103,13 @@ $ rake pg_extras:cache_hit
101
103
 
102
104
  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.
103
105
 
106
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#cache-hit)
107
+
104
108
  ### `index_cache_hit`
105
109
 
106
- ```
110
+ ```ruby
111
+
112
+ RailsPGExtras.index_cache_hit
107
113
 
108
114
  $ rake pg_extras:index_cache_hit
109
115
 
@@ -117,9 +123,13 @@ $ rake pg_extras:index_cache_hit
117
123
 
118
124
  The same as `cache_hit` with each table's indexes cache hit info displayed separately.
119
125
 
126
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#cache-hit)
127
+
120
128
  ### `table_cache_hit`
121
129
 
122
- ```
130
+ ```ruby
131
+
132
+ RailsPGExtras.table_cache_hit
123
133
 
124
134
  $ rake pg_extras:table_cache_hit
125
135
 
@@ -133,9 +143,35 @@ $ rake pg_extras:table_cache_hit
133
143
 
134
144
  The same as `cache_hit` with each table's cache hit info displayed seperately.
135
145
 
136
- ### `index_usage`
146
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#cache-hit)
147
+
148
+ ### `db_settings`
149
+
150
+ ```ruby
151
+
152
+ RailsPGExtras.db_settings
153
+
154
+ $ rake pg_extras:db_settings
155
+
156
+ name | setting | unit |
157
+ ------------------------------+---------+------+
158
+ checkpoint_completion_target | 0.7 | |
159
+ default_statistics_target | 100 | |
160
+ effective_cache_size | 1350000 | 8kB |
161
+ effective_io_concurrency | 1 | |
162
+ (truncated results for brevity)
137
163
 
138
164
  ```
165
+
166
+ This method displays values for selected PostgreSQL settings. You can compare them with settings recommended by [PGTune](https://pgtune.leopard.in.ua/#/) and tweak values to improve performance.
167
+
168
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#cache-hit)
169
+
170
+ ### `index_usage`
171
+
172
+ ```ruby
173
+ RailsPGExtras.index_usage
174
+
139
175
  $ rake pg_extras:index_usage
140
176
 
141
177
  relname | percent_of_times_index_used | rows_in_table
@@ -152,7 +188,9 @@ This command provides information on the efficiency of indexes, represented as w
152
188
 
153
189
  ### `locks`
154
190
 
155
- ```
191
+ ```ruby
192
+ RailsPGExtras.locks
193
+
156
194
  $ rake pg_extras:locks
157
195
 
158
196
  procpid | relname | transactionid | granted | query_snippet | mode | age
@@ -168,9 +206,13 @@ $ rake pg_extras:locks
168
206
 
169
207
  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.
170
208
 
209
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#deadlocks)
210
+
171
211
  ### `all_locks`
172
212
 
173
- ```
213
+ ```ruby
214
+ RailsPGExtras.all_locks
215
+
174
216
  $ rake pg_extras:all_locks
175
217
  ```
176
218
 
@@ -178,7 +220,9 @@ This command displays all the current locks, regardless of their type.
178
220
 
179
221
  ### `outliers`
180
222
 
181
- ```
223
+ ```ruby
224
+ RailsPGExtras.outliers(args: { limit: 20 })
225
+
182
226
  $ rake pg_extras:outliers
183
227
 
184
228
  qry | exec_time | prop_exec_time | ncalls | sync_io_time
@@ -196,9 +240,13 @@ This command displays statements, obtained from `pg_stat_statements`, ordered by
196
240
 
197
241
  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.
198
242
 
243
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#missing-indexes)
244
+
199
245
  ### `calls`
200
246
 
201
- ```
247
+ ```ruby
248
+ RailsPGExtras.calls(args: { limit: 10 })
249
+
202
250
  $ rake pg_extras:calls
203
251
 
204
252
  qry | exec_time | prop_exec_time | ncalls | sync_io_time
@@ -213,9 +261,13 @@ $ rake pg_extras:calls
213
261
 
214
262
  This command is much like `pg:outliers`, but ordered by the number of times a statement has been called.
215
263
 
264
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#missing-indexes)
265
+
216
266
  ### `blocking`
217
267
 
218
- ```
268
+ ```ruby
269
+ RailsPGExtras.blocking
270
+
219
271
  $ rake pg_extras:blocking
220
272
 
221
273
  blocked_pid | blocking_statement | blocking_duration | blocking_pid | blocked_statement | blocked_duration
@@ -226,9 +278,13 @@ $ rake pg_extras:blocking
226
278
 
227
279
  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.
228
280
 
229
- #### `total_index_size`
281
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#deadlocks)
282
+
283
+ ### `total_index_size`
284
+
285
+ ```ruby
286
+ RailsPGExtras.total_index_size
230
287
 
231
- ```
232
288
  $ rake pg_extras:total_index_size
233
289
 
234
290
  size
@@ -241,7 +297,9 @@ This command displays the total size of all indexes on the database, in MB. It i
241
297
 
242
298
  ### `index_size`
243
299
 
244
- ```
300
+ ```ruby
301
+ RailsPGExtras.index_size
302
+
245
303
  $ rake pg_extras:index_size
246
304
  name | size
247
305
  ---------------------------------------------------------------+---------
@@ -262,7 +320,9 @@ This command displays the size of each each index in the database, in MB. It is
262
320
 
263
321
  ### `table_size`
264
322
 
265
- ```
323
+ ```ruby
324
+ RailsPGExtras.table_size
325
+
266
326
  $ rake pg_extras:table_size
267
327
 
268
328
  name | size
@@ -279,7 +339,9 @@ This command displays the size of each table and materialized view in the databa
279
339
 
280
340
  ### `table_indexes_size`
281
341
 
282
- ```
342
+ ```ruby
343
+ RailsPGExtras.table_indexes_size
344
+
283
345
  $ rake pg_extras:table_indexes_size
284
346
 
285
347
  table | indexes_size
@@ -296,7 +358,9 @@ This command displays the total size of indexes for each table and materialized
296
358
 
297
359
  ### `total_table_size`
298
360
 
299
- ```
361
+ ```ruby
362
+ RailsPGExtras.total_table_size
363
+
300
364
  $ rake pg_extras:total_table_size
301
365
 
302
366
  name | size
@@ -313,7 +377,9 @@ This command displays the total size of each table and materialized view in the
313
377
 
314
378
  ### `unused_indexes`
315
379
 
316
- ```
380
+ ```ruby
381
+ RailsPGExtras.unused_indexes(args: { min_scans: 20 })
382
+
317
383
  $ rake pg_extras:unused_indexes
318
384
 
319
385
  table | index | index_size | index_scans
@@ -326,10 +392,14 @@ $ rake pg_extras:unused_indexes
326
392
 
327
393
  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.
328
394
 
395
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#unused-indexes)
396
+
329
397
  ### `null_indexes`
330
398
 
331
399
  ```ruby
332
400
 
401
+ RailsPGExtras.null_indexes(args: { min_relation_size_mb: 10 })
402
+
333
403
  $ rake pg_extras:null_indexes
334
404
 
335
405
  oid | index | index_size | unique | indexed_column | null_frac | expected_saving
@@ -340,11 +410,15 @@ $ rake pg_extras:null_indexes
340
410
 
341
411
  ```
342
412
 
343
- 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).
413
+ This command 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.
414
+
415
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#null-indexes)
344
416
 
345
417
  ### `seq_scans`
346
418
 
347
- ```
419
+ ```ruby
420
+ RailsPGExtras.seq_scans
421
+
348
422
  $ rake pg_extras:seq_scans
349
423
 
350
424
  name | count
@@ -357,22 +431,18 @@ $ rake pg_extras:seq_scans
357
431
  messages | 3922247
358
432
  contests_customers | 2915972
359
433
  classroom_goals | 2142014
360
- contests | 1370267
361
- goals | 1112659
362
- districts | 158995
363
- rollup_reports | 115942
364
- customers | 93847
365
- schools | 92984
366
- classrooms | 92982
367
- customer_settings | 91226
368
434
  (truncated results for brevity)
369
435
  ```
370
436
 
371
437
  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.
372
438
 
439
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#missing-indexes)
440
+
373
441
  ### `long_running_queries`
374
442
 
375
- ```
443
+ ```ruby
444
+ RailsPGExtras.long_running_queries(args: { threshold: "200 milliseconds" })
445
+
376
446
  $ rake pg_extras:long_running_queries
377
447
 
378
448
  pid | duration | query
@@ -387,7 +457,9 @@ This command displays currently running queries, that have been running for long
387
457
 
388
458
  ### `records_rank`
389
459
 
390
- ```
460
+ ```ruby
461
+ RailsPGExtras.records_rank
462
+
391
463
  $ rake pg_extras:records_rank
392
464
 
393
465
  name | estimated_count
@@ -405,7 +477,9 @@ This command displays an estimated count of rows per table, descending by estima
405
477
 
406
478
  ### `bloat`
407
479
 
408
- ```
480
+ ```ruby
481
+ RailsPGExtras.bloat
482
+
409
483
  $ rake pg_extras:bloat
410
484
 
411
485
  type | schemaname | object_name | bloat | waste
@@ -420,9 +494,13 @@ $ rake pg_extras:bloat
420
494
 
421
495
  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.
422
496
 
497
+ [More info](https://pawelurbanek.com/postgresql-fix-performance#bloat)
498
+
423
499
  ### `vacuum_stats`
424
500
 
425
- ```
501
+ ```ruby
502
+ RailsPGExtras.vacuum_stats
503
+
426
504
  $ rake pg_extras:vacuum_stats
427
505
 
428
506
  schema | table | last_vacuum | last_autovacuum | rowcount | dead_rowcount | autovacuum_threshold | expect_autovacuum
@@ -446,20 +524,49 @@ RailsPGExtras.kill_all
446
524
 
447
525
  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.
448
526
 
527
+ ### `buffercache_stats`
528
+
529
+ ```ruby
530
+ RailsPGExtras.buffercache_stats(args: { limit: 10 })
531
+ ```
532
+
533
+ This command shows the relations buffered in database share buffer, ordered by percentage taken. It also shows that how much of the whole relation is buffered.
534
+
535
+ ### `buffercache_usage`
536
+
537
+ ```ruby
538
+ RailsPGExtras.buffercache_usage(args: { limit: 20 })
539
+ ```
540
+
541
+ This command calculates how many blocks from which table are currently cached.
542
+
449
543
  ### `extensions`
450
544
 
451
545
  ```ruby
452
546
 
453
547
  RailsPGExtras.extensions
454
548
 
549
+ $ rake pg_extras:extensions
550
+
551
+ | pg_stat_statements | 1.7 | 1.7 | track execution statistics of all SQL statements executed
552
+ (truncated results for brevity)
553
+
455
554
  ```
456
555
 
457
556
  This command lists all the currently installed and available PostgreSQL extensions.
458
557
 
459
558
  ### mandelbrot
460
559
 
461
- ```
560
+ ```ruby
561
+ RailsPGExtras.mandelbrot
562
+
462
563
  $ rake pg_extras:mandelbrot
463
564
  ```
464
565
 
465
566
  This command outputs the Mandelbrot set, calculated through SQL.
567
+
568
+ ## Query sources
569
+
570
+ - [https://github.com/heroku/heroku-pg-extras](https://github.com/heroku/heroku-pg-extras)
571
+ - [https://hakibenita.com/postgresql-unused-index-size](https://hakibenita.com/postgresql-unused-index-size)
572
+ - [https://sites.google.com/site/itmyshare/database-tips-and-examples/postgres/useful-sqls-to-check-contents-of-postgresql-shared_buffer](https://sites.google.com/site/itmyshare/database-tips-and-examples/postgres/useful-sqls-to-check-contents-of-postgresql-shared_buffer)
@@ -1,12 +1,30 @@
1
1
  version: '3'
2
2
 
3
3
  services:
4
- postgres:
4
+ postgres11:
5
5
  image: postgres:11.5-alpine
6
+ command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
6
7
  environment:
7
8
  POSTGRES_USER: postgres
8
9
  POSTGRES_DB: rails-pg-extras-test
9
10
  POSTGRES_PASSWORD: secret
10
11
  ports:
11
12
  - '5432:5432'
12
-
13
+ postgres12:
14
+ image: postgres:12.7-alpine
15
+ command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
16
+ environment:
17
+ POSTGRES_USER: postgres
18
+ POSTGRES_DB: rails-pg-extras-test
19
+ POSTGRES_PASSWORD: secret
20
+ ports:
21
+ - '5433:5432'
22
+ postgres13:
23
+ image: postgres:13.3-alpine
24
+ command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
25
+ environment:
26
+ POSTGRES_USER: postgres
27
+ POSTGRES_DB: rails-pg-extras-test
28
+ POSTGRES_PASSWORD: secret
29
+ ports:
30
+ - '5434:5432'
@@ -6,6 +6,7 @@ require 'ruby-pg-extras'
6
6
  module RailsPGExtras
7
7
  QUERIES = RubyPGExtras::QUERIES
8
8
  DEFAULT_ARGS = RubyPGExtras::DEFAULT_ARGS
9
+ NEW_PG_STAT_STATEMENTS = RubyPGExtras::NEW_PG_STAT_STATEMENTS
9
10
 
10
11
  QUERIES.each do |query_name|
11
12
  define_singleton_method query_name do |options = {}|
@@ -18,6 +19,16 @@ module RailsPGExtras
18
19
  end
19
20
 
20
21
  def self.run_query(query_name:, in_format:, args: {})
22
+ if %i(calls outliers).include?(query_name)
23
+ pg_stat_statements_ver = RailsPGExtras.connection.execute("select installed_version from pg_available_extensions where name='pg_stat_statements'")
24
+ .to_a[0].fetch("installed_version", nil)
25
+ if pg_stat_statements_ver != nil
26
+ if Gem::Version.new(pg_stat_statements_ver) < Gem::Version.new(NEW_PG_STAT_STATEMENTS)
27
+ query_name = "#{query_name}_legacy".to_sym
28
+ end
29
+ end
30
+ end
31
+
21
32
  sql = if (custom_args = DEFAULT_ARGS[query_name].merge(args)) != {}
22
33
  RubyPGExtras.sql_for(query_name: query_name) % custom_args
23
34
  else
@@ -36,8 +47,6 @@ module RailsPGExtras
36
47
  def self.connection
37
48
  ActiveRecord::Base.connection
38
49
  end
39
-
40
- private_class_method :connection
41
50
  end
42
51
 
43
52
  require 'rails-pg-extras/railtie' if defined?(Rails)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsPGExtras
4
- VERSION = "1.5.0"
4
+ VERSION = "2.0.0"
5
5
  end
data/spec/smoke_spec.rb CHANGED
@@ -3,9 +3,12 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe RailsPGExtras do
6
- PG_STATS_DEPENDENT_QUERIES = %i(calls outliers)
6
+ before(:all) do
7
+ RailsPGExtras.connection.execute("CREATE EXTENSION IF NOT EXISTS pg_buffercache;")
8
+ RubyPGExtras.connection.exec("CREATE EXTENSION IF NOT EXISTS pg_stat_statements;")
9
+ end
7
10
 
8
- (RailsPGExtras::QUERIES - PG_STATS_DEPENDENT_QUERIES).each do |query_name|
11
+ RailsPGExtras::QUERIES.each do |query_name|
9
12
  it "#{query_name} query can be executed" do
10
13
  expect do
11
14
  RailsPGExtras.run_query(
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,19 @@ require 'bundler/setup'
5
5
  require 'active_record'
6
6
  require_relative '../lib/rails-pg-extras'
7
7
 
8
- ENV["DATABASE_URL"] ||= "postgresql://postgres:secret@localhost:5432/rails-pg-extras-test"
8
+ pg_version = ENV["PG_VERSION"]
9
+
10
+ port = if pg_version == "11"
11
+ "5432"
12
+ elsif pg_version == "12"
13
+ "5433"
14
+ elsif pg_version == "13"
15
+ "5434"
16
+ else
17
+ "5432"
18
+ end
19
+
20
+ ENV["DATABASE_URL"] ||= "postgresql://postgres:secret@localhost:#{port}/rails-pg-extras-test"
9
21
 
10
22
  RSpec.configure do |config|
11
23
  config.before :suite do
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.5.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-13 00:00:00.000000000 Z
11
+ date: 2021-07-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: 1.5.0
19
+ version: 2.0.0
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.5.0
26
+ version: 2.0.0
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.4
111
+ rubygems_version: 3.1.6
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Rails PostgreSQL performance database insights