lib/pg-reindex.rb in pg_reindex-0.1.2 vs lib/pg-reindex.rb in pg_reindex-0.1.3
- old
+ new
@@ -3,14 +3,14 @@
class PgReindex
MIN_SIZE = 50 # megabyte min, total table size
def initialize(conf)
- cfg = {:host => conf['host'] || '127.0.0.1', :dbname => conf['database'],
+ @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
+ @conn = PGconn.new @cfg
end
def get_struct_relations(min_size = nil)
res = get_raw_relations
@@ -49,11 +49,12 @@
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"
+ 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_%'
@@ -127,21 +128,21 @@
res = @conn.exec("SELECT pg_size_pretty(pg_database_size('#{database}')) as db_size")
res.first['db_size']
end
def index_sqls(index_row)
- index_sql_array(index_row['table'], index_row['index_oid'], index_row['index'])
+ index_sql_array(index_row['table'], index_row['index_oid'], index_row['index'], index_row['primary'] == 't')
end
- def index_sql_array(table, oid, name)
+ def index_sql_array(table, oid, name, primary = false)
new_name = if name.size < 61
name + "_2"
else
name[0..-3] + "_2"
end
- if name.end_with?('_pkey')
+ if primary
[
index_sql(oid, name, new_name),
"ANALYZE #{table}",
"SELECT swap_for_pkey('public', '#{name}', '#{new_name}')"
]
@@ -185,9 +186,16 @@
virtualxid IS NOT NULL order by age desc;
SQL
end
def cancel(procpid)
- @conn.exec "select pg_cancel_backend(#{procpid.to_i})"
+ @conn.exec("select pg_cancel_backend(#{procpid.to_i})")
end
-
+
+ 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
+
end
\ No newline at end of file