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 1tmXTv-007Tbg-Sb for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Feb 2025 12:22:44 +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 1tmXTt-002Flj-Sv for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Feb 2025 12:22:41 +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.94.2) (envelope-from ) id 1tmXTt-002FlW-H3 for pgsql-hackers@lists.postgresql.org; Mon, 24 Feb 2025 12:22:41 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tmXTq-000Wyc-33 for pgsql-hackers@lists.postgresql.org; Mon, 24 Feb 2025 12:22:41 +0000 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-43aac0390e8so3503905e9.2 for ; Mon, 24 Feb 2025 04:22:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740399758; x=1741004558; 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=L+6+vTKygX9sgqRXRB+jwrv2W3gQtx6ysgw8ADRzh6s=; b=Dgqf2Cd03PEi0SfJ3JSsf61g3wQGxersS1Sh5PGx/53SuveMv6d53hwms6ilGoRrft TVh9hOpbQPPF27XGimEMPI23HLk95zqapRXxgahgPHNG8UxjS2fVdCP0L7WeOUJCXdIL Ad6LjlyACD9oYMfCnpadIM8vTfFxd9S8F/SDmF6hFkAdCi0EWJWIJxEhYXx1pFSwJReH hisrM041LbccCRtLNi27ri4UTdUZyp9rtb1OFCb1HIwhctjApKqRsoIRcvqbMiU+P3ke w0LjtgOY7juHqDtWaoyYBMicBsjnNWvnI9LOp3LoDFpZRuJBm4P0emrOnY3AIqnnQEDv pByQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740399758; x=1741004558; 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=L+6+vTKygX9sgqRXRB+jwrv2W3gQtx6ysgw8ADRzh6s=; b=CMZbKgOFh0kVtoqGEVXDcGiplOMWqi9AGrUmUqTPQ53bzFnAC+WFQ3du39kk7+y8Eq tnBv3bVqhS1iXbdSuNvLpNQO/QxRCOGmrY5QVUlH3NZrzNqLjVsCvmz78sS1beYBhugZ 4f5UIsLLYHmKFtGMX+n44apVEfUehB+Fq1oo35kk4lu1JycXZZQuFBWMlg4Gv+Jc88+7 LXgmsgZoMt1bpc1QyMmP6VXYQdBCPVUzcHxkLbOcU+46hXSf8rEBIzxYn5CpwggouLFe MyUFnzXXWNteScaWgCFrKv7hU61bC7Au99MY27qv73j4/MGjBAmaMVXgklOuZF07jjOh HYrA== X-Forwarded-Encrypted: i=1; AJvYcCUpJOmHSk+AUU7cHo5LYYtzZAhWdsL+J1XBe5KesOgLNVxAi3Lwd5QS8M47XBnRrSMLT99PjVH56iz6DKDS@lists.postgresql.org X-Gm-Message-State: AOJu0Yw2ONXaWG4topNBpo2+qlS8F84yf0b8oQVgLhUVlIf54GpFCOlI MDBBpEhBdhb3jqDT5BQmB5xlV8z9zjLTQMzhKr9s1N6J0Y1Gne9Q X-Gm-Gg: ASbGncujrg1WTpvWzYIKspHF+qbp6/NM0gdDzaJG1Emy2koM4JMd92RcVWBCOBKoHU/ wb2eeCdlLIa5oFyLFI0jRoQofIC7WRNvNWog063FfgjUlyYXRIXOsbeBqO6xeturlt+81Clzmw0 UUqvN1cGLl12lEh+UNO0TTQGP+QmvYhfawuoF/PVbC7hHLdV+eAkydohi1KiCMMbQAW6flscH1c BZXGKWHPer+4g0+vZ6rduiXrK4/RUCs0NSrCbvs7imKvlE5BOAmsqAfMEvg4aUhROepXEsgKSwd BIPjcvbyEHpRM3l5cnSoBBvJvs1N0XSrkeM= X-Google-Smtp-Source: AGHT+IFXpoiv8swzAx/gsWM6HmZyv5ZaZCmpDeQMJER10HMP9Pc0BxQVarZ5/UbVDrCJwmdYY4Tklw== X-Received: by 2002:a05:600c:1c98:b0:439:8ec2:a9dc with SMTP id 5b1f17b1804b1-439ae227e58mr117932495e9.31.1740399757681; Mon, 24 Feb 2025 04:22:37 -0800 (PST) Received: from [172.20.10.2] ([77.211.4.125]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-439b031c5casm104356835e9.39.2025.02.24.04.22.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 04:22:37 -0800 (PST) Message-ID: <5b0ca934-4c61-4c29-8ad4-8988bed1ca0c@gmail.com> Date: Mon, 24 Feb 2025 13:22:35 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Removing unneeded self joins To: Alexander Korotkov Cc: Tom Lane , Alena Rybakina , PostgreSQL Hackers , Robert Haas , Richard Guo , Alexander Lakhin , "Gregory Stark (as CFM)" , =?UTF-8?Q?Micha=C5=82_K=C5=82eczek?= , Dean Rasheed References: <64486b0b-0404-e39e-322d-0801154901f3@postgrespro.ru> <5934ce1e-be02-4640-904c-f7fec3962784@gmail.com> <914330.1740330169@sss.pgh.pa.us> Content-Language: en-US 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 24/2/2025 11:57, Alexander Korotkov wrote: > Hi, Andrei! > > Thank you for your feedback. > > On Mon, Feb 24, 2025 at 12:12 AM Andrei Lepikhov wrote: >> On 23/2/2025 22:15, Alexander Korotkov wrote: >>> There is my attempt to implement this approach. Getting rid of local >>> variable (and computation of the same value other way) required to >>> change arguments of remove_rel_from_eclass() as well. I'm going to >>> further polish this tomorrow. >> I passed through the patch. It works correctly. >> >> Multiple ifs in a single routine is not ideal. > > I would say there is a brachcing anyway. Even if it is inside > adjust_relid_set(). Patch makes it more explicit. Ideal or not, I > don't think it gets worse. > >> I have thought about it >> already, and it seems the remove_rel_from_query needs refactoring: when >> I first reused it for self-join removal, we didn't have the 'ojrelid' >> machinery, and it was implemented smoothly. >> Right now, this code contains multiple places where we need to remove >> the outer join relid and separating the removal code for the baserel and >> outer join may simplify the logic and make it safer. >> But the way to do it is not apparent now. May be if we implement a new >> technique of query tree reduction, the approach will become more evident. > > Could you, please, elaborate more on what you mean by "new technique > of query tree reduction"? I mean any transformations and optimisations that reduce search space for optimisation. Right now, I see the features reduce_unique_semijoins, remove_useless_joins, and remove_useless_self_joins. In practice, I see at least a join on a foreign key, where some cases potentially allow the removal of the JOIN operator. -- regards, Andrei Lepikhov