create table test (
"id" serial,
"hash" varchar(64),
"on" boolean
);
insert into test values
(1, 'aaa', true),
(2, 'aaa', true),
(3, 'bbb', true),
(4, 'aaa', false),
(5, 'ccc', true)
;
select * from test
join (
select "hash" from test where "on" is true order by random() limit 1
) t on t.hash = test.hash;