Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YrxiF-00029C-Nm for pgsql-bugs@arkaria.postgresql.org; Tue, 12 May 2015 00:06:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YrxiE-0003qm-RB for pgsql-bugs@arkaria.postgresql.org; Tue, 12 May 2015 00:06:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YrxiD-0003qf-Rs for pgsql-bugs@postgresql.org; Tue, 12 May 2015 00:06:17 +0000 Received: from mx-out.zmailcloud.com ([192.198.85.98] helo=mx-out-1.zmailcloud.com) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1Yrxi4-0004bL-UN for pgsql-bugs@postgresql.org; Tue, 12 May 2015 00:06:17 +0000 Received: from smtp.01.com (smtp.01.com [10.10.0.43]) by mx-out-1.zmailcloud.com (Postfix) with ESMTP id 18054563F62 for ; Mon, 11 May 2015 19:06:07 -0500 (CDT) Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id F33D7A043E for ; Mon, 11 May 2015 19:06:06 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pGfaUNSWoK8v for ; Mon, 11 May 2015 19:06:06 -0500 (CDT) Received: from smtp.01.com (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id CD5B4A0442 for ; Mon, 11 May 2015 19:06:06 -0500 (CDT) Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id A5297A043F for ; Mon, 11 May 2015 19:06:06 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id y06p6WShHK65 for ; Mon, 11 May 2015 19:06:06 -0500 (CDT) Received: from [172.47.23.104] (50-0-190-244.dsl.dynamic.fusionbroadband.com [50.0.190.244]) by smtp-out-1.01.com (Postfix) with ESMTPSA id 4F5B0A043E for ; Mon, 11 May 2015 19:06:06 -0500 (CDT) Message-ID: <555143EC.1000808@agliodbs.com> Date: Mon, 11 May 2015 17:06:04 -0700 From: Josh Berkus Organization: PostgreSQL Experts Inc. User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: Prepare/Execute silently discards prohibited ORDER BY values Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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