Re: LISTAGG à la Oracle in PostgreSQL - Mailing list pgsql-general

From Peter Eisentraut
Subject Re: LISTAGG à la Oracle in PostgreSQL
Date
Msg-id 7d03be7f-9d20-4285-9342-af1d51435b5a@eisentraut.org
Whole thread Raw
In response to LISTAGG à la Oracle in PostgreSQL  (Pierre Forstmann <pierre.forstmann@gmail.com>)
List pgsql-general
On 09.03.26 21:21, Pierre Forstmann wrote:
> Hello,
> 
> I can write a LISTAGG aggregate for:
> 
> create table emp(deptno numeric, ename text);
> 
> SELECT deptno, LISTAGG(ename, ','::text ORDER BY ename) AS employees 
> FROM   emp GROUP BY deptno ORDER BY deptno;
> 
> I would like to know if is possible to create an aggregate LISTAGG that 
> would work like in Oracle:
> 
> SELECT deptno,
>         listagg(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees
> FROM emp
> GROUP BY deptno
> ORDER BY deptno;
> 
> I failed and IA also failed. Claude says:
> 
> It is not possible to exactly replicate listagg(ename, ',') WITHIN GROUP 
> (ORDER BY ename) as a custom PostgreSQL aggregate
> because PostgreSQL strictly forbids ungrouped columns as direct 
> arguments to ordered-set aggregates.
> 
> Do you agree ?

One of the reasons that PostgreSQL hasn't implemented LISTAGG is that it 
is a misdesign.  It uses ordered-set aggregate syntax even
though it is not very similar to the other ordered-set aggregates.
Its syntax should be more similar to ARRAY_AGG or
JSON_ARRAYAGG, for example.  But it's too late to fix the standard on this.




pgsql-general by date:

Previous
From: Shiju Sivadazz
Date:
Subject: Question about heap_inplace_update and VACUUM behavior
Next
From: pierre.forstmann@gmail.com
Date:
Subject: Re: LISTAGG à la Oracle in PostgreSQL