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.96) (envelope-from ) id 1vsRUt-001NwO-2Z for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Feb 2026 20:16:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vsRUs-00By1m-3A for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Feb 2026 20:16:39 +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.96) (envelope-from ) id 1vsRUs-00By1Y-1r for pgsql-hackers@lists.postgresql.org; Tue, 17 Feb 2026 20:16:38 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vsRUq-00000001Fak-0kCJ for pgsql-hackers@lists.postgresql.org; Tue, 17 Feb 2026 20:16:38 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-43626796202so4262653f8f.3 for ; Tue, 17 Feb 2026 12:16:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1771359394; cv=none; d=google.com; s=arc-20240605; b=hQgbgt5vKVV3hy7tGvN1YmkcdMja200RKMNpoKFGX1rk6xZra29mGwbvOIQawfehDu k3+ApfLK5iMwIekfKsZyRQU//L7UYlkLZ52wu4tNxNUhA5KcHhOQxA/fmwnB8Vk2OZua ih7ozVjyv7QzGWy+AhdSH0WYV62MPPonbX35sUilz/BTLL49+f5y7OE5lZy4OALbj7Do 9d42yLSyc7FxY8+XhAs8mVNTMkpPM+mC/htrcvuv/ya6R50kkyvC6s6+INDp4g0k20he 02h91j2Xm3/Xpq7Z0w7ncCkxgkVEJdqshrsHswqxOZUo1l/lCCV4w8mcXjEIg4q3cqHQ ziag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=nwlz+jpXRbzCsEo52z+86F5JLfgD9sg6kIIEXAf7mlQ=; fh=9ivMf9xLe6qnHW4Ztw5lPUasnuBiyYPxLc0C1gsYp7E=; b=Ow5wenwpM/BLRNzd+stjp0TdqEfcJ/45qqvQ0PqZvbsIBekXoWbFShogXq4t/ch7Jz SwShUj/ho9YITg52pO+jxZHEm4BjvLZlBCUJZB/hbX0BUDUMNY3OMAo0o3UBZ+aBHUvY SONSKMrUdOEGat3ugeukhBnjCey7bU9RnfyZnpwcpE/gokK9oqTFpSkFEvGsDARBky+z tW4LtatkgbklElNtLCp9EAhyPODQmfzqICdIdshUDZrfHMmdJKcbjt2ul1QrfePC4s4O eFW6iQE4JFr4Jbt8dE6oCcYPYVP73CsmZnHaLJ7UJvAnSJ5ZEbLPL4LWQ+d3Kk/PyxE3 8GfA==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bowt-ie.20230601.gappssmtp.com; s=20230601; t=1771359394; x=1771964194; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=nwlz+jpXRbzCsEo52z+86F5JLfgD9sg6kIIEXAf7mlQ=; b=zg/XrhFJ5QHnEep0uIjRG2z3M76xuaU54aLD/5x5ovuurbkIXjj5mW3ybRKw5T+4WH YRW3ooCMhgA1ZjkKnBci/xHumNscqecYsMfmXZUtlJBFOeAkkBBs0AoZGLHvWDs/FYjt Z2Er1yH4QJp14yFKQc+YFKl/XguwVjjXfKGG+LWJ8RbfqH36YnUEZFA/j3G7yq3/UmVp p8c9lN8aTAvBxWL/jQsdFP7AJgyz/E+kfdKBWpsOAtYJPjRtZHCCVM985XUm4qgTbz2F 0faGS9F6aAsiPUVssC7cPNUj0J6s/qTSOwovZZD34pDXXbXzr3n5mzw4ho7sRvuLN0r7 xsyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771359394; x=1771964194; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=nwlz+jpXRbzCsEo52z+86F5JLfgD9sg6kIIEXAf7mlQ=; b=oFLi3rkJB0EhCSXgfyCpSxz3t72dh4wovQaaF09QKAK/W+RzVvlDPL0M2Z9cLQDxk+ E9aBSCAsgjN3sROPwjunsE3lEY2+paIw6z3mlM1rZ8ijpiC1TZjEFtIBRi8P7v1f2FiB BEfxUkxJ1/Lwsp0E4PNNlrdCi6pLeSQGV9d3vsFktTNiH0FPu8yFDYHSdngp6i2FV+Sn xnIdlCrn+3J0BBigw6kaKqPKkz77+z/BrCUCkfG22yumvWmBRr/v/a1139ywwUSxUimE Dcc7d+wr5XDY7Gem9tLBZTzApbSEYVXCHTUbWculRhqYwf2oWl/I5hkZk8sDAslAszap 6M+g== X-Forwarded-Encrypted: i=1; AJvYcCXD33tvN6nH4J7F6SCwar31SAXopVrI9TKFZ6IvXGYtxWAbfmj6wQWHLcQeHPdxZUej5c3TNJlFmWujmkor@lists.postgresql.org X-Gm-Message-State: AOJu0YwSCz2QgnZJ2DpNzaCF71XMoYNTTUHRLEpsk8CrEF+Kp/n65jLc 09j+NtmjcnhjLuxCPfaHY/vu4NgSlMeZdrWK/yYH8p8pzkfdIkeXbvad6VWbxdapuss8qy+q/oW dGhNBo51cRMKhuBLxNJCXzGhOHYLiSXbbP/0irtgP4w== X-Gm-Gg: AZuq6aKGnTuEca4WtbmbkD/WDelXzAs4LinFGF4e3HPKYUxN/TCLYgFWrQNQPBSyJ0u CE4+rGLgHlUgFSMI49h/CuCECW9sorfA4XZR9SGuH80P/rrNjKkOyB4cMsij4tPs8df+VPVpmbr V7hK7Xoz/BElxrvQNfkmxkeaoYBxR9mcF8o7cMnX+ejOXdcawF/eivdPD+9x0r0ADKbUcacAQLM 4R5ixcqaxOeUhJioFUKdG4JNQ6JSTRfG9BUjchUZEFdgt32Rg2ZPYSJcPhF5KQzmiGO4Ey2Ydbb IVvVetc= X-Received: by 2002:a05:6000:208a:b0:433:1d30:44c with SMTP id ffacd0b85a97d-4379792bf14mr26151665f8f.43.1771359394107; Tue, 17 Feb 2026 12:16:34 -0800 (PST) MIME-Version: 1.0 References: <64a2re223ajj4popowsyu4xekbuvvyfwkrihn5yzyrkwsmsuvp@2lls3tpww5dl> <52512325-b1f2-4fff-819e-f68122b2e427@vondra.me> <64mfcfv7iihc4pmqlxarii4esnmqry52ckz5m7lmwylnfnuxuz@oxh4ioxkjtep> In-Reply-To: <64mfcfv7iihc4pmqlxarii4esnmqry52ckz5m7lmwylnfnuxuz@oxh4ioxkjtep> From: Peter Geoghegan Date: Tue, 17 Feb 2026 15:16:06 -0500 X-Gm-Features: AaiRm50eS-1VADFZpFKCwz1R5EedHDn3IW_yvRKLRkA_ljgIYjEXaQKD1iEBsb4 Message-ID: Subject: Re: index prefetching To: Andres Freund Cc: Tomas Vondra , Alexandre Felipe , Thomas Munro , Nazir Bilal Yavuz , Robert Haas , Melanie Plageman , PostgreSQL Hackers , Georgios , Konstantin Knizhnik , Dilip Kumar Content-Type: multipart/mixed; boundary="00000000000074b58d064b0abf03" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000074b58d064b0abf03 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 17, 2026 at 2:27=E2=80=AFPM Andres Freund = wrote: > On 2026-02-17 12:16:23 -0500, Peter Geoghegan wrote: > > On Mon, Feb 16, 2026 at 11:48=E2=80=AFAM Andres Freund wrote: > > I agree that the current heuristics (which were invented recently) are > > too conservative. I overfit the heuristics to my current set of > > adversarial queries, as a stopgap measure. > > Are you doing any testing on higher latency storage? I found it to be qu= ite > valuable to use dm_delay to have a disk with reproducible (i.e. not cloud= ) > higher latency (i.e. not just a local SSD). I sometimes use dm_delay (with the minimum 1ms delay) when testing, but don't do so regularly. Just because it's inconvenient to do so (perhaps not a great reason). > Low latency NVMe can reduce the > penalty of not enough readahead so much that it's hard to spot problems..= . I'll keep that in mind. > > ISTM that we need the yields to better cooperate with whatever's > > happening on the read stream side. > > Plausible. It could be that we could get away with controlling the rampu= p to > be slower in potentially problematic cases, without needing the yielding,= but > not sure. > > If that doesn't work, it might just be sufficient to increase the number = of > batches that trigger yields as the scan goes on (perhaps by taking the nu= mber > of already "consumed" batches into account). It could make sense to take the number of consumed batches into account. In general, I think the best approach will be one that combines multiple complementary strategies. Passing down a LIMIT N hint has proven to be a good idea -- and it doesn't really require applying any information related to the read stream. That's enough to prevent problems in the really extreme cases (e.g., nested loop antijoins with a LIMIT 1 on the inner side). The problematic merge join I showed you is a not-so-extreme case, which makes it trickier. ISTM that taking into consideration the number of "consumed" batches will not help that particular merge join query, precisely because it's not-so-extreme: the inner index scan consumes plenty of batches, but is nevertheless significantly regressed (at least when we don't yield at all). > To evaluate the amount of wasted work, it could be useful to make the rea= d > stream stats page spit out the amount of "unconsumed" IOs at the end of t= he > scan. That would make sense. You can already tell when that's happened by comparing the details shown by EXPLAIN ANALYZE against the same query execution on master, but that approach is inconvenient. Automating my microbenchmarks has proven to be important with this project. There's quite a few competing considerations, and it's too easy to improve one query at the cost of regressing another. > > The main motivation for yielding is to deal with things like merge > > joins fed by at least one plain index scan, and plain scans for an > > "ORDER BY .... LIMIT N" query. > > Would be good to document why the yielding exists more extensively in the > comment above it... I agree that the comments aren't fully worked out. Mostly because my understanding of what's going on here isn't fully worked out. > > I attach an example of where disabling the yield mechanism hurts > > instead of helping, to give you a sense of the problems in this area. > > What data/schema is that? Looks kinda but not really TPC-H like. Attached SQL script generates the same test data. There is a dimension table, which might make it similar to TPC-H, though that wasn't really intentional. > I assume that there are no mark & restores in the query, given that presu= mably > the inner side is unique? Right; this particular query doesn't use mark and restore. I do have another test query that does use mark and restore (a self-join + range conditions), but so far that doesn't seem to be too much of a problem. We have to reset the read stream when we restore a mark, which creates noticeable overhead. But (it seems) usually not enough added overhead for it to really matter. FWIW when the inner side of a merge join is an index-only scan, and we have to mark + restore a lot, the patch is quite a lot faster than master -- even when everything is cached. We don't have to repeatedly do the same VM lookups on the inner side (we can just use our local VM cache instead). --=20 Peter Geoghegan --00000000000074b58d064b0abf03 Content-Type: application/octet-stream; name="generate_benchmark_data.sql" Content-Disposition: attachment; filename="generate_benchmark_data.sql" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_mlr0zctu0 LS0gR2VuZXJhdGUgZGF0YSBmb3IgdGhlIGRlZmF1bHQgYmVuY2htYXJrIHN1aXRlIGluIHByZWZl dGNoX2JlbmNobWFyay5weS4KClx0aW1pbmcgb24KCi0tIERyb3AgZXhpc3RpbmcgdGFibGVzCkRS T1AgVEFCTEUgSUYgRVhJU1RTIHByZWZldGNoX29yZGVycyBDQVNDQURFOwpEUk9QIFRBQkxFIElG IEVYSVNUUyBwcmVmZXRjaF9jdXN0b21lcnMgQ0FTQ0FERTsKRFJPUCBUQUJMRSBJRiBFWElTVFMg cHJlZmV0Y2hfcHJvZHVjdHMgQ0FTQ0FERTsKRFJPUCBUQUJMRSBJRiBFWElTVFMgcHJlZmV0Y2hf c2VxdWVudGlhbCBDQVNDQURFOwpEUk9QIFRBQkxFIElGIEVYSVNUUyBwcmVmZXRjaF9zcGFyc2Ug Q0FTQ0FERTsKCi0tIE1haW4gZmFjdCB0YWJsZTogfjUwTSByb3dzIHdpdGggbG93IGZpbGxmYWN0 b3IKQ1JFQVRFIFVOTE9HR0VEIFRBQkxFIHByZWZldGNoX29yZGVycyAoCiAgb3JkZXJfaWQgYmln aW50LAogIGN1c3RvbWVyX2lkIGludCwKICBwcm9k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