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 1tX8uw-004GtL-4l for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Jan 2025 01:06:58 +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 1tX8uu-0052Br-PC for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Jan 2025 01:06:57 +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 1tX8uu-0052Bc-E2 for pgsql-hackers@lists.postgresql.org; Mon, 13 Jan 2025 01:06:56 +0000 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tX8us-0005EL-1l for pgsql-hackers@postgresql.org; Mon, 13 Jan 2025 01:06:56 +0000 Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-5d3bbb0f09dso6841068a12.2 for ; Sun, 12 Jan 2025 17:06:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736730413; x=1737335213; darn=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=XUAJfSW02ioNSYnGeI850/N7Md/Vf8fnFoAPayPSOWE=; b=JlpTDyJQuT8naW04DVWHOavDDq/Zt6lHo7+XP0R8jej8k8OkWHlwVAxEg6PMPyL1tL VGiEqkrP3lTL6x/FqSc1GX7liOmQnSswPryPOh53ETkJpsQRhmQhh3YpmnjTqjytQCeR /mncZaBdR2ZNtlDFjrNIif0zPWJ5zfcrlcGaza5J7WH6pqLOuWEwUaXAApt7SHDOgwHP gGqh2NV44NV10j+k1OVECRCdYv69AogXqWZQQ/wMINy/tuekyfv4m9xLoMXIyqbfd2X3 hQFBR3kiA0JGVY3tl75yW5ugkNjtO5XX4IqVe74/2XFYHb6GBwRyYi7Xc+n1RdxIj9YY NaIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736730413; x=1737335213; 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=XUAJfSW02ioNSYnGeI850/N7Md/Vf8fnFoAPayPSOWE=; b=T4zKagdqlsoZRBXtJLmPRzcSOxyq8l8uxfXgR/M4VDyceohdrFfNq+53UrwIrHDsB2 djOzZ8icpbZzqsbhXvbGgLMoz9O9hwi16auV8vCCU/V8+Iz+f6NMlWA23CUom0B5xWco 29SbgZMp2NfqTxHj7nYmP/+RtLqFDQ3+eaqL4t8/8fSPt9942162/eXvJvqlFh4BJEvD nVGyh2sKic4mr0d51gI4lTgmH7QcAN5yzRYqrdPZQO+NPnJKYNZ32GWNp/tOK6UXyhdj Jtpb6KL4y1V8oWWtyR6JaPM3yIzIUa/cy5VUNcS5gQ89StOBoZRbt/uR7xic2pOejTj/ 9vZw== X-Forwarded-Encrypted: i=1; AJvYcCUHhDgx5clPESaWV5i8XLR5VcZe3mldSkn5IMamqEvMSx8EvwRRQhf4W596v5RU+ic7+r+ectQN9i3aRdMn@postgresql.org X-Gm-Message-State: AOJu0Yyzj95UIJ0K+rWtblCnm5Jqeskezlijci0ltNv82KyrZh58+0g3 4dbJPgq85j6sKA2VRe7fpRuTIUMlrDTW6GXIa3pn7FnsTEewaBARuitRGOUuPteDuJv7PY9luHH gqt1bB1s2PCdYWvAV/P27nh7UmBY= X-Gm-Gg: ASbGncu0bAg3Fojt2DG+LXXvZf3LCRNrCpl45MYw78sDIJ7CuIWJNsZGx7BAyxBNU3Q 33Hqsd3O/jKS+928CGLnGUTNF2YIGXGXXj1k1DzE= X-Google-Smtp-Source: AGHT+IFhiSc8bA0nB8dzBmt0nsBbButMfdBwT7yzkhp+1onqNXxPJXL6GDfm86fOeR1O4jpeuEqR90O9eWZgOKXUThY= X-Received: by 2002:a05:6402:3585:b0:5d1:2631:b877 with SMTP id 4fb4d7f45d1cf-5d972e08387mr16266610a12.9.1736730413233; Sun, 12 Jan 2025 17:06:53 -0800 (PST) MIME-Version: 1.0 References: <567ED6CA.2040504@sigaev.ru> <45050ba9-3777-4cc1-89ad-a02deebe7729@gmail.com> <0026f562-d403-4f97-b9dc-3fe3a279e8c9@postgrespro.ru> <37bf870a-22e0-4aba-b845-549482118f8d@gmail.com> <8174de69-9e1a-0827-0e81-ef97f56a5939@gmail.com> <3b717020-14d9-425f-8c60-ce336b5a23b3@postgrespro.ru> In-Reply-To: From: Alexander Korotkov Date: Mon, 13 Jan 2025 03:06:42 +0200 X-Gm-Features: AbW1kvY5_SXs9Ax_GWjAjSF5ZaZHwRsB3t2dCyWfOxgIQzNA_94aDK3bOkWiwG8 Message-ID: Subject: Re: POC, WIP: OR-clause support for indexes To: Yura Sokolov Cc: Alena Rybakina , Ranier Vilela , Alexander Lakhin , Andrei Lepikhov , jian he , Robert Haas , Nikolay Shaplov , pgsql-hackers@lists.postgresql.org, pgsql-hackers@postgresql.org, Peter Geoghegan , Marcos Pegoraro , teodor@sigaev.ru, Peter Eisentraut 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, Jan 13, 2025 at 2:47=E2=80=AFAM Yura Sokolov wrote: > =D0=92=D1=81, 12 =D1=8F=D0=BD=D0=B2. 2025=E2=80=AF=D0=B3. =D0=B2 21:39, A= lexander Korotkov : >> >> On Fri, Nov 29, 2024 at 9:54=E2=80=AFAM Alexander Korotkov wrote: >> > On Fri, Nov 29, 2024 at 7:51=E2=80=AFAM Alena Rybakina >> > wrote: >> > > >> > > On 29.11.2024 03:04, Alexander Korotkov wrote: >> > > > On Thu, Nov 28, 2024 at 9:33=E2=80=AFPM Alena Rybakina >> > > > wrote: >> > > >> On 28.11.2024 22:28, Ranier Vilela wrote: >> > > >> >> > > >> Em qui., 28 de nov. de 2024 =C3=A0s 16:03, Alena Rybakina escreveu: >> > > >>> Hi! Thank you for the case. >> > > >>> >> > > >>> On 28.11.2024 21:00, Alexander Lakhin wrote: >> > > >>>> Hello Alexander, >> > > >>>> >> > > >>>> 21.11.2024 09:34, Alexander Korotkov wrote: >> > > >>>>> I'm going to push this if no objections. >> > > >>>> Please look at the following query, which triggers an error aft= er >> > > >>>> ae4569161: >> > > >>>> SET random_page_cost =3D 1; >> > > >>>> CREATE TABLE tbl(u UUID); >> > > >>>> CREATE INDEX idx ON tbl USING HASH (u); >> > > >>>> SELECT COUNT(*) FROM tbl WHERE u =3D '0000000000000000000000000= 0000000' OR >> > > >>>> u =3D '11111111111111111111111111111111'; >> > > >>>> >> > > >>>> ERROR: XX000: ScalarArrayOpExpr index qual found where not allo= wed >> > > >>>> LOCATION: ExecIndexBuildScanKeys, nodeIndexscan.c:1625 >> > > >>>> >> > > >>>> >> > > >>> I found out what the problem is index scan method was not genera= ted. We >> > > >>> need to check this during OR clauses for SAOP transformation. >> > > >>> >> > > >>> There is a patch to fix this problem. >> > > >> Hi. >> > > >> Thanks for the quick fix. >> > > >> >> > > >> But I wonder if it is not possible to avoid all if the index is u= seless? >> > > >> Maybe moving your fix to the beginning of the function? >> > > >> >> > > >> diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/= optimizer/path/indxpath.c >> > > >> index d827fc9f4d..5ea0b27d01 100644 >> > > >> --- a/src/backend/optimizer/path/indxpath.c >> > > >> +++ b/src/backend/optimizer/path/indxpath.c >> > > >> @@ -3248,6 +3248,10 @@ match_orclause_to_indexcol(PlannerInfo *ro= ot, >> > > >> Assert(IsA(orclause, BoolExpr)); >> > > >> Assert(orclause->boolop =3D=3D OR_EXPR); >> > > >> >> > > >> + /* Ignore index if it doesn't support index scans */ >> > > >> + if(!index->amsearcharray) >> > > >> + return NULL; >> > > >> + >> > > >> >> > > >> Agree. I have updated the patch >> > > >> >> > > >> /* >> > > >> * Try to convert a list of OR-clauses to a single SAOP expression= . Each >> > > >> * OR entry must be in the form: (indexkey operator constant) or (= constant >> > > >> >> > > >> The test bug: >> > > >> EXPLAIN SELECT COUNT(*) FROM tbl WHERE u =3D '0000000000000000000= 0000000000000' OR u =3D '11111111111111111111111111111111'; >> > > >> QUERY PLAN >> > > >> -----------------------------------------------------------------= ----------------------------------------------------------------- >> > > >> Aggregate (cost=3D12.46..12.47 rows=3D1 width=3D8) >> > > >> -> Bitmap Heap Scan on tbl (cost=3D2.14..12.41 rows=3D18 width=3D= 0) >> > > >> Recheck Cond: ((u =3D '00000000-0000-0000-0000-000000000000'::uui= d) OR (u =3D '11111111-1111-1111-1111-111111111111'::uuid)) >> > > >> -> BitmapOr (cost=3D2.14..2.14 rows=3D18 width=3D0) >> > > >> -> Bitmap Index Scan on idx (cost=3D0.00..1.07 rows=3D9 width=3D0= ) >> > > >> Index Cond: (u =3D '00000000-0000-0000-0000-000000000000'::uuid) >> > > >> -> Bitmap Index Scan on idx (cost=3D0.00..1.07 rows=3D9 width=3D0= ) >> > > >> Index Cond: (u =3D '11111111-1111-1111-1111-111111111111'::uuid) >> > > >> (8 rows) >> > > > I slightly revised the fix and added similar check to >> > > > group_similar_or_args(). Could you, please, review that before >> > > > commit? >> > > > >> > > I agree with changes. Thank you! >> > >> > Andrei, Alena, thank you for the feedback. Pushed! >> >> I think we should give some more attention to the patch enabling OR to >> SAOP transformation for joins (first time posted in [1]). I think we >> tried to only work with Const and Param, because we were previously >> working during parse stage. So, at that stage if we have the clause >> like "a.x =3D 1 OR a.x =3D b.x OR b.x =3D 2", then we don't know if we >> should transform it into "a.x =3D ANY(1, b.x) OR b.x =3D 2" or into "a.x >> =3D1 OR b.x =3D ANY(a.x, 2)". But if we do the transformation during the >> index matching, we would actually be able to try the both and select >> the best. > > > But why not =E2=80=9Ca.x =3D ANY(1, b.x) OR b.x =3D ANY(a.x, 2)=E2=80=9D = ? Looks strange, but correct )) That could probably work for a parse stage, but as you can check that approach has a lot of other problems. As we do during index matching, that doesn't matter. I just wanted to state that nothing in the current approach prevent us from working the same way for joins. ------ Regards, Alexander Korotkov Supabase