pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@2ndquadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Dilyan Palauzov <dilyan.palauzov@aegee.org>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Query planner skipping index depending on DISTINCT parameter order (2)
Date: Wed, 20 Sep 2017 00:01:32 +0200
Message-ID: <580b8874-4655-1454-2459-c1cd3432c61b@2ndquadrant.com> (raw)
In-Reply-To: <7066.1505835619@sss.pgh.pa.us>
References: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org>
	<57ccc03b-279a-63f7-19c6-6fe3b2d0e1be@aegee.org>
	<cd89d79f-96df-1a1e-5821-ef111950eccf@2ndquadrant.com>
	<a68737c6-601f-6846-2ba0-3795c942c81f@aegee.org>
	<271e4961-abfa-06ef-a502-cf00a31b236a@2ndquadrant.com>
	<12044.1505668534@sss.pgh.pa.us>
	<ecf623e7-04f5-e20a-4301-8e056c398eba@2ndquadrant.com>
	<7066.1505835619@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 09/19/2017 05:40 PM, Tom Lane wrote:
> Tomas Vondra <tomas.vondra@2ndquadrant.com> 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



view thread (13+ messages)  latest in thread

Message-ID: <580b8874-4655-1454-2459-c1cd3432c61b@2ndquadrant.com>
Permalink:  ../580b8874-4655-1454-2459-c1cd3432c61b@2ndquadrant.com/
Also on:    postgresql.org/message-id/580b8874-4655-1454-2459-c1cd3432c61b@2ndquadrant.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tomas.vondra@2ndquadrant.com, tgl@sss.pgh.pa.us, dilyan.palauzov@aegee.org
  Subject: Re: Query planner skipping index depending on DISTINCT parameter order (2)
  In-Reply-To: <580b8874-4655-1454-2459-c1cd3432c61b@2ndquadrant.com>
  References: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> <57ccc03b-279a-63f7-19c6-6fe3b2d0e1be@aegee.org> <cd89d79f-96df-1a1e-5821-ef111950eccf@2ndquadrant.com> <a68737c6-601f-6846-2ba0-3795c942c81f@aegee.org> <271e4961-abfa-06ef-a502-cf00a31b236a@2ndquadrant.com> <12044.1505668534@sss.pgh.pa.us> <ecf623e7-04f5-e20a-4301-8e056c398eba@2ndquadrant.com> <7066.1505835619@sss.pgh.pa.us> <580b8874-4655-1454-2459-c1cd3432c61b@2ndquadrant.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox