Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hQ5q5-0002pR-FI for pgsql-bugs@arkaria.postgresql.org; Mon, 13 May 2019 07:57:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hQ5q4-0007tZ-6n for pgsql-bugs@arkaria.postgresql.org; Mon, 13 May 2019 07:57:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hQ5q3-0007tS-Ve for pgsql-bugs@lists.postgresql.org; Mon, 13 May 2019 07:57:36 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hQ5q2-0002hv-3q for pgsql-bugs@lists.postgresql.org; Mon, 13 May 2019 07:57:35 +0000 Received: from [192.168.127.1] (port=30576 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1hQ5q0-000NoR-K7; Mon, 13 May 2019 07:57:32 +0000 Received: from [127.0.0.1] (port=34495 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1hQ5pz-0009Kl-V6; Mon, 13 May 2019 07:57:32 +0000 From: Andrew Gierth To: pgsql-bugs@lists.postgresql.org, alessio.gennari78@gmail.com Subject: Re: BUG #15800: Order by random in functions In-Reply-To: <15800-5c54ab25e4c8a6ed@postgresql.org> (PG Bug reporting form's message of "Mon, 13 May 2019 06:35:18 +0000") Message-ID: <87r292g58w.fsf@news-spur.riddles.org.uk> References: <15800-5c54ab25e4c8a6ed@postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Mon, 13 May 2019 08:57:31 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form writes: PG> in version 9.5 (select version(): PostgreSQL 9.5.12 on PG> x86_64-pc-linux-gnu (Debian 9.5.12-1.pgdg80+1), compiled by gcc PG> (Debian 4.9.2-10+deb8u1) 4.9.2, 64-bit) this statement returns PG> element (id_card_type) randomly ordered: PG> select jsonb_array_elements('[...]')...order by random() PG> While in version 9.6 (PostgreSQL 9.6.12 on x86_64-pc-linux-gnu, PG> compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit) PG> and above (I tested it in Postgres 10.8) the query return elements PG> not ordered but in the same sequence as it is in json array. PG> Is it a bug or an expected behavior? Expected behavior, though I'm not sure it's adequately documented. The preferred way to do this is: SELECT a.value->>'id_card_type' FROM jsonb_array_elements('[...]') a ORDER BY random(); which will randomize the order regardless of postgresql version. -- Andrew (irc:RhodiumToad)