pg-statistics-rails 0.3.0 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11ce78ef3227a25b112d01fb970df1843da30f6e24352f7dd1b97bf19823f2a4
4
- data.tar.gz: d5d45ad5d5ef54ed9c281318c2853c1583f976ae4dd83e66caf865de5fc0fdc5
3
+ metadata.gz: e404844e378d2520a0684d71a938fce19e48d221814b06dd5d1a8d3be39dff95
4
+ data.tar.gz: b197537afaed5af5cb1fc3b4441f05f5051955e40216b658bf575b71b53f5e48
5
5
  SHA512:
6
- metadata.gz: 71facf13566e90ccfd9354aaa9724858faa988e4952ab5eccee04bdc5ab5cb9abfea41d25ea097d1ad3a8e70993ff3e05ebf87ef451a8d1ff0e2a14890e6b469
7
- data.tar.gz: 0ff6ac216dde4f54006ff9783e3e7422c98288aba8d9e245c258b0d675cc220b6e1b5fc1f728f09549545f813f49fb029eae7202e343d160869d0e5bac217747
6
+ metadata.gz: 248502d7eb94d3027ec7316b81ef286a21cf8937f2ca7678a1e2d613945e1500c744cfdbb64d09fe33275cbc66ec50f7d01047a81a1c712e524f88639579858c
7
+ data.tar.gz: 40a7d654d1ce2b686b1095da8c5982de56e99df3aad9fc7464fbfa395af1743bd3a1a84fa876dbdb29943de335955d5568fa5e1c34464c4766a72fe4b663a308
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1 (2026-11-26)
4
+
5
+ - In a migration, `create_statistics` analyzes each table once, after the migration's last step, instead of after every call. A table or statistics object the migration drops is not analyzed.
6
+
3
7
  ## 0.3.0 (2026-11-26)
4
8
 
5
9
  - `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.
data/README.md CHANGED
@@ -53,7 +53,7 @@ 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.
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. In a migration the `ANALYZE` waits for the migration's last step and runs once per table, so several `create_statistics` calls on one table sample it once, and a table or object the migration drops is not analyzed. On a partitioned table it runs `ANALYZE ONLY` on PostgreSQL 18+, so the partitions are not analyzed one by one.
57
57
 
58
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.
59
59
 
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PgStatisticsRails
4
+ module Migration
5
+ def exec_migration(connection, direction)
6
+ return super unless connection.respond_to?(:analyzing_statistics_once)
7
+
8
+ connection.analyzing_statistics_once { super }
9
+ end
10
+ end
11
+ end
@@ -66,7 +66,28 @@ module PgStatisticsRails
66
66
 
67
67
  execute sql
68
68
  execute "ALTER STATISTICS #{quote_table_name(name)} SET STATISTICS #{Integer(options[:target])}" unless options[:target].nil?
69
- analyze_statistics(table_name) if analyze
69
+ return unless analyze
70
+
71
+ if @statistics_tables_to_analyze
72
+ @statistics_tables_to_analyze << table_name.to_s
73
+ else
74
+ analyze_statistics(table_name)
75
+ end
76
+ end
77
+
78
+ # Runs the block, then analyzes each table that `create_statistics` was
79
+ # asked to analyze in it once. Every migration runs inside this.
80
+ def analyzing_statistics_once
81
+ return yield if @statistics_tables_to_analyze
82
+
83
+ begin
84
+ @statistics_tables_to_analyze = []
85
+ result = yield
86
+ @statistics_tables_to_analyze.uniq.each { |table_name| analyze_statistics(table_name) }
87
+ result
88
+ ensure
89
+ @statistics_tables_to_analyze = nil
90
+ end
70
91
  end
71
92
 
72
93
  # Sets a column's statistics target, how much `ANALYZE` samples it. A nil
@@ -232,10 +253,13 @@ module PgStatisticsRails
232
253
  columns = select_values(<<~SQL, "SCHEMA")
233
254
  SELECT DISTINCT a.attname
234
255
  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
256
+ LEFT JOIN pg_attribute a ON a.attrelid = s.stxrelid AND a.attnum = ANY (s.stxkeys)
257
+ WHERE s.stxrelid = to_regclass(#{quote(quote_table_name(table_name))})
237
258
  ORDER BY a.attname
238
259
  SQL
260
+ return if columns.empty?
261
+
262
+ columns.compact!
239
263
 
240
264
  sql = +"ANALYZE"
241
265
  sql << " ONLY" if database_version >= 18_00_00 && partitioned_table?(table_name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgStatisticsRails
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
@@ -6,10 +6,12 @@ require_relative "pg_statistics_rails/statistics_definition"
6
6
  require_relative "pg_statistics_rails/schema_statements"
7
7
  require_relative "pg_statistics_rails/schema_dumper"
8
8
  require_relative "pg_statistics_rails/command_recorder"
9
+ require_relative "pg_statistics_rails/migration"
9
10
 
10
11
  ActiveSupport.on_load(:active_record) do
11
12
  require "active_record/migration/command_recorder"
12
13
  ActiveRecord::Migration::CommandRecorder.include(PgStatisticsRails::CommandRecorder)
14
+ ActiveRecord::Migration.prepend(PgStatisticsRails::Migration)
13
15
  end
14
16
 
15
17
  ActiveSupport.on_load(:active_record_postgresqladapter) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-statistics-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Fader
@@ -53,6 +53,7 @@ files:
53
53
  - lib/pg-statistics-rails.rb
54
54
  - lib/pg_statistics_rails.rb
55
55
  - lib/pg_statistics_rails/command_recorder.rb
56
+ - lib/pg_statistics_rails/migration.rb
56
57
  - lib/pg_statistics_rails/schema_dumper.rb
57
58
  - lib/pg_statistics_rails/schema_statements.rb
58
59
  - lib/pg_statistics_rails/statistics_definition.rb