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 1rlaau-003Dur-CD for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Mar 2024 20:25:28 +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 1rlaas-000Cqz-SE for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Mar 2024 20:25:27 +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 1rlaas-000CnU-H2 for pgsql-hackers@lists.postgresql.org; Sat, 16 Mar 2024 20:25:27 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rlaao-004tul-6m for pgsql-hackers@postgresql.org; Sat, 16 Mar 2024 20:25:25 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-513e14b2bd9so754288e87.2 for ; Sat, 16 Mar 2024 13:25:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1710620719; x=1711225519; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=eHe0mSyO2sklF962csXCPGukxrCDwPikWoUkCiylhCo=; b=Mh/mLo+fp4lSiOpb5XpWQsHO2ZQkwTVhhjifjPK/ZSJ0P7XHLE9VWFW7nWfJZSonbD YY0Lpmfr6AifPPRjmwcy1kSWGyXnKsvL572jGHH2Gna6UVKWfdNNqxckoBSaWMAGpqoJ ltT3QbYSWjfNc7BdkQB1NdDeRTFlO+FFjNahzIWDzdg7493AWBR34wlZVBny5hDOUWc2 byX3zsQ8Pwgq15ZuEyjm/QXznKWgwA2GhfDlFqkymwHShQri7HTJ7ygBUQe09750lPlS fBMkZhBFzjrY+axLNTy/mzuPNQNknavcY0i0Z8DdnAlykpZbcDmcGAXGB/bbomOwuDy3 NtrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710620719; x=1711225519; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eHe0mSyO2sklF962csXCPGukxrCDwPikWoUkCiylhCo=; b=DP5bXDZZvveLqzPauszPmCLKPn62QpziWfP9ncieLz1ugTBuO/2HYiSaCZBsf273fE 6ENOHDaYTRZ+Jd5rJXLttaUsQfNYewNMLZLVlvr02URpfH/F4dclXZU88oiQDdVKyS6B Z2d9L/mP5jJqpJxeauTObvtQBL8siXcjNaxjaYvl2NJu0JEZXVBG2iIVx89hChr5l/d1 VjcFTWsXrSjKnKnxv+SKUeHDRP7fPSVTE3z06M4E1L00h/hhqrUG09hiDRze6snnWJRx NJgUDPS8k27M9ou5J7DQUoUdO3vbyL8RboEHQRZ2PFNx+mkUE6IU2yUGEcNLJ8Nr8+Rw Mesw== X-Forwarded-Encrypted: i=1; AJvYcCWhdgvkPIo481v40uRp71UQRfsd0NZnzF8Z8UNH7N91DPmratWBwnQSE1FS+TG0b3tpOsflD/0g5+PMJoGlh5sseouzR4k7DQCIy0+B X-Gm-Message-State: AOJu0YyItnlxAvxlEbXMaK9H/MZ/t4jfw7OIDu4VdclKYxsl0mQghUb/ CxJd05hypmb0BIwdYI4qYmGyLWfqEq6GUu7sYlR4rlWs79If1BJqqbDteYqEwg== X-Google-Smtp-Source: AGHT+IFLKWxQJQOGmPo/akQ2bEqN8Izs2dQKYEmNCnKAMSw0VWIGzCjrnajTDsfft0Pa/gughPa1Uw== X-Received: by 2002:ac2:4e90:0:b0:513:e369:cc41 with SMTP id o16-20020ac24e90000000b00513e369cc41mr823421lfr.49.1710620719427; Sat, 16 Mar 2024 13:25:19 -0700 (PDT) Received: from [10.137.0.18] (84-217-81-189.customers.ownit.se. [84.217.81.189]) by smtp.gmail.com with ESMTPSA id h13-20020a19ca4d000000b00513a0876870sm1080244lfj.267.2024.03.16.13.25.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 16 Mar 2024 13:25:19 -0700 (PDT) Message-ID: Date: Sat, 16 Mar 2024 21:25:18 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BitmapHeapScan streaming read user and prelim refactoring To: Andres Freund , Melanie Plageman Cc: Heikki Linnakangas , Dilip Kumar , Robert Haas , Nazir Bilal Yavuz , Pg Hackers , Thomas Munro References: <20240314181625.a7uigo5ujaogfd6x@liskov> <2ed1e06b-feae-4c61-9f58-3fcc358104c7@enterprisedb.com> <20240315211449.en2jcmdqxv5o6tlz@alap3.anarazel.de> <20240316191226.b3c2vodhs23ibom4@alap3.anarazel.de> Content-Language: en-US From: Tomas Vondra In-Reply-To: <20240316191226.b3c2vodhs23ibom4@alap3.anarazel.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/16/24 20:12, Andres Freund wrote: > Hi, > > On 2024-03-15 18:42:29 -0400, Melanie Plageman wrote: >> On Fri, Mar 15, 2024 at 5:14 PM Andres Freund wrote: >>> On 2024-03-14 17:39:30 -0400, Melanie Plageman wrote: >>> I spent a good amount of time looking into this with Melanie. After a bunch of >>> wrong paths I think I found the issue: We end up prefetching blocks we have >>> already read. Notably this happens even as-is on master - just not as >>> frequently as after moving BitmapAdjustPrefetchIterator(). >>> >>> From what I can tell the prefetching in parallel bitmap heap scans is >>> thoroughly broken. I added some tracking of the last block read, the last >>> block prefetched to ParallelBitmapHeapState and found that with a small >>> effective_io_concurrency we end up with ~18% of prefetches being of blocks we >>> already read! After moving the BitmapAdjustPrefetchIterator() to rises to 86%, >>> no wonder it's slower... >>> >>> The race here seems fairly substantial - we're moving the two iterators >>> independently from each other, in multiple processes, without useful locking. >>> >>> I'm inclined to think this is a bug we ought to fix in the backbranches. >> >> Thinking about how to fix this, perhaps we could keep the current max >> block number in the ParallelBitmapHeapState and then when prefetching, >> workers could loop calling tbm_shared_iterate() until they've found a >> block at least prefetch_pages ahead of the current block. They >> wouldn't need to read the current max value from the parallel state on >> each iteration. Even checking it once and storing that value in a >> local variable prevented prefetching blocks after reading them in my >> example repro of the issue. > > That would address some of the worst behaviour, but it doesn't really seem to > address the underlying problem of the two iterators being modified > independently. ISTM the proper fix would be to protect the state of the > iterators with a single lock, rather than pushing down the locking into the > bitmap code. OTOH, we'll only need one lock going forward, so being economic > in the effort of fixing this is also important. > Can you share some details about how you identified the problem, counted the prefetches that happen too late, etc? I'd like to try to reproduce this to understand the issue better. If I understand correctly, what may happen is that a worker reads blocks from the "prefetch" iterator, but before it manages to issue the posix_fadvise, some other worker already did pread. Or can the iterators get "out of sync" in a more fundamental way? If my understanding is correct, why would a single lock solve that? Yes, we'd advance the iterators at the same time, but surely we'd not issue the fadvise calls while holding the lock, and the prefetch/fadvise for a particular block could still happen in different workers. I suppose a dirty PoC fix should not be too difficult, and it'd allow us to check if it works. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company