Why no "array_sort" function?

From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Why no "array_sort" function?
Date: 2020-05-08 08:02:52
Message-ID: alpine.DEB.2.22.394.2005080947190.914063@pseudo
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Hello devs,

although having arrays is an anathema in a relational world, pg has them,
and I find it useful for some queries, mostly in an aggregation to show
in a compact way what items were grouped together.

There are a few functions available to deal with arrays. Among these
functions, there is no "array_sort". It is easy enough to provide one that
seems to work, such as:

CREATE OR REPLACE FUNCTION array_sort(a ANYARRAY) RETURNS ANYARRAY
IMMUTABLE STRICT AS $$
SELECT ARRAY_AGG(i) FROM (SELECT i FROM UNNEST(a) AS i ORDER BY 1) AS i;
$$ LANGUAGE sql;

but I'm afraid that is is not particularly efficient, and I'm not even
sure that it is deterministic (ok, the subquery is sorted, but the outside
query could still decide to scan it out of order for some reason?).

Is there a reason *not* to provide an "array_sort" function?

--
Fabien.

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2020-05-08 08:17:07 Re: ALTER TABLE ... SET STORAGE does not propagate to indexes
Previous Message Justin Pryzby 2020-05-08 07:25:45 should INSERT SELECT use a BulkInsertState?