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 1raMP6-009OiU-Fl for pgsql-hackers@arkaria.postgresql.org; Wed, 14 Feb 2024 21:02:52 +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 1raMP4-008kig-NF for pgsql-hackers@arkaria.postgresql.org; Wed, 14 Feb 2024 21:02:50 +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 1raMP4-008kiY-70 for pgsql-hackers@lists.postgresql.org; Wed, 14 Feb 2024 21:02:50 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1raMOx-006lkv-Cx for pgsql-hackers@lists.postgresql.org; Wed, 14 Feb 2024 21:02:48 +0000 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-290d59df3f0so187835a91.2 for ; Wed, 14 Feb 2024 13:02:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707944562; x=1708549362; 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=lhfjXTQdnWwJSNBzwN7sDCKRIh4OUxUgYTs0xie7e4M=; b=RB0JLWI/dFqbF4fu81ze+TKMnDjbIigjzhiD6b5HHFbERaOI0fBkZqGpBGBvkwLrwG GqzdJ04kX8SO1HzVW7m2hjorKUwgWvnG7bfkprIIgpdeV0hbXbjRagJU7gr/ap6hLaSV 5WEHW18ghUJyn0FgY0pbiSxxG/YuUF8ue3VClpxMhOevx5FfPm9qTQhQSIkJxF0I6njF zhtCsRGtrYcwJT00eJ0LJV4wWIOtp/aDU1F5+dZnwcDte7aIIwnrmN2NfTceb3nBMxGd UHE1/UC9HRQUWtDJqoBT6lnwpzSCDsz/3RV8fAEdOvn1b3YIUk3JyqJgJDi858Yjfd+p 54CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707944562; x=1708549362; 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=lhfjXTQdnWwJSNBzwN7sDCKRIh4OUxUgYTs0xie7e4M=; b=g/gjWMq4e3F31jwOfGjf+VvQUtcBR+47MgTJBdI+eOwBI5e5CrMChiPJuHJSpmph/W jlfti82IgM4xgnEgOLi876WZ3xtSaatObhHzH8wKPuWNRxtn4fd9yp9GWjl/DfYo4UBX er3FsnGlfzhjnfbKt3NH0A8TNYS8I880BxRk2XzHxN+uyuO+QV0u3bWBhJQR3YJ+3fDx 0p8znkFJ+a4GrY3VnSJJ767GCw/TkeL/ZVTtV5/BAQvKRarVgEYIi3aziAYmsMOx3niX pt9ecvOcYv69MINMOZqWo4EqI9Vg7AOP0jNDhIU6erS4jqjcyPHg6LLa57aHaxKupkj3 rZ1A== X-Forwarded-Encrypted: i=1; AJvYcCWme8/xVaDbRRL/pQAhvdpPlYEHlqm4cvsrKo2yM4VpoH+UZMFUDua71wTgsqZyp0BUS87cm6FOcEl/XOVS1bQ38LVtS7BfwXRFoRfRHb9KI1NS X-Gm-Message-State: AOJu0YyxR54AWQhE+EJG3CcVOQb2wWG3nMtJUqSpfK+oCLS16+O6TbUP B/8dWxX8Wq/7D/pQSM2nB2/Tik6O7P13hJH/fl031dZCqSlnUVpxINDsElgJHsh/cS9p2B81Qu9 ySaisEmeA0E4k3U/vSozDjJY/tMM= X-Google-Smtp-Source: AGHT+IEDgF4XOUdg5jtG6AlrDJAl48zkVnt2joY7XMniK456FX5vUJFFZ+kVqb+Ib8wGRzFh0/fthYFzob+GLDRwBlc= X-Received: by 2002:a17:90b:1005:b0:298:f8de:449f with SMTP id gm5-20020a17090b100500b00298f8de449fmr481249pjb.12.1707944561942; Wed, 14 Feb 2024 13:02:41 -0800 (PST) MIME-Version: 1.0 References: <8ec36f51-b863-60e3-20e2-b9c981c5ce5e@enterprisedb.com> <280dc83c-a16f-4424-1319-95e7e3f798bd@enterprisedb.com> <98ba4b25-fae8-c1f4-1597-8093375a1986@enterprisedb.com> <20231221134314.wf2rs62d37u62j7t@alap3.anarazel.de> <20231221154352.ijtg6wloa3nowivh@alap3.anarazel.de> <482ec3ff-52ad-415d-96fd-f3832a894023@enterprisedb.com> <56176b8d-956c-487e-ab09-310db4581c07@enterprisedb.com> <4867452a-b853-4813-a6da-9bb06a336f8b@enterprisedb.com> <4f5f16ef-df1e-4e09-9b3f-2e0961ab5117@enterprisedb.com> In-Reply-To: From: Melanie Plageman Date: Wed, 14 Feb 2024 16:02:29 -0500 Message-ID: Subject: Re: index prefetching To: Tomas Vondra Cc: Robert Haas , Andres Freund , PostgreSQL Hackers , Georgios , Thomas Munro , Konstantin Knizhnik , Dilip Kumar , Peter Geoghegan 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 Wed, Feb 14, 2024 at 11:40=E2=80=AFAM Melanie Plageman wrote: > > On Tue, Feb 13, 2024 at 2:01=E2=80=AFPM Tomas Vondra > wrote: > > > > On 2/7/24 22:48, Melanie Plageman wrote: > > > ... > > > - switching scan directions > > > > > > If the index scan switches directions on a given invocation of > > > IndexNext(), heap blocks may have already been prefetched and read fo= r > > > blocks containing tuples beyond the point at which we want to switch > > > directions. > > > > > > We could fix this by having some kind of streaming read "reset" > > > callback to drop all of the buffers which have been prefetched which > > > are now no longer needed. We'd have to go backwards from the last TID > > > which was yielded to the caller and figure out which buffers in the > > > pgsr buffer ranges are associated with all of the TIDs which were > > > prefetched after that TID. The TIDs are in the per_buffer_data > > > associated with each buffer in pgsr. The issue would be searching > > > through those efficiently. > > > > > > > Yeah, that's roughly what I envisioned in one of my previous messages > > about this issue - walking back the TIDs read from the index and added > > to the prefetch queue. > > > > > The other issue is that the streaming read API does not currently > > > support backwards scans. So, if we switch to a backwards scan from a > > > forwards scan, we would need to fallback to the non streaming read > > > method. We could do this by just setting the TID queue size to 1 > > > (which is what I have currently implemented). Or we could add > > > backwards scan support to the streaming read API. > > > > > > > What do you mean by "support for backwards scans" in the streaming read > > API? I imagined it naively as > > > > 1) drop all requests in the streaming read API queue > > > > 2) walk back all "future" requests in the TID queue > > > > 3) start prefetching as if from scratch > > > > Maybe there's a way to optimize this and reuse some of the work more > > efficiently, but my assumption is that the scan direction does not > > change very often, and that we process many items in between. > > Yes, the steps you mention for resetting the queues make sense. What I > meant by "backwards scan is not supported by the streaming read API" > is that Thomas/Andres had mentioned that the streaming read API does > not support backwards scans right now. Though, since the callback just > returns a block number, I don't know how it would break. > > When switching between a forwards and backwards scan, does it go > backwards from the current position or start at the end (or beginning) > of the relation? Okay, well I answered this question for myself, by, um, trying it :). FETCH backward will go backwards from the current cursor position. So, I don't see exactly why this would be an issue. > If it is the former, then the blocks would most > likely be in shared buffers -- which the streaming read API handles. > It is not obvious to me from looking at the code what the gap is, so > perhaps Thomas could weigh in. I have the same problem with the sequential scan streaming read user, so I am going to try and figure this backwards scan and switching scan direction thing there (where we don't have other issues). - Melanie