Get random row from table foo.
create table foo as
select generate_series(1,1000000) as val;
'val' is serial field (not primary). Field dosn't have breaks.
This query might return 0,1,2,3,4... rows and in all rows val has different value. Why?
select * from foo where val =
(floor(random() * (select max(val) from foo))+1)::int;
Slightly change query to
select * from foo where val =
(select (floor(random() * (select max(val) from foo))+1)::int as v);
Result as expected, single random row from table
create tablestatement for thefootable)create table bar as select generate_series(1,1000000)