Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dsWDq-0006Cp-OS for pgsql-bugs@arkaria.postgresql.org; Thu, 14 Sep 2017 15:38:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dsWDo-0000WV-Ed for pgsql-bugs@arkaria.postgresql.org; Thu, 14 Sep 2017 15:38:32 +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 1dsWDn-0000TU-Mz for pgsql-bugs@postgresql.org; Thu, 14 Sep 2017 15:38:31 +0000 Received: from mail.aegee.org ([144.76.142.78]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dsWDk-0002aC-52 for pgsql-bugs@postgresql.org; Thu, 14 Sep 2017 15:38:30 +0000 Authentication-Results: aegeeserv.aegee.org; auth=pass (PLAIN) smtp.auth=didopalauzov DKIM-Filter: OpenDKIM Filter v2.10.3 mail.aegee.org v8EFe9ju001547 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aegee.org; s=k4096; t=1505403610; i=dkim+MSA-tls@aegee.org; r=y; bh=hJd7jc9naNyNOSW8NpEl893S9y94qJZOV9gTIPaaUJI=; h=From:To:Subject:Date; b=Ip83LBD+mq5nghO4SGBJ+hIQlGohUEmkS5HhABxLClLAHWdgTMSJjwJaTCAUDXQu3 w+JZQj2D1QE4AQrIXdc/lI9neeyN8OLCq/eej2bCzWBDwAuPyn5XrGfaHWg5fGNq1Y ZM10xXfI3nxez6o3YCDqchRR/MQuEqa4Ry6FctjXEGXTIwz4Tx+7tg0uNn3QESygo4 XRkuwwk20WHKdK172dutj9bIhvkyabeU/vzStjH45ZowiaHJD+ocH2rjnHcuvgh6pS Q5f8PNIwBdxfCQpOVuvrjBu7e068PqnYkRPdvKYDErpi9tf6SSzQ86tFTJTBRMTERm 7BnykexWlwOKUqKjI0gsLEewO5aDrJgjU3dfhXtlIiFfQRhPg4shjsk0whLa7nLzqv kBgCpbOe1lMorMNmeIOkUtbOQEtwCerzWZ82n1pP9MV4Q5A0mzCx+1DLNm97yEaT4r ONRpnf066cMVu7+CWsERSzMDwtf7M84touTgi//J4E1Mfj8NPo1javs80XaDLO5gkm Zhl9V0wL6LIcQuX+B15KxsVGZAtrQrtqDDiL7MnakHJtkw2WxwuyiP2/ULWb8N3Mp0 Rp1cK0GuAvIagEjp3y3MuHQkSES1GZftSZFNUhvT/VtS5rr0d4Ntnn1JxTMCVbeJcW ElQkG5HKzLjlworL5+ewQ7Hw= Authentication-Results: mail.aegee.org; dkim=none Received: from [10.128.8.92] (port-87-193-154-82.static.qsc.de [87.193.154.82]) (authenticated bits=0) by mail.aegee.org (8.15.2/8.15.2) with ESMTPSA id v8EFe9ju001547 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Thu, 14 Sep 2017 15:40:09 GMT From: =?UTF-8?B?0JTQuNC70Y/QvSDQn9Cw0LvQsNGD0LfQvtCy?= To: pgsql-bugs@postgresql.org Subject: Query planner skipping index depending on DISTINCT parameter order Message-ID: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> Date: Thu, 14 Sep 2017 17:38:25 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Virus-Scanned: clamav-milter 0.99.2 at mail.aegee.org X-Virus-Status: Clean 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 Hello, I have this database in Pg 9.6.5, the schema comes from spamassassin: ┌────────────┬─────────┬──────────────────────────────┐ │ Column │ Type │ Modifiers │ ├────────────┼─────────┼──────────────────────────────┤ │ id │ integer │ not null default 0 │ │ token │ bytea │ not null default '\x'::bytea │ │ spam_count │ integer │ not null default 0 │ │ ham_count │ integer │ not null default 0 │ │ atime │ integer │ not null default 0 │ └────────────┴─────────┴──────────────────────────────┘ Indexes: "bayes_token_pkey" PRIMARY KEY, btree (id, token) "bayes_token_idx1" btree (token) "the_index" btree (id, token) with 261000 rows. According to my understanding, "SELECT DISTINCT ON(token, id) token FROM bayes_token;" and "SELECT DISTINCT ON(id, token) token FROM bayes_token;" are supposed to deliver the same results, as only the "token" and "id" columns are exchanged in DISTINCT, so the query optimizer is supposed to generate the same query plan. But it does not: EXPLAIN ANALYZE SELECT DISTINCT ON(token, id) token FROM bayes_token; ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ QUERY PLAN │ ├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ Unique (cost=32624.56..34588.73 rows=261889 width=10) (actual time=154.947..264.439 rows=261935 loops=1) │ │ -> Sort (cost=32624.56..33279.28 rows=261889 width=10) (actual time=154.945..215.378 rows=261935 loops=1) │ │ Sort Key: token, id │ │ Sort Method: external merge Disk: 5624kB │ │ -> Seq Scan on bayes_token (cost=0.00..4579.89 rows=261889 width=10) (actual time=0.015..35.282 rows=261935 loops=1) │ │ Planning time: 0.085 ms │ │ Execution time: 285.303 ms │ └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ EXPLAIN ANALYZE SELECT DISTINCT ON(id, token) token FROM bayes_token; ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ QUERY PLAN │ ├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ Unique (cost=0.42..13819.07 rows=262023 width=10) (actual time=0.028..207.695 rows=262018 loops=1) │ │ -> Index Only Scan using the_index on bayes_token (cost=0.42..12508.95 rows=262023 width=10) (actual time=0.026..147.882 rows=262018 loops=1) │ │ Heap Fetches: 261729 │ │ Planning time: 0.086 ms │ │ Execution time: 232.598 ms │ └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ The_index and the SELECTs are invented for the sake of this demonstration. Regards Diluan -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs