Re: Get more from indices. - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Get more from indices.
Date
Msg-id 23472.1383231585@sss.pgh.pa.us
Whole thread Raw
In response to Get more from indices.  (Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>)
Responses Re: Get more from indices.
Re: Get more from indices.
List pgsql-hackers
Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> writes:
> Unique indexes can sort the tuples in corresponding tables
> prefectly. So this query might can use index.

>> uniquetest=# create table t (a int, b int, c int, d text);
>> uniquetest=# create unique index i_t_pkey on t(a, b);
>> uniquetest=# insert into t
>> (select a % 10, a / 10, a, 't' from generate_series(0, 100000) a);
>> uniquetest=# analyze;
>> 
>> uniquetest=# explain (costs off, analyze on) select distinct * from t;

ISTM the right way to deal with this is not what you've done here, but
just to deem the DISTINCT a no-op if there's a unique index on some subset
of the distinct-ed columns.  This query is actually legally satisfied by
a simple seqscan, which would be faster than either of the plans you
mention.  In any case, it seems like a bad idea to me to conflate
distinct-ness with ordering, so I don't like what you did to PathKeys.

Having said that, there is the kernel of a useful idea here, I think.
The reason you don't get an indexscan already is that the DISTINCT
assumes it needs to sort by (a,b,c,d), which an index on just (a,b)
doesn't appear to satisfy.  However, if the index is unique, wouldn't
scanning the index produce data that actually satisfies the longer sort
key?  It doesn't matter what the values of c,d are if there are no
duplicates in the a,b columns.  So maybe as a separate patch, we could
look at claiming that a unique index satisfies the entire query_pathkeys
if it matches the first N columns of that.
        regards, tom lane



pgsql-hackers by date:

Previous
From: Noah Misch
Date:
Subject: ERROR during end-of-xact/FATAL
Next
From: "MauMau"
Date:
Subject: Re: [bug fix] PostgreSQL fails to start on Windows if it crashes after tablespace creation