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.96) (envelope-from ) id 1wPMAz-000g6J-2i for pgsql-hackers@arkaria.postgresql.org; Tue, 19 May 2026 15:16:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wPMAw-004hlO-1B for pgsql-hackers@arkaria.postgresql.org; Tue, 19 May 2026 15:16:07 +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.96) (envelope-from ) id 1wPMAw-004hlF-03 for pgsql-hackers@lists.postgresql.org; Tue, 19 May 2026 15:16:07 +0000 Received: from mail-yw1-x112a.google.com ([2607:f8b0:4864:20::112a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wPMAu-00000000OTd-2eQv for pgsql-hackers@lists.postgresql.org; Tue, 19 May 2026 15:16:06 +0000 Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-7b41fdf9de2so23812037b3.0 for ; Tue, 19 May 2026 08:16:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1779203762; cv=none; d=google.com; s=arc-20240605; b=E8tAfGfRFUlpA13O/dJOrqbfXzPHHn63O7L619gXUF+YzWUceJXW5FwAB0mbQMK/vs 3n9NJzI4NXTzQPL8AWUn0DPNKM9u3bNEX01GsgMIw/LS5ufvAw33aVCGnaIdnNzKk03K WqDkKPUdpCYZWFYG25q33icW0UDHYPHBL19ECKAIWpphrYG/QDyo7YiOxctMV8/infTZ Pw0eRbNbNo4zHnRUZtqNePYpimoj66099403mMZr1vRZgpKLkFrV4FCd703LYV4zliN5 2Ze7jCPVpQsQW624/E0tbgKDL+vdpQFuqzaRKWhoF+pFZlSBFM60qnBWUxUcOrRxQPXN 51KA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=QOuNRXuy+RZgXbPUARhDOwFHwfsw7CAHQov7xOypmyk=; fh=8O8fhkxjO5SCiWMLPsFVNlgWFTVTcMkzLPK+n0jafwQ=; b=XPuRg/qcWp8pN29d2OBXR8YoQABD1tmv87/+XH00OeUAzxhJMBqo43DsYLrW9HaA59 PWKFxjlhTwBHUHmDmC5lH/wEdWek0l7OXCVl0BPavcbld2HmhbFlP2FknE3ioyIceQLh cQxAOEOcOSQCnvzo3FpMgemVEXsyCwUU5joNKgfP0dr2lkB7EbZWmJEglZn/7iiNnI9k 5r3iqH6c3guFVJggkEmAZFGDMfZw4UqED044vg1w3ubFDxhQXfiv81lgXmuxJy9X4yDm pN7gMlWYKsbM5APo8uNgOKS8NVawHFV/LGsgpPq3Aw0SF3SLta89ifzZu1VvkB01eXL7 kRfQ==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779203762; x=1779808562; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=QOuNRXuy+RZgXbPUARhDOwFHwfsw7CAHQov7xOypmyk=; b=KXlSY2DPUCP9667/XFdgBJNCPmQxA+2qi4CVfgjWB+juvupBLBY4kjbgoFqM2wEOfN XraJ5lpUeISnWO21M+pitCAZXfgNDgxnvG03ks+QzHezY/s5AXfH6N/2mrsLkqaByUb3 c+PMaxCVTIuQKidunnHS0udUFgE+h/+9lKC5oNb0bh9Bi5N1JllVn4eTUUhais27dx1c GTtSnEBqSslj9dGbMFQAssQbPOIlg77/J5YIONUydNFDwmy9CGN7Cgi7KflpUJyQkzA/ 5CxwBzg8w8DxFmK0TZwJNDL7tac6gcXlNUuGZ9TVkMY++ed8kirug9xyCuVcxluGjXHw 0Y0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779203762; x=1779808562; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=QOuNRXuy+RZgXbPUARhDOwFHwfsw7CAHQov7xOypmyk=; b=fC092lu8wRWoZ2gnys7HP6JLm9jTP30ozKQjIAIcmWYAgCsN1nJaejifkFO0hfZGDN b+NSRr6mvPvaO7tfQLCpyTCOvjlAOl9mr/FdMmAzdynjzs5CuGIFztigtgLh5jPYv6uz 2lIwLULSSmd/yx+YrecGcZ5ootRgw9Gt6J8n56mY8MBDaKVUJRqT4PLaE5NG8y0IhnDQ ezZAxbWG92gQAGug6L4aeWoxE8axmySHOV3PcQe6sSrNNnq/T96SKT9gSxNuTNOOegWs gNoJPKQ+7zsREiiUgzMeX6Sq5dvggH/5eK7z9+VB64ixcMf3Iu512AKMswA1HiUFt55+ RTjQ== X-Forwarded-Encrypted: i=1; AFNElJ9WycLAaQ/FRW7ipXwx2W4a1qweG0cbdUMCbCIAkbG14KcZtnBCzI9L2QCE6G/LLTGnf9tK5+QgQkLhBFNk@lists.postgresql.org X-Gm-Message-State: AOJu0Yz8S/7V3meDqP+6n25xT9AyQ4Ew5YG50DhVPBzWzvrGNF40G2yD t93beWWLHH/DZeAkHTSVB9bEcSiuJwtL6m8s0ng/UqhfL0/WSld2zhEOAFCDnn6eYV1lNdvH6LJ qmaf25QnxVUanZK+y1LO34vIgMnLBLUA= X-Gm-Gg: Acq92OFFpvejlMC/VoytUxMFjfNRzB3dcxeKXkpMXgurDLNIFEi015TNOmALuoqoPjU DgiNV1hWAW1sTpxhY1PejUNIMjeUvX6hhrODHjUsA65UfSC4fmt4s+fLVyuAwEfzvkpAWG/a6lq P1AzjJjSuWQA606V9qgRq/ANac8trs1rmRfYbejdGHqvL8+aWDJS6BuT4GGmTjDnaOfGhxf03Yb DsSK7EmSvaGrLcPOaIitSXIm2vtpEZIKdMK1ipozsZc3xNZopEnr+PlRuQXED5E1yXbWfDAtnnA xrPriXDlnn4bxQp7LTHJQhkZ5zcanHWxx4kndAY= X-Received: by 2002:a05:690c:ed3:b0:7b4:31cc:376d with SMTP id 00721157ae682-7c95c20141fmr210727437b3.39.1779203761925; Tue, 19 May 2026 08:16:01 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ayush Tiwari Date: Tue, 19 May 2026 20:45:50 +0530 X-Gm-Features: AVHnY4JN3PqJyVQnm5awtCmZEwIizqfHMjY_qkFcAmjvBLlqiD4WMD28fQ84B5w Message-ID: Subject: Re: [Bug]Assertion failure in LATERAL GRAPH_TABLE with multi-label pattern To: SATYANARAYANA NARLAPURAM Cc: Ashutosh Bapat , PostgreSQL Hackers Content-Type: multipart/mixed; boundary="00000000000036d45006522d2826" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000036d45006522d2826 Content-Type: multipart/alternative; boundary="00000000000036d45006522d2824" --00000000000036d45006522d2824 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, On Tue, 12 May 2026 at 23:12, SATYANARAYANA NARLAPURAM < satyanarlapuram@gmail.com> wrote: > HI, > > On Tue, May 12, 2026 at 4:15=E2=80=AFAM Ashutosh Bapat < > ashutosh.bapat.oss@gmail.com> wrote: > >> On Thu, May 7, 2026 at 9:43=E2=80=AFAM SATYANARAYANA NARLAPURAM >> wrote: >> > >> > Hi hackers, >> > >> > A LATERAL GRAPH_TABLE whose pattern matches more than one path query >> > fails with the assert >> > >> > TRAP: failed Assert("!bms_is_member(rti, lateral_relids)"), File: >> "initsplan.c", Line: 1428, PID: 3586144 >> > postgres: postgres postgres [local] >> SELECT(ExceptionalCondition+0x70)[0x63488e3cc070] >> > postgres: postgres postgres [local] >> SELECT(create_lateral_join_info+0x468)[0x63488e14ac28] >> > postgres: postgres postgres [local] >> SELECT(query_planner+0x13a)[0x63488e14dfca] >> > >> > Repro: >> > SELECT v1.vname, gt.aname >> > FROM v1, LATERAL (SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1 | vl= 2 >> WHERE a.vprop1 =3D v1.vprop1) COLUMNS (a.vname AS aname)) g) gt >> > ORDER BY 1, 2; >> > >> > Single-label GRAPH_TABLE with the same outer reference works fine. >> > rewriteGraphTable() turns the GRAPH_TABLE RTE into a subquery RTE and >> > bumps outer Vars by one sublevel. When the pattern produces multiple >> > path queries, generate_setop_from_pathqueries() wraps each one in >> > another subquery RTE for the UNION ALL but does not bump again, so the >> > lateral reference collapses onto GRAPH_TABLE's own RTE. >> >> + /* Wrapping lquery in a subquery RTE adds one query level, so bump >> + * outer-level Vars accordingly. */ >> + IncrementVarSublevelsUp((Node *) lquery, 1, 1); >> + >> >> Multiline comments start with /* and end with */ on separate lines >> respectively. >> >> From the comment it feels like we will add as many varlevels as the >> depth of setop tree, since we will add a subquery RTE for each setop >> level. In reality all the legs of the setop tree will be at the same >> varlevel. A better comment would be "Vars in each path query are one >> level away from the setop query combining them.". >> >> The connection between varlevelsup, addition of subquery RTE and the >> assertion failure isn't clear. Assertion failure indicates that the >> relation being examined has lateral reference to itself which boils >> down to range table entry index but the varlevelsup is about depth of >> a given query. The connection between these two seemingly different >> things needs to be explained in the commit message. Though the code >> changes fix the problem, there may be a better place to increment >> varlevels up. That will be clear once the connection is clear. >> >> > >> > Tried fixing this by bumping each lquery's sublevels by 1 before the >> addRangeTableEntry >> > ForSubquery() wrap. Single-pathquery queries skip this path entirely. >> > >> > Attached a patch with the tests. >> > >> >> Do we need both the tests? The second one has no label expression >> which means it will try all the labels. So the test will reproduce the >> bug only when there are multiple labels. The first test explicitly >> adds the multi-label pattern. I think we should just leave the first >> one and remove the second one. Also this test should be placed in the >> section which tests other lateral references. myshop property graph >> has two sets of elements that share a label - order and wishlist, >> order_items and wishlist_items. >> > > Thanks for reviewing, will address the comments shortly. > Attached is v2 of Satya's patch, addressing the review comments. Changes in v2: - Reworded the inline comment in PostgreSQL multi-line style. It now explains that each path query is inserted as a subquery RTE of the setop query, so Vars that already refer outside the path query must be adjusted for the additional query level. Local Vars belonging to the path query are unchanged (the call uses min_sublevels_up =3D 1). - Expanded the commit message to spell out the connection between the missing varlevelsup adjustment and the lateral self-reference assertion. - Reduced the regression coverage to a single explicit multi-label case, an= d moved it next to the existing lateral-reference tests rather than at the end of the file. The fix is kept at the same place (generate_setop_from_pathqueries), since that is where the extra subquery RTE wrapping happens. Doing it any earlier would over-adjust the single-pathquery path. Regards, Ayush --00000000000036d45006522d2824 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

On Tue, 12 May 2= 026 at 23:12, SATYANARAYANA NARLAPURAM <satyanarlapuram@gmail.com> wrote:
HI,=

= On Tue, May 12, 2026 at 4:15=E2=80=AFAM Ashutosh Bapat <ashutosh.bapat.oss@gmail.= com> wrote:
On Thu, May 7, 2026 at 9:43=E2=80=AFAM SATYANARAYANA NARLAPURAM
<satyanar= lapuram@gmail.com> wrote:
>
> Hi hackers,
>
> A LATERAL GRAPH_TABLE whose pattern matches more than one path query > fails with the assert
>
> TRAP: failed Assert("!bms_is_member(rti, lateral_relids)"), = File: "initsplan.c", Line: 1428, PID: 3586144
> postgres: postgres postgres [local] SELECT(ExceptionalCondition+0x70)[= 0x63488e3cc070]
> postgres: postgres postgres [local] SELECT(create_lateral_join_info+0x= 468)[0x63488e14ac28]
> postgres: postgres postgres [local] SELECT(query_planner+0x13a)[0x6348= 8e14dfca]
>
> Repro:
> SELECT v1.vname, gt.aname
>=C2=A0 =C2=A0FROM v1, LATERAL (SELECT * FROM GRAPH_TABLE (g1 MATCH (a I= S vl1 | vl2 WHERE a.vprop1 =3D v1.vprop1) COLUMNS (a.vname AS aname)) g) gt=
>=C2=A0 =C2=A0ORDER BY 1, 2;
>
> Single-label GRAPH_TABLE with the same outer reference works fine.
> rewriteGraphTable() turns the GRAPH_TABLE RTE into a subquery RTE and<= br> > bumps outer Vars by one sublevel.=C2=A0 When the pattern produces mult= iple
> path queries, generate_setop_from_pathqueries() wraps each one in
> another subquery RTE for the UNION ALL but does not bump again, so the=
> lateral reference collapses onto GRAPH_TABLE's own RTE.

+ /* Wrapping lquery in a subquery RTE adds one query level, so bump
+ * outer-level Vars accordingly. */
+ IncrementVarSublevelsUp((Node *) lquery, 1, 1);
+

Multiline comments start with /* and end with */ on separate lines respecti= vely.