Create array index on bit_prefs (#2644)

This commit is contained in:
Type-kun
2016-09-17 23:59:08 +05:00
committed by r888888888
parent 09ec2dfc08
commit 15d4d8300f

View File

@@ -0,0 +1,24 @@
class AddIndexToUsersBitPrefs < ActiveRecord::Migration
def up
execute "set statement_timeout = 0"
execute <<-'SQL'
CREATE OR REPLACE FUNCTION bit_position_array(x bigint)
RETURNS integer[] AS $BODY$
select array_agg(i)
from generate_series(0, floor(log(2, x))::integer) i
where (x & (1::bigint << i)) > 0;
$BODY$ LANGUAGE sql IMMUTABLE
SQL
execute <<-'SQL'
CREATE INDEX index_users_on_bit_prefs_array
ON users USING gin (bit_position_array(bit_prefs) _int4_ops)
SQL
end
def down
execute "set statement_timeout = 0"
execute "DROP INDEX index_users_on_bit_prefs_array"
execute "DROP FUNCTION bit_position_array(x bigint)"
end
end