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 1qOh34-008fRh-7t for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Jul 2023 16:07:38 +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 1qOh32-005NtE-CG for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Jul 2023 16:07:36 +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 1qOh32-005Nt6-0j for pgsql-hackers@lists.postgresql.org; Wed, 26 Jul 2023 16:07:36 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qOh2z-000Vec-RL for pgsql-hackers@lists.postgresql.org; Wed, 26 Jul 2023 16:07:35 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1bb893e6365so25251285ad.2 for ; Wed, 26 Jul 2023 09:07:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690387652; x=1690992452; 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=AMdq46jrlMtk4bxfz/fUk5QXk6QGYvyqDqq7A4G5Lj4=; b=UD+cr1VvAIUWy+tT9A+Ynj0iWgsZ9zqu6BqDlIMcmBC0IQ7jEAZTYOImxpoufB92C4 ZHT7SB9QndUltJV50sfZEv2wUrUKUHiyl3t4BBCDhy3Sx7oUbFvr7mRPh+8Swx9Yzg12 Lyqwi937KtQNeY5gEmTjXBzrQFdsjHGO22bCJpqn4xCbNcXq1dWnAHlzXNpntWiKQc4b vX17mBoWIur7X0XwXwziKcxx/jT4VcixN1J1zS3ZcRGnc/wGXKYjZbWeXcCOIyRq5uag 2V60bUdE5jNduXW4CHtoaPBGEUp2luQv3OZBnXiU0wRRME78YVxYiwm4cV7vQRH7ZJ/x rWsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690387652; x=1690992452; 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=AMdq46jrlMtk4bxfz/fUk5QXk6QGYvyqDqq7A4G5Lj4=; b=fK13xpVgte4Ms70WsBpB279c+DyHpAvD+GFLwM76fv6FKlMhgLnuDQj/QzCJ9ukvSj gPoc8gzMaei3z948A6S0+hApJtlPbjnUtc7MSPsmuC/1Fe5sJ98m5uxlQJjBcYVL9pir RTM9z8pyMuHObx2QB2NPLbEFBQ/IGvkHquzZERbRGkD7wlg9WJvfd/BYf7fWRjuVTtkt EzkVBBmn8dDJNhob6+2t2ecJf29riMYCGKSumuEkFMgNgr0PDTkNOuCVOxBh9CZJwLVP 1XiIipNUTjurB3VQ4AqEmQ+RkuaDfiLx4MZ2qq81g1o04z5W2p1lKkgjtKDbPzJJxcK7 tolQ== X-Gm-Message-State: ABy/qLZKFQdb/8olro+GP/xqKDO/9Qqmj44077nEKFl5Q5BS1E2bWu7n TEIT0Be+TU66urcTX9L2HsEg/DRAsRatAeEBg3I= X-Google-Smtp-Source: APBJJlF6MOlMOc/FRKWp7OiOKo23ItnKoahWnHuvvc/dK+saex9uPx5XFYWMcUv6j/WgnFWys5OtY9fR7CyPMmVWTuo= X-Received: by 2002:a17:902:e5cb:b0:1b8:a328:c1e6 with SMTP id u11-20020a170902e5cb00b001b8a328c1e6mr2459383plf.63.1690387651664; Wed, 26 Jul 2023 09:07:31 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Matthias van de Meent Date: Wed, 26 Jul 2023 18:07:20 +0200 Message-ID: Subject: Re: Optimizing nbtree ScalarArrayOp execution, allowing multi-column ordered scans, skip scan To: Peter Geoghegan Cc: PostgreSQL Hackers , Tom Lane , Tomas Vondra , Jeff Davis , benoit 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, 26 Jul 2023 at 15:42, Peter Geoghegan wrote: > > On Wed, Jul 26, 2023 at 5:29=E2=80=AFAM Matthias van de Meent > wrote: > > Considering that it caches/reuses the page across SAOP operations, can > > (or does) this also improve performance for index scans on the outer > > side of a join if the order of join columns matches the order of the > > index? > > It doesn't really cache leaf pages at all. What it does is advance the > array keys locally, while the original buffer lock is still held on > that same page. Hmm, then I had a mistaken understanding of what we do in _bt_readpage with _bt_saveitem. > > That is, I believe this caches (leaf) pages across scan keys, but can > > (or does) it also reuse these already-cached leaf pages across > > restarts of the index scan/across multiple index lookups in the same > > plan node, so that retrieval of nearby index values does not need to > > do an index traversal? > > I'm not sure what you mean. There is no reason why you need to do more > than one single descent of an index to scan many leaf pages using many > distinct sets of array keys. Obviously, this depends on being able to > observe that we really don't need to redescend the index to advance > the array keys, again and again. Note in particularly that this > usually works across leaf pages. In a NestedLoop(inner=3Dseqscan, outer=3Dindexscan), the index gets repeatedly scanned from the root, right? It seems that right now, we copy matching index entries into a local cache (that is deleted on amrescan), then we drop our locks and pins on the buffer, and then start returning values from our local cache (in _bt_saveitem). We could cache the last accessed leaf page across amrescan operations to reduce the number of index traversals needed when the join key of the left side is highly (but not necessarily strictly) correllated. The worst case overhead of this would be 2 _bt_compares (to check if the value is supposed to be fully located on the cached leaf page) plus one memcpy( , , BLCKSZ) in the previous loop. With some smart heuristics (e.g. page fill factor, number of distinct values, and whether we previously hit this same leaf page in the previous scan of this Node) we can probably also reduce this overhead to a minimum if the joined keys are not correllated, but accellerate the query significantly when we find out they are correllated. Of course, in the cases where we'd expect very few distinct join keys the planner would likely put a Memoize node above the index scan, but for mostly unique join keys I think this could save significant amounts of time, if only on buffer pinning and locking. I guess I'll try to code something up when I have the time, as it sounds not quite exactly related to your patch but an interesting improvement nonetheless. Kind regards, Matthias van de Meent