Re: [BUGS] ORDER BY $1 behaves inconsistently - Mailing list pgsql-bugs

From Jordan Lewis
Subject Re: [BUGS] ORDER BY $1 behaves inconsistently
Date
Msg-id CAALgziKW_n9b7javO+xUMr-6VsXg9biiwCa0c3Gf91rmxqVKNQ@mail.gmail.com
Whole thread Raw
In response to [BUGS] ORDER BY $1 behaves inconsistently  (Jordan Lewis <jordanthelewis@gmail.com>)
Responses Re: [BUGS] ORDER BY $1 behaves inconsistently  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
Actually, it's even worse than I thought. It seems that placeholders in ORDER BY clauses get entirely ignored,
as running `EXECUTE x(1)` on the previous example with unsorted table data does not sort the table by the 1st
column as expected.

On Fri, Oct 27, 2017 at 12:18 PM Jordan Lewis <jordanthelewis@gmail.com> wrote:
Version: 10.0

As I understand it, the only valid constant datatype in an ORDER BY is integer. That's validated by the following test:

jordan=# SELECT * FROM t ORDER BY 'foo';
ERROR:  non-integer constant in ORDER BY
LINE 1: SELECT * FROM t ORDER BY 'foo';

However, using a prepared statement, this behavior can be avoided:

jordan=# PREPARE x as SELECT * FROM t ORDER BY $1;
PREPARE
jordan=# EXECUTE x('foo');
 c
---
 1

It seems to me that there is some missing type checking from ORDER BY.

pgsql-bugs by date:

Previous
From: Jordan Lewis
Date:
Subject: [BUGS] ORDER BY $1 behaves inconsistently
Next
From: Tom Lane
Date:
Subject: Re: [BUGS] ORDER BY $1 behaves inconsistently