Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1duKfd-0005mF-0Y for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Sep 2017 15:42:45 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1duKfc-0008Ui-JN for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Sep 2017 15:42:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1duKfc-0008UR-6I for pgsql-bugs@postgresql.org; Tue, 19 Sep 2017 15:42:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1duKfX-0001DC-H1 for pgsql-bugs@postgresql.org; Tue, 19 Sep 2017 15:42:43 +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 v8JFeJCk007067; Tue, 19 Sep 2017 11:40:19 -0400 From: Tom Lane To: Tomas Vondra cc: Dilyan Palauzov , pgsql-bugs@postgresql.org Subject: Re: Query planner skipping index depending on DISTINCT parameter order (2) In-reply-to: References: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> <57ccc03b-279a-63f7-19c6-6fe3b2d0e1be@aegee.org> <271e4961-abfa-06ef-a502-cf00a31b236a@2ndquadrant.com> <12044.1505668534@sss.pgh.pa.us> Comments: In-reply-to Tomas Vondra message dated "Tue, 19 Sep 2017 16:10:23 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <7065.1505835619.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 19 Sep 2017 11:40:19 -0400 Message-ID: <7066.1505835619@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 Tomas Vondra writes: > On 09/17/2017 07:15 PM, Tom Lane wrote: >> For the DISTINCT ON case, the user-visible semantics are actually pretty >> tightly tied to ORDER BY, so that it would not be very reasonable to >> consider any other orderings than the given column order anyway. > Tied in what sense? In the docs we explicitly say this: > https://www.postgresql.org/docs/10/static/sql-select.html#sql-distinct > SELECT DISTINCT ON ( expression [, ...] ) keeps only the first row of > each set of rows where the given expressions evaluate to equal. The > DISTINCT ON expressions are interpreted using the same rules as for > ORDER BY (see above). Note that the “first row” of each set is > unpredictable unless ORDER BY is used to ensure that the desired row > appears first. Right, so the behavior is undefined unless you have an ORDER BY clause that includes the DISTINCT ON columns plus some more columns. That's pretty tightly tied in my book. > So if an ORDER BY is not specified, why couldn't we pick an arbitrary > ordering matching based on available indexes? The case is not of any real-world use, and so I'm unwilling to expend the large amount of coding effort that would be needed to make the planner behave this way. 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