Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dtNUN-00036v-4p for pgsql-bugs@arkaria.postgresql.org; Sun, 17 Sep 2017 00:31:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dtNUL-0002rn-Dg for pgsql-bugs@arkaria.postgresql.org; Sun, 17 Sep 2017 00:31:09 +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 1dtNTc-0001cb-0T for pgsql-bugs@postgresql.org; Sun, 17 Sep 2017 00:30:24 +0000 Received: from mail.aegee.org ([144.76.142.78]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dtNTT-0001oP-7K for pgsql-bugs@postgresql.org; Sun, 17 Sep 2017 00:30:23 +0000 Authentication-Results: aegeeserv.aegee.org; auth=pass (PLAIN) smtp.auth=didopalauzov DKIM-Filter: OpenDKIM Filter v2.10.3 mail.aegee.org v8H0Vu6r019834 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aegee.org; s=k4096; t=1505608317; i=dkim+MSA-tls@aegee.org; r=y; bh=lvxODQEtKPEH4/HCZfwbZcj3nokPhDK0ZrcpMPEY0Go=; h=Subject:To:References:From:Date:In-Reply-To; b=ZckJJ2T6UnOzgPfLTR7nidla79e6QIHF71Vxg16tPfk6RLpVtgvf0o02HjyO7U5QX 9UZPD33IsokM7J5ds8EXhOkSSSrael/soJjsVIBpUSXAkF5hzh2McDasBbbln4666L oblk7ryBy/nKIwFBimCziEsZmVsyuslmd8WEgyfM5Y1bKd6jRMK8y95JeYppXP//aT uoITOlpX8wWFjU48HMck63xD7hL9uuG30Hd9QuqKIaJRp8j7oOl9aMniypQ1xF/v4q bBE0xN/dJ12XlibIfDdXCz66Ew5JQnHiADlHyH+IbNoQbnSj4MQVjLatO8bI75cImM fqj94ZtYVejDAZuUe4ImAnOSx5eqEgyJj0vba7kOUP1kM7uLMFwWGp1xYlWfyoNa+z awtprcf/XP9dZFaCS6sB7PrqtjK3xsrALbz8stEGUROXpp4Tyrf2v9UMyeIhXTL2xt RxjjExkL1BmlmMnzmsSRkjtfo/kk2mDQr9k14/40/GOBLtAsNTSCr0cHQHxShGvhGc lG6Fs8I96Jli33r3b8Qfslh2AWcdrzHpGEibnrjosKMegbM2mkjVegLklc3zQbFNUb 6jjDUkqZVwbYU48ls0cWVEWzlYpk3/LWAe+9HAEOx4WfA6PHS8UqyteCMNSy09+DrO u3hbIkpe7vSp55anUBL17Ij0= Authentication-Results: mail.aegee.org; dkim=none Received: from [192.168.178.39] (x5f74ec51.dyn.telefonica.de [95.116.236.81]) (authenticated bits=0) by mail.aegee.org (8.15.2/8.15.2) with ESMTPSA id v8H0Vu6r019834 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO); Sun, 17 Sep 2017 00:31:57 GMT Subject: Re: Query planner skipping index depending on DISTINCT parameter order (2) To: Tomas Vondra , pgsql-bugs@postgresql.org References: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> <57ccc03b-279a-63f7-19c6-6fe3b2d0e1be@aegee.org> From: Dilyan Palauzov Message-ID: Date: Sun, 17 Sep 2017 00:30:11 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: clamav-milter 0.99.2 at mail.aegee.org X-Virus-Status: Clean 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 Hello Tomas, thanks for your answer. While adding an ORDER BY uses the index, it does not help, as the user has to take care of the order of provided columns. Whether the care is is taken in DISTINCT ON or in ORDER BY is secondary. The behaviour behind DISTINCT and indexes surprises me, as the query planner does reorder the columns for SELECT to determine the most suitable index. My proposal to reflect this: diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -1069,7 +1069,10 @@ SELECT DISTINCT ON (location) location, time, report Currently, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE and FOR KEY SHARE cannot be - specified with DISTINCT. + specified with DISTINCT. Contrary to + SELECT, which reorders its parameters to find a best, + matching index, DISTINCT ON constructs an expression, e.g. from the + provided rows, and checks then if an index can serve the expression. Please ignore my email from Friday, I have forgotten an ON after DISTINCT. Greetings Dilyan On 09/16/2017 10:23 AM, Tomas Vondra wrote: > Hello Dilyan, > > You're right - we're currently not able to use the index if it's not > consistent with the DISTINCT ordering. That is, if you have index on > (a,b) and DISTINCT ON (b,a) we fail to leverage the index. > > The reason for this simple - if you look at create_distinct_paths [1], > which is where the Unique path comes from, you'll see it iterates over > all paths and compares the ordering using pathkeys_is_contained [2]. > > That however only ensures the path matches the expected Unique ordering > (determined by the column list in DISTINCT ON clause), we don't try to > re-shuffle the columns in any way at this point. > > So this is more a missing optimization than a bug, I'd guess. But it > seems worthwhile and possibly not extremely difficult to implement, so I > may look into it - but that's PG11 at the earliest. > > But, looking at the code in create_distinct_paths, ISTM you can easily > convince the planner to use the index by simply adding a matching ORDER > BY clause. That is > > SELECT DISTINCT ON(token, id) token FROM bayes_token > ORDER BY id, token; > > should be able to use the index on (id,token). > > > [1] > https://github.com/postgres/postgres/blob/master/src/backend/optimizer/plan/planner.c#L4725 > > [2] > https://github.com/postgres/postgres/blob/master/src/backend/optimizer/plan/planner.c#L4811 > > > regards > -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs