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 1w2ju4-000YT2-2Z for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Mar 2026 05:57:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w2ju3-007kdK-1H for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Mar 2026 05:57:11 +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 1w2ju3-007kdB-0J for pgsql-bugs@lists.postgresql.org; Wed, 18 Mar 2026 05:57:11 +0000 Received: from mail-qv1-xf34.google.com ([2607:f8b0:4864:20::f34]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w2ju0-00000000saC-2V6S for pgsql-bugs@lists.postgresql.org; Wed, 18 Mar 2026 05:57:10 +0000 Received: by mail-qv1-xf34.google.com with SMTP id 6a1803df08f44-899ee491af3so76057566d6.1 for ; Tue, 17 Mar 2026 22:57:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1773813427; cv=none; d=google.com; s=arc-20240605; b=cccO1080ZaXOJefsLt+7DcXl6CpIeyPZT7kA929Y1pNKaKhHFZ5LYFWb48C9iYbm6P Q7PuT43JpdLXnn55TTJL7yvzhaHvI4Zz1qIIvFXR5aktax4t5iTFDmE1kEaQyUkc2tYI W/J3p+IGwUWEFX19v0cqGjdiwfKKKsyLn2b3tjHG6kyHddMjn5OIDBF+d/OnE+9GI8pc EyVgKYj5MYYLwpEBHid7RIPyenvCO90yUyeWwQzDY3hn079wJSNv/6eACvGO3tCvi6y/ 5Zt/gJN7lTh1kIWZsDJPIe7AS/xvULNZToF2s79lU3CQ2vVVIADbLx34WcLkUg0L/XS3 B1jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=GvQCB+/Gynzvgm/2UJ/5PK3i90rKkj+KvAkku2z1oaU=; fh=x+wc1+/P/94r5j0VfZTmXXSitPiZ+AUe1HVVo7MyXHI=; b=UHiXtHKySuM47O97N24Vou0qHzcC8FiIAAVHo99609Ajv3sEnRezYH5SPWOpU7cQ53 J57qEd9XMiX8rkMBB0dU5v38mZ/24dULWIbsKfjv/EDeZUwBTQ4d/fLhDjTI8MKXij71 eHj15qvXiE8BtkxWbgyBAdKwciC86aNsWLMxcGuC6Wpd1Rv0Qzi1vW7tVAhmPW5+9HU2 2iDktN3c0vUYHycNKjdrAtXPYRKMaaXh1cuZoD6u6Xe3MTrirSb3EMYEfupUD/uwzybo 3JfR8HshZirzrB0JeY74ORvJqVCL+MfUKbY1OMkBajUbVTblida/82EqUnJFIFUsF0Tx klaQ==; 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=20230601; t=1773813427; x=1774418227; 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=GvQCB+/Gynzvgm/2UJ/5PK3i90rKkj+KvAkku2z1oaU=; b=ZwLm/kD8UuXaYhkMDpCoE6ipSeBnDD6ouxDeydZ2KlSuJVCWWPAfsxzQx2imNTdeWM UiYVIdIqkJrBBlGIRmMtV+0VgFQ+6FoJosUt0Rpt9Rmemh7X/lZ++nmI9ATHfXxG2wQt KV9O23jfaCv9ccMwrZTz3qZ3GUZkfSpWCZ0EBcApLJEJhsj36iSUoauQHfoCdZMdJlEb IefYhao2LqHvoCxCvXtk2t9LSQAyi3PM86h8rDad7xUN8HucxGNap/ZczEx4Z4nLaZvc YcBxVTjodW7xJEr/o2MovbsG6dojUeI0/Qmw9jHh8XMPjiNfcsxsdAJjiGLUMJtmaOT2 oRpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773813427; x=1774418227; h=content-transfer-encoding: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=GvQCB+/Gynzvgm/2UJ/5PK3i90rKkj+KvAkku2z1oaU=; b=BZZGdWDF/xcpyVUpamXe5Jyvb5wBkTanUS2+qO160Kcl2Mi7VuK9/bwlSXh4UT0pSJ 9MWkvM5v9Yzj0HB/uV+zVExKv4HAfVbrGdn4M2DJxcpJRm+KPBX6VsTdcCaE1ms+KGwQ G0riKzmUJmb5FVI5cLT5om2esLIJxiZ+NPhqNQfw27ENjKPuCMLrQBxvdUSmuyX+8Zyn yDov7Hrm+47AtaAfxUvuDjvo2VZsxfy0JudcmEv2g9dGxxL3RQcXNxLToB6kaLUfsh/X bI3Achr/0J8cwCbHQ9jKFUCbIN98gSfsi3PrtuiBNafnsqCiQymldByFOD1nVBeexMHa dsfg== X-Forwarded-Encrypted: i=1; AJvYcCW2Zucos29uAONpTrwsBthRHvpyOB5ffBTkf6JqMLhruw7aHACWu6rYt1xXa/13/H/UPtxkb0RdUasJ@lists.postgresql.org X-Gm-Message-State: AOJu0YxuSf06gPtrGYU9qSAmd5pGYz+zC8k640xzQvsH3vS6ihmaxtFc 0uqxFUl+ItoA3Pt33mFYWBclRUwycoea2HUm7o7hw5WssaMj9mYKsVLoW3bCYO4Ixm1wm/7UcrX EqDG0jBU5wYOCmVm3EDtmiDDVu7COwHw= X-Gm-Gg: ATEYQzx3VYK2COKPr2iUgyA/aNceid1b1euiF+3T83j1gHTk0E8BSpwQeTKuMkoR4fx UxOsbCahvU2M7pfslgg2L3KSH6pmntXlq1wUkxw8KZWeJMdR47pnF707oMpt9/tl8Hbrs8R/fsh NY47pqbhk0i6GDorRLRK1hkoVJ6qbdS3+Jo7EJRWcrVVXGxEJHvWW1OqPAm2c4te+m0goWnYBxD 7VKCYhO2SCQsNl5Tc/wpQ4Ll2+HQzEs6+7LQ5rxcGlMZIhYXwnJJ3ry9JkoCjVYki96kNnJg8lP lrVJOGgeEJ2bfNm+hyOmR0Z+zw4jciesG7cJV88RDBZzR/x6yEf3/DX3IY8ia2q2v9fQx9w3Sas VyZMZUg== X-Received: by 2002:a05:6214:470b:b0:89a:13a5:77b8 with SMTP id 6a1803df08f44-89c6b59145dmr24194686d6.38.1773813427056; Tue, 17 Mar 2026 22:57:07 -0700 (PDT) MIME-Version: 1.0 References: <19435-3cc1a87f291129f1@postgresql.org> In-Reply-To: From: Kirill Reshke Date: Wed, 18 Mar 2026 10:56:55 +0500 X-Gm-Features: AaiRm53kAZ90AZddvd_Pi9EqCOcJAZDbnTvj9UOTtPYex9wyjxASmzoV7eDXbac Message-ID: Subject: Re: BUG #19435: Error: "No relation entry for relid 2" Triggered by Complex Join with Self-Referencing Tables To: Tender Wang Cc: Alexander Korotkov , Fujii Masao , ammmkilo@163.com, pgsql-bugs@lists.postgresql.org 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, 18 Mar 2026 at 10:44, Tender Wang wrote: > > Tender Wang =E4=BA=8E2026=E5=B9=B43=E6=9C=8818=E6=97= =A5=E5=91=A8=E4=B8=89 09:12=E5=86=99=E9=81=93=EF=BC=9A > > > > Alexander Korotkov =E4=BA=8E2026=E5=B9=B43=E6=9C= =8818=E6=97=A5=E5=91=A8=E4=B8=89 01:46=E5=86=99=E9=81=93=EF=BC=9A > > > > > > On Tue, Mar 17, 2026 at 3:30=E2=80=AFPM Tender Wang wrote: > > > > Kirill Reshke =E4=BA=8E2026=E5=B9=B43=E6= =9C=8817=E6=97=A5=E5=91=A8=E4=BA=8C 21:24=E5=86=99=E9=81=93=EF=BC=9A > > > > > > > > > > On Tue, 17 Mar 2026 at 18:20, Tender Wang wr= ote: > > > > > Yes, it looks like your analysis is valid. Will you share a patch= for > > > > > updating `clause` ? > > > > > > > > > Sorry, it's already the middle of the night here, so I'm afraid I > > > > don't have much time to work on this right now. > > > > > > Thank you for your research. I've written a simple draft patch. It > > > fixes the reported case, but I doubt it is correct in general. I'll > > > continue the investigation. > > I tried the above fix, no error again. But I got a plan like this: > > QUERY PLAN > > -----------------------------------------------------------------------= -------------------- > > Nested Loop Left Join (cost=3D0.00..115164616.71 rows=3D7458350250 wi= dth=3D4) > > -> Seq Scan on pg_table_a tom3 (cost=3D0.00..34.70 rows=3D2470 wid= th=3D0) > > -> Materialize (cost=3D0.00..99509.82 rows=3D3019575 width=3D0) > > -> Nested Loop Left Join (cost=3D0.00..75564.95 rows=3D30195= 75 width=3D0) > > Join Filter: tom2.col_bool > > -> Seq Scan on pg_table_a tom2 (cost=3D0.00..34.70 > > rows=3D2445 width=3D5) > > Filter: ((col_bool IS NOT NULL) AND (col_bool IS N= OT NULL)) > > -> Materialize (cost=3D0.00..47.05 rows=3D2470 width= =3D0) > > -> Seq Scan on pg_table_a tom0 > > (cost=3D0.00..34.70 rows=3D2470 width=3D0) > > (9 rows) > > > > Filter: ((col_bool IS NOT NULL) AND (col_bool IS NOT NULL)) > > This makes me unhappy. > > Your patch gets the same plan. > > > In replace_relid_callback(), we add NullTest to rinfo, but it is not a > logical equal check by restrict_infos_logically_equal(). > I think for baserestrictinfo, we can just use rinfo->clause, no need > to check the equality of RestrictInfo. > > I tried this way, the plan looks as follows: > QUERY PLAN > -------------------------------------------------------------------------= ------------------ > Nested Loop Left Join (cost=3D0.00..115776846.35 rows=3D7498006100 widt= h=3D4) > -> Seq Scan on pg_table_a tom3 (cost=3D0.00..34.70 rows=3D2470 width= =3D0) > -> Materialize (cost=3D0.00..100038.47 rows=3D3035630 width=3D0) > -> Nested Loop Left Join (cost=3D0.00..75966.32 rows=3D3035630= width=3D0) > Join Filter: tom2.col_bool > -> Seq Scan on pg_table_a tom2 (cost=3D0.00..34.70 > rows=3D2458 width=3D5) > Filter: (col_bool IS NOT NULL) > -> Materialize (cost=3D0.00..47.05 rows=3D2470 width=3D0= ) > -> Seq Scan on pg_table_a tom0 > (cost=3D0.00..34.70 rows=3D2470 width=3D0) > (9 rows) > > No redundant filter anymore. > > Please see the attached patch. > > -- > Thanks, > Tender Wang Hi! Your patch looks solid. ChangeVarNodesWalkExpression looks like a generic rewriter utility function, so I was wondering why we never got complaints about bugs related it. But this functions is reachable only when SJE optimisation is allowed, so looks like this explains the issue. --=20 Best regards, Kirill Reshke