Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XbcIl-0004sW-Ub for pgsql-performance@arkaria.postgresql.org; Tue, 07 Oct 2014 21:28:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XbcIk-0007OO-FI for pgsql-performance@arkaria.postgresql.org; Tue, 07 Oct 2014 21:28:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XbcIj-0007OI-Jr for pgsql-performance@postgresql.org; Tue, 07 Oct 2014 21:28:09 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:c:538::196]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XbcIf-0006J1-QF for pgsql-performance@postgresql.org; Tue, 07 Oct 2014 21:28:08 +0000 Received: from mfilter31-d.gandi.net (mfilter31-d.gandi.net [217.70.178.162]) by relay4-d.mail.gandi.net (Postfix) with ESMTP id 540CA172055; Tue, 7 Oct 2014 23:28:02 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mfilter31-d.gandi.net Received: from relay4-d.mail.gandi.net ([217.70.183.196]) by mfilter31-d.gandi.net (mfilter31-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id KcE8bcBugor9; Tue, 7 Oct 2014 23:28:00 +0200 (CEST) X-Originating-IP: 98.27.58.255 Received: from [192.168.10.146] (cpe-098-027-058-255.nc.res.rr.com [98.27.58.255]) (Authenticated sender: adsend@dunslane.net) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 1F7B6172067; Tue, 7 Oct 2014 23:27:59 +0200 (CEST) Message-ID: <54345ADE.8030806@dunslane.net> Date: Tue, 07 Oct 2014 17:27:58 -0400 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 MIME-Version: 1.0 To: Sergey Konoplev CC: George Neuner , "pgsql-performance@postgresql.org" Subject: Re: help: function failing References: <542DD92A.9000305@comcast.net> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 10/07/2014 04:41 PM, Sergey Konoplev wrote: > On Thu, Oct 2, 2014 at 4:00 PM, George Neuner 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. > Why does this code want an array of byteas? It looks like the code thinks bytea[6] is a declaration of a bytea of length 6, which of course it is not. Shouldn't it just be declared as: bytes bytea; ? Oh, and pgsql-performance is completely the wrong forum for this query. usage questions should be on pgsql-general. cheers andrew -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance