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 1r08d5-003dUn-QT for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Nov 2023 23:03:35 +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 1r08d3-002NyD-7Z for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Nov 2023 23:03:33 +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 1r08d2-002Nwq-Ou for pgsql-hackers@lists.postgresql.org; Mon, 06 Nov 2023 23:03:32 +0000 Received: from mail-vs1-xe2d.google.com ([2607:f8b0:4864:20::e2d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r08cv-004J8d-I5 for pgsql-hackers@lists.postgresql.org; Mon, 06 Nov 2023 23:03:31 +0000 Received: by mail-vs1-xe2d.google.com with SMTP id ada2fe7eead31-45d96a9eac8so1466141137.0 for ; Mon, 06 Nov 2023 15:03:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bowt-ie.20230601.gappssmtp.com; s=20230601; t=1699311804; x=1699916604; 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=CMlLDff/GbF1C/HOoRKWBoEcJjNq1dDxd8ViOF8HECc=; b=O7l7S96CKUGaYgzIsdAQ9+FA82Oi+mh7yVmUZg50TeRDybZJwvwLfpGxT+oJWykFqR r15iW9ZXI4wGp/rjX7JxL6IMB0gXPiekpUQmURr8JKRS1GcFTLBNAFZ+8XoJ01JheIO6 CxypBlcYMCW0qxN9fYdDBPMlJkeY95V3ZywZv7RBrMMhuCQK6k8DN499G7hJ17zDdenT saVRq6MJSxjRFZltbKxNjNFKNjO9NoSr3O+n0rks7iHGZWod3tTUMsbGUr8K8HTlpclW pHI9PQvJg0z+Rgj9B/wZZmZ8XUQha9u8+y8poKUmIsb5bgdU001QsUMELE0ssRtCJ1U4 mbTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699311804; x=1699916604; 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=CMlLDff/GbF1C/HOoRKWBoEcJjNq1dDxd8ViOF8HECc=; b=sc+UNrObyBWinSvsR2a4G2cysvIlGGdbXsoM1jhvV0l9pXMWeBU2fXO++lgdYdjJC4 gWwg3Jand5LW4RSujZSQGQKsrQGlN3oC7IheMtFmfkGMEcWotZfUnlWVoPo4fhqMDQBu FI4kZg/T6hCTlJih+WNFSMakvT/Jz29iR0jcDMpHTKIG2ylN/dMmH/bi1N2fo1nttnzI PmxHw/VGf2PpT7Ym8Abx5BFVokylheF0/pP6dB+Ri8KJVLcGi2BVhdicVqPFTtsNDpjD 7ACRWOCB2kK/b7Zb8xvHoy8LqCsY0XlIlLmit4cU9gKhx9FFmNSV4nDuALb5dbvBKB/I YMZw== X-Gm-Message-State: AOJu0YylsN3Y+LrjexR1oPeSfW+nvF1w9EqT4cQ6W29QOjQjLYJs3+dm vEPk7wqgCsBE0Dc2QZINufid0xhSMXC1J+QrjGd8ng== X-Google-Smtp-Source: AGHT+IHbG4nVhorCqRgcJu0RDlvG1ETKeccqdQf66MFaZPFh0Ax4vjkEgOsdxcB7h5ZI7koQhyMaLZWVREhuAMvRlck= X-Received: by 2002:a05:6102:2089:b0:458:32f7:30ad with SMTP id h9-20020a056102208900b0045832f730admr28499100vsr.24.1699311804173; Mon, 06 Nov 2023 15:03:24 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Peter Geoghegan Date: Mon, 6 Nov 2023 15:02:57 -0800 Message-ID: Subject: Re: Optimizing nbtree ScalarArrayOp execution, allowing multi-column ordered scans, skip scan To: Matthias van de Meent Cc: PostgreSQL Hackers , Tom Lane , Tomas Vondra , Jeff Davis , benoit , Alexander Korotkov 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, Nov 6, 2023 at 1:28=E2=80=AFPM Matthias van de Meent wrote: > I'm planning on reviewing this patch tomorrow, but in an initial scan > through the patch I noticed there's little information about how the > array keys state machine works in this new design. Do you have a more > toplevel description of the full state machine used in the new design? This is an excellent question. You're entirely right: there isn't enough information about the design of the state machine. In v1 of the patch, from all the way back in July, the "state machine" advanced in the hackiest way possible: via repeated "incremental" advancement (using logic from the function that we call _bt_advance_array_keys() on HEAD) in a loop -- we just kept doing that until the function I'm now calling _bt_tuple_before_array_skeys() eventually reported that the array keys were now sufficiently advanced. v2 greatly improved matters by totally overhauling _bt_advance_array_keys(): it was taught to use binary searches to advance the array keys, with limited remaining use of "incremental" array key advancement. However, version 2 (and all later versions to date) have somewhat wonky state machine transitions, in one important respect: calls to the new _bt_advance_array_keys() won't always advance the array keys to the maximum extent possible (possible while still getting correct behavior, that is). There were still various complicated scenarios involving multiple "required" array keys (SK_BT_REQFWD + SK_BT_REQBKWD scan keys that use BTEqualStrategyNumber), where one single call to _bt_advance_array_keys() would advance the array keys to a point that was still < caller's tuple. AFAICT this didn't cause wrong answers to queries (that would require failing to find a set of exactly matching array keys where a matching set exists), but it was kludgey. It was sloppy in roughly the same way as the approach in my v1 prototype was sloppy (just to a lesser degree). I should be able to post v6 later this week. My current plan is to commit the other nbtree patch first (the backwards scan "boundary cases" one from the ongoing CF) -- since I saw your review earlier today. I think that you should probably wait for this v6 before starting your review. The upcoming version will have simple preconditions and postconditions for the function that advances the array key state machine (the new _bt_advance_array_keys). These are enforced by assertions at the start and end of the function. So the rules for the state machine become crystal clear and fairly easy to keep in your head (e.g., tuple must be >=3D required array keys on entry and <=3D required array keys on exit, the array keys must always either advance by one increment or be completely exhausted for the top-level scan in the current scan direction). Unsurprisingly, I found that adding and enforcing these invariants led to a simpler and more general design within _bt_advance_array_keys. That code is still the most complicated part of the patch, but it's much less of a bag of tricks. Another reason for you to hold off for a few more days. --=20 Peter Geoghegan