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 1wFkTi-005VD9-2Q for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Apr 2026 03:11:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wFkTf-00GYQN-2x for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Apr 2026 03:11: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 1wFkTf-00GYPx-20 for pgsql-hackers@lists.postgresql.org; Thu, 23 Apr 2026 03:11:43 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wFkTd-00000002LhU-2K9b for pgsql-hackers@lists.postgresql.org; Thu, 23 Apr 2026 03:11:42 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-483487335c2so60276635e9.2 for ; Wed, 22 Apr 2026 20:11:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776913900; cv=none; d=google.com; s=arc-20240605; b=jQs+fSP3EqFOOblc/o4R9NurumAwpWUx5UUo2mjJnKweb22fc/pyPq+ROl/36PHviG zBUHmVh0JdpctqMHojLRMyZg/CFkcqmp0s1tkXAznZpb9bdsWAIiXxuFaz59ZjuY6n6B P0l8VkAXrwO80UsNR1CeN16fCHfIkenwMTGa9JmplnLzol/MgSVe/JB1dtDkIO71BriB AOAmW31Whh6w8PFsEndWdx2Pp7H60JOrFFdLoiJykcmdAz5WF9Mtt4c6qSUCeZmCD6HS ox0LQZyOH0IeEfGYycAPJaKawdPdwu/yghEVB/Um4qumuzhR717ckdhHmFpCpcnSkKPq dMBw== 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=8Q6a+CgUfuzvK6Iyc7m81Wepn8VQUxDkKzblaZX/z8M=; fh=NOo30AAV3JZ2gV+RE/nn4WjuTPJNrVMgF4c+LPp6+pw=; b=LGEwQkzuJo+msw1XVMioACv+kvJ0WJiy+JZ18V/CbsRdMUeqNgrlYVcm7nhqdy4qMW /QRZOcbbxw5A5KIUM9WI5cdVlU57iTayaYBZtIZekxiWIQmtlpW/xwU92wh38mAlpl6c TKxOYYnzio2Nx5g7oQJXYZYZAmVgIzR+aOl1iZHNExVN8rq3O3Vq4+dpnDjSxqDfGsxq XrBpFz8WOdewyZ3DaeuVI7n9eQx+BJUU0OEkr9PrtqDkorq5S9/Mhvh7DKxHRW4vX3Ul DKWLP3JLU9CdbrqMt/a9f3hV1cL24UFeltp+cnT+rCin1EftpjYTa3pYRoaIfu3xJ6mh 0eoA==; 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=1776913900; x=1777518700; 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=8Q6a+CgUfuzvK6Iyc7m81Wepn8VQUxDkKzblaZX/z8M=; b=VgKnjQBUcgbbkwkMM1Yrjm/xUyQ+uDB/2EwVoAEihs+2oydVkj++dkrBgwDsK+t3jr 5iVL88QxWD6c5RejUHcrrF6/qqnC8Lq6aWP64biCmqdemvJW2Rq0rZghcXaZXgQDK4Dq sqVFelqF2crMfrYn4EqA855jIhsl1SvE2Cq5ZLHGuYdtV+NFxgy6PYO9pb6r9+7YONtx h7jC3Wi6wJdEscc22QEYEy8rHHnMq25BuhSamyBZNzaVa3YH+esO6et2t7yhnlJDLsKM 5cstgDd5VrtwthzZzcr9hnl2FNO7jzXERW5Ba54XFvBcgaANN+Ctp20S4XewHDKXZIFi AJIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776913900; x=1777518700; 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=8Q6a+CgUfuzvK6Iyc7m81Wepn8VQUxDkKzblaZX/z8M=; b=XYrMpLVCNjjpEliIcRx6yRVypJm+yyMpB3N+V+2mjEV4bGyn4UUHhkzvdgBJKjfIEa pP2H2vKJcivX5keH/cZHQeiPU2fZeHYgQUhM2J/bZEwDUprQDIYplVfSlRmDts3KctsX ELwnl+qzFg1MFJAvvOLbg03luzqVx6qIRb/qrDff7MiGhjvkCLu+Q6IDVmRiDOjLfcpN kFzjxpDOwxmORSUm2r3I0NpgE40m0787VNAQJljw3eV/mtz1LxtHuIua1L0Iy1rlDOw3 ALVMQ0uJp9uDXAkM4jtpsGuKrdJAVnp6m2OhYD8lcim6QTcFVLSnSJos5UE48uqtfKtk Y52g== X-Gm-Message-State: AOJu0Yz25hYh90KHH0a9sGj9FUaP+nhW380vYrbOSNtoTtp122RVg6+t oDavENutAvF+AEDNt+uGWtiZ9V1cJULJor4ce7qQ24S6noAeQzFc8r+l1aB5seoq6np+pSy8PSi KQYeUIlXtXDf7FP/YZ3gx6UO9xasnz89waUAB X-Gm-Gg: AeBDiestxXNDbZxqmnW154Ym86DlpeudJcKoO/WHYX7dgnb9dcqapLFRJIgEPU/0NSx ZRHuI/M592/umyLbwAQycYq6w3C2k1z8iG3ypGhKFPy1Ph1DUDhuTdpzM1ckxJnM6ETdCgJPyRw a9kjH4jyubbd+Kwxyq6l/NW3jZhPvGFHLc8+Aob8jsnt7oE1K5XcgX+m30qrmSH3kh8+9FgxfA5 v2t0s7Tllm3st9uVmIWnrwWDxdJLtq3Yv/0Sl+wEvjUQDKM09V7sZAJ51BceoLOSGNA5w6W753d YoVTzk8y77SiDqelHKXpMwZWSuC1XDpf6Hrv3ismofZ1oygDAFBEstP4p42mssxq+9UBmCofkwe kdlChBk4= X-Received: by 2002:a05:600d:8454:b0:48a:5970:2003 with SMTP id 5b1f17b1804b1-48a59702216mr68948435e9.3.1776913900290; Wed, 22 Apr 2026 20:11:40 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Thu, 23 Apr 2026 15:11:27 +1200 X-Gm-Features: AQROBzBTLfw3ODGwEcgMmhC93N4XgJriRJoqYu8gA0RLH-Ax0ILD52CcU0Mwork Message-ID: Subject: Re: The bogus calls in remove_self_join_rel() To: Richard Guo Cc: Pg Hackers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 23 Apr 2026 at 14:46, Richard Guo wrote: > > I noticed these two calls in remove_self_join_rel(): > > adjust_relid_set(root->all_result_relids, toRemove->relid, toKeep->relid); > adjust_relid_set(root->leaf_result_relids, toRemove->relid, toKeep->relid); > > There's no comment explaining them, and as far as I can tell they do > nothing: adjust_relid_set returns a Relids and does not modify the > input in place. > > Rather than make the calls do the cleanup they pretend to do, I think > a better way is to replace them with assertions: toRemove->relid is > not a member of either set. This is true as these two sets contain > only parse->resultRelation (rejected as an SJE candidate to preserve > EvalPlanQual) and inheritance children of the target, which never > appear in the joinlist that SJE scans for candidates. Yeah, it certainly shouldn't be removing any result relations. I see there's a check in remove_self_joins_recurse() for varno != root->parse->resultRelation. Have you followed through on what happens for CTEs that do DML and RETURNING? I assume that fails on the nearby rte->relkind == RELKIND_RELATION, but I didn't debug to check. The only place I see all_result_relids being added to, aside from the initial setting with bms_make_singleton() is for the inheritance expansion in expand_single_inheritance_child(), which happens after join removals. For leaf_result_relids, it's similar. I think the Asserts should go at the top of the function next to the other Asserts. Putting them near the top makes it clearer when reading code. The Asserts will be very close to the function's header comment, so it's easier to get a picture about what the function supports and does, plus, it helps ensure we still get the Asserts before any early returns are taken. It may also be useful to decorate adjust_relid_set() with pg_nodiscard. David