Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1duJF9-0007dR-Jj for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Sep 2017 14:11:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1duJF8-0004EH-Ns for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Sep 2017 14:11:18 +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 1duJEQ-000301-Ic for pgsql-bugs@postgresql.org; Tue, 19 Sep 2017 14:10:34 +0000 Received: from mail-wm0-x22a.google.com ([2a00:1450:400c:c09::22a]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1duJEJ-0007eO-Ak for pgsql-bugs@postgresql.org; Tue, 19 Sep 2017 14:10:34 +0000 Received: by mail-wm0-x22a.google.com with SMTP id v142so230423wmv.5 for ; Tue, 19 Sep 2017 07:10:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=ZFE06VgooR5IngFo7KvCgfXi7OVC4sLchmjivaPuDQw=; b=yhQm5PMNLtjfvVBwpNW7RsVXcGXByGWLGNCal5+0vvJ4bUTtUS5B3AXTJ2jp8/kRvz +TVcjFozZee0T/l31L6xK5Rrz3wSsjJuBW/QOnNvOZq5lZtFbA4q/5N4zq48pCZZQsdZ UVYn2a1u/JtL4FoN+sQzyNjY/3cHSZnX8rVrUqmMfv5+b7K6FD10aHhyYY7HOpvsUEgM 21WyyKufWDNHd2H6y1HeUUMCMkIP3iHWV2SIImBeNnMOQgs+dAEhQDI77pulgSiw3BhE 8Wx97jfOpO1K2Dul3RQE2A1ObNRll2bGZf05YiCzK3kIAwRKDniC+u175OQ5BBoUsray sBkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ZFE06VgooR5IngFo7KvCgfXi7OVC4sLchmjivaPuDQw=; b=r6R5XnY0neYRh84cg8kLjrmhVfzvGPuws5wrUMsSLlUtIyiTY0V4dk2zZJ9OebGLuw H0HbXq0s50+asb7j6pY7w/m8LDrQePKBEEgo6rkHHuVK4EyN3RLnSLvBPL7u7EdR2NOT IUSAFi96fSImPjqzBDAJXzFxbUpfHmtZNxNYAPsKhE76eNbdzDcgD7eljYEjIJjTIP/3 gbd7Zc8Jx+J4zqmlNiix06uHPRNldNZ+qSvZ6juSpSkv0ZPy0PzhkkY8Z+fKXKQORAJ8 RN5K5B/8qDkTFOTNl7ZLejgs9dQ+ZhvjxOqhbGroKJ6ofpQ3HPzm/KICcbaqYxWZrtQf O+RQ== X-Gm-Message-State: AHPjjUgS/qsc0aD8NJIqGI8KkN5vlAGlkTKR/w9N/Fsc+AGBgudjIozU G/GNg1JOJqx2G/F3zxeIHg0wHM5XDSl8oCydHZ4o8LOQGiVjkcWoVePhmsOz0jzFWFJMJLVv274 1yUfeISGIbUWAhvgpCZqi8jnFnmVHMhZOyG7iHGX0B08Qanws13VE2IhGzrYcvBtnz+26A57yHc 6w8wKA5cE= X-Google-Smtp-Source: AOwi7QAYz0itmUU3nPFUlf0VgB8sGWjWnxG+lEbV6AaRsmU5vkpNanF5lVv0CPG3bDNGKL0aS+ZEYw== X-Received: by 10.28.97.11 with SMTP id v11mr1230775wmb.45.1505830225547; Tue, 19 Sep 2017 07:10:25 -0700 (PDT) Received: from [10.137.2.17] ([212.24.157.174]) by smtp.gmail.com with ESMTPSA id a19sm10637923wra.64.2017.09.19.07.10.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Sep 2017 07:10:24 -0700 (PDT) Subject: Re: Query planner skipping index depending on DISTINCT parameter order (2) To: Tom Lane Cc: Dilyan Palauzov , pgsql-bugs@postgresql.org 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> From: Tomas Vondra Message-ID: Date: Tue, 19 Sep 2017 16:10:23 +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: <12044.1505668534@sss.pgh.pa.us> 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 07:15 PM, Tom Lane wrote: > Tomas Vondra writes: >> On 09/17/2017 02:30 AM, Dilyan Palauzov wrote: >>> 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. > > 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. which in my understanding is that while we use the same rules as ORDER BY, we don't guarantee any particular ordering (i.e. which row we keep) unless an explicit ORDER BY clause is used. So if an ORDER BY is not specified, why couldn't we pick an arbitrary ordering matching based on available indexes? > For plain DISTINCT, yeah we could consider other orderings ... but > we're rather unlikely to find an index that matches all the output > columns, regardless of what order they're in. So it's just not that > exciting. > Not necessarily. For example if we get the incremental sort in, we might pick from a much wider set of indexes. > IOW, somebody might get around to this someday, but don't hold your > breath; there's lots of higher-value fruit to be reaching for. > Sure. But the perceived value really depends on the user - what's worthless for one user may be quite valuable for another one. 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