pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedORDER BY $1 behaves inconsistently
4+ messages / 2 participants
[nested] [flat]
* ORDER BY $1 behaves inconsistently
@ 2017-10-27 16:18 Jordan Lewis <jordanthelewis@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Jordan Lewis @ 2017-10-27 16:18 UTC (permalink / raw)
To: pgsql-bugs
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.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: ORDER BY $1 behaves inconsistently
@ 2017-10-27 16:53 Jordan Lewis <jordanthelewis@gmail.com>
parent: Jordan Lewis <jordanthelewis@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Jordan Lewis @ 2017-10-27 16:53 UTC (permalink / raw)
To: 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.
>
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: ORDER BY $1 behaves inconsistently
@ 2017-10-27 18:33 Tom Lane <tgl@sss.pgh.pa.us>
parent: Jordan Lewis <jordanthelewis@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Tom Lane @ 2017-10-27 18:33 UTC (permalink / raw)
To: Jordan Lewis <jordanthelewis@gmail.com>; +Cc: pgsql-bugs
Jordan Lewis <jordanthelewis@gmail.com> writes:
> 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.
"ORDER BY $1" is asking to order by some constant value (constant within
any one execution of the command, anyway), which is useless, because all
rows in the query will have the same sort key. The planner will throw
that away as being a no-op.
"ORDER BY 1" is asking to order by the first output column. This is
not the same thing. You cannot get that effect with a parameter;
if you could, it would probably represent a SQL-injection hazard.
If the value of $1 were "foo" and you complained that it didn't order
by column foo, it would be exactly the same issue...
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: ORDER BY $1 behaves inconsistently
@ 2017-10-27 21:05 Jordan Lewis <jordanthelewis@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 4+ messages in thread
From: Jordan Lewis @ 2017-10-27 21:05 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
Thanks for the explanation - that makes sense.
On Fri, Oct 27, 2017 at 2:33 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Jordan Lewis <jordanthelewis@gmail.com> writes:
> > 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.
>
> "ORDER BY $1" is asking to order by some constant value (constant within
> any one execution of the command, anyway), which is useless, because all
> rows in the query will have the same sort key. The planner will throw
> that away as being a no-op.
>
> "ORDER BY 1" is asking to order by the first output column. This is
> not the same thing. You cannot get that effect with a parameter;
> if you could, it would probably represent a SQL-injection hazard.
>
> If the value of $1 were "foo" and you complained that it didn't order
> by column foo, it would be exactly the same issue...
>
> regards, tom lane
>
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2017-10-27 21:05 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-10-27 16:18 ORDER BY $1 behaves inconsistently Jordan Lewis <jordanthelewis@gmail.com>
2017-10-27 16:53 ` Jordan Lewis <jordanthelewis@gmail.com>
2017-10-27 18:33 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-10-27 21:05 ` Jordan Lewis <jordanthelewis@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox