Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s5Wld-005sIh-Gu for pgsql-general@arkaria.postgresql.org; Fri, 10 May 2024 20:22:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s5Wla-001IL8-Tu for pgsql-general@arkaria.postgresql.org; Fri, 10 May 2024 20:22:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s5Wla-001IL0-Il for pgsql-general@lists.postgresql.org; Fri, 10 May 2024 20:22:54 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s5WlX-000PAc-L3 for pgsql-general@lists.postgresql.org; Fri, 10 May 2024 20:22:54 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 44AKMmcc1629464; Fri, 10 May 2024 16:22:48 -0400 From: Tom Lane To: Dimitrios Apostolou cc: pgsql-general@lists.postgresql.org Subject: Re: SELECT DISTINCT chooses parallel seqscan instead of indexscan on huge table with 1000 partitions In-reply-to: <559b0e40-63e6-fa9a-6b03-d1eba10f30f8@gmx.net> References: <7886a68f-b466-2131-1747-f69f0fb71a37@gmx.net> <69077f15-4125-2d63-733f-21ce6eac4f01@gmx.net> <559b0e40-63e6-fa9a-6b03-d1eba10f30f8@gmx.net> Comments: In-reply-to Dimitrios Apostolou message dated "Fri, 10 May 2024 21:35:57 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1629462.1715372568.1@sss.pgh.pa.us> Date: Fri, 10 May 2024 16:22:48 -0400 Message-ID: <1629463.1715372568@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dimitrios Apostolou writes: > Further digging into this simple query, if I force the non-parallel plan > by setting max_parallel_workers_per_gather TO 0, I see that the query > planner comes up with a cost much higher: > Limit (cost=363.84..1134528847.47 rows=10 width=4) > -> Unique (cost=363.84..22690570036.41 rows=200 width=4) > -> Append (cost=363.84..22527480551.58 rows=65235793929 width=4) > ... > The total cost on the 1st line (cost=363.84..1134528847.47) has a much > higher upper limit than the total cost when > max_parallel_workers_per_gather is 4 (cost=853891608.79..853891608.99). > This explains the planner's choice. But I wonder why the cost estimation > is so far away from reality. I'd say the blame lies with that (probably-default) estimate of just 200 distinct rows. That means the planner expects to have to read about 5% (10/200) of the tables to get the result, and that's making fast-start plans look bad. Possibly an explicit ANALYZE on the partitioned table would help. regards, tom lane