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 1s6Vms-0002Z5-Bs for pgsql-general@arkaria.postgresql.org; Mon, 13 May 2024 13:32:19 +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 1s6Vmr-001L9I-DZ for pgsql-general@arkaria.postgresql.org; Mon, 13 May 2024 13:32:17 +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 1s6Vmr-001L8v-2B for pgsql-general@lists.postgresql.org; Mon, 13 May 2024 13:32:17 +0000 Received: from mail-lf1-x134.google.com ([2a00:1450:4864:20::134]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s6Vmo-0000ur-9p for pgsql-general@lists.postgresql.org; Mon, 13 May 2024 13:32:16 +0000 Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-51fcb7dc722so4156483e87.1 for ; Mon, 13 May 2024 06:32:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715607134; x=1716211934; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=pFi7KSihI9j6TDDF8f3EXCxv1UbcBl7byf8E3LnONFk=; b=k5DiWOiPqv0yKTuE6nWclu/tzYi9iAtNbcdcy0/+37qh3t2vlk2O/3GImnCuFdJW7r DXP5ofX26SOYnX7StrpNS4gkMe1bHzvEF9hqCarkojfV2fSGVZKrTuygc76PeBla6SHi wgGX9bl3bbvSOltduDPIrAawVgTqIKENPZDPInW+SH3283LY+WfHkJVIrWJg8h0IRewS pKIjFq+HlN3bAiQaxBAiMXPPtyYfJyyrBfX09cJ+cVBVNplzd3eYsv/QQF8CPjrF6FQB wX3qoIIumSnSSY8sxoA2umgV6XY9AXu+95G/0ot05ZGZKhlJv6LYgrWGjC5zEalR3H/I dOOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715607134; x=1716211934; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=pFi7KSihI9j6TDDF8f3EXCxv1UbcBl7byf8E3LnONFk=; b=SxhSAyiCqFujS+KFE3q1t6q6Ux71+ZiJ465qe5Zz+CD6tri2DFWc2rhBoS1XZQ/5OX RlRxIO6smvZ6WW70otJSnV15KOCqguH8LZEeRTprwV7XCkszGMxlywBkAvRtJ5Zpiled 06nke9O+b9h1kt8Wa0WRnFF1Yzqab4Huzq1zUtBxtD3IuO5qe+Kc6pxxUecX5IJ86rcj rKwuyYArMfgjXOD98mbNacmblM1eoWyQCg3ONU78dFIWrwPejWRBC2SOi8wj0M5tdewn NSUlxo7Kq2Vwyr933BcIU5S6yRGzUtqjAPT/JrR+zuVxU9GE36DI8kFY33sSYtz7hRb3 HC5Q== X-Forwarded-Encrypted: i=1; AJvYcCXfnnkFI7HXS8MFEdS8XkR/5lR1utAsbdG0VhT3YpV1l8v3g4mpyBif5jo8Mn89IDu8QTQ5qsPXUCDxcdbobSd/ttcI16Bu5yxmUjn93mR1FlSf X-Gm-Message-State: AOJu0Yw63AllrpneX58eTfSo6p6PHBEUIwljah1MC02XrCiHhv223NXp dCAwBthgcN62Q0gKyR7MfxHtiuB7jRBWOC2pO9xZO+1l9ffbSQjiomn7h0kRXNKqu6dfs2nh62e u7TciAjFr2avniGPbb7J4X/TAwk0= X-Google-Smtp-Source: AGHT+IHr4l8HShbH7+0hFHJpb9pQyk7wNQ/LDvyZ6BxXXWFhbOKSsi+8ORSTSmulWpsr7PseCS+aLpac38MLBMWaqNg= X-Received: by 2002:a05:6512:1307:b0:51f:3c32:d061 with SMTP id 2adb3069b0e04-5220e68bb95mr3980505e87.34.1715607133868; Mon, 13 May 2024 06:32:13 -0700 (PDT) MIME-Version: 1.0 References: <7886a68f-b466-2131-1747-f69f0fb71a37@gmx.net> <69077f15-4125-2d63-733f-21ce6eac4f01@gmx.net> <559b0e40-63e6-fa9a-6b03-d1eba10f30f8@gmx.net> <1629463.1715372568@sss.pgh.pa.us> <6a2f3906-3d7a-6924-7403-8f77d57a18e4@gmx.net> <3efce60b-48c0-160d-0444-474b02f76739@gmx.net> In-Reply-To: <3efce60b-48c0-160d-0444-474b02f76739@gmx.net> From: David Rowley Date: Tue, 14 May 2024 01:32:02 +1200 Message-ID: Subject: Re: SELECT DISTINCT chooses parallel seqscan instead of indexscan on huge table with 1000 partitions To: Dimitrios Apostolou Cc: Tom Lane , pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 14 May 2024 at 00:41, Dimitrios Apostolou wrote: > > On Sat, 11 May 2024, David Rowley wrote: > > It will. It's just that Sorting requires fetching everything from its subnode. > > Isn't it plain wrong to have a sort step in the plan than? The different > partitions contain different value ranges with no overlap, and the last > query I posted doesn't even contain an ORDER BY clause, just a DISTINCT > clause on an indexed column. The query does contain an ORDER BY, so if the index is not chosen to provide pre-sorted input, then something has to put the results in the correct order before the LIMIT is applied. > Even with bad estimates, even with seq scan instead of index scan, the > plan should be such that it concludes all parallel work as soon as it > finds the 10 distinct values. And this is actually achieved if I disable > parallel plans. Could it be a bug in the parallel plan generation? If you were to put the n_distinct_inherited estimate back to 200 and disable sort, you should see the costs are higher for the index plan. If that's not the case then there might be a bug. It seems more likely that due to the n_distinct estimate being so low that the planner thought that a large enough fraction of the rows needed to be read and that made the non-index plan appear cheaper. I'd be interested in seeing what the costs are for the index plan. I think the following will give you that (untested): alter table test_runs_raw alter column workitem_n set (n_distinct_inherited=200); analyze test_runs_raw; set enable_sort=0; explain SELECT DISTINCT workitem_n FROM test_runs_raw ORDER BY workitem_n DESC LIMIT 10; -- undo alter table test_runs_raw alter column workitem_n set (n_distinct_inherited=-1); reset enable_sort; David