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 1rGL63-00EnY5-8w for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Dec 2023 15:36:27 +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 1rGL62-00AnQ5-0D for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Dec 2023 15:36:26 +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 1rGL2i-00Aics-ST for pgsql-hackers@lists.postgresql.org; Thu, 21 Dec 2023 15:33:00 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rGL2b-00DC97-Tu for pgsql-hackers@lists.postgresql.org; Thu, 21 Dec 2023 15:33:00 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a22deb95d21so111740466b.3 for ; Thu, 21 Dec 2023 07:32:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1703172772; x=1703777572; darn=lists.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=gcP4o6xd2r/9z9Rd7nhjtvfv56SV7TU4ohKyOa5ES80=; b=a/9El843RHG9d/eSjTp2vjEgmHCrHsFZcbB83xmNKE1IP/+d1ym5AxrXONb/f1XO/s zAqIyR5w7D6N/mPyfBVG5yPEKFQXoSb7PaV8WzWPn5W9L4BRtwUau2dQ4DWNJn6gM4ZT TrHYD5kryjIAWpXQvkC9nThJL1F++Ga2zRvj1twaB2qOFIW1GP7pUYMlc3SckguayPGY /iYiGkkKQEkswhjWTSNT5FKVhU7TA28HH4fpAhtf/6ygfB2A9tWWBFXQvMs0GyfCpKsj nZNJj2XWx2pQXYXCsFG+n6We3S3OoFoL7hXTdCBUzsQJbEfpbKYYrAgNe0vT7RNpNrx5 GCLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703172772; x=1703777572; 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=gcP4o6xd2r/9z9Rd7nhjtvfv56SV7TU4ohKyOa5ES80=; b=qbi4xMgvQu+/Y2UQPyFOUhpkpmU4bOkJEhz51QroE9E2tFzqiXsq09vCcpVPhBlG7z +p94r8TyIQSmzoe2qZ77LFIZOCE1R+2iCnYWfNpUfHEMfifxs0IzQNMVw5hAMOdIkOi0 jvrUlegIuNLZlokwCuwCVHj2k+DQFLJINC+gkzj9E0Xnb6y6462wCMHq3GGXci1P0u5w 0yPtamadxBxwW7ohfpbSk0Ff03WnnQU4YSQvAya2b2amxcurKU9BTZWWwohlnItjm9cF T3yddoEFru2f6p7TDWktvmU715kNeVi4UOwsnJKXMKz1TPlj/Dwt9TnLH+mn5YVhlBJK kegQ== X-Gm-Message-State: AOJu0Yz6yWV1aJhqIkIOm38PBg2ARLRb3iQHvhv0uBdBLtiHNq4vRDLN KoKTb7vBrjPneoup8f+yAQKAIQ== X-Google-Smtp-Source: AGHT+IGJCxzkBU8mx6bDO4EbZiG6QbUlnSiFCdHA0rjHE5hdtGr5IhQlP4jlH6gpnfzaOXLyl2U/Tw== X-Received: by 2002:a17:906:5352:b0:a23:8929:97c7 with SMTP id j18-20020a170906535200b00a23892997c7mr2396380ejo.24.1703172772006; Thu, 21 Dec 2023 07:32:52 -0800 (PST) Received: from [10.137.0.17] (srv1.mobissw.com. [89.235.0.226]) by smtp.gmail.com with ESMTPSA id p16-20020a170907911000b00a1b6ec7a88asm1062369ejq.113.2023.12.21.07.32.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Dec 2023 07:32:51 -0800 (PST) Message-ID: <3cfa05ce-d7f2-b2ba-313f-ed0c3c067fc4@enterprisedb.com> Date: Thu, 21 Dec 2023 16:32:51 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: index prefetching Content-Language: en-US To: Andres Freund Cc: PostgreSQL Hackers , Georgios References: <20230609000600.syqy447e6metnvyj@awork3.anarazel.de> <20230610203456.5gancfekm4pj4pbs@awork3.anarazel.de> <6030d836-e8b7-e7b9-2cbb-144309679d03@enterprisedb.com> <8c86c3a6-074e-6c88-3e7e-9452b6a37b9b@enterprisedb.com> <3cd40425-965a-5ce1-1af3-d51971c44b93@enterprisedb.com> <8ec36f51-b863-60e3-20e2-b9c981c5ce5e@enterprisedb.com> <06bb7d02-2c44-3062-731e-a735ba13da7e@enterprisedb.com> <367160ea-b1ed-4481-e804-bca509128878@enterprisedb.com> <20231221132742.kqxt3iujna3z33ab@alap3.anarazel.de> From: Tomas Vondra In-Reply-To: <20231221132742.kqxt3iujna3z33ab@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 12/21/23 14:27, Andres Freund wrote: > Hi, > > On 2023-12-09 19:08:20 +0100, Tomas Vondra wrote: >> But there's a layering problem that I don't know how to solve - I don't >> see how we could make indexam.c entirely oblivious to the prefetching, >> and move it entirely to the executor. Because how else would you know >> what to prefetch? > >> With index_getnext_tid() I can imagine fetching XIDs ahead, stashing >> them into a queue, and prefetching based on that. That's kinda what the >> patch does, except that it does it from inside index_getnext_tid(). But >> that does not work for index_getnext_slot(), because that already reads >> the heap tuples. > >> We could say prefetching only works for index_getnext_tid(), but that >> seems a bit weird because that's what regular index scans do. (There's a >> patch to evaluate filters on index, which switches index scans to >> index_getnext_tid(), so that'd make prefetching work too, but I'd ignore >> that here. > > I think we should just switch plain index scans to index_getnext_tid(). It's > one of the primary places triggering index scans, so a few additional lines > don't seem problematic. > > I continue to think that we should not have split plain and index only scans > into separate files... > I do agree with that opinion. Not just because of this prefetching thread, but also because of the discussions about index-only filters in a nearby thread. > >> There are other index_getnext_slot() callers, and I don't >> think we should accept does not work for those places seems wrong (e.g. >> execIndexing/execReplication would benefit from prefetching, I think). > > I don't think it'd be a problem to have to opt into supporting > prefetching. There's plenty places where it doesn't really seem likely to be > useful, e.g. doing prefetching during syscache lookups is very likely just a > waste of time. > > I don't think e.g. execReplication is likely to benefit from prefetching - > you're just fetching a single row after all. You'd need a lot of dead rows to > make it beneficial. I think it's similar in execIndexing.c. > Yeah, systable scans are unlikely to benefit from prefetching of this type. I'm not sure about execIndexing/execReplication, it wasn't clear to me but maybe you're right. > > I suspect we should work on providing executor nodes with some estimates about > the number of rows that are likely to be consumed. If an index scan is under a > LIMIT 1, we shoulnd't prefetch. Similar for sequential scan with the > infrastructure in > https://postgr.es/m/CA%2BhUKGJkOiOCa%2Bmag4BF%2BzHo7qo%3Do9CFheB8%3Dg6uT5TUm2gkvA%40mail.gmail.com > Isn't this mostly addressed by the incremental ramp-up at the beginning? Even with target set to 1000, we only start prefetching 1, 2, 3, ... blocks ahead, it's not like we'll prefetch 1000 blocks right away. I did initially plan to also consider the number of rows we're expected to need, but I think it's actually harder than it might seem. With LIMIT for example we often don't know how selective the qual is, it's not like we can just stop prefetching after the reading the first N tids. With other nodes it's good to remember those are just estimates - it'd be silly to be bitten both by a wrong estimate and also prefetching doing the wrong thing based on an estimate. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company