Re: [BUGS] BUG #14737: Wrong PL/pgSQL behaviour - Mailing list pgsql-bugs

From David G. Johnston
Subject Re: [BUGS] BUG #14737: Wrong PL/pgSQL behaviour
Date
Msg-id CAKFQuwZn6DOm=+8vL3AnTXM4Jt1jJZFADpHOmk6WwfpXiZ5srA@mail.gmail.com
Whole thread Raw
In response to [BUGS] BUG #14737: Wrong PL/pgSQL behaviour  (frank.von.postgresql.org@familie-gard.de)
Responses Re: [BUGS] BUG #14737: Wrong PL/pgSQL behaviour  (Frank Gard <frank@familie-gard.de>)
List pgsql-bugs
On Fri, Jul 7, 2017 at 9:10 AM, <frank.von.postgresql.org@familie-gard.de> wrote:
Bug reference:      14737
Logged by:          Frank Gard
Email address:      frank.von.postgresql.org@familie-gard.de
PostgreSQL version: 9.6.3
Operating system:   Debian GNU/Linux

​Not a bug - RANDOM() is a volatile function so it gets evaluated once per row.

        UPDATE ttb_histogramm
            SET
                anzahl = anzahl + 1
            WHERE
                wert = p_von + FLOOR( ( 1 + p_bis - p_von ) * RANDOM() )

Unfortunately this is not the case. When calling it
multiple times, it returns numbers smaller and greater, and always different
values for each call. Very strange (to me)!!!

​Which is the symptom one will see if, for every row, the value of random is different.
When I change my function a little bit, writing the random number into an
INTEGER variable, and using this variable within my UPDATE statement,
everything works fine:

​Then this is what you should do.

​David J.

pgsql-bugs by date:

Previous
From: frank.von.postgresql.org@familie-gard.de
Date:
Subject: [BUGS] BUG #14737: Wrong PL/pgSQL behaviour
Next
From: Grzegorz Grabek
Date:
Subject: [BUGS] GIN index not working for integer[] if there is more then one columnin table