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 1rlwJC-0051Sn-K8 for pgsql-hackers@arkaria.postgresql.org; Sun, 17 Mar 2024 19:36:39 +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 1rlwJA-00CYXh-SM for pgsql-hackers@arkaria.postgresql.org; Sun, 17 Mar 2024 19:36:37 +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 1rlwJA-00CYXZ-Dy for pgsql-hackers@lists.postgresql.org; Sun, 17 Mar 2024 19:36:37 +0000 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rlwJ5-0051TF-UN for pgsql-hackers@postgresql.org; Sun, 17 Mar 2024 19:36:35 +0000 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-33ed4dd8659so1106686f8f.0 for ; Sun, 17 Mar 2024 12:36:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1710704191; x=1711308991; 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=14yN1ETxKZtpqWsTGYEo+qJmVi0I3gl0aWus+BgpKDY=; b=HjhUHkRCjAt3DGgKU7uA7IT0kSdz7GUmmJV4hFhMDMXHKX0CNwOQlc44NlLznjt1FF tHUBiZ8lDfbpucOK8NsgXYwmoNH63wJ9ZnW7hh5Mmoq9LBZowuj1OFQZkp1tBf8EdSWn BXNIIIbYnrju78etCrL+Wec9dEjDOvSDWK4/H7nHTEXbtkSLotWZhMAk+HEzOU/VfyDZ bE99oLVXps25EBKE3WK+JadN8z+oxrq8FCA9ygvmxuDvXiiDZSjmjIpZZKMY4ihyiXgp 6Lfxrwme9py8/Cs+7FnxIMD8HSY/TNhiuuvnlbhimv31KWxh0vju5v4+v8JoFp+cr5kR 1B5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710704191; x=1711308991; 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=14yN1ETxKZtpqWsTGYEo+qJmVi0I3gl0aWus+BgpKDY=; b=QlyPv9f/35AWQNJUfZGnS/DSKIUrEOBlqXaeXOrIvuGbxgcWfOg5tipVvzfBPdEV3u x7YIWeapHUkukcR0oDAg5RRL1rEa9pU1M6Tcw+vZi5cUVCkbMYkeG25C1gR93y8JbOPa kFc3Hs0wIl6wD4t3Aqq3QNRjVGQlKM7dCxBOiSoTBFbGojGZTB25R6vzJxy1sTURINDE doPfPUidY3TCSobJLZx0icS+xMaTQ2F30M1unONQTIjTvAzIgTb2hk97IkvGJhgQZpvD C8yJDuMIpma7QtNRUL3Rwkb4rGVskNk/ywNzy0l/YZpz2fBbjOXIuobAmyCbL8iBX53T 8bLQ== X-Forwarded-Encrypted: i=1; AJvYcCV1ZEDFkOxyTYVTv/snEXENQkYQk+LCA0UZbz5lrnAiHEFPLQN6XQL0oweN4lgRnQ6WAxpREn2qLnPitXV3pYfC6+R+P916MvPyF6Fu X-Gm-Message-State: AOJu0Yyrjl/wrfnzy7n37yA9nHcILmPNHUkRnpqEvIOPr7j4bnGEjHq9 sfTUILiEyb/9D6FafeJ71pYpgER2XjKxk1xiFatPyDK7kKRlqh8ooMgN7aPsYQ== X-Google-Smtp-Source: AGHT+IHaSo+VeRQmO59yJM4g0BTk303oVBggns6+y85nzSalveZ5joeNn3ZFc2tqWTuC+TO4qv+rPw== X-Received: by 2002:adf:ee4f:0:b0:33e:ceea:8299 with SMTP id w15-20020adfee4f000000b0033eceea8299mr8366472wro.12.1710704191523; Sun, 17 Mar 2024 12:36:31 -0700 (PDT) Received: from [10.137.0.18] (ip-86-49-229-30.bb.vodafone.cz. [86.49.229.30]) by smtp.gmail.com with ESMTPSA id bk28-20020a0560001d9c00b0033e22a7b3f8sm3155038wrb.75.2024.03.17.12.36.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 17 Mar 2024 12:36:31 -0700 (PDT) Message-ID: <1d9902c0-43b9-4ae7-8c19-70bab3802bd1@enterprisedb.com> Date: Sun, 17 Mar 2024 20:36:29 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BitmapHeapScan streaming read user and prelim refactoring Content-Language: en-US To: Andres Freund Cc: Melanie Plageman , 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> <20240317163809.2y6h7btfuo76r4z7@alap3.anarazel.de> From: Tomas Vondra In-Reply-To: <20240317163809.2y6h7btfuo76r4z7@alap3.anarazel.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/17/24 17:38, Andres Freund wrote: > Hi, > > On 2024-03-16 21:25:18 +0100, Tomas Vondra wrote: >> On 3/16/24 20:12, Andres Freund wrote: >>> 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. > > There's two aspects. Originally I couldn't reliably reproduce the regression > with Melanie's repro on my laptop. I finally was able to do so after I > a) changed the block device's read_ahead_kb to 0 > b) used effective_io_concurrency=1 > > That made the difference between the BitmapAdjustPrefetchIterator() locations > very significant, something like 2.3s vs 12s. > Interesting. I haven't thought about read_ahead_kb, but in hindsight it makes sense it affects these cases. OTOH I did not set it to 0 on either machine (the 6xSATA RAID0 has it at 12288, for example) and yet that's how we found the regressions. For eic it makes perfect sense that setting it to 1 is particularly vulnerable to this issue - it only takes a small "desynchronization" of the two iterators for the prefetch to "fall behind" and frequently prefetch blocks we already read. > Besides a lot of other things, I finally added debugging fprintfs printing the > pid, (prefetch, read), block number. Even looking at tiny excerpts of the > large amount of output that generates shows that two iterators were out of > sync. > Thanks. I did experiment with fprintf, but it's quite cumbersome, so I was hoping you came up with some smart way to trace this king of stuff. For example I was wondering if ebpf would be a more convenient way. > >> 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? > > I agree that the current scheme of two shared iterators being used has some > fairly fundamental raciness. But I suspect there's more than that going on > right now. > > Moving BitmapAdjustPrefetchIterator() to later drastically increases the > raciness because it means table_scan_bitmap_next_block() happens between > increasing the "real" and the "prefetch" iterators. > > An example scenario that, I think, leads to the iterators being out of sync, > without there being races between iterator advancement and completing > prefetching: > > start: > real -> block 0 > prefetch -> block 0 > prefetch_pages = 0 > prefetch_target = 1 > > W1: tbm_shared_iterate(real) -> block 0 > W2: tbm_shared_iterate(real) -> block 1 > W1: BitmapAdjustPrefetchIterator() -> tbm_shared_iterate(prefetch) -> 0 > W2: BitmapAdjustPrefetchIterator() -> tbm_shared_iterate(prefetch) -> 1 > W1: read block 0 > W2: read block 1 > W1: BitmapPrefetch() -> prefetch_pages++ -> 1, tbm_shared_iterate(prefetch) -> 2, prefetch block 2 > W2: BitmapPrefetch() -> nothing, as prefetch_pages == prefetch_target > > W1: tbm_shared_iterate(real) -> block 2 > W2: tbm_shared_iterate(real) -> block 3 > > W2: BitmapAdjustPrefetchIterator() -> prefetch_pages-- > W2: read block 3 > W2: BitmapPrefetch() -> prefetch_pages++, tbm_shared_iterate(prefetch) -> 3, prefetch block 3 > > So afaict here we end up prefetching a block that the *same process* just had > read. > Uh, that's very weird. I'd understood if there's some cross-process issue, but if this happens in a single process ... strange. > ISTM that the idea of somehow "catching up" in BitmapAdjustPrefetchIterator(), > separately from advancing the "real" iterator, is pretty ugly for non-parallel > BHS and just straight up broken in the parallel case. > Yeah, I agree with the feeling it's an ugly fix. Definitely seems more like fixing symptoms than the actual problem. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company