Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUo9l-0001Fq-Et for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Feb 2023 12:23:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pUo8m-00040m-CZ for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Feb 2023 12:22:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUo8m-00040d-2o for pgsql-hackers@lists.postgresql.org; Wed, 22 Feb 2023 12:22:32 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pUo8k-0002Pp-6w for pgsql-hackers@postgresql.org; Wed, 22 Feb 2023 12:22:31 +0000 Received: by mail-wm1-x329.google.com with SMTP id p18-20020a05600c359200b003dc57ea0dfeso6118754wmq.0 for ; Wed, 22 Feb 2023 04:22:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=arWHXZecimKgj2NgIGw11Rnj1QWxgCMT7NM1/SwZkCE=; b=j4tDe9IcYxPdPlRRnQMyQM0D6QX4b/p+lPDxtPVj9tiMe7rCXAs0o507EK7jH97MKJ joU8sGWSygb356ER0U6OaqjtnLdr1kLnZXQU+AWI/MeerOpYBEe7w8dHfbzuZinibUPS S+tsBl7IMgaNHQd1KsvMmZb2uZbMvIAMDWF2fAojbffxyfOaqiTxF00e12u+twHJF41e RY6EbKxyqlqH7Tqn5k2eU/W0pOWZhV+VBHbVLsZe+DPtkikJMl64u0W3hsXHnHtXU7p1 pmM1ya6Q2M6JuVLG2yci/TYEF3iIQ6UlMzNKbk3fpfaAmUb9g0v0VbgbLanD4Bp3EC3Y uuyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=arWHXZecimKgj2NgIGw11Rnj1QWxgCMT7NM1/SwZkCE=; b=4zB1Urf+eERLfcGhiwvPu4IhwnpluPMDls7VJbT886O1UsodEXVoQ+EXUwEA219mqG nu2GstXqHp4Mg1zCSZXNDZVAqyEx/WlmcgBe+e5z5VCJ67kt0baZiZLAhBi7h2FcJ8BK BCl1rTLPf5RLbRO1dRmL3TG+W1pLlex7HezhCrhngvTiHpK+XquJUI9QBnS8VAjJ85wo LWLNJopGxXBR9MDkHJpWMCvIfhg4bpFl7bVroT3K1PQHwQ9nmk0oxoo5Cp+wcA7ciSiK 4wzxWopoVY30KU/15GaFJ+hYq4wiyDGMmKTZx+uWu9Jvn0QoYB7vE7aeYamfQTLUJTUT rYGQ== X-Gm-Message-State: AO0yUKUml3ciRUXLUWnGALmsEBC/hx4aG4VZ9tDlTq0lGVlU9PZTVmOL YQsFpNLvJwgzzUEaLfDyiTVZHPgN9THZqEN0oQT3Mvp8eaYMDb25F7CAfR1MKP0lf7RADT35H6T bS45iNW+s/pxYoMr528oXdpmYJSfytgA2fH4wwWmShSWO8FjHUiG5yWh0SbuE5QjjoQOgvh5lJF 8YQl31tUURh8MGcHMMwroD0OruW+4JBJtLES3f X-Google-Smtp-Source: AK7set+sCQ081afqj0zqh0VNRJtcD/aqqov7nh1RmeqpCyUroEhfDr2ntAutnLk9UjsDi2TvMubVBw== X-Received: by 2002:a05:600c:3d95:b0:3e8:35a2:6abf with SMTP id bi21-20020a05600c3d9500b003e835a26abfmr3533774wmb.23.1677068549089; Wed, 22 Feb 2023 04:22:29 -0800 (PST) Received: from [10.137.0.17] (static-84-42-175-93.bb.vodafone.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id z6-20020adfe546000000b002425be3c9e2sm6893653wrm.60.2023.02.22.04.22.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Feb 2023 04:22:28 -0800 (PST) Message-ID: Date: Wed, 22 Feb 2023 13:22:27 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: Performance issues with parallelism and LIMIT Content-Language: en-US To: David Geier , PostgreSQL-development Cc: Robert Haas , dilipbalaut@gmail.com References: <2c4686b2-635b-8cb8-8f22-03e1fa336f14@gmail.com> <49ce2f52-e80b-c2ff-35e7-1485f22c5441@enterprisedb.com> <2d62033f-9475-1e4e-14e0-76e4b7b9b552@gmail.com> From: Tomas Vondra In-Reply-To: <2d62033f-9475-1e4e-14e0-76e4b7b9b552@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/20/23 19:18, David Geier wrote: > Hi, > > On 2/8/23 11:42, Tomas Vondra wrote: >> On 2/1/23 14:41, David Geier wrote: >> >> Yeah, this is a pretty annoying regression. We already can hit poor >> behavior when matching rows are not distributed uniformly in the tables >> (which is what LIMIT costing assumes), and this makes it more likely to >> hit similar issues. A bit like when doing many HTTP requests makes it >> more likely to hit at least one 99% outlier. > Are you talking about the use of ordering vs filtering indexes in > queries where there's both an ORDER BY and a filter present (e.g. using > an ordering index but then all rows passing the filter are at the end of > the table)? If not, can you elaborate a bit more on that and maybe give > an example. Yeah, roughly. I don't think the explicit ORDER BY is a requirement for this to happen - it's enough when the part of the plan below LIMIT produces many rows, but the matching rows are at the end. >> No opinion on these options, but worth a try. Alternatively, we could >> try the usual doubling approach - start with a low threshold (and set >> the latch frequently), and then gradually increase it up to the 1/4. >> >> That should work both for queries expecting only few rows and those >> producing a lot of data. > > I was thinking about this variant as well. One more alternative would be > latching the leader once a worker has produced 1/Nth of the LIMIT where > N is the number of workers. Both variants have the disadvantage that > there are still corner cases where the latch is set too late; but it > would for sure be much better than what we have today. > > I also did some profiling and - at least on my development laptop with 8 > physical cores - the original example, motivating the batching change is > slower than when it's disabled by commenting out: > >     if (force_flush || mqh->mqh_send_pending > (mq->mq_ring_size >> 2)) > > SET parallel_tuple_cost TO 0; > CREATE TABLE b (a int); > INSERT INTO b SELECT generate_series(1, 200000000); > ANALYZE b; > EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM b; > >  Gather  (cost=1000.00..1200284.61 rows=200375424 width=4) (actual > rows=200000000 loops=1) >    Workers Planned: 7 >    Workers Launched: 7 >    ->  Parallel Seq Scan on b  (cost=0.00..1199284.61 rows=28625061 > width=4) (actual rows=25000000 loops=8) > > Always latch: 19055 ms > Batching:     19575 ms > > If I find some time, I'll play around a bit more and maybe propose a patch. > OK. Once you have a WIP patch maybe share it and I'll try to do some profiling too. >>> ... >>> >>> We would need something similar to CHECK_FOR_INTERRUPTS() which returns >>> a NULL slot if a parallel worker is supposed to stop execution (we could >>> e.g. check if the queue got detached). Or could we amend >>> CHECK_FOR_INTERRUPTS() to just stop the worker gracefully if the queue >>> got detached? >>> >> That sounds reasonable, but I'm not very familiar the leader-worker >> communication, so no opinion on how it should be done. > > I think an extra macro that needs to be called from dozens of places to > check if parallel execution is supposed to end is the least preferred > approach. I'll read up more on how CHECK_FOR_INTERRUPTS() works and if > we cannot actively signal the workers that they should stop. > IMHO if this requires adding another macro to a bunch of ad hoc places is rather inconvenient. It'd be much better to fix this in a localized manner (especially as it seems related to a fairly specific place). regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company