rails-pg-extras 1.6.0 → 2.3.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: b44baf89a4349ef720b371a8a83a8397e0e848794b1bf248398cfd19c5f606d5
4
- data.tar.gz: ebdabe9d275ed5ef7132ce604f894a42520226db0b1dc5d50acd092a0aed6dd4
3
+ metadata.gz: 67b102fb8ff0b2dc05c7bf14662b8a4359808356f0f36e55ec6ab9012824a936
4
+ data.tar.gz: 41c9c07be416ccf5a3558d2488a5c242e14e594864935545693940b8cf40875d
5
5
  SHA512:
6
- metadata.gz: d0b9e1505835974df1959e620385461f7c5c92838efadd18abb57a5656d1b942d8f097f7c70389e815a96a5dcb239b67b16cee430188e0532ccef57eb2a6ff50
7
- data.tar.gz: 269a9b8bb29c5114c43885d61f849b3ed73664cb7c5e9e772c85281cacebbce641b224fe0e246bda5a7ab5426210ee3a75a0ac0a36c6555abb64988d7b66b733
6
+ metadata.gz: 8208f2581593a9fff622add53dc6d3e8051abf570ce404f2cc48df9e6b5793635f8679e489b457b319951926a81803304d6a3b464114c989722f6917bf9ff121
7
+ data.tar.gz: aef18c1ccdcff519549efae8c4dfb379f8ef4eb20662e2948af4b54ad04e46c316c7cc10164415793bfd8364c70785eab7b4f115d20d38f830993441830371d1
data/.circleci/config.yml CHANGED
@@ -6,6 +6,22 @@ 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
10
+ name: postgres11
11
+ environment:
12
+ POSTGRES_USER: postgres
13
+ POSTGRES_DB: rails-pg-extras-test
14
+ POSTGRES_PASSWORD: secret
15
+ - image: circleci/postgres:12.7
16
+ command: postgres -c shared_preload_libraries=pg_stat_statements
17
+ name: postgres12
18
+ environment:
19
+ POSTGRES_USER: postgres
20
+ POSTGRES_DB: rails-pg-extras-test
21
+ POSTGRES_PASSWORD: secret
22
+ - image: circleci/postgres:13.3
23
+ command: postgres -c shared_preload_libraries=pg_stat_statements
24
+ name: postgres13
9
25
  environment:
10
26
  POSTGRES_USER: postgres
11
27
  POSTGRES_DB: rails-pg-extras-test
@@ -16,13 +32,24 @@ jobs:
16
32
  - run: gem update --system
17
33
  - run: gem install bundler
18
34
  - run: bundle install --path vendor/bundle
19
- - run: sudo apt-get update
35
+ - run: sudo apt-get update --allow-releaseinfo-change
20
36
  - run: sudo apt install postgresql-client
21
- - run: dockerize -wait tcp://localhost:5432 -timeout 1m
37
+ - run: dockerize -wait tcp://postgres11:5432 -timeout 1m
38
+ - run:
39
+ name: Run specs for PG 11
40
+ environment:
41
+ DATABASE_URL: postgresql://postgres:secret@postgres11:5432/rails-pg-extras-test
42
+ command: bundle exec rspec spec/
43
+ - run:
44
+ name: Run specs for PG 12
45
+ environment:
46
+ DATABASE_URL: postgresql://postgres:secret@postgres12:5432/rails-pg-extras-test
47
+ command: bundle exec rspec spec/
22
48
  - run:
23
- name: Run specs
24
- command: |
25
- bundle exec rspec spec/
49
+ name: Run specs for PG 13
50
+ environment:
51
+ DATABASE_URL: postgresql://postgres:secret@postgres13:5432/rails-pg-extras-test
52
+ command: bundle exec rspec spec/
26
53
  workflows:
27
54
  version: 2
28
55
  test:
data/README.md CHANGED
@@ -4,7 +4,13 @@ Rails port of [Heroku PG Extras](https://github.com/heroku/heroku-pg-extras) wit
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
- 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).
7
+ You can read 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
+
9
+ **Shameless plug:** rails-pg-extras is one of the tools that I use when conducting Rails performance audits. [Check out my offer](https://pawelurbanek.com/#rails-performance-tuning) if you need help with fine-tuning your app.
10
+
11
+ Use [rails-pg-extras-web](https://github.com/defkode/rails-pg-extras-web) if you want to see SQL metrics in the UI instead of a command line interface.
12
+
13
+ ![Web interface](https://github.com/pawurb/rails-pg-extras/raw/master/rails-pg-extras-web.png)
8
14
 
9
15
  Alternative versions:
10
16
 
@@ -167,6 +173,20 @@ This method displays values for selected PostgreSQL settings. You can compare th
167
173
 
168
174
  [More info](https://pawelurbanek.com/postgresql-fix-performance#cache-hit)
169
175
 
176
+ ### `ssl_used`
177
+
178
+ ```ruby
179
+
180
+ RailsPGExtras.ssl_used
181
+
182
+ | ssl_is_used |
183
+ +---------------------------------+
184
+ | t |
185
+
186
+ ```
187
+
188
+ Returns boolean indicating if an encrypted SSL is currently used. Connecting to the database via an unencrypted connection is a critical security risk.
189
+
170
190
  ### `index_usage`
171
191
 
172
192
  ```ruby
@@ -394,6 +414,19 @@ This command displays indexes that have < 50 scans recorded against them, and ar
394
414
 
395
415
  [More info](https://pawelurbanek.com/postgresql-fix-performance#unused-indexes)
396
416
 
417
+ ### `duplicate_indexes`
418
+
419
+ ```ruby
420
+
421
+ RailsPGExtras.duplicate_indexes
422
+
423
+ | size | idx1 | idx2 | idx3 | idx4 |
424
+ +------------+--------------+----------------+----------+-----------+
425
+ | 128 k | users_pkey | index_users_id | | |
426
+ ```
427
+
428
+ This command displays multiple indexes that have the same set of columns, same opclass, expression and predicate - which make them equivalent. Usually it's safe to drop one of them.
429
+
397
430
  ### `null_indexes`
398
431
 
399
432
  ```ruby
@@ -524,22 +557,30 @@ RailsPGExtras.kill_all
524
557
 
525
558
  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.
526
559
 
527
- ### `buffercache_stats`
560
+ ### `pg_stat_statements_reset`
528
561
 
529
- 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.
562
+ ```ruby
563
+ RailsPGExtras.pg_stat_statements_reset
564
+ ```
565
+
566
+ This command discards all statistics gathered so far by pg_stat_statements.
567
+
568
+ ### `buffercache_stats`
530
569
 
531
570
  ```ruby
532
571
  RailsPGExtras.buffercache_stats(args: { limit: 10 })
533
572
  ```
534
573
 
535
- ### `buffercache_usage`
574
+ 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.
536
575
 
537
- This command calculates how many blocks from which table are currently cached.
576
+ ### `buffercache_usage`
538
577
 
539
578
  ```ruby
540
579
  RailsPGExtras.buffercache_usage(args: { limit: 20 })
541
580
  ```
542
581
 
582
+ This command calculates how many blocks from which table are currently cached.
583
+
543
584
  ### `extensions`
544
585
 
545
586
  ```ruby
@@ -565,8 +606,17 @@ $ rake pg_extras:mandelbrot
565
606
 
566
607
  This command outputs the Mandelbrot set, calculated through SQL.
567
608
 
609
+ ## Testing
610
+
611
+ ```bash
612
+ cp docker-compose.yml.sample docker-compose.yml
613
+ docker compose up -d
614
+ rake test_all
615
+ ```
616
+
568
617
  ## Query sources
569
618
 
570
619
  - [https://github.com/heroku/heroku-pg-extras](https://github.com/heroku/heroku-pg-extras)
571
620
  - [https://hakibenita.com/postgresql-unused-index-size](https://hakibenita.com/postgresql-unused-index-size)
572
621
  - [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)
622
+ - [https://wiki.postgresql.org/wiki/Index_Maintenance](https://wiki.postgresql.org/wiki/Index_Maintenance)
data/Rakefile CHANGED
@@ -3,3 +3,8 @@ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
+ desc 'Test all PG versions'
7
+ task :test_all do
8
+ system("PG_VERSION=11 bundle exec rspec spec/ && PG_VERSION=12 bundle exec rspec spec/ && PG_VERSION=13 bundle exec rspec spec/")
9
+ end
10
+
@@ -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
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
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
25
+ environment:
26
+ POSTGRES_USER: postgres
27
+ POSTGRES_DB: rails-pg-extras-test
28
+ POSTGRES_PASSWORD: secret
29
+ ports:
30
+ - '5434:5432'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsPGExtras
4
- VERSION = "1.6.0"
4
+ VERSION = "2.3.0"
5
5
  end
@@ -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)
Binary file
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.6.0
4
+ version: 2.3.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-04-05 00:00:00.000000000 Z
11
+ date: 2021-10-27 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.6.0
19
+ version: 2.3.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.6.0
26
+ version: 2.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -86,6 +86,7 @@ files:
86
86
  - lib/rails-pg-extras/railtie.rb
87
87
  - lib/rails-pg-extras/tasks/all.rake
88
88
  - lib/rails-pg-extras/version.rb
89
+ - rails-pg-extras-web.png
89
90
  - rails-pg-extras.gemspec
90
91
  - spec/smoke_spec.rb
91
92
  - spec/spec_helper.rb
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  - !ruby/object:Gem::Version
109
110
  version: '0'
110
111
  requirements: []
111
- rubygems_version: 3.1.4
112
+ rubygems_version: 3.1.6
112
113
  signing_key:
113
114
  specification_version: 4
114
115
  summary: Rails PostgreSQL performance database insights