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 1sTH7I-00EdZS-KP for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jul 2024 08:31:28 +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 1sTH7G-0093rY-U4 for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jul 2024 08:31:26 +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.94.2) (envelope-from ) id 1sTH7G-0093rQ-K8 for pgsql-hackers@lists.postgresql.org; Mon, 15 Jul 2024 08:31:26 +0000 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sTH7E-0028Tx-2x for pgsql-hackers@lists.postgresql.org; Mon, 15 Jul 2024 08:31:25 +0000 Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-52e9b9fb3dcso4898770e87.1 for ; Mon, 15 Jul 2024 01:31:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721032279; x=1721637079; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=m7A/xAQcsvsr8R34g56G3rWMPRgD/WWJUjeYthEB2ys=; b=JT86CZVXq6tvybZSZ94qozHAgjRabAg+dvSsqt4lBV8RcJXEM7WG6MM18/bvTHIRXe VQXtAWT5VFRBrn3nBgfia7tJpkRWnWM/uMR2czch6uHj4y9O/epzW41zW0105omQHfsq UJi/hq92GVROWovY+Txd9hwJY6EqojePFuO+BGlGVJcLA9ivGXm470O9CTbROZQNwPT+ 9mm+NEWGI/dt/29XFgWsTKbBMIvihW5LFIj7HgfZDJbIcC28ZQcSH8LJ3iz7sEeC+izV t7nt4NEFDsFIxIqcJbUXI+olPEt1oKgb5darL9Xv2jtlksF1Kn9COc5QyCGuqvjFxowf z01A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721032279; x=1721637079; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=m7A/xAQcsvsr8R34g56G3rWMPRgD/WWJUjeYthEB2ys=; b=eiAxAEr73aXGJQtNI8ZIC7WQSK15EOYngRCU3XznZiIXy32Oi0cotbXRQJpp5tTqCb FDxiuuWYk7AGjwrpPT3sCRT8iofnuGrT1IApcFfmgoh+kYScRs+blDL4plZLsHQsWbwb V4JwPiz4tuii6/1oLDcbfbFl3GPY6tEjT6HeEqNRvAMmnK8+vAxnQS88B0gJvr2+jnJH uiWjHSy11WO7accKkOBFJEfGdh7iGONpLThs2jcbqEEY2L9lXbx4Zg9ew1m5c1YD9BTX bi89K/QSqM2pLg8kGfEJ9Cxm+gu8kwww8tOaJKQ4Njmk+5FIwCx7vTfNrMLWvcyyugYF QmLw== X-Forwarded-Encrypted: i=1; AJvYcCVRQGZ51G5QSnhIvYh5BTyDBgz3ac9dKYtZ+nTcZF8JBJ25oxmVmEINBaVEPfACq3DQo/E9DqwuKNRgeV6VATE82JuuOgBol65kOAoFJ9UQAAB5 X-Gm-Message-State: AOJu0YwUEHM/+STKc0Nihn0M+p3RBEd5MMfuaqny+jHlojKWW+ThjcBY yeOfbBhWsWUKbI9TU+5tDDRc+EH2sk2MfSEVdLgBU2dXNYi5nc9i X-Google-Smtp-Source: AGHT+IEPgHzCv76XOuljmlQwJWxVZziICxDbrpMzeo0vHcr/iXIeEryKehp/E/nTqYdrkF4nfptYMQ== X-Received: by 2002:a05:6512:23a6:b0:52c:a88b:9992 with SMTP id 2adb3069b0e04-52eb99d201amr14353759e87.52.1721032278494; Mon, 15 Jul 2024 01:31:18 -0700 (PDT) Received: from [10.10.20.99] (node-boq.pool-113-53.dynamic.totinternet.net. [113.53.59.42]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52ed2540ac3sm767079e87.298.2024.07.15.01.31.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Jul 2024 01:31:18 -0700 (PDT) Message-ID: <3d9457cc-a440-48a5-bf44-8eb3f65316f7@gmail.com> Date: Mon, 15 Jul 2024 15:31:08 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Removing unneeded self joins To: jian he Cc: Alexander Korotkov , Tom Lane , Robert Haas , Andrei Lepikhov , Richard Guo , Alexander Lakhin , "Gregory Stark (as CFM)" , =?UTF-8?Q?Micha=C5=82_K=C5=82eczek?= , PostgreSQL Hackers References: <64486b0b-0404-e39e-322d-0801154901f3@postgrespro.ru> <2422119.1714691974@sss.pgh.pa.us> <24e93efd-eeac-43a6-a75a-ad68eacb32ee@postgrespro.ru> <3622801.1715010885@sss.pgh.pa.us> <96250a42-20e3-40f0-9d45-f53ae852f8ed@gmail.com> <5b49501c-9cb3-4c5d-9d56-49704ff08143@gmail.com> <6da9bdb2-3ff2-4b8b-818a-8052772f2163@gmail.com> Content-Language: en-AU From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/15/24 14:35, jian he wrote: > On Mon, Jul 15, 2024 at 2:08 PM Andrei Lepikhov wrote: >> >> On 7/15/24 12:31, jian he wrote: >>> hi. >>> Here is the latest patch (v6), >>> I've made the following changes. >>> >>> * disallow original Query->resultRelation participate in SJE. >>> for SELECT, nothing is changed. for UPDATE/DELETE/MERGE >>> we can do: >>> EXPLAIN (COSTS OFF) >>> UPDATE sj sq SET b = sq.b + sz.a FROM (select s1.* from sj s1 join sj >>> s2 on s1.a = s2.a) as sz >>> WHERE sz.a = sq.a; >>> >>> here, only "(select s1.* from sj s1 join sj s2 on s1.a = s2.a)" can >>> apply to SJE. >>> >>> but for now we cannot apply SJE to >>> EXPLAIN (COSTS OFF) >>> UPDATE sj sq SET b = sq.b + sz.a FROM sj as sz WHERE sz.a = sq.a; >>> >>> so the EPQ abnormality issue[1] won't happen. >>> >>> >>> * add a new function: ChangeVarNodesExtended for >>> address concerns in [2] >> I see you still stay with the code line: >> if (omark && imark && omark->markType != imark->markType) >> >> It is definitely an error. What if omark is NULL, but imark is not? Why >> not to skip this pair of relids? Or, at least, insert an assertion to >> check that you filtered it earlier. >> > > i think "omark is NULL, but imark is not" case won't reach to > remove_self_joins_one_group. > In that case, omark associated RangeTblEntry->rtekind will be RTE_SUBQUERY, > and will be skipped earlier in remove_self_joins_recurse. > > > Still, do you think the following code is the right way to go? > > if ((omark == NULL && imark != NULL) || > (omark != NULL && imark == NULL) || > (omark && imark && omark->markType != imark->markType)) > continue; Sure, if query block needs RowMark it applies proper RowMark to each base relation. All pull-up transformations executes before this code. But it is worth to set Assert at the point to check that nothing changed in the code above and the patch works correctly, am I wrong? -- regards, Andrei Lepikhov