Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dymKn-000255-Fi for pgsql-bugs@arkaria.postgresql.org; Sun, 01 Oct 2017 22:03:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dymKn-000725-2X for pgsql-bugs@arkaria.postgresql.org; Sun, 01 Oct 2017 22:03:37 +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 1dymKm-00071W-Cq for pgsql-bugs@postgresql.org; Sun, 01 Oct 2017 22:03:36 +0000 Received: from mail-wm0-x22c.google.com ([2a00:1450:400c:c09::22c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dymKj-00011B-FV for pgsql-bugs@postgresql.org; Sun, 01 Oct 2017 22:03:35 +0000 Received: by mail-wm0-x22c.google.com with SMTP id i124so7302446wmf.3 for ; Sun, 01 Oct 2017 15:03:33 -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=JyBL7mCGyMW6w0A1f81p8N3/Wvy1BLVS5F49LQJEYBA=; b=g0dqA83MsJItQyp4hjvKxqFs5Pr3My+8rNIbKN8FQcouhL1jZPhwjWqGBh/Y9GpbcU d5DW+1+g+dGLmBnsn6CbzdDQDrKnJ8kJlOoD7KtK+N7Huj+O73ZmIvAMCI9N5YpKGE17 4meSIVoNc8sR9cR8XiEiL4pAgzId/0JohoxihIihvTgb5yTYa8Lf+y+Z+o+UT0bbD36L xrraI0gkHkFwpJ6aS+Zt+cw1t2nmDODj/o/ZnMWep+5idOO1Prm0JSzqloRftTd6jNAX 3xvWeVv3mUnIA1YgjkdqFhKou5o83DIE5u1WuSbgEHmHdLChyyMXlKBrR/xvrw+HZUEb HBPw== 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=JyBL7mCGyMW6w0A1f81p8N3/Wvy1BLVS5F49LQJEYBA=; b=FcHEoZ006rsugxp3JjqzfioNqm0JYiQYdEF4I+IU6IpC1TNsyssGyVZ9x+6GECf85J uXVaaL17MeQhlv+7KU25BzrQcDiXqRhLwcNSepDF6dqwMk8WtquAXt9UWBvRPfNcjVBY pXcD/SU4kqp3ypqcLTa9cHZep8Ta/rEphgyrD9H1HCMEpduKB7guh5m+dFOJM/Qob1GL Yct6fmJ6cQywM/oAI4ZQL9Aci8Gfiqge6hB5o392iKLx6kZWELrk0uDSTlk77LtVwYJS QIWaHKiXAHTNzzGvgV+eSfvJaBTZ+G2MPm6V61IWwgaLR1RLVNJf+0kgeofv7UvwUWOG EjCg== X-Gm-Message-State: AHPjjUiS3aRh4K8jKVMjfXlWMiNYIX+FTYAZmIJ8LcScpCyZVT9KzCmG 1kDA022rcWOY9MgUSs+kymtKTN1ygnNv7evs2slcoqtxXKn0ajA7g9N43SIVuYFx25dunwrKD4s ufNaBf3VFJKKmUBn9mNB2o6mZJiFQFnitaG4QasG7D7AKVNWJZRKpYVf4Ao3Dz9edHC4ddH69f3 ID7e4O09o= X-Google-Smtp-Source: AOwi7QBiiMbrfYWT1IRz10n2Nw6KKV+E7JkwJA4eo6UkVuLQ4GDraUJtxVf0Pd5/XZ/fOK3tNrPVUQ== X-Received: by 10.80.194.146 with SMTP id o18mr17619785edf.19.1506895411559; Sun, 01 Oct 2017 15:03:31 -0700 (PDT) Received: from [10.137.2.17] ([31.149.229.2]) by smtp.gmail.com with ESMTPSA id l33sm10196646edd.94.2017.10.01.15.03.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Oct 2017 15:03:30 -0700 (PDT) Subject: Re: Query planner skipping index depending on DISTINCT parameter order (2) To: Greg Stark , Tom Lane Cc: Dilyan Palauzov , PostgreSQL Bugs 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: Mon, 2 Oct 2017 00:03:29 +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: 7bit 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/25/2017 12:43 PM, Greg Stark wrote: > On 17 September 2017 at 18:15, Tom Lane wrote: > >> 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. > > I don't follow this part. Are you saying a simple "select distinct a,b > from table" is unlikely to find a matching index on ? Don't we > already do exactly this for "select a,b from table group by a,b"? FWIW we don't do that: create table t (a int, b int); insert into t select mod(i, 1000), mod(i, 1000) from generate_series(1,1000000) S(i); create index on t (a,b); vacuum analyze t; explain (costs off) select a, b, count(*) from t group by a , b; QUERY PLAN -------------------------------------------- GroupAggregate Group Key: a, b -> Index Only Scan using t_a_b_idx on t (3 rows) explain (costs off) select a, b, count(*) from t group by b , a; QUERY PLAN --------------------------- GroupAggregate Group Key: b, a -> Sort Sort Key: b, a -> Seq Scan on t (5 rows) But yeah, fixing this is probably more worthwhile than the original DISTINCT ON issue ... 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