Class: PgReindex
- Inherits:
-
Object
- Object
- PgReindex
- Defined in:
- lib/pg-reindex.rb
Constant Summary collapse
- MIN_SIZE =
megabyte min, total table size
50
Instance Method Summary collapse
- #cancel(procpid) ⇒ Object
- #check_swap_for_pkey ⇒ Object
- #database_size(database) ⇒ Object
- #exec(sql) ⇒ Object
-
#filter_relations(filter) ⇒ Object
filter is a string with ,.
- #get_index_size(relation_name) ⇒ Object
- #get_raw_relations ⇒ Object
- #get_struct_relations(min_size = nil) ⇒ Object
- #have_rebuild_for_relation?(relation) ⇒ Boolean
- #index_def(oid) ⇒ Object
- #index_sql(oid, name, new_name) ⇒ Object
- #index_sql_array(table, oid, name, primary = false) ⇒ Object
- #index_sqls(index_row) ⇒ Object
-
#initialize(conf) ⇒ PgReindex
constructor
A new instance of PgReindex.
- #install_swap_for_pkey ⇒ Object
- #queries ⇒ Object
- #swap_for_pkey_sql ⇒ Object
Constructor Details
#initialize(conf) ⇒ PgReindex
Returns a new instance of PgReindex.
7 8 9 10 11 12 |
# File 'lib/pg-reindex.rb', line 7 def initialize(conf) @cfg = {:host => conf['host'] || '127.0.0.1', :dbname => conf['database'], :user => conf['username'] || `whoami`.chop, :password => conf['password'] || 'password', :port => conf['port'].to_s} @conn = PGconn.new @cfg end |
Instance Method Details
#cancel(procpid) ⇒ Object
190 191 192 |
# File 'lib/pg-reindex.rb', line 190 def cancel(procpid) @conn.exec("select pg_cancel_backend(#{procpid.to_i})") end |
#check_swap_for_pkey ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/pg-reindex.rb', line 79 def check_swap_for_pkey res = @conn.exec <<-SQL SELECT proname FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_proc p ON pronamespace = n.oid WHERE nspname = 'public' and proname = 'swap_for_pkey' SQL res.count == 1 end |
#database_size(database) ⇒ Object
127 128 129 130 |
# File 'lib/pg-reindex.rb', line 127 def database_size(database) res = @conn.exec("SELECT pg_size_pretty(pg_database_size('#{database}')) as db_size") res.first['db_size'] end |
#exec(sql) ⇒ Object
175 176 177 |
# File 'lib/pg-reindex.rb', line 175 def exec(sql) @conn.exec sql end |
#filter_relations(filter) ⇒ Object
filter is a string with ,
33 34 35 36 37 38 39 40 41 |
# File 'lib/pg-reindex.rb', line 33 def filter_relations(filter) # filter is a string with , return [] if !filter || filter.empty? filter = filter.split(",") get_raw_relations.select do |row| filter.include?(row['index']) || filter.include?(row['table']) end end |
#get_index_size(relation_name) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/pg-reindex.rb', line 66 def get_index_size(relation_name) res = @conn.exec "SELECT pg_relation_size(oid) as i_size, pg_size_pretty(pg_relation_size(oid)) as i_size_p from pg_class WHERE relname = E'#{relation_name}'" res[0] rescue {} end |
#get_raw_relations ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pg-reindex.rb', line 43 def get_raw_relations res = @conn.exec <<-SQL SELECT C.relname AS "table", i.relname "index", i.oid "index_oid", pg_relation_size(C.oid) AS "size", pg_total_relation_size(C.oid) AS "total_size", pg_size_pretty(pg_relation_size(C.oid)) AS "size_p", pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size_p", pg_size_pretty(pg_total_relation_size(C.oid) - pg_relation_size(C.oid)) AS "total_i_size_p", pg_relation_size(i.oid) as "i_size", pg_size_pretty(pg_relation_size(i.oid)) as "i_size_p", ix.indisprimary as "primary" FROM pg_class C, pg_class i, pg_index ix, pg_namespace N WHERE nspname IN ('public') AND C.oid = ix.indrelid and i.oid = ix.indexrelid AND C.oid = ix.indrelid and i.oid = ix.indexrelid AND C.relname not like 'pg_%' AND N.oid = C.relnamespace ORDER BY c.relname, i.relname SQL end |
#get_struct_relations(min_size = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pg-reindex.rb', line 14 def get_struct_relations(min_size = nil) res = get_raw_relations result = {} res.each do |row| next if row['total_size'].to_i < ((min_size || MIN_SIZE).to_f * 1024 * 1024).to_i result[row['table']] ||= [] result[row['table']] << row end result = result.sort_by{|el| el[1][0]['total_size'].to_i}.reverse result = result.map do |table, res| [table, res] end result end |
#have_rebuild_for_relation?(relation) ⇒ Boolean
194 195 196 197 198 199 |
# File 'lib/pg-reindex.rb', line 194 def have_rebuild_for_relation?(relation) res = @conn.exec "select has_table_privilege(E'#{@cfg[:user]}', E'#{relation}', 'update,references')" res[0]['has_table_privilege'] == 't' ? true : false rescue false end |
#index_def(oid) ⇒ Object
170 171 172 173 |
# File 'lib/pg-reindex.rb', line 170 def index_def(oid) sql = "SELECT pg_get_indexdef(#{oid}) as q" exec(sql)[0]['q'] end |
#index_sql(oid, name, new_name) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/pg-reindex.rb', line 159 def index_sql(oid, name, new_name) str = index_def(oid).sub(name, new_name) pos = str.index(new_name) before = str[0..pos-2] after = str[pos..-1] "#{before} CONCURRENTLY #{after}" end |
#index_sql_array(table, oid, name, primary = false) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/pg-reindex.rb', line 136 def index_sql_array(table, oid, name, primary = false) new_name = if name.size < 61 name + "_2" else name[0..-3] + "_2" end if primary [ index_sql(oid, name, new_name), "ANALYZE #{table}", "SELECT swap_for_pkey('public', '#{name}', '#{new_name}')" ] else [ index_sql(oid, name, new_name), "ANALYZE #{table}", "DROP INDEX #{name}", "ALTER INDEX #{new_name} RENAME TO #{name}" ] end end |
#index_sqls(index_row) ⇒ Object
132 133 134 |
# File 'lib/pg-reindex.rb', line 132 def index_sqls(index_row) index_sql_array(index_row['table'], index_row['index_oid'], index_row['index'], index_row['primary'] == 't') end |
#install_swap_for_pkey ⇒ Object
74 75 76 77 |
# File 'lib/pg-reindex.rb', line 74 def install_swap_for_pkey @conn.exec("create language plpgsql") rescue nil @conn.exec(swap_for_pkey_sql) end |
#queries ⇒ Object
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/pg-reindex.rb', line 179 def queries @conn.exec <<-SQL SELECT procpid, now() - query_start as age, current_query FROM pg_stat_activity AS a JOIN pg_locks AS l ON a.procpid = l.pid WHERE virtualxid IS NOT NULL order by age desc; SQL end |
#swap_for_pkey_sql ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/pg-reindex.rb', line 88 def swap_for_pkey_sql <<-SQL CREATE OR REPLACE FUNCTION swap_for_pkey(text,text,text) returns integer AS $$ DECLARE cmd text; oid1 integer; oid2 integer; filenode1 integer; filenode2 integer; relation text; BEGIN select oid::integer into oid1 from pg_class where relname=$2 and relnamespace = (select oid from pg_namespace where nspname=$1); RAISE NOTICE 'PKEY OID: %',oid1; select relfilenode::integer into filenode1 from pg_class where oid=oid1; select oid::integer into oid2 from pg_class where relname=$3 and relnamespace = (select oid from pg_namespace where nspname=$1); RAISE NOTICE 'PKEY OID: %',oid2; select relfilenode::integer into filenode2 from pg_class where oid=oid2; select (indrelid::regclass)::text into relation from pg_index where indexrelid=oid1; RAISE NOTICE 'RELATION NAME: %',relation; cmd:='LOCK '||relation||';'; RAISE NOTICE 'Executing :- %',cmd; Execute cmd; cmd:='UPDATE pg_class SET relfilenode='||filenode2|| ' WHERE oid='||oid1||';'; RAISE NOTICE 'Executing :- %',cmd; Execute cmd; cmd:='UPDATE pg_class SET relfilenode='||filenode1|| ' WHERE oid='||oid2||';'; RAISE NOTICE 'Executing :- %',cmd; Execute cmd; cmd:='DROP INDEX '||$1||'.'||$3||';'; RAISE NOTICE 'Executing :- %',cmd; Execute cmd; return 0; END; $$language plpgsql; SQL end |