mu already demonstrated how an index on an expression can solve your problem.
My attention was caught by the used functions. Both seem like overkill for arrays of two integers. This may be a simplification of the real situation? Either way, I was intrigued and ran a test with a couple of variants.
Test setup
Temporary table with 10000 random pairs of integer:
CREATE TEMP TABLE arr (i int[]);
INSERT INTO arr
SELECT ARRAY[(random() * 1000)::int, (random() * 1000)::int]
FROM generate_series(1,10000);
Test candidates with a short comment to explain each one:
- mu's query
CREATE OR REPLACE FUNCTION sort_array1(integer[])
RETURNS int[]
LANGUAGE sql STRICT IMMUTABLE AS
$func$
SELECT array_agg(n) FROM (SELECT n FROM unnest($1) AS t(n) ORDER BY n) AS a;
$func$;
- The same with
ORDER BY
inside aggregate (pg 9.0+)
CREATE OR REPLACE FUNCTION sort_array2(int[])
RETURNS int[]
LANGUAGE sql STRICT IMMUTABLE AS
$func$
SELECT array_agg(n ORDER BY n) FROM unnest($1) AS t(n);
$func$;
- uralbash's query
CREATE OR REPLACE FUNCTION sort_array3(anyarray)
RETURNS anyarray
LANGUAGE sql STRICT IMMUTABLE AS
$func$
SELECT ARRAY(
SELECT $1[i]
FROM generate_series(array_lower($1,1), array_upper($1,1)) g(i)
ORDER BY 1)
$func$;
- Change parameter to
int[]
CREATE OR REPLACE FUNCTION sort_array4(int[])
RETURNS int[]
LANGUAGE sql STRICT IMMUTABLE AS
$func$
SELECT ARRAY(
SELECT $1[i]
FROM generate_series(array_lower($1,1), array_upper($1,1)) g(i)
ORDER BY 1)
$func$;
- Simplify
array_lower()
- it is always 1
CREATE OR REPLACE FUNCTION sort_array5(int[])
RETURNS int[]
LANGUAGE sql STRICT IMMUTABLE AS
$func$
SELECT ARRAY(
SELECT $1[i]
FROM generate_series(1, array_upper($1,1)) g(i)
ORDER BY 1)
$func$;
- Further simplify to case with 2 elements
CREATE OR REPLACE FUNCTION sort_array6(int[])
RETURNS int[]
LANGUAGE sql STRICT IMMUTABLE AS
$func$
SELECT ARRAY(
SELECT i
FROM (VALUES ($1[1]),($1[2])) g(i)
ORDER BY 1)
$func$;
- My simple query
CREATE OR REPLACE FUNCTION sort_array7(int[])
RETURNS int[]
LANGUAGE sql STRICT IMMUTABLE AS
$func$
SELECT CASE WHEN $1[1] > $1[2] THEN ARRAY[$1[2], $1[1]] ELSE $1 END;
$func$;
- Without
STRICT
modifier (!)
CREATE OR REPLACE FUNCTION sort_array8(int[])
RETURNS int[]
LANGUAGE sql IMMUTABLE AS
$func$
SELECT CASE WHEN $1[1] > $1[2] THEN ARRAY[$1[2], $1[1]] ELSE $1 END;
$func$;
Results
I executed each around 20 times and took the best result from EXPLAIN ANALYZE
.
SELECT sort_array1(i) FROM arr -- Total runtime: 183 ms
SELECT sort_array2(i) FROM arr -- Total runtime: 175 ms
SELECT sort_array3(i) FROM arr -- Total runtime: 183 ms
SELECT sort_array4(i) FROM arr -- Total runtime: 183 ms
SELECT sort_array5(i) FROM arr -- Total runtime: 177 ms
SELECT sort_array6(i) FROM arr -- Total runtime: 144 ms
SELECT sort_array7(i) FROM arr -- Total runtime: 103 ms
SELECT sort_array8(i) FROM arr -- Total runtime: 43 ms (!!!)
These are the results from a v9.0.5 server on Debian Squeeze. Similar results on v.8.4.
I also tested PL/pgSQL variants which were a bit slower as expected: too much overhead for a tiny operation, no query plan to cache.
The simple function (nr. 7) is substantially faster than the others. That was expected, the overhead of the other variants is just too much for a tiny array.
Leaving away the STRICT
declaration more than doubles the speed. I did not expect that and posted this follow-up uncover why: