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 1w9vTk-001tau-2I for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Apr 2026 01:43:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w9vTj-00DIWn-0d for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Apr 2026 01:43:43 +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.96) (envelope-from ) id 1w9vTi-00DIWc-2Y for pgsql-hackers@lists.postgresql.org; Tue, 07 Apr 2026 01:43:43 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w9vTh-00000000wxk-1gcd for pgsql-hackers@lists.postgresql.org; Tue, 07 Apr 2026 01:43:42 +0000 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-66e129e457dso4436619a12.1 for ; Mon, 06 Apr 2026 18:43:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775526220; cv=none; d=google.com; s=arc-20240605; b=NjqH1QU069Jrq2T4LV+OVXB5/d5GIIc0dBt1PH2l3XKvoRh8bBCjMHJDa3Pb18DYtl Stvw/i/u7TbA+jrM4eUD6xSVEJq4HiNJEorNHj14nTM/JvzwCcOGKA2ObRBNmplDBIo/ nmT4W0iCbxS1DIuUDyOBTpbjKnkXicjh8MFAadOZWqUO+QPux1mLbMI8shOhrupDLwNA 0CvNogVvuUolnosNo4IiQ/G384hAld2HFCg4PKExHDqMoDR8fJya5DnkNRdODlPIRum2 t9bc2lltNHFfgSRj2KTLW5bNW6i8W9adE67bQSrTbdS3d0V+Q6fZ+ONNvTYSthZ+F8Qo UD8A== 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=LH8xhtUnEEQEpGOyLz+kpC21rpthGsazW8Le6mgxHgE=; fh=NOo30AAV3JZ2gV+RE/nn4WjuTPJNrVMgF4c+LPp6+pw=; b=CTx3nVQ0h3kUGkHtGdMz3ua1sSITeigNLKn4zYna3dfWCwK6bhS9fGYC5q56pZqHqw QKWwKguGF+AQF9zzRI7DUb5TyMFevbNFh/hmHj/tYWNMNRTH5y5Rkc9sCTKgNF3voHTy lrAVLiELIt4HyYDL8Azs8LawUCffga6LXiJuCR16moIWBUY4FBm7RCjq4QScUvhvZmzE plDDwsM9RfMjSYehtK7eHLpdmjT+36B1BFvP+rBAQ/oB+M2qNyswVpnUYTGW47BIrN27 afYC9AkiyteH/5AvWQ63yd7kumWTVR8VVHrSkMzBqjcANbXmqboOgwW8zZFtCLky/CBs lovg==; 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=1775526220; x=1776131020; 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=LH8xhtUnEEQEpGOyLz+kpC21rpthGsazW8Le6mgxHgE=; b=Av1AIwm/z3jxVqF1nKyOhoEIvmpnzRADAX9lteJfbE+J/MS63jaOiMug09SFHJb/BT R+c53+mcTvzps4YbkDHmyqdUVcjkhYubhXQa4E3R+jXGeTk825k0lXRtBiqtxI8D+ZTt aOGdKN1exvMW/Z6c1mCL1pPgbd8/2aNZu/OhQlcuUHPGUKJ/nl/i+qy5wDt3LDe4yayf qNLknV+uzJx87IJGrNp/NnHeD4v0B1uG8mmfkcvkH6LvSS5lB+tSElLzCP1EzdQ4Ssn+ NI92Q1lS+rPBuIcyKgY3W3xHmR/4nJUJbCHbbd0nnKkxPfH2wNemk8I/UcYB763qr+4C qKrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775526220; x=1776131020; 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=LH8xhtUnEEQEpGOyLz+kpC21rpthGsazW8Le6mgxHgE=; b=GAl3yXBMYEBW+FUelMjsJaieQ12akkybv5IxwABj35vvxUfW6neQCLMCYPmSAe+aur RoTN696oWUrqCmvHJ5q1ICfxY9h2ci9MMErXLY0KFRDfuRCcxcdOPh3Zv+KOGdeb/vwR sNONjMC2PHupfeZ6aqEHUKS5LtW3c0zUdeGBHuTL6VD1pzqYkFWBFV+LN3XzHqiALnYE dP6KDm8ryII8Jz7gP3m3CpTiFTyvzjLbLGkPg6KV6uS7hOBfkQ66UnlxriHn7qIpqLzV 21aF0W1T5WDzRDopVs8S21R4t1QW9xV4hEskFYKeQzrKT8nVXuyTpRHSAhwro6IkZHkU WOsA== X-Gm-Message-State: AOJu0YxZCRUcHAFJGhJEe+1fN17drbJUwE2GLlycsMmRqBfHT9TPlkhw Kmakxq3x+MFkqy+mj6vkbJN9AlmCb5l3AKtS7G23waPu0/SI6Ps2yGJoUZNo6vqSYc9r9uMOn2D LJbG28IWiRXbsNDnwsKdCqrWS/YZAXJ8= X-Gm-Gg: AeBDievOCNgl7szX56j5xiE4dsyHGudVI3GKEL9lNa4MZNzbQDkjw/qsWMw0Xh9AD8L Of3sNH6nNB2FWZYDEy1YTeI2g6tzkvspHUaawx2Nma9OrcbunStGWtzFij7vM4gV3gMRI4X63Rb eEK0st7NePVxmBI7VOm+f/NNwCjlKDjmtvB368P7Wk84CO7yk3GA3JOtyftnFxmEj979NP7qgTk Xl+RgfGWp9jpHclAc85sn7u4wTPABEMuvh5MFuNUh92coUzlsE5tG630iMYKJQFTsp34Zz4ssN7 PSFQt/qAZWMA5+N8tG4= X-Received: by 2002:a17:907:da1:b0:b9c:9dcb:36c6 with SMTP id a640c23a62f3a-b9c9dcb38ecmr485230766b.47.1775526219847; Mon, 06 Apr 2026 18:43:39 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Tender Wang Date: Tue, 7 Apr 2026 09:43:27 +0800 X-Gm-Features: AQROBzAUHRCHXciGhWoT8RCUfhrt-zvWU5gbxVWWqUYfdGvcMmxBUNYNPUEGe_8 Message-ID: Subject: Re: Clean up remove_rel_from_query() after self-join elimination commit To: Richard Guo Cc: Pg Hackers 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 Richard Guo =E4=BA=8E2026=E5=B9=B44=E6=9C=886=E6= =97=A5=E5=91=A8=E4=B8=80 16:11=E5=86=99=E9=81=93=EF=BC=9A > > While working on reusing remove_rel_from_query() for inner-join > removal, I noticed that the function is in pretty rough shape. The > self-join elimination (SJE) commit grafted self-join removal onto > remove_rel_from_query(), which was originally written for left-join > removal only. Yes, I noticed this a few days ago when I tried to remove a redundant filter added during SJE. >This resulted in several issues: > 1. Comments throughout remove_rel_from_query() still assumed only > left-join removal, making the code misleading. For example: > > * This is relevant in case the outer join we're deleting is nested insi= de > * other outer joins: > The current logic of remove_rel_from_query() is not easy to read. For examp= le, It distinguishes between left-join removal and SJE based on whether sjinfo is NULL. > 2. This was called even for left-join removal, with subst=3D-1. This is > pointless and confusing. > > ChangeVarNodesExtended((Node *) phv->phexpr, relid, subst, 0, > replace_relid_callback); > Yes, it is. > 3. phinfo->ph_lateral was adjusted for left-join removal, which is > also confusing ... > > phinfo->ph_lateral =3D adjust_relid_set(phinfo->ph_lateral, relid,= subst); > > /* > * ph_lateral might contain rels mentioned in ph_eval_at after the > * replacement, remove them. > */ > phinfo->ph_lateral =3D bms_difference(phinfo->ph_lateral, > phinfo->ph_eval_at); > > ... since you can find this Assert just above: > > Assert(sjinfo =3D=3D NULL || !bms_is_member(relid, phinfo->ph_late= ral)); > > 4. The comment about attr_needed reconstruction was in > remove_rel_from_query(), but the actual rebuild is performed by > the callers. > > 5. The EquivalenceClass processing in remove_rel_from_query(): > > /* > * Likewise remove references from EquivalenceClasses. > */ > foreach(l, root->eq_classes) > { > EquivalenceClass *ec =3D (EquivalenceClass *) lfirst(l); > > if (bms_is_member(relid, ec->ec_relids) || > (sjinfo =3D=3D NULL || bms_is_member(sjinfo->ojrelid, ec->ec_= relids))) > remove_rel_from_eclass(ec, sjinfo, relid, subst); > } > > The condition always evaluates to true for self-join elimination > (i.e., sjinfo =3D=3D NULL), meaning every EquivalenceClass gets adjusted. > But this is redundant because the caller remove_self_join_rel() > already handles ECs via update_eclasses(). > > 6. In remove_self_join_rel(), I notice this code: > > /* At last, replace varno in root targetlist and HAVING clause */ > ChangeVarNodesExtended((Node *) root->processed_tlist, toRemove->reli= d, > toKeep->relid, 0, replace_relid_callback); > ChangeVarNodesExtended((Node *) root->processed_groupClause, > toRemove->relid, toKeep->relid, 0, > replace_relid_callback); > > The comment mentions "HAVING clause", but neither processed_tlist nor > processed_groupClause has anything to do with the HAVING clause. > Furthermore, processed_groupClause contains SortGroupClause nodes, > which have no Var nodes to rewrite, so calling ChangeVarNodesExtended > on it is pointless. I didn't find as many as you listed here. I agree that we should do something for current logic. > > The attached patch is an attempt to fix all these issues. It also > aims to leave the code better structured for adding new types of join > removal (such as inner-join removal) in the future. I looked through the attached patch. LGTM. --=20 Thanks, Tender Wang