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 1rANZT-006Mxd-Gy for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Dec 2023 05:02:11 +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 1rANZR-002Gzl-Ac for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Dec 2023 05:02:09 +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 1rANZQ-002Gzd-RH for pgsql-hackers@lists.postgresql.org; Tue, 05 Dec 2023 05:02:08 +0000 Received: from mail-vk1-xa32.google.com ([2607:f8b0:4864:20::a32]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rANZN-008rXt-CI for pgsql-hackers@lists.postgresql.org; Tue, 05 Dec 2023 05:02:07 +0000 Received: by mail-vk1-xa32.google.com with SMTP id 71dfb90a1353d-4b2ee35bff8so386926e0c.2 for ; Mon, 04 Dec 2023 21:02:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bowt-ie.20230601.gappssmtp.com; s=20230601; t=1701752524; x=1702357324; 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=T/+YPoiThrr4AKeWWIJhph148lLD7YiC4cpT/mnFj8k=; b=xWYrlDtD15pa700B+R+t9QxeWwpwXukkPU2tjI2cDvnIf/IcxKIVqQJmxGCNO31eBn DFPHHd+/WotkVaj9U4gOr2gX6DS9kRy24LgMxESiTgKPylgAI3qmddd+ruh1/OciZNJR pmBJwi8eOVa12QTKTq6NxX9RSEF1ZHWoe+UlKR6ClHDqoksM/zTNs2YjTjbDWvbako1f WJ8//5r2LNJVPIqF0RJ5HiBvBvn8LssXRN/qWbiRdtQAaNevBK0Lv1bJJ1aeiUEVQHkk 8NcjT7diu7y/tFTy1aqLb8GXR4M0FIlVesWdWA6IE/0e/0HhXO18NBvKAls25stnYvMu xS4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701752524; x=1702357324; 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=T/+YPoiThrr4AKeWWIJhph148lLD7YiC4cpT/mnFj8k=; b=FP/+EEuK342e8GqlJNZ6fZy1WRhuxx4viCxV92rz2Ks7dim6DFv39Rs0orncpIwJDW 4Lv432DSVqOn4ZpJKrAAu9A2R/6advpraoSSkbJS7bXm7Ubgy/HLF+pOJO4j6hL3K8PB AqKa+xKXk4Wbjqcx2nxvwDuro88Jm3kOKfQkkdwigFK5Ke3RPCCjaHiGyaIKkWqIYtba mDcjqN3ujPON7ujG9wablDMI5PtvUQsJuGI1pPTQGvGYd1a7YO4r7BkbDjxYeUnhET7v QNrN/XFepFEsKoIHPbZxj9FCea+KfMg/xL4fQMBBVjpBsavHC48zH7SwoRDd+IwwSc4T Pg1g== X-Gm-Message-State: AOJu0YyJo0/VmGUFro2bdbvuNLCbigGXbQHoblfq9MaEYkqTdgQAJVl5 o8UDdEi9Ii7c/SLLF1PYI3AkQ7m9txy1QyqgW0BcmQ== X-Google-Smtp-Source: AGHT+IHJtqwaQ8FQrzAq7ZSAoJMYL7QkNakGkLMpueho/APrvMz8o6tSwdX/JU6j9PnPEQCYImT/arYZqZPrVJCWS74= X-Received: by 2002:a1f:7d45:0:b0:4b2:c554:dfc6 with SMTP id y66-20020a1f7d45000000b004b2c554dfc6mr3768855vkc.21.1701752524106; Mon, 04 Dec 2023 21:02:04 -0800 (PST) MIME-Version: 1.0 References: <86930045-5df5-494a-b4f1-815bc3fbcce0@iki.fi> In-Reply-To: From: Peter Geoghegan Date: Mon, 4 Dec 2023 21:01:37 -0800 Message-ID: Subject: Re: Optimizing nbtree ScalarArrayOp execution, allowing multi-column ordered scans, skip scan To: Heikki Linnakangas Cc: PostgreSQL Hackers , Tom Lane , Tomas Vondra , Jeff Davis , benoit , Alexander Korotkov , Matthias van de Meent 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 Mon, Dec 4, 2023 at 7:25=E2=80=AFPM Peter Geoghegan wrote: > 2. The optimization that has _bt_checkkeys skip non-required scan keys > that are *only* required in the direction *opposite* the current scan > direction -- this can work even without any precheck from > _bt_readpage. > > Note that this second optimization relies on various behaviors in > _bt_first() that make it impossible for _bt_checkkeys() to ever see a > tuple that could fail to satisfy such a scan key -- we must always > have passed over non-matching tuples, thanks to _bt_first(). That > prevents my patch with a problem: the logic for determining whether or > not we need a new primitive index scan only promises to never require > the scan to grovel through many leaf pages that _bt_first() could and > should just skip over instead. This new logic makes no promises about > skipping over small numbers of tuples. So it's possible that > _bt_checkkeys() will see a handful of tuples "after the end of the > _bt_first-wise primitive index scan", but "before the _bt_first-wise > start of the next would-be primitive index scan". BTW, I have my doubts about this actually being correct without the patch. The following comment block appears above _bt_preprocess_keys: * Note that one reason we need direction-sensitive required-key flags is * precisely that we may not be able to eliminate redundant keys. Suppose * we have "x > 4::int AND x > 10::bigint", and we are unable to determine * which key is more restrictive for lack of a suitable cross-type operator= . * _bt_first will arbitrarily pick one of the keys to do the initial * positioning with. If it picks x > 4, then the x > 10 condition will fai= l * until we reach index entries > 10; but we can't stop the scan just becau= se * x > 10 is failing. On the other hand, if we are scanning backwards, the= n * failure of either key is indeed enough to stop the scan. (In general, w= hen * inequality keys are present, the initial-positioning code only promises = to * position before the first possible match, not exactly at the first match= , * for a forward scan; or after the last match for a backward scan.) As I understand it, this might still be okay, because the optimization in question from Alexander's commit e0b1ee17 (what I've called optimization 2) is careful about NULLs, which were the one case that definitely had problems. Note that IS NOT NULL works kind of like WHERE foo < NULL here (see old bug fix commit 882368e8, "Fix btree stop-at-nulls logic properly", for more context on this NULLs behavior). In any case, my patch isn't compatible with "optimization 2" (as in my tests break in a rather obvious way) due to a behavior that these old comments claim is normal within any scan (or perhaps normal in any scan with scan keys that couldn't be deemed redundant due to a lack of cross-type support in the opfamily). Something has to be wrong here -- could just be the comment, I suppose. But I find it easy to believe that Alexander's commit e0b1ee17 might not have been properly tested with opfamilies that lack a suitable cross-type operator. That's a pretty niche thing. (My patch doesn't need that niche thing to be present to easily break when combined with "optimization 2", which could hint at an existing and far more subtle problem.) --=20 Peter Geoghegan