Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dtcbZ-0002ts-RU for pgsql-bugs@arkaria.postgresql.org; Sun, 17 Sep 2017 16:39:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dtcbY-00045d-OA for pgsql-bugs@arkaria.postgresql.org; Sun, 17 Sep 2017 16:39:36 +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 1dtcbY-00045U-9U for pgsql-bugs@postgresql.org; Sun, 17 Sep 2017 16:39:36 +0000 Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dtcbV-0001Ef-4C for pgsql-bugs@postgresql.org; Sun, 17 Sep 2017 16:39:34 +0000 Received: by mail-wm0-x234.google.com with SMTP id i189so17181857wmf.1 for ; Sun, 17 Sep 2017 09:39:32 -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=oDqIPxjfRc/GeH6NlcVxQrDZWJWlQ36Lwprjz1F6v0o=; b=WhjgqCjVGQMhHHIj9rdJw/Y/7pdthM5hpMIrQTgkrqF+efTHpTnM+I9HPn0kAbGToW KKfsKIDCP00Umu8pQWWhZHJnMIRTU+RQKpMIP8TJytdgcqFt2VZ6bujT6XV+OCDXv2vy dUxqmbiJ+JuSPWEfMiQ9GF5E5ZgjHyQ+/bvc5crPhoLCMpKdklc4l/X9R6VHtB2+Izoh sbLzEgSqfcPmOL8MUEcBGTWqFuJPh+CqB3FKd+iDaIsA1mgDGLjMsavqbMYS7hZ2AadY KpTJ4vsNZwoJBgw0Dbiahpq8zGUeEkctqmCEOTWH8THqk4fEHVxXgXAHbwCh3x4yaiEG e+9w== 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=oDqIPxjfRc/GeH6NlcVxQrDZWJWlQ36Lwprjz1F6v0o=; b=MuCOS7S1Qk3cGcYKOdSIWOiKGckYBDsXu81wYUfbiGSFpHc53AYZBMdHK8kexUB0oq LD7u4SSpvLTVDTf0o4+ki8Vh/dtKEK50S25TiZwBKAfdbo0ZlEKJPgrQCpXd4zMU9C2h R/4LAFC2wpHUxzvtWJOdMj0f34HJpYmFvL39HowTsZ6V+4C3Y7fIR0Xbj7V2vqIU9kEE 67zO0lP+teev4rAjL0K2HJkhUy80bGfTeTIeemKAsNmQeuuSsVWGThQTKKnchzmPyT61 9bQ5P4Tog81alkcei440DwhE5D6+9LNGwTgbDhp7IgVScxNgu2sSW67alpHhhV/3r1z0 o+Kw== X-Gm-Message-State: AHPjjUjYTJntxAuqxCUS8uGD/EIpGhNyz4sT79PBo6uFvzvl8y2HYkwR DBW297wm7Eatr2ahKjCI0THnHLpffoJIMbpsb2CJhbJx2Z/AlLN4LQV3HEXI/7RxEwDYge1TecO /eWEUywHw8Twk29Oopy+QkKRyQG1pxijZo8Q2FH7PhKDvCsAHPASlFcbqYep2unugvdQWUl0= X-Google-Smtp-Source: AOwi7QBnzesCqk0IvyQCv6YiMfM3nx12iWLOGg/28b++03VAcXWXqwXK1nJyJaUSLWbs/AF2EkqKEg== X-Received: by 10.28.65.8 with SMTP id o8mr7501441wma.146.1505666370839; Sun, 17 Sep 2017 09:39:30 -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 q4sm5182820wmd.3.2017.09.17.09.39.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Sep 2017 09:39:30 -0700 (PDT) Subject: Re: Query planner skipping index depending on DISTINCT parameter order (2) To: Dilyan Palauzov , pgsql-bugs@postgresql.org References: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> <57ccc03b-279a-63f7-19c6-6fe3b2d0e1be@aegee.org> From: Tomas Vondra Message-ID: <271e4961-abfa-06ef-a502-cf00a31b236a@2ndquadrant.com> Date: Sun, 17 Sep 2017 18:39:27 +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: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit 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 On 09/17/2017 02:30 AM, Dilyan Palauzov wrote: > 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. > Well, I agree it's somewhat reasonable optimization. The thing is, the planner/optimizer does not start with all features on day 1, it gets improved over time. And no one implemented this bit yet. > 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. >     >    > I don't think we want to change the docs like this. Notice that the SELECT documentation does not mention indexes at all, and I'm pretty sure we don't want to start doing that. The docs are user-level, deal only explaining properties of the output relation, and not with implementation-level details like index optimizations. 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