pg_query_optimizer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/pg_query_optimizer/version.rb +5 -0
- data/lib/pg_query_optimizer.rb +78 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1ce5a4026e2bb5739220739b2e273dfc78661657e2ed9ae4ede358b508c8282a
|
4
|
+
data.tar.gz: 218f3542fa3912a8e002c642733d867f1c6385dd82db497e32c941ff5d187255
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 03f381c2b3c74fe83c395c2c788265af67e41ed67ea07b7666157d298018eaab6b433d5f5e077f117dfe030903bcd10d2ef64243e6358ed566a161fbbe4ebfea
|
7
|
+
data.tar.gz: 8852fcaacd8bb317d1a20fa8518a0d3c75b8473d20e6725946369e8f0490de4c44cc28e168ea05c9e384950c01834a0615bce38623bcf3e8aa1bafebfcd796c4
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "pg_query_optimizer/version"
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
module PgQueryOptimizer
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
DEFAULT_SETTINGS = {
|
15
|
+
max_parallel_workers_per_gather: 4,
|
16
|
+
parallel_setup_cost: 1000,
|
17
|
+
parallel_tuple_cost: 0.1,
|
18
|
+
min_parallel_table_scan_size: '8MB',
|
19
|
+
min_parallel_index_scan_size: '512kB'
|
20
|
+
}.freeze
|
21
|
+
|
22
|
+
def store_current_values
|
23
|
+
@previous_values = {
|
24
|
+
max_parallel_workers_per_gather: ActiveRecord::Base.connection.execute("SHOW max_parallel_workers_per_gather").first["max_parallel_workers_per_gather"],
|
25
|
+
parallel_setup_cost: ActiveRecord::Base.connection.execute("SHOW parallel_setup_cost").first["parallel_setup_cost"],
|
26
|
+
parallel_tuple_cost: ActiveRecord::Base.connection.execute("SHOW parallel_tuple_cost").first["parallel_tuple_cost"],
|
27
|
+
min_parallel_table_scan_size: ActiveRecord::Base.connection.execute("SHOW min_parallel_table_scan_size").first["min_parallel_table_scan_size"],
|
28
|
+
min_parallel_index_scan_size: ActiveRecord::Base.connection.execute("SHOW min_parallel_index_scan_size").first["min_parallel_index_scan_size"]
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
def estore_previous_values
|
33
|
+
@previous_values.each do |key, value|
|
34
|
+
ActiveRecord::Base.connection.execute("SET #{key} = '#{value}'")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def sufficient_resources?
|
39
|
+
# Implement checks to ensure system has sufficient resources
|
40
|
+
total_memory = `free -m | grep Mem: | awk '{print $2}'`.to_i
|
41
|
+
required_memory = 1024 # required memory in MB
|
42
|
+
|
43
|
+
total_memory >= required_memory
|
44
|
+
end
|
45
|
+
|
46
|
+
def enable_parallel_execution
|
47
|
+
store_current_values
|
48
|
+
DEFAULT_SETTINGS.each do |key, value|
|
49
|
+
ActiveRecord::Base.connection.execute("SET #{key} = '#{value}'")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def reset_parallel_execution
|
54
|
+
restore_previous_values
|
55
|
+
end
|
56
|
+
|
57
|
+
def pg_optimize(max_workers: 4)
|
58
|
+
verify_postgresql_adapter!
|
59
|
+
if sufficient_resources?
|
60
|
+
enable_parallel_execution
|
61
|
+
yield
|
62
|
+
else
|
63
|
+
# If resources are insufficient, run the query without parallel execution
|
64
|
+
yield
|
65
|
+
end
|
66
|
+
ensure
|
67
|
+
reset_parallel_execution
|
68
|
+
end
|
69
|
+
|
70
|
+
def verify_postgresql_adapter!
|
71
|
+
unless ActiveRecord::Base.connection.adapter_name.downcase == 'postgresql'
|
72
|
+
raise UnsupportedDatabaseError, "PgQueryOptimizer only supports PostgreSQL"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
ActiveRecord::Base.include(PgQueryOptimizer)
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_query_optimizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jana
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.21'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.21'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
description: pg_query_optimizer is a gem that provides a method to enable and manage
|
42
|
+
parallel execution of PostgreSQL queries, improving the performance of complex queries.
|
43
|
+
email:
|
44
|
+
- shanmugamjanarthan24@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- lib/pg_query_optimizer.rb
|
50
|
+
- lib/pg_query_optimizer/version.rb
|
51
|
+
homepage: https://github.com/janarthanan-shanmugam/pg_query_optimizer
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
homepage_uri: https://github.com/janarthanan-shanmugam/pg_query_optimizer
|
56
|
+
source_code_uri: https://github.com/janarthanan-shanmugam/pg_query_optimizer
|
57
|
+
changelog_uri: https://github.com/janarthanan-shanmugam/pg_query_optimizer
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 3.0.0
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.5.11
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Optimizes PostgreSQL queries for better performance by enabling parallel
|
77
|
+
execution.
|
78
|
+
test_files: []
|