pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #6607: Strange select behavior
4+ messages / 3 participants
[nested] [flat]
* BUG #6607: Strange select behavior
@ 2012-04-22 19:17 suvisor.root@gmail.com
2012-04-23 14:53 ` Re: BUG #6607: Strange select behavior Kevin Grittner <Kevin.Grittner@wicourts.gov>
0 siblings, 1 reply; 4+ messages in thread
From: suvisor.root@gmail.com @ 2012-04-22 19:17 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 6607
Logged by: Sheplyakov Alexander
Email address: suvisor.root@gmail.com
PostgreSQL version: 9.1.3
Operating system: Debian GNU/Linux Wheezy kernel ver 3.3.1
Description:
Create test table in such manner:
create table testt (id serial PRIMARY KEY, val text not null);
Populate table with test data:
insert into testt (val) values ('1'), ('2'), ('3'), ('4'), ('5'), ('6'),
('7'), ('8'), ('9');
(repeat next line until table doesn't contain approx 1000000 rows)
insert into testt (id, val) select nextval('testt_id_seq'::regclass),
currval('testt_id_seq'::regclass) + val::integer from testt;
And then, repeat some times such query:
select * from testt where id = (random()* 100000)::integer;
And sometimes it comes out something like this:
id | val
-------+--------
11894 | 15051
29233 | 42198
80725 | 90213
85688 | 100992
88017 | 108075
(5 rows)
Here can be 2, 3 or other rows amount in result... But must be only one!
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: BUG #6607: Strange select behavior
2012-04-22 19:17 BUG #6607: Strange select behavior suvisor.root@gmail.com
@ 2012-04-23 14:53 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2012-04-23 22:23 ` Re: BUG #6607: Strange select behavior Александр Шепляков <suvisor.root@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Kevin Grittner @ 2012-04-23 14:53 UTC (permalink / raw)
To: suvisor.root@gmail.com; pgsql-bugs
<suvisor.root@gmail.com> wrote:
> select * from testt where id = (random()* 100000)::integer;
>
> And sometimes it comes out something like this:
> id | val
> -------+--------
> 11894 | 15051
> 29233 | 42198
> 80725 | 90213
> 85688 | 100992
> 88017 | 108075
> (5 rows)
> Here can be 2, 3 or other rows amount in result... But must be
> only one!
No, what you have written will scan the entire table and give each
row a 1 in 100000 chance of being selected. Maybe something like
this would give you what you want:
select t.* from (select (random()* 100000)::integer) n(r)
join testt t on (t.id = n.r);
By the way, you might want to tweak that random number before
casting it to int, or you might not get *any* rows back:
test=# select ('0.0000000001'::float * 100000)::int;
int4
------
0
(1 row)
Maybe something like:
(select floor(random() * 100000)::int + 1)
-Kevin
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: BUG #6607: Strange select behavior
2012-04-22 19:17 BUG #6607: Strange select behavior suvisor.root@gmail.com
2012-04-23 14:53 ` Re: BUG #6607: Strange select behavior Kevin Grittner <Kevin.Grittner@wicourts.gov>
@ 2012-04-23 22:23 ` Александр Шепляков <suvisor.root@gmail.com>
2012-04-23 22:36 ` Re: BUG #6607: Strange select behavior Kevin Grittner <Kevin.Grittner@wicourts.gov>
0 siblings, 1 reply; 4+ messages in thread
From: Александр Шепляков @ 2012-04-23 22:23 UTC (permalink / raw)
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: pgsql-bugs
23 апреля 2012 г. 17:53 пользователь Kevin Grittner
<Kevin.Grittner@wicourts.gov> написал:
> <suvisor.root@gmail.com> wrote:
>
>> select * from testt where id = (random()* 100000)::integer;
>>
>> And sometimes it comes out something like this:
>> id | val
>> -------+--------
>> 11894 | 15051
>> 29233 | 42198
>> 80725 | 90213
>> 85688 | 100992
>> 88017 | 108075
>> (5 rows)
>> Here can be 2, 3 or other rows amount in result... But must be
>> only one!
>
> No, what you have written will scan the entire table and give each
> row a 1 in 100000 chance of being selected. Maybe something like
> this would give you what you want:
>
Hmm, is this because random() marked as volatile (and would be called
for every row)? Ups, I not taken this into account... Sorry for bothering
and thanks for reply!
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: BUG #6607: Strange select behavior
2012-04-22 19:17 BUG #6607: Strange select behavior suvisor.root@gmail.com
2012-04-23 14:53 ` Re: BUG #6607: Strange select behavior Kevin Grittner <Kevin.Grittner@wicourts.gov>
2012-04-23 22:23 ` Re: BUG #6607: Strange select behavior Александр Шепляков <suvisor.root@gmail.com>
@ 2012-04-23 22:36 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
0 siblings, 0 replies; 4+ messages in thread
From: Kevin Grittner @ 2012-04-23 22:36 UTC (permalink / raw)
To: ********* ******** <suvisor.root@gmail.com>; +Cc: pgsql-bugs
********* ********<suvisor.root@gmail.com> wrote:
> Kevin Grittner <Kevin.Grittner@wicourts.gov> *******:
>> what you have written will scan the entire table and give each
>> row a 1 in 100000 chance of being selected.
>>
> Hmm, is this because random() marked as volatile (and would be
> called for every row)? Ups, I not taken this into account...
The VOLATILE marker *guarantees* that random() will be run for every
row, but even with an immutable function you would be taking your
chances on how it might run, with the way it was coded. The
semantics are made unambiguous (I think) by making it a subquery in
the FROM clause.
-Kevin
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2012-04-23 22:36 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-04-22 19:17 BUG #6607: Strange select behavior suvisor.root@gmail.com
2012-04-23 14:53 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2012-04-23 22:23 ` Александр Шепляков <suvisor.root@gmail.com>
2012-04-23 22:36 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox