Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e89RW-0006g8-EC for pgsql-bugs@arkaria.postgresql.org; Fri, 27 Oct 2017 18:33:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e89RW-0008EZ-0i for pgsql-bugs@arkaria.postgresql.org; Fri, 27 Oct 2017 18:33:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e89RV-0008Dm-0l for pgsql-bugs@postgresql.org; Fri, 27 Oct 2017 18:33:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e89RS-0005pT-KF for pgsql-bugs@postgresql.org; Fri, 27 Oct 2017 18:33:15 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v9RIXCwG031442; Fri, 27 Oct 2017 14:33:12 -0400 From: Tom Lane To: Jordan Lewis cc: pgsql-bugs@postgresql.org Subject: Re: ORDER BY $1 behaves inconsistently In-reply-to: References: Comments: In-reply-to Jordan Lewis message dated "Fri, 27 Oct 2017 16:53:12 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31440.1509129192.1@sss.pgh.pa.us> Date: Fri, 27 Oct 2017 14:33:12 -0400 Message-ID: <31441.1509129192@sss.pgh.pa.us> 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 Jordan Lewis 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