Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dsq9o-0003b7-Sj for pgsql-bugs@arkaria.postgresql.org; Fri, 15 Sep 2017 12:55:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dsq9o-0004bv-FN for pgsql-bugs@arkaria.postgresql.org; Fri, 15 Sep 2017 12:55:44 +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 1dsq94-0002jd-W0 for pgsql-bugs@postgresql.org; Fri, 15 Sep 2017 12:54:59 +0000 Received: from mail.aegee.org ([144.76.142.78]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dsq92-0004bO-3r for pgsql-bugs@postgresql.org; Fri, 15 Sep 2017 12:54:57 +0000 Authentication-Results: aegeeserv.aegee.org; auth=pass (PLAIN) smtp.auth=didopalauzov DKIM-Filter: OpenDKIM Filter v2.10.3 mail.aegee.org v8FCubm7028950 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aegee.org; s=k4096; t=1505480198; i=dkim+MSA-tls@aegee.org; r=y; bh=N9TQZB2LctegL+4IFSAm9zhGn08aCR+Ry4rbMrw07vM=; h=Subject:From:To:References:Date:In-Reply-To; b=lXZbBQOXdGD2ya+Cq5aRhx7PE4sYWoETY0QYZV3KHaXdjriP+/OeGSQMuiPIN1Q6i EmBTKjGEw9Em1EldUQKDt30+duPPOZuIr8OCPkVHwZkcPAlyIXv6CPVLIurcnHOoRo CiJbTMogJ1K4YyCgcgv7GMwLhhwv6i1+uP+dnTrsDfI/abzaIJuFBkFkRq1YiMKtvy x6OlxBFQyS938DKOnrkNDTz96Jq8bHdwB2lxvRZQyvZadVem6bzK/KOmd7zy3hJ2oc QWZLfvcm9GmtIlBFU1puHk+AdAmobnu1HBCXuruO92CcRf+OHhcXkRwt84oW/JctS8 SKyuzYbLNec7wMiUr23zyyDQ4x7b3fb6JFBYSMGfvWRLYTT0434ACS/zc+78UGymgV 20EA4qo5MYZlA6mj7B9PkLr4VhUWBj2NtYNXss2cuNgvGKndwucSwKuxBvoEQ1bjif CmP/Zi1u2ZKM5aekv+lPtrIxngZJ4hMBXiPTTzxHuhBY6JQJXrd1vE2xNwtjoQT7IC 6FYDzJqWmUTZV12taX4CDTT68ODXGt8UHvudky8d/KwdRr/8FrSKoSXfbOLTmgHiGY siB0gwrgJ/bsY69Oz3516gnDgMh4YJg0of7UeYlJ2h9YPCdWMSn7lpV8F6fY3k1prB bWxqzvW8t/pCauUU9DQD5Jm0= 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 v8FCubm7028950 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Fri, 15 Sep 2017 12:56:37 GMT Subject: Query planner skipping index depending on DISTINCT parameter order (2) From: =?UTF-8?B?0JTQuNC70Y/QvSDQn9Cw0LvQsNGD0LfQvtCy?= To: pgsql-bugs@postgresql.org References: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> Message-ID: <57ccc03b-279a-63f7-19c6-6fe3b2d0e1be@aegee.org> Date: Fri, 15 Sep 2017 14:54:53 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <3c651d99-a943-4e21-4ec9-604b142c85e0@aegee.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit 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 Post Scriptum to my email from yesterday. I deleted the_index, so that I have now spamassassin=> \d bayes_token Table "public.bayes_token" ┌────────────┬─────────┬──────────────────────────────┐ │ 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) and as you can see, there is still a btree index on (id, token). I expect the same query plans, as before deleting the_index, as bayes_token_pkey has the same information as the_index had. This does not happen on my system and I have not tweaked the default configuration files. spamassassin=> EXPLAIN ANALYZE SELECT DISTINCT(id, token) token FROM bayes_token; ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ QUERY PLAN │ ├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ Unique (cost=21737.73..22555.11 rows=163476 width=32) (actual time=914.275..1206.239 rows=165523 loops=1) │ │ -> Sort (cost=21737.73..22146.42 rows=163476 width=32) (actual time=914.274..1125.674 rows=165523 loops=1) │ │ Sort Key: (ROW(id, token)) │ │ Sort Method: external merge Disk: 6616kB │ │ -> Seq Scan on bayes_token (cost=0.00..3668.76 rows=163476 width=32) (actual time=0.015..50.849 rows=165523 loops=1) │ │ Planning time: 0.079 ms │ │ Execution time: 1216.047 ms │ └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ What is the difference makes ROW between the one case here Sort Key: (ROW(id, token)) and in the other below: Sort Key: token, id Regards Dilyan On 09/14/17 17:38, Дилян Палаузов wrote: > 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