In a number of places on the web I've seen it claimed that ordering can be set via prepared statements. Indeed, the expected syntax is accepted on my 9.3 server without errors:
sandbox=# CREATE TABLE test (
id serial PRIMARY KEY,
gender char
);
sandbox=# INSERT INTO test(gender) VALUES('m') VALUES('f') VALUES('m') VALUES('f') VALUES('m');
sandbox=# PREPARE testplan(text) AS
SELECT * FROM test ORDER BY $1;
But the output is not what one would expect:
sandbox=# EXECUTE testplan('gender');
id | gender
----+--------
1 | m
2 | f
3 | m
4 | f
5 | m
6 | f
(6 rows)
As opposed to:
sandbox=# SELECT * FROM test ORDER BY gender;
id | gender
----+--------
2 | f
4 | f
6 | f
1 | m
3 | m
5 | m
(6 rows)
It would seem that the ORDER BY clause is simply ignored in the prepared statement. Is this deliberate behaviour? I can well understand that supporting this kind of query would be tricky, but it would be very handy.
Many thanks,
Bryn