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 1sivot-00BGW1-B5 for pgsql-hackers@arkaria.postgresql.org; Tue, 27 Aug 2024 13:01:11 +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 1sivns-007BwK-RC for pgsql-hackers@arkaria.postgresql.org; Tue, 27 Aug 2024 13:00:09 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sivns-007Bvf-Cs for pgsql-hackers@lists.postgresql.org; Tue, 27 Aug 2024 13:00:09 +0000 Received: from mail-lf1-x12c.google.com ([2a00:1450:4864:20::12c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sivnq-001gd2-8j for pgsql-hackers@postgresql.org; Tue, 27 Aug 2024 13:00:07 +0000 Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-533488ffaf7so7354018e87.0 for ; Tue, 27 Aug 2024 06:00:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724763604; x=1725368404; darn=postgresql.org; 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=joiabU8qZRfSmaccCEzubpwS+Rg2b0Ki3Hxany3IXuA=; b=Owa10i1jpr49j+FNWNmlXjqAm54Zt7sy0APyNBD2VRotm+qD1MRitlSEdx8l9tNFZK e4C0wj0m3zOlNxqv2OLKiCdtwP7FOym4YYCLsFR2AJK1WWGN/c4YGcnImzs5277OPqkN oGTkhDL/U77XqVAz0oxzLJ3XGpyVg93g7Qyqv1jzKILtkmwfpQxUy3aNZvTzGMqucKpp iO5sMAmPH+amenkG4nRv2FFFrm7p8q5TsrUmeujS76FBK1q5bBiw4D0MOCnA0yXVgUgD ecR+wjw2KpvHr/0Jv3ZJBXeQssy9+BvAk2FcNDhpkai5gsl5FoAS6dKithBezFbqcpo5 WXTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724763604; x=1725368404; 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=joiabU8qZRfSmaccCEzubpwS+Rg2b0Ki3Hxany3IXuA=; b=Yo8SvhQjqUfBH+VjBd0uZrmsHOihbrYQsow8Qkh2HAQjxWxpLc+G+w3Uv8tAIXWoYd 0csYZ3eAaiQueHKne+ni/HQ9sKwC1u+4BfYeGzdhWq6Q7Ky5NXKCx+uGmg1ESP5HNWsM XPpJfsFjRvnZ8xveWThrA+w4BwqRgd73BXMx2jdCuwuP369ApyDt7RIpuzwiIP4AGhZl 5WZx7oTqzARJlt/H3bd6LAXMX7FxsGcRnU/GEdVqr9x2dsFza0X8A2prBA7lAvR3oyyF iJg4jSjA3sLltQ6/Bm04gwJF5WXRM1xBU/k/ldA1ovtW/jVGc73YLpcqkzYnQ42emhHf atGg== X-Forwarded-Encrypted: i=1; AJvYcCVFaR+/0gvT2MBzPJgqEUFWxC9P5r/NJ7Eps7cmfDmHr4bdTv/mlaRzSMpWNKLVSY8b3ZQrKcfwZguWqFW8@postgresql.org X-Gm-Message-State: AOJu0YxsRRLrLHT0KojS2D+SN7A/Znw5kGL5TwBvE5tkooJ9ijgSEl0x xAmWxa19AELmj98YQYh094oeDJyvB92xZpIsurb74a58fkX0V76E X-Google-Smtp-Source: AGHT+IFtMz5JSzRPT2EKdNQkcW6FssJ/YaFVosCAjzeY87GoBCUpTMnLkCXeM7bFOTCLxqYksUD/HA== X-Received: by 2002:a2e:612:0:b0:2f3:f5f9:f68f with SMTP id 38308e7fff4ca-2f514a25ad3mr19030171fa.20.1724763602933; Tue, 27 Aug 2024 06:00:02 -0700 (PDT) Received: from [1.0.0.7] ([91.185.76.193]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2f40484b57dsm15529011fa.80.2024.08.27.06.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 27 Aug 2024 06:00:02 -0700 (PDT) Message-ID: <7e090ca5-1d3d-b4f3-9060-8ec88f444c6c@gmail.com> Date: Tue, 27 Aug 2024 16:00:00 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: Streaming read-ready sequential scan code Content-Language: en-US To: Thomas Munro , Melanie Plageman Cc: Andres Freund , David Rowley , Heikki Linnakangas , Pg Hackers References: <20240407221229.upgvhyf2gzflcawi@awork3.anarazel.de> <63a63690-dd92-c809-0b47-af05459e95d1@gmail.com> From: Alexander Lakhin In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Thomas, 27.08.2024 09:52, Thomas Munro wrote: > Here's a really simple way to see the new unfairness at the end of a > parallel scan: > > drop table if exists t; > create table t (i int); > insert into t select generate_series(1, 100000); > alter table t set (parallel_workers = 2); > set parallel_setup_cost = 0; > set parallel_leader_participation = off; > explain (analyze, buffers, verbose) select count(*) from t; > > On my machine, unpatched master shows: > > Worker 0: actual time=0.036..12.452 rows=51076 loops=1 > Buffers: shared hit=226 > Worker 1: actual time=0.037..12.003 rows=48924 loops=1 > Buffers: shared hit=217 > > The attached patch, which I'd like to push, is effectively what > Alexander tested (blocknums[16] -> blocknums[1]). There's no point in > using an array of size 1, so I've turned it into a simple variable and > deleted the relevant comments. My machine shows: > > Worker 0: actual time=0.038..12.115 rows=49946 loops=1 > Buffers: shared hit=221 > Worker 1: actual time=0.038..12.109 rows=50054 loops=1 > Buffers: shared hit=222 > > That difference may not seem huge, but other pre-existing things are > going pathologically wrong in the reported query that magnify it (see > my earlier analysis). It's an interesting problem that will require > more study (my earlier analysis missed a detail that I'll write about > separately), but it doesn't seem to be new or have easy fixes, so that > will have to be for later work. I've tried your query and could not get sustainable results, unfortunately. The following script: rm -rf "$PGDATA"; initdb -D "$PGDATA" >initdb.log 2>&1 pg_ctl -s -l server.log start cat << EOF | psql | grep 'Parallel Seq Scan' -A10 | grep 'Worker ' -A1 create table t (i int); insert into t select generate_series(1, 100000); alter table t set (parallel_workers = 2); set parallel_setup_cost = 0; set parallel_leader_participation = off; explain (analyze, buffers, verbose) select count(*) from t; EOF pg_ctl -s stop gives me unstable numbers on unpatched master:                      Worker 0:  actual time=0.024..5.814 rows=51076 loops=1                        Buffers: shared hit=226                      Worker 1:  actual time=0.023..5.614 rows=48924 loops=1                        Buffers: shared hit=217 ---                      Worker 0:  actual time=0.027..5.130 rows=36612 loops=1                        Buffers: shared hit=162                      Worker 1:  actual time=0.013..5.605 rows=63388 loops=1                        Buffers: shared hit=281 ---                      Worker 0:  actual time=0.025..5.447 rows=47460 loops=1                        Buffers: shared hit=210                      Worker 1:  actual time=0.019..5.688 rows=52540 loops=1                        Buffers: shared hit=233 and also with the patch applied:                      Worker 0:  actual time=0.012..4.486 rows=55478 loops=1                        Buffers: shared hit=246                      Worker 1:  actual time=0.014..4.430 rows=44522 loops=1                        Buffers: shared hit=197 ---                      Worker 0:  actual time=0.013..4.269 rows=55822 loops=1                        Buffers: shared hit=247                      Worker 1:  actual time=0.017..4.238 rows=44178 loops=1                        Buffers: shared hit=196 ---                      Worker 0:  actual time=0.014..4.974 rows=50624 loops=1                        Buffers: shared hit=224                      Worker 1:  actual time=0.016..4.932 rows=49376 loops=1                        Buffers: shared hit=219 ---                      Worker 0:  actual time=0.012..5.459 rows=65648 loops=1                        Buffers: shared hit=291                      Worker 1:  actual time=0.022..5.109 rows=34352 loops=1                        Buffers: shared hit=152 Please correct me, if I'm doing something wrong. Best regards, Alexander