String modification is really slow when you're using string_agg, and unnest and ordinality. You'll find plperl to be over twice as fast as the sql method.
CREATE FUNCTION reverse_perl(p_words text)
RETURNS text
AS
$$
return join ".", reverse split /\./, $_[0];
$$
LANGUAGE plperl
STRICT
IMMUTABLE;
Benchmark,
test=# EXPLAIN ANALYZE SELECT reverse_perl( array_to_string(ARRAY[x,x,x,x], '.') ) FROM generate_series(1,100) AS gs(x);
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Function Scan on generate_series gs (cost=0.00..262.50 rows=1000 width=4) (actual time=0.104..1.477 rows=100 loops=1)
Planning time: 0.054 ms
Execution time: 1.519 ms
(3 rows)
Time: 2.030 ms
test=# EXPLAIN ANALYZE SELECT reverse_elements( array_to_string(ARRAY[x,x,x,x], '.') ) FROM generate_series(1,100) AS gs(x);
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Function Scan on generate_series gs (cost=0.00..262.50 rows=1000 width=4) (actual time=0.236..4.161 rows=100 loops=1)
Planning time: 0.147 ms
Execution time: 4.208 ms
(3 rows)
Time: 4.770 ms
Granted, this is a pretty simple query and it probably doesn't matter for most reasonable use cases.