pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: pgsql-bugs@postgresql.org
Subject: Prepare/Execute silently discards prohibited ORDER BY values
Date: Mon, 11 May 2015 17:06:04 -0700
Message-ID: <555143EC.1000808@agliodbs.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Tested On: 9.4.1, 9.3.6
Severity: minor
Summary: PREPARE/EXECUTE appears to silently discard ORDER BY parameters.

josh=# \d test
    Table "public.test"
 Column | Type | Modifiers
--------+------+-----------
 test   | text |

josh=# insert into test values ('test1'),('test9'),('test3'),('test2');
INSERT 0 4

josh=# prepare foo as select * from test order by $1;
PREPARE

josh=# execute foo('test');
 test
-------
 test1
 test9
 test3
 test2
(4 rows)

josh=# explain analyze
josh-# execute foo('test');
                                            QUERY PLAN

---------------------------------------------------------------------------------------------------
 Seq Scan on test  (cost=0.00..23.10 rows=1310 width=32) (actual
time=0.007..0.007 rows=4 loops=1)
 Execution time: 0.026 ms
(2 rows)

What appears to be happening is that the prohibited parameter for ORDER
BY is being silently discarded during EXECUTE.  At first I thought it
might just be doing ORDER BY 'test' in the background, but that's not it:

josh=# select * from test order by 'test';
ERROR:  non-integer constant in ORDER BY
LINE 1: select * from test order by 'test';

josh=# execute foo(1);
 test
-------
 test1
 test9
 test3
 test2
(4 rows)

josh=# select * from pg_prepared_statements ;
 name |                   statement                    |
prepare_time          | parameter_types | from_sql
------+------------------------------------------------+-------------------------------+-----------------+----------
 foo  | prepare foo as select * from test order by $1; | 2015-05-11
16:52:55.369479-07 | {text}          | t
(1 row)


So something else is happening here.  What should probably be happening
is that PREPARE should throw an error if it gets a parameter in the
ORDER BY clause.

-- 
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (7+ messages)  latest in thread

Message-ID: <555143EC.1000808@agliodbs.com>
Permalink:  ../555143EC.1000808@agliodbs.com/
Also on:    postgresql.org/message-id/555143EC.1000808@agliodbs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: josh@agliodbs.com
  Subject: Re: Prepare/Execute silently discards prohibited ORDER BY values
  In-Reply-To: <555143EC.1000808@agliodbs.com>
  References: <555143EC.1000808@agliodbs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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