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 1umfvw-001m32-IR for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Aug 2025 21:56: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 1umfvt-009yPH-20 for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Aug 2025 21:56:25 +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 1umfvs-009yP8-Oe for pgsql-hackers@lists.postgresql.org; Thu, 14 Aug 2025 21:56:24 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1umfvq-000cA5-0f for pgsql-hackers@lists.postgresql.org; Thu, 14 Aug 2025 21:56:23 +0000 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-45a1b065d59so7481035e9.1 for ; Thu, 14 Aug 2025 14:56:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bowt-ie.20230601.gappssmtp.com; s=20230601; t=1755208580; x=1755813380; darn=lists.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=FV/Uwo4DFsSd9O6imJFiOgHzP7eddNYCg2xP3/Qkny8=; b=pJ5SUdlO7pYwZMFPHJEXSh+1P2qpzZCRK+weMpj4iwTraDTmGiJGHn716LfmjrSfwt r0bFf0cAFkwyOZZHwLP9N4/6l2lpNMH64n10KFrUv7LHGSuEx5MgxC9y6q8JxSB06wHR IUuOzaxjjDfZwVBFz4wy3B9UVSuYIscSXaGYacvemkQ7sTjXZfkcFq2AYTXdufglcU05 asI7rmG27lxJ23zXs0CaAp4cFIHdYUUMP7I7Yr3FoDnHaW9MkjPmDepfc49cVRVUFKfo /kwvFBcNlJ3vS1CpahNbxdSz1tr/fHRhWUV/TUPohgXZOCGoLDsKkzmbtq/kDeMze6vA GlJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755208580; x=1755813380; 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=FV/Uwo4DFsSd9O6imJFiOgHzP7eddNYCg2xP3/Qkny8=; b=mU2Kvh/LfWCbFjzuOjGn0WyIU+Wy64amaOBaZg3faWFGByKzZuaXNnCp28j94BY4nm ajNSHuSuqmqIGOe5e9Vi6EuFIF6aaC65nxyvznkeWpYaWJqkvFa5rQMfX5j+GZEdIXzG bN+vFUJR44DtbYhg73ojsaddLVXSUrGO8758Xq+HnMmpvwidpGs+rNlWfzDA6QtM1Lc+ SEKroPkSo2pW0KL6845z/wzqce/lN1rWF/WURbZq24YiYG25HIMOGN79x26FP51kimkm mN2i4s4gtv+ZIf/kujku0Ys5zGExAA/qBdNpYFPoJUi077x3ONZUwtNAE254I3Wn+Y64 saGQ== X-Forwarded-Encrypted: i=1; AJvYcCVTHWqMcrICPT4zuRhiTNw5+9OxcSQ8atAJJNPbn1BHepxcUhtQvEW/eD1NOEL3l4gaiSzfs6Qb/FB1GYpE@lists.postgresql.org X-Gm-Message-State: AOJu0YzK95smFKu6LEqer98+nZyZdYpJcwebD3PhyFnahGbBaGQ6A9o/ oK8L1SKapPA9hS7co6nQuOB4FthvQLsilppsoVgwdcedtodN9yddho7tGRQAk/n9dLpe2mGoelV CfAG7tB+oMdphuTm79oRni1zaGCPuhGygc9RW53LtUA== X-Gm-Gg: ASbGncvafK6LyGPnTwS55MG0gSGZcw73pDFvioC4q124xRgIHjSPAK9eGq0V1a8WgvJ 7nCWRiJgY93gWBt0I63FGqhuzYu8p92/Z6suIm4JwZbCADWpkAJdUg9kTgeawAQA66V+mukT7il uNORkTsLwqJkQzQQcXyAnv47NdHJqy4vkylhO5K+f448+kwWOx12U9mPN05MYk/NyYfcwE+EFYE K9vhoQafAIrd31jsw== X-Google-Smtp-Source: AGHT+IEoBy8lMwJajjKAzI0PUZOuqh0A2nuofWuxLNLLEhlThSHI/is3itKQdNF7nEG2DN1D0JLvbP59xjrzMUlsA1k= X-Received: by 2002:a05:600c:3b93:b0:459:d709:e5c9 with SMTP id 5b1f17b1804b1-45a1b614c95mr33624155e9.6.1755208579907; Thu, 14 Aug 2025 14:56:19 -0700 (PDT) MIME-Version: 1.0 References: <7c308720-5cd4-4508-8cc0-6e55a6352731@vondra.me> <6wyxbnry2unm3kbcu2sabhzhs7baoedlg77xqm42chpofjq45g@igst42zpl7ok> In-Reply-To: From: Peter Geoghegan Date: Thu, 14 Aug 2025 17:55:53 -0400 X-Gm-Features: Ac12FXxPuigVPfZCYpEG49T0GrLzAa3G38k0rV0wEx2GZr96RpNTc_VG7VkZ500 Message-ID: Subject: Re: index prefetching To: Andres Freund Cc: Tomas Vondra , Thomas Munro , Nazir Bilal Yavuz , Robert Haas , Melanie Plageman , PostgreSQL Hackers , Georgios , Konstantin Knizhnik , Dilip Kumar 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, Aug 14, 2025 at 5:06=E2=80=AFPM Peter Geoghegan wrote: > If this same mechanism remembered (say) the last 2 heap blocks it > requested, that might be enough to totally fix this particular > problem. This isn't a serious proposal, but it'll be simple enough to > implement. Hopefully when I do that (which I plan to soon) it'll fully > validate your theory. I spoke too soon. It isn't going to be so easy, since heapam_index_fetch_tuple wants to consume buffers as a simple stream. There's no way that index_scan_stream_read_next can just suppress duplicate block number requests (in a way that's more sophisticated than the current trivial approach that stores the very last block number in IndexScanBatchState.lastBlock) without it breaking the whole concept of a stream of buffers. > > We can optimize that by deferring the StartBufferIO() if we're encounte= ring a > > buffer that is undergoing IO, at the cost of some complexity. I'm not = sure > > real-world queries will often encounter the pattern of the same block b= eing > > read in by a read stream multiple times in close proximity sufficiently= often > > to make that worth it. > > We definitely need to be prepared for duplicate prefetch requests in > the context of index scans. Can you (or anybody else) think of a quick and dirty way of working around the problem on the read stream side? I would like to prioritize getting the patch into a state where its overall performance profile "feels right". From there we can iterate on fixing the underlying issues in more principled ways. FWIW it wouldn't be that hard to require the callback (in our case index_scan_stream_read_next) to explicitly point out that it knows that the block number it's requesting has to be a duplicate. It might make sense to at least place that much of the burden on the callback/client side. -- Peter Geoghegan