Re: help: function failing

From: Sergey Konoplev <gray(dot)ru(at)gmail(dot)com>
To: George Neuner <gneuner2(at)comcast(dot)net>
Cc: "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: help: function failing
Date: 2014-10-07 20:41:04
Message-ID: CAL_0b1uwhpvPrrtC6ba6E=bBn+euj78VM-qWH+cp3jxrYyskPw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Thu, Oct 2, 2014 at 4:00 PM, George Neuner <gneuner2(at)comcast(dot)net> wrote:
> --------------- code ----------------
> CREATE OR REPLACE FUNCTION gen_random()
> RETURNS double precision AS
> $BODY$
> DECLARE
> num float8 := 0;
> den float8 := 281474976710655; -- 0xFFFFFFFFFFFF
> bytes bytea[6];
> BEGIN
> -- get random bytes from crypto module
> bytes := ext.gen_random_bytes(6);
>
> -- assemble a double precision value
> num := num + get_byte( bytes, 0 );
> FOR i IN 1..5 LOOP
> num := num * 256;
> num := num + get_byte( bytes, i );
> END LOOP;
>
> -- normalize value to range 0.0 .. 1.0
> RETURN num / den;
> END;
> $BODY$
> LANGUAGE plpgsql VOLATILE;
> --------------- code ----------------
>
> The error is:
> ERROR: array value must start with "{" or dimension information
> SQL state: 22P02
> Context: PL/pgSQL function gen_random() line 8 at assignment
>
> which, if I'm counting correctly, is
> bytes := ext.gen_random_bytes(6);

Guessing on the name of ext.gen_random_bytes(6) it returns a value
that is incompatible with bytea[] array representation time from time,
so take a closer look at ext.gen_random_bytes() first. You can test
the case using DO block.

> If I comment out that line, it then tells me get_byte() is undefined,
> which should be impossible because it's built in.

Feels like somewhere inside ext.gen_random_bytes() you set a
search_path that allows to see get_byte() and the search_path that was
set before the gen_random() call doesn't allow it.

--
Kind regards,
Sergey Konoplev
PostgreSQL Consultant and DBA

http://www.linkedin.com/in/grayhemp
+1 (415) 867-9984, +7 (499) 346-7196, +7 (988) 888-1979
gray(dot)ru(at)gmail(dot)com

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Andrew Dunstan 2014-10-07 21:27:58 Re: help: function failing
Previous Message Marti Raudsepp 2014-10-07 20:26:38 Re: query plan question, nested loop vs hash join