pg-statistics-rails 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +11 -1
- data/lib/pg_statistics_rails/command_recorder.rb +41 -1
- data/lib/pg_statistics_rails/schema_statements.rb +59 -7
- data/lib/pg_statistics_rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 11ce78ef3227a25b112d01fb970df1843da30f6e24352f7dd1b97bf19823f2a4
|
|
4
|
+
data.tar.gz: d5d45ad5d5ef54ed9c281318c2853c1583f976ae4dd83e66caf865de5fc0fdc5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 71facf13566e90ccfd9354aaa9724858faa988e4952ab5eccee04bdc5ab5cb9abfea41d25ea097d1ad3a8e70993ff3e05ebf87ef451a8d1ff0e2a14890e6b469
|
|
7
|
+
data.tar.gz: 0ff6ac216dde4f54006ff9783e3e7422c98288aba8d9e245c258b0d675cc220b6e1b5fc1f728f09549545f813f49fb029eae7202e343d160869d0e5bac217747
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0 (2026-11-26)
|
|
4
|
+
|
|
5
|
+
- `create_statistics` analyzes the table right after creating the object, so the planner uses it before autovacuum next analyzes the table. The `ANALYZE` covers the plain columns of every statistics object on the table, which builds their expressions too, or the whole table when they have only expressions. `if_not_exists: true` skips it when the object already exists. `analyze: false` skips it. On PostgreSQL 18+ a partitioned table gets `ANALYZE ONLY`, leaving its partitions alone.
|
|
6
|
+
- `set_column_statistics_target` and `set_index_statistics_target` take `from:` and `to:`, which makes them reversible in `change`. Given a bare target, rolling them back raises `ActiveRecord::IrreversibleMigration` instead of applying the target again. A `from:` without `to:`, or the reverse, raises `ArgumentError`.
|
|
7
|
+
- Package every file world-readable. 0.2.0 shipped four files under `lib/` with mode 0640, so Ruby running as any user other than the one that installed the gem cannot load it. `rake build` and `rake release` now stop when a packaged file is not world-readable.
|
|
8
|
+
|
|
3
9
|
## 0.2.0 (2026-10-24)
|
|
4
10
|
|
|
5
11
|
- Statistics targets: `create_statistics` takes `target:`, and `set_column_statistics_target` / `set_index_statistics_target` set column and expression-index targets. All three are read back by `statistics` / `statistics_targets` and dumped into `db/schema.rb` after each table.
|
data/README.md
CHANGED
|
@@ -53,6 +53,9 @@ Options:
|
|
|
53
53
|
- `:kinds` — Array of kinds to build (`:ndistinct`, `:dependencies`, `:mcv`). Defaults to all kinds.
|
|
54
54
|
- `:if_not_exists` — Emits `CREATE STATISTICS IF NOT EXISTS`.
|
|
55
55
|
- `:target` — The object's statistics target (`ALTER STATISTICS ... SET STATISTICS`). Defaults to `default_statistics_target`.
|
|
56
|
+
- `:analyze` — Set to `false` to skip the `ANALYZE` that follows. By default the table is analyzed right after the object is created, so the planner uses it before autovacuum next analyzes the table. The `ANALYZE` reads only the plain columns of the table's statistics objects, which also builds their expressions, or the whole table when they have only expressions. With `:if_not_exists`, an object that already exists is not analyzed again. On a partitioned table it runs `ANALYZE ONLY` on PostgreSQL 18+, so the partitions are not analyzed one by one.
|
|
57
|
+
|
|
58
|
+
`ANALYZE` reads a sample of 300 × the largest statistics target rows, whatever the table's size: about 30,000 rows at the default target of 100, and up to 3,000,000 at 10,000. It holds a `SHARE UPDATE EXCLUSIVE` lock, the same lock `CREATE STATISTICS` takes, until the migration's transaction commits. Reads and writes go on, but autovacuum, other `ANALYZE` runs and most DDL on the table wait. On a large table with a high target, pass `analyze: false` and run `ANALYZE` outside the migration.
|
|
56
59
|
|
|
57
60
|
### Statistics targets
|
|
58
61
|
|
|
@@ -66,7 +69,14 @@ set_index_statistics_target "index_addresses_on_lower_city", 1, 1000 # column by
|
|
|
66
69
|
set_column_statistics_target :addresses, :city, nil
|
|
67
70
|
```
|
|
68
71
|
|
|
69
|
-
|
|
72
|
+
Given the previous target as `from:` and the new one as `to:`, they are reversible in `change`. Without `from:`, rolling back raises `ActiveRecord::IrreversibleMigration`:
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
set_column_statistics_target :addresses, :city, from: nil, to: 1000
|
|
76
|
+
set_index_statistics_target "index_addresses_on_lower_city", 1, from: nil, to: 1000
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Explicit targets are dumped after the table:
|
|
70
80
|
|
|
71
81
|
```ruby
|
|
72
82
|
create_statistics "addresses", ["city", "zip_code"], name: "addresses_city_zip_code_stat", target: 900
|
|
@@ -6,7 +6,7 @@ module PgStatisticsRails
|
|
|
6
6
|
if recorder_class.const_defined?(:ReversibleAndIrreversibleMethods) &&
|
|
7
7
|
!recorder_class::ReversibleAndIrreversibleMethods.frozen? &&
|
|
8
8
|
!recorder_class::ReversibleAndIrreversibleMethods.include?(:create_statistics)
|
|
9
|
-
recorder_class::ReversibleAndIrreversibleMethods.push(:create_statistics, :drop_statistics)
|
|
9
|
+
recorder_class::ReversibleAndIrreversibleMethods.push(:create_statistics, :drop_statistics, :set_column_statistics_target, :set_index_statistics_target)
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
if recorder_class.private_instance_methods.include?(:inverse_of) &&
|
|
@@ -17,6 +17,14 @@ module PgStatisticsRails
|
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
def self.invert_statistics_target(command, args, changes)
|
|
21
|
+
unless changes.is_a?(Hash) && changes.key?(:from) && changes.key?(:to)
|
|
22
|
+
raise ActiveRecord::IrreversibleMigration, "#{command} is only reversible if given a :from and :to option."
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
[command, [*args.first(2), { from: changes[:to], to: changes[:from] }]]
|
|
26
|
+
end
|
|
27
|
+
|
|
20
28
|
module KwargsRecorder
|
|
21
29
|
def create_statistics(*args, **kwargs, &block)
|
|
22
30
|
record(:create_statistics, args, kwargs, &block)
|
|
@@ -26,6 +34,14 @@ module PgStatisticsRails
|
|
|
26
34
|
record(:drop_statistics, args, kwargs, &block)
|
|
27
35
|
end
|
|
28
36
|
|
|
37
|
+
def set_column_statistics_target(*args, **kwargs, &block)
|
|
38
|
+
record(:set_column_statistics_target, args, kwargs, &block)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def set_index_statistics_target(*args, **kwargs, &block)
|
|
42
|
+
record(:set_index_statistics_target, args, kwargs, &block)
|
|
43
|
+
end
|
|
44
|
+
|
|
29
45
|
private
|
|
30
46
|
def invert_create_statistics(args, kwargs, &block)
|
|
31
47
|
kwargs = kwargs.dup
|
|
@@ -41,6 +57,14 @@ module PgStatisticsRails
|
|
|
41
57
|
kwargs[:if_not_exists] = kwargs.delete(:if_exists) if kwargs.key?(:if_exists)
|
|
42
58
|
[:create_statistics, args, kwargs, block]
|
|
43
59
|
end
|
|
60
|
+
|
|
61
|
+
def invert_set_column_statistics_target(args, kwargs, &block)
|
|
62
|
+
[*CommandRecorder.invert_statistics_target(:set_column_statistics_target, args, args[2] || kwargs), {}, block]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def invert_set_index_statistics_target(args, kwargs, &block)
|
|
66
|
+
[*CommandRecorder.invert_statistics_target(:set_index_statistics_target, args, args[2] || kwargs), {}, block]
|
|
67
|
+
end
|
|
44
68
|
end
|
|
45
69
|
|
|
46
70
|
module LegacyArgsRecorder
|
|
@@ -52,6 +76,14 @@ module PgStatisticsRails
|
|
|
52
76
|
record(:drop_statistics, args, &block)
|
|
53
77
|
end
|
|
54
78
|
|
|
79
|
+
ruby2_keywords def set_column_statistics_target(*args, &block)
|
|
80
|
+
record(:set_column_statistics_target, args, &block)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
ruby2_keywords def set_index_statistics_target(*args, &block)
|
|
84
|
+
record(:set_index_statistics_target, args, &block)
|
|
85
|
+
end
|
|
86
|
+
|
|
55
87
|
private
|
|
56
88
|
def invert_create_statistics(args, &block)
|
|
57
89
|
if (options = args.last).is_a?(Hash)
|
|
@@ -69,6 +101,14 @@ module PgStatisticsRails
|
|
|
69
101
|
end
|
|
70
102
|
[:create_statistics, args, block]
|
|
71
103
|
end
|
|
104
|
+
|
|
105
|
+
def invert_set_column_statistics_target(args, &block)
|
|
106
|
+
[*CommandRecorder.invert_statistics_target(:set_column_statistics_target, args, args[2]), block]
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def invert_set_index_statistics_target(args, &block)
|
|
110
|
+
[*CommandRecorder.invert_statistics_target(:set_index_statistics_target, args, args[2]), block]
|
|
111
|
+
end
|
|
72
112
|
end
|
|
73
113
|
end
|
|
74
114
|
end
|
|
@@ -47,10 +47,16 @@ module PgStatisticsRails
|
|
|
47
47
|
# : The object's statistics target (`ALTER STATISTICS ... SET STATISTICS`).
|
|
48
48
|
# Defaults to the server's `default_statistics_target`.
|
|
49
49
|
#
|
|
50
|
-
#
|
|
50
|
+
# `:analyze`
|
|
51
|
+
# : Set to `false` to leave building the statistics to the next `ANALYZE`
|
|
52
|
+
# of the table. Defaults to `true`.
|
|
53
|
+
#
|
|
54
|
+
# Statistics are built by an `ANALYZE` of the table straight after, so the
|
|
55
|
+
# planner can use them before autovacuum next analyzes the table.
|
|
51
56
|
def create_statistics(table_name, columns, **options)
|
|
52
57
|
name = statistics_name(table_name, columns: columns, **options)
|
|
53
58
|
kinds = Array(options[:kinds])
|
|
59
|
+
analyze = options[:analyze] != false && !(options[:if_not_exists] && statistics_exist?(name))
|
|
54
60
|
|
|
55
61
|
sql = +"CREATE STATISTICS"
|
|
56
62
|
sql << " IF NOT EXISTS" if options[:if_not_exists]
|
|
@@ -60,6 +66,7 @@ module PgStatisticsRails
|
|
|
60
66
|
|
|
61
67
|
execute sql
|
|
62
68
|
execute "ALTER STATISTICS #{quote_table_name(name)} SET STATISTICS #{Integer(options[:target])}" unless options[:target].nil?
|
|
69
|
+
analyze_statistics(table_name) if analyze
|
|
63
70
|
end
|
|
64
71
|
|
|
65
72
|
# Sets a column's statistics target, how much `ANALYZE` samples it. A nil
|
|
@@ -68,8 +75,14 @@ module PgStatisticsRails
|
|
|
68
75
|
# ```
|
|
69
76
|
# set_column_statistics_target :addresses, :city, 1000
|
|
70
77
|
# ```
|
|
71
|
-
|
|
72
|
-
|
|
78
|
+
#
|
|
79
|
+
# Passing the previous target as `:from` makes it reversible in `change`:
|
|
80
|
+
#
|
|
81
|
+
# ```
|
|
82
|
+
# set_column_statistics_target :addresses, :city, from: nil, to: 1000
|
|
83
|
+
# ```
|
|
84
|
+
def set_column_statistics_target(table_name, column_name, target_or_changes)
|
|
85
|
+
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET STATISTICS #{statistics_target_value(target_or_changes)}"
|
|
73
86
|
end
|
|
74
87
|
|
|
75
88
|
# Sets the statistics target of an expression index's column, by position
|
|
@@ -78,8 +91,10 @@ module PgStatisticsRails
|
|
|
78
91
|
# ```
|
|
79
92
|
# set_index_statistics_target "index_addresses_on_lower_city", 1, 1000
|
|
80
93
|
# ```
|
|
81
|
-
|
|
82
|
-
|
|
94
|
+
#
|
|
95
|
+
# Like `set_column_statistics_target`, it takes `from:` and `to:`.
|
|
96
|
+
def set_index_statistics_target(index_name, column_number, target_or_changes)
|
|
97
|
+
execute "ALTER INDEX #{quote_table_name(index_name)} ALTER COLUMN #{Integer(column_number)} SET STATISTICS #{statistics_target_value(target_or_changes)}"
|
|
83
98
|
end
|
|
84
99
|
|
|
85
100
|
# Returns the explicit statistics targets on the given table's columns and
|
|
@@ -202,8 +217,45 @@ module PgStatisticsRails
|
|
|
202
217
|
end
|
|
203
218
|
end
|
|
204
219
|
|
|
205
|
-
def
|
|
206
|
-
|
|
220
|
+
def statistics_exist?(name)
|
|
221
|
+
schema, bare_name = extract_schema_qualified_name(name.to_s)
|
|
222
|
+
select_value(<<~SQL, "SCHEMA")
|
|
223
|
+
SELECT EXISTS (
|
|
224
|
+
SELECT 1 FROM pg_statistic_ext s
|
|
225
|
+
JOIN pg_namespace n ON n.oid = s.stxnamespace
|
|
226
|
+
WHERE s.stxname = #{quote(bare_name)} AND n.nspname = #{schema ? quote(schema) : 'current_schema()'}
|
|
227
|
+
)
|
|
228
|
+
SQL
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def analyze_statistics(table_name)
|
|
232
|
+
columns = select_values(<<~SQL, "SCHEMA")
|
|
233
|
+
SELECT DISTINCT a.attname
|
|
234
|
+
FROM pg_statistic_ext s
|
|
235
|
+
JOIN pg_attribute a ON a.attrelid = s.stxrelid AND a.attnum = ANY (s.stxkeys)
|
|
236
|
+
WHERE s.stxrelid = #{quote(quote_table_name(table_name))}::regclass
|
|
237
|
+
ORDER BY a.attname
|
|
238
|
+
SQL
|
|
239
|
+
|
|
240
|
+
sql = +"ANALYZE"
|
|
241
|
+
sql << " ONLY" if database_version >= 18_00_00 && partitioned_table?(table_name)
|
|
242
|
+
sql << " #{quote_table_name(table_name)}"
|
|
243
|
+
sql << " (#{columns.map { |column| quote_column_name(column) }.join(', ')})" if columns.any?
|
|
244
|
+
|
|
245
|
+
execute sql
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def partitioned_table?(table_name)
|
|
249
|
+
select_value("SELECT relkind FROM pg_class WHERE oid = #{quote(quote_table_name(table_name))}::regclass", "SCHEMA") == "p"
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def statistics_target_value(target_or_changes)
|
|
253
|
+
if target_or_changes.is_a?(Hash)
|
|
254
|
+
raise ArgumentError, "Statistics target changes need both :from and :to" unless target_or_changes.key?(:from) && target_or_changes.key?(:to)
|
|
255
|
+
|
|
256
|
+
target_or_changes = target_or_changes[:to]
|
|
257
|
+
end
|
|
258
|
+
target_or_changes.nil? ? -1 : Integer(target_or_changes)
|
|
207
259
|
end
|
|
208
260
|
|
|
209
261
|
def fetch_statistics_by_schema(tables, &block)
|