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 1rfqmq-002hp6-Ae for pgsql-hackers@arkaria.postgresql.org; Fri, 01 Mar 2024 00:30:04 +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 1rfqmo-00A24M-FV for pgsql-hackers@arkaria.postgresql.org; Fri, 01 Mar 2024 00:30:02 +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 1rfqmo-00A24E-1q for pgsql-hackers@lists.postgresql.org; Fri, 01 Mar 2024 00:30:02 +0000 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rfqmk-002ExM-Ia for pgsql-hackers@postgresql.org; Fri, 01 Mar 2024 00:30:01 +0000 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-29b10d73630so909083a91.0 for ; Thu, 29 Feb 2024 16:29:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709252998; x=1709857798; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=OQ9HzG6mrOBif1XyqcKgeCBZnWEOBpCruWbHw38+OzM=; b=QXNFbYLx5wAKfvC0gCX/HwvO2vpJCWm39GejARmi9zR2qAlqWkNaclGQ/+i31bQDQA qdJZl+IAqd0h5Cwgomfdgg/YDiza+MWDPgwjnMQ3T2KPIrzC4ecdNIlenhM5P49YZKn9 QKal5X9h49pgymFT3Ip2bjiq2KPxWxB8NeLdAMX/YuWZzuib+yje+CibhEyCB5DbaCi9 j/Nah1uH1+gk6s2VE/zLHtQgsxO5QbRsDz8uzo4/uC0vb8mV/2L4omttbwKBhaxOv31y S0w20boVUjvi+KRZSEKHCXgzVkTp3yLVfPEbUFgBr0Tl38mjJ7Ki8QUT8Lz2BoCSonC6 p9Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709252998; x=1709857798; h=content-transfer-encoding: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=OQ9HzG6mrOBif1XyqcKgeCBZnWEOBpCruWbHw38+OzM=; b=YZNwQYwKYBoYahJEfLKw9sTdLPIIvBLeolbrwicgsm49wrH/vth6BAYqSKmd3qDE93 wT9BFNDhz4C6QXymKROQhtPYm5BdnoALS46lv78Qw5sOox57LGzHhhfOcxNizyjFtGjE FzNPYrdp5SemqKQ/9x6025bdBYKm6Wo4M4387tixtnkmKTrfUQjrYHELadGbG/G1qCM5 To0LJiBqvRJZkAVL5VpZ5ZIotd14pZQrufRcgtTR4/QOMg2clnolR0s+y08Q1OxBB6LP 9lda0ErOgi0lHDw0FITwTUW96f5DteW/+Yr7jhFXdTVqEKmN54uTBinc0Fdr4z26lzgb lypw== X-Forwarded-Encrypted: i=1; AJvYcCWrIryicDzRC1JtlV+uVUuaLB+Exk3VW1XSpYnZ39SQ8hc8p6y1/17H0N3auMm8eogixL64b6NWOQcKxwvnMbwOyQtwkqTErQAgHCWJ X-Gm-Message-State: AOJu0Yxuv8FOUEQw2jEdx4XGfVSxtXu86e7nEPugHTOSEULk5nr7X0cu dvnEdia21LDYFBgd1RSMWljgElnWHVT4YmwJzZ7CegJmBFmDPyEVzA98WCDWz0tDkYJs4hnE21I 9aEG6foAtiP5kkHJvNuctNZozCLM= X-Google-Smtp-Source: AGHT+IHHSCkXAdkQNJgD+0+tFMuW+iu4oKKpamy/cWaynRtFts3DeeFhHb6Bpl5uAxZO1QGXXsmJtmpzYi/5+FYHH9E= X-Received: by 2002:a17:90a:7504:b0:299:35a1:f716 with SMTP id q4-20020a17090a750400b0029935a1f716mr212226pjk.12.1709252997856; Thu, 29 Feb 2024 16:29:57 -0800 (PST) MIME-Version: 1.0 References: <20240214194228.jf4o7luxtyra5ggv@awork3.anarazel.de> <20240216173559.xiy5xcl5dqmsprns@liskov> <20240227015028.knohvy3spaqwk7lf@liskov> <20240227142230.nu3ytvcjwouvczlt@liskov> <40c213cc-6a15-4e2c-9e56-dae344d52a73@enterprisedb.com> <4b751bc5-446f-4357-a1e1-827585bf22b0@enterprisedb.com> <45bed4f3-a5bf-4a34-b544-7e751bd437e1@enterprisedb.com> <91090d58-7d3f-4447-9425-f24ba66e292a@enterprisedb.com> <8de6f6d7-9b9d-47a4-98cc-84bf31a314db@enterprisedb.com> In-Reply-To: <8de6f6d7-9b9d-47a4-98cc-84bf31a314db@enterprisedb.com> From: Melanie Plageman Date: Thu, 29 Feb 2024 19:29:45 -0500 Message-ID: Subject: Re: BitmapHeapScan streaming read user and prelim refactoring To: Tomas Vondra Cc: Andres Freund , Pg Hackers , Thomas Munro , Heikki Linnakangas , Nazir Bilal Yavuz Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Feb 29, 2024 at 5:44=E2=80=AFPM Tomas Vondra wrote: > > > > On 2/29/24 22:19, Melanie Plageman wrote: > > On Thu, Feb 29, 2024 at 7:54=E2=80=AFAM Tomas Vondra > > wrote: > >> > >> > >> > >> On 2/29/24 00:40, Melanie Plageman wrote: > >>> On Wed, Feb 28, 2024 at 6:17=E2=80=AFPM Tomas Vondra > >>> wrote: > >>>> > >>>> > >>>> > >>>> On 2/28/24 21:06, Melanie Plageman wrote: > >>>>> On Wed, Feb 28, 2024 at 2:23=E2=80=AFPM Tomas Vondra > >>>>> wrote: > >>>>>> > >>>>>> On 2/28/24 15:56, Tomas Vondra wrote: > >>>>>>>> ... > >>>>>>> > >>>>>>> Sure, I can do that. It'll take a couple hours to get the results= , I'll > >>>>>>> share them when I have them. > >>>>>>> > >>>>>> > >>>>>> Here are the results with only patches 0001 - 0012 applied (i.e. w= ithout > >>>>>> the patch introducing the streaming read API, and the patch switch= ing > >>>>>> the bitmap heap scan to use it). > >>>>>> > >>>>>> The changes in performance don't disappear entirely, but the scale= is > >>>>>> certainly much smaller - both in the complete results for all runs= , and > >>>>>> for the "optimal" runs that would actually pick bitmapscan. > >>>>> > >>>>> Hmm. I'm trying to think how my refactor could have had this impact= . > >>>>> It seems like all the most notable regressions are with 4 parallel > >>>>> workers. What do the numeric column labels mean across the top > >>>>> (2,4,8,16...) -- are they related to "matches"? And if so, what doe= s > >>>>> that mean? > >>>>> > >>>> > >>>> That's the number of distinct values matched by the query, which sho= uld > >>>> be an approximation of the number of matching rows. The number of > >>>> distinct values in the data set differs by data set, but for 1M rows > >>>> it's roughly like this: > >>>> > >>>> uniform: 10k > >>>> linear: 10k > >>>> cyclic: 100 > >>>> > >>>> So for example matches=3D128 means ~1% of rows for uniform/linear, a= nd > >>>> 100% for cyclic data sets. > >>> > >>> Ah, thank you for the explanation. I also looked at your script after > >>> having sent this email and saw that it is clear in your script what > >>> "matches" is. > >>> > >>>> As for the possible cause, I think it's clear most of the difference > >>>> comes from the last patch that actually switches bitmap heap scan to= the > >>>> streaming read API. That's mostly expected/understandable, although = we > >>>> probably need to look into the regressions or cases with e_i_c=3D0. > >>> > >>> Right, I'm mostly surprised about the regressions for patches 0001-00= 12. > >>> > >>> Re eic 0: Thomas Munro and I chatted off-list, and you bring up a > >>> great point about eic 0. In old bitmapheapscan code eic 0 basically > >>> disabled prefetching but with the streaming read API, it will still > >>> issue fadvises when eic is 0. That is an easy one line fix. Thomas > >>> prefers to fix it by always avoiding an fadvise for the last buffer i= n > >>> a range before issuing a read (since we are about to read it anyway, > >>> best not fadvise it too). This will fix eic 0 and also cut one system > >>> call from each invocation of the streaming read machinery. > >>> > >>>> To analyze the 0001-0012 patches, maybe it'd be helpful to run tests= for > >>>> individual patches. I can try doing that tomorrow. It'll have to be = a > >>>> limited set of tests, to reduce the time, but might tell us whether = it's > >>>> due to a single patch or multiple patches. > >>> > >>> Yes, tomorrow I planned to start trying to repro some of the "red" > >>> cases myself. Any one of the commits could cause a slight regression > >>> but a 3.5x regression is quite surprising, so I might focus on trying > >>> to repro that locally and then narrow down which patch causes it. > >>> > >>> For the non-cached regressions, perhaps the commit to use the correct > >>> recheck flag (0004) when prefetching could be the culprit. And for th= e > >>> cached regressions, my money is on the commit which changes the whole > >>> control flow of BitmapHeapNext() and the next_block() and next_tuple(= ) > >>> functions (0010). > >>> > >> > >> I do have some partial results, comparing the patches. I only ran one = of > >> the more affected workloads (cyclic) on the xeon, attached is a PDF > >> comparing master and the 0001-0014 patches. The percentages are timing > >> vs. the preceding patch (green - faster, red - slower). > > > > Just confirming: the results are for uncached? > > > > Yes, cyclic data set, uncached case. I picked this because it seemed > like one of the most affected cases. Do you want me to test some other > cases too? So, I actually may have found the source of at least part of the regression with 0010. I was able to reproduce the regression with patch 0010 applied for the unached case with 4 workers and eic 8 and 100000000 rows for the cyclic dataset. I see it for all number of matches. The regression went away (for this specific example) when I moved the BitmapAdjustPrefetchIterator call back up to before the call to table_scan_bitmap_next_block() like this: diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c index f7ecc060317..268996bdeea 100644 --- a/src/backend/executor/nodeBitmapHeapscan.c +++ b/src/backend/executor/nodeBitmapHeapscan.c @@ -279,6 +279,8 @@ BitmapHeapNext(BitmapHeapScanState *node) } new_page: + BitmapAdjustPrefetchIterator(node, node->blockno); + if (!table_scan_bitmap_next_block(scan, &node->recheck, &lossy, &node->blockno)) break; @@ -287,7 +289,6 @@ new_page: else node->exact_pages++; - BitmapAdjustPrefetchIterator(node, node->blockno); /* Adjust the prefetch target */ BitmapAdjustPrefetchTarget(node); } It makes sense this would fix it. I haven't tried all the combinations you tried. Do you mind running your tests with the new code? I've pushed it into this branch. https://github.com/melanieplageman/postgres/commits/bhs_pgsr/ Note that this will fix none of the issues with 0014 because that has removed all of the old prefetching code anyway. Thank you sooo much for running these to begin with and then helping me figure out what is going on! - Melanie