Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1duQaM-0006lJ-To for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Sep 2017 22:01:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1duQaM-0001KY-Aa for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Sep 2017 22:01:42 +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 1duQaL-0001HI-Iy for pgsql-bugs@postgresql.org; Tue, 19 Sep 2017 22:01:41 +0000 Received: from mail-wr0-x22a.google.com ([2a00:1450:400c:c0c::22a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1duQaI-0002GT-7M for pgsql-bugs@postgresql.org; Tue, 19 Sep 2017 22:01:40 +0000 Received: by mail-wr0-x22a.google.com with SMTP id c23so715215wrg.9 for ; Tue, 19 Sep 2017 15:01:37 -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=kWXgsxtyz+Ohf01gHXRaRcRfUGzZie5VPQ1G/zpJ2M8=; b=FVZMEt/bxY1O37hOZBF76DAfnQxcYRoL9F1N1wV8wCcoHwu+kfjekhWwf6ZstasR2h 7HVjlH7YqI/m6S7EOrhcTfXyi3UQxzUcSkRECYrgLGhZIAomBtg2bFouUEUb8Pu4Octb 0T270R9x6VA35qV8KATlHtowkc6x4PIn4e+716QKM9lcz8CSuqg/vM+h2FXSZsapB3Pu +fp8Es6pnfyD5VPieIv0fY3po2caAXnf9t15qRWrTtkj/pwiOtiMCBuXplCbvjk0ZgpA CS1daTyv+yiTVRAzVBrnkLeEZmHJRhsGJtKiFDdtXSpP2erS/UaYoKqoB+dushJCirJu 5xKQ== 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=kWXgsxtyz+Ohf01gHXRaRcRfUGzZie5VPQ1G/zpJ2M8=; b=BJZss9YnaDgeOazcQIZOINFpPx1cuT5heurzDAr7Ujee98XaaJHMCZ+hNzoelVLWL4 OVgX3Hs5i5N/uXUTzXE81Qb6vs4Nv0XpoLQCF6U5EccCHX/l/V55s2H+sGlp1Ji+iUae zwttAkPtb2NoFmJOYV1BbO/AxluDAMQJbbXI2S2F8A9SKOioGEZv5F0IFG6aXIKPagi6 33mCDErc6gCSGRVd90kriRN6Fn1AnbIF9h7fzEAie4DD458PHMDzsu1mjrr/+CS5A7pC uYrXJjUJ1n7T1lAB0K80Kd/PpWIR/KXIKxGXn+3VtQ31e+fgBkhiIVJXn99oIwk2BQi4 h/Rw== X-Gm-Message-State: AHPjjUhGWQfQFSQQdclucDs+daRKKA2K6U7Hr/kmv2ZJ7urbG/t1OO+A H5u/9tQhWVKgq4z/VVN8xvJ9K3JPR3Nw8hobG8JgReEXzgv2s0nM+sosf6WN1lfGtZ1pT71yFHa Atvm4cRusFT+18E392sxe/wLUgEqhMQztp3ktoE7f8tsyZgdQV0uCQDLMS1akildwgjy97aW/P8 z1R4Le X-Google-Smtp-Source: AOwi7QBtufrrDU5uKFAs1QJM2szu/euVucmsVs/wztCPTMtqpot5LbObA8SfqwyQ7mimXedkkdX4TQ== X-Received: by 10.223.154.81 with SMTP id z75mr243321wrb.124.1505858494902; Tue, 19 Sep 2017 15:01:34 -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 204sm270722wms.1.2017.09.19.15.01.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Sep 2017 15:01:34 -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> <7066.1505835619@sss.pgh.pa.us> From: Tomas Vondra Message-ID: <580b8874-4655-1454-2459-c1cd3432c61b@2ndquadrant.com> Date: Wed, 20 Sep 2017 00:01:32 +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: <7066.1505835619@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/19/2017 05:40 PM, Tom Lane wrote: > 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. > Ah, OK. I thought you're suggesting we're required to produce the data sorted by the DISTINCT ON columns. But you meant that ORDER BY clause is required for well-defined result, which limits our options when picking an index. Right? >> 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. > I don't think the "no real-world use" is so clear. Imagine for example a denormalized table where the user knows that for a given ID, the other columns match too. Then SELECT DISTINCT ON (id), x, y, z FROM t; SELECT DISTINCT ON (id), x, y, z FROM t ORDER BY id, x, y, z; SELECT DISTINCT id, x, y, z, FROM t; will all produce the same result, except that the first case only needs compare values in "id". If sorts on "x", "y" and "z" are expensive (say, because those are text columns with non-C collations), that may be quite a difference. That being said, I'm not sure how much code would this be, and how much overhead would it mean. 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