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 1iUITM-0000Nr-1v for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Nov 2019 22:47:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iUITK-0008EP-E4 for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Nov 2019 22:47:46 +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 1iUITK-0008EI-6k for pgsql-bugs@lists.postgresql.org; Mon, 11 Nov 2019 22:47:46 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUITI-0001lp-1Z for pgsql-bugs@lists.postgresql.org; Mon, 11 Nov 2019 22:47:45 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id xABMldhh027257; Mon, 11 Nov 2019 17:47:39 -0500 From: Tom Lane To: andrew.wheelwright@familysearch.org cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16107: string_agg looses first item In-reply-to: <16107-f46d9b66440d7f45@postgresql.org> References: <16107-f46d9b66440d7f45@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Mon, 11 Nov 2019 22:34:28 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27255.1573512459.1@sss.pgh.pa.us> Date: Mon, 11 Nov 2019 17:47:39 -0500 Message-ID: <27256.1573512459@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > I ran into a scenario where I found results getting dropped from string > aggregation. Here's a basic example which reproduces the problem on three > different servers running PostgreSQL 9.6.6, 10.6, and 11.5, respectively. I > don't have an instance running version 12. > with dataset as ( > select 'One' "Label", 1 "ID" > union > select 'Two' "Label", 2 "ID" > union > select 'Three' "Label", 3 "ID" > ) > select > string_agg(', ', "Label" order by "ID") "String Aggregated Labels", > array_agg("Label" order by "ID") "Array Aggregated Labels" > from > dataset > ; > Which renders the following result: > String Aggregated Labels: `, Two, Three, ` > Array Aggregated Labels: `{One,Two,Three}` I think you've got the string_agg parameters backwards, it should be regression=# with dataset as ( select 'One' "Label", 1 "ID" union select 'Two' "Label", 2 "ID" union select 'Three' "Label", 3 "ID" ) select string_agg("Label", ', ' order by "ID") "String Aggregated Labels", array_agg("Label" order by "ID") "Array Aggregated Labels" from dataset ; String Aggregated Labels | Array Aggregated Labels --------------------------+------------------------- One, Two, Three | {One,Two,Three} (1 row) regards, tom lane