Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dtAGJ-0007gj-4p for pgsql-bugs@arkaria.postgresql.org; Sat, 16 Sep 2017 10:23:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dtAGI-00085V-Ho for pgsql-bugs@arkaria.postgresql.org; Sat, 16 Sep 2017 10:23:46 +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 1dtAGH-00084Q-IP for pgsql-bugs@postgresql.org; Sat, 16 Sep 2017 10:23:45 +0000 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dtAGD-0006LC-Hl for pgsql-bugs@postgresql.org; Sat, 16 Sep 2017 10:23:44 +0000 Received: by mail-wm0-x22b.google.com with SMTP id 13so13092517wmq.2 for ; Sat, 16 Sep 2017 03:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=RHfm9kMSyAyffJAwAgFSF71Jrr7G+em43kHpIEPE+S8=; b=HNcHXforVUskwWHDzu7s2EWT0WaWP6MmvXaJLFD7RrygXWxOPRTpDwhV04OwJx8zrf dHiiNNgfk8y9AeFvbCxgvFN2cTsl/FigLXNOszH8qisDb7CK5pAELHCQ0sEWUnMfuXbU TFcp5TYomvXV5rewfTe9DzKDLQrxNHQmPy9E1JKk7EUsE8xAGhkCGFdm2t6E3y4SpDj7 aklc/LycalAywc3bHC/6kr6uEkLBe+KdjVrCPjQ1odHfBR25pwuXTjaqhN7HXYpeY+Eb xQMZtj0YcR4s9fpHw4ZYRa8GqDAXE+3WHUFcN7DuHgwthfzIE2Mr69G8CVMBOalVfOja FCog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=RHfm9kMSyAyffJAwAgFSF71Jrr7G+em43kHpIEPE+S8=; b=j+QteCPsl4gRaNSbJtEsBtOSuNDhi966TCPYnfgzCclcaTXiWI/HlkMEXgS0I5zZM6 detYFhP1OLjCvncD2j1bdBfiX5tL93m3ypbiP13A+gWo6PQrgHHUIwIfHpZme4yoz4dM bNwknymjC/Wh0jqCBn8gy8i5aL6ThjdFtqEH9//HRHjO9Hyj6k98KEKGu5XiLb7YZdFL +2bT8nOeZ5ZmaCKzKGrhTmAQMAUwFWQWYxPxVzUWPppgoqiKkj0kIOaP/RZzkqtzCQDw AbsTJIcFp4+iV6DRVIUdvj5dKml97Y+eeG3UbeS2cgmtRKppbPAnDzvTUrBQDg5m0Kl4 PJ0g== X-Gm-Message-State: AHPjjUiB8R38bxaIaWoWDPAbAGur0RrpXTG8tcyO83t9oecuEd7b5gNX uAImYQ3P+9AICvr2+JolKhnFkuHOaaoXyfzavpXC/JDwX2KR/LqKiy9uH1aiGuukb56oC/rjc3z 9regBvzxOFrhZtynsUnpSixnEFLJLlujofBfIvSs4CooSS+3rii3GPgbCjQ1nxBOk+Uf/aeg= X-Google-Smtp-Source: AOwi7QDP21J2auMJWAeRwkma1OMug4izAt9NYsxlvRWZsCKzKIOH4jDQ6zphOHZf35P3FSPUchoA6Q== X-Received: by 10.28.230.3 with SMTP id d3mr4417543wmh.54.1505557419215; Sat, 16 Sep 2017 03:23:39 -0700 (PDT) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id o65sm2631596wmd.8.2017.09.16.03.23.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Sep 2017 03:23:38 -0700 (PDT) Subject: Re: Query planner skipping index depending on DISTINCT parameter order (2) To: =?UTF-8?B?0JTQuNC70Y/QvSDQn9Cw0LvQsNGD0LfQvtCy?= , pgsql-bugs@postgresql.org References: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> <57ccc03b-279a-63f7-19c6-6fe3b2d0e1be@aegee.org> From: Tomas Vondra Message-ID: Date: Sat, 16 Sep 2017 12:23:35 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <57ccc03b-279a-63f7-19c6-6fe3b2d0e1be@aegee.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit 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 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 -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs