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 1s27h4-0005QR-4l for pgsql-hackers@arkaria.postgresql.org; Wed, 01 May 2024 11:00:09 +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 1s27gz-00CFsc-ND for pgsql-hackers@arkaria.postgresql.org; Wed, 01 May 2024 11:00:06 +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 1s27gz-00CFsU-Dc for pgsql-hackers@lists.postgresql.org; Wed, 01 May 2024 11:00:06 +0000 Received: from mail-lf1-x12e.google.com ([2a00:1450:4864:20::12e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s27gx-000zvk-S3 for pgsql-hackers@lists.postgresql.org; Wed, 01 May 2024 11:00:05 +0000 Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-51dc5c0ffdbso3732069e87.2 for ; Wed, 01 May 2024 04:00:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714561203; x=1715166003; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=FCZZBJuMqy4womVfagxcrW+Oa7m7aPsjgEr2pV6/lP8=; b=Ka74hrfJZ5V9kxXVgy+gurDg1WKf1F1i4GS/EAAhH6rcXCxZ/7WXw/A6GXl4or5ynM fVA4OyjoRqRYKpmBUDO44LklA6ujDrdF0DZqdK8VS+AjtMSAhbURms/+bBTASuDBrR5S HrU66e/rwEq2h/NE81GnRyDM3XJMeypIPm58OLThU0LGMOlSupBfmJhTKNpb1EkCOo6y UGn9jCCmFZXQeXACbtq11OYj3S/BmtoN4OUaPDvA9GRunUGUSPy3sOtpW0ZeFn8P8nST /ZOUTPlxqHmQg2cowj9bbhU8aDH6haKZSiR0z7Tp3x+1ecYAwZHyAcUXpINqlJ3tOf8L yAfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714561203; x=1715166003; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FCZZBJuMqy4womVfagxcrW+Oa7m7aPsjgEr2pV6/lP8=; b=eunMFC3PXLjvHMt+Lz68bnNLDNvG3Zqkztbkh9MNn2nOBZP0foDyftO6tyLHE7sGE0 FzkZD8vwPWNllCgXVxA9Irt/xxWP0GtgkN9IczkXCM5d9bvn6aBgjBLDyc1g2zhF78go AJrOBQ54O36UO8+5cpNZlC7NT3yiBZXuhv6pNTM683WCB5yYE7qcSed0J6G3gQZ8SB5z fzSK4YjM/w7muyvnf6OQUpE3lTsYhamkoGun4f6rfD3q7hR+LKUXrrcs9QJrre45FMJy CcEfBn1zdsjnNc9HumaG9Ngp9WWNyqO9AAc8bqPEfsEPZWiE5WzBXOjSAIGpFcieyLeQ 0asQ== X-Forwarded-Encrypted: i=1; AJvYcCWF3BrTVgnx4xXI+q1px+JodOC2ojjpZuKlORxleovZIu542oM3PCN2HuDmhnlSFLt4zNyr8KhxX8AiE21sVH59T8k55hXaQAauPQeOCB8OZ2rz X-Gm-Message-State: AOJu0YyyBHjfwmq7K96EdvCd0pND+nBFRYZctsqr44iHQn+LzLss4aTg TBr/C6URul0X3GTHH8Pc7Ikcc6WUn1TccuxUDhwFr1NFTqrNAGT+ X-Google-Smtp-Source: AGHT+IHpihxvUJbwWsQzCLFt3g2zeKf1a6K46emJ5jVW0gwqiNj+qt/3Mb/0YcyvjqAE9jf5tEaN3A== X-Received: by 2002:a05:6512:1248:b0:518:dbcc:4f8b with SMTP id fb8-20020a056512124800b00518dbcc4f8bmr1721287lfb.12.1714561202374; Wed, 01 May 2024 04:00:02 -0700 (PDT) Received: from [1.0.0.7] ([178.155.23.194]) by smtp.gmail.com with ESMTPSA id u12-20020a196a0c000000b00516d05bb083sm4794100lfu.206.2024.05.01.04.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 May 2024 04:00:01 -0700 (PDT) Message-ID: <735b5cd4-568d-fbf7-ad8c-dcfb4c3af86e@gmail.com> Date: Wed, 1 May 2024 14:00:00 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: Removing unneeded self joins Content-Language: en-US To: Alexander Korotkov Cc: Andrei Lepikhov , "Gregory Stark (as CFM)" , =?UTF-8?Q?Micha=c5=82_K=c5=82eczek?= , PostgreSQL Hackers References: <64486b0b-0404-e39e-322d-0801154901f3@postgrespro.ru> <05469e80-454b-41d7-d04f-421f42bbd4a9@postgrespro.ru> <48c11b23-9c23-440e-9a3c-020e05196449@postgrespro.ru> <8800ccf2-ae82-461c-92a1-d4c0a22f8cf2@postgrespro.ru> <1bd80083-a13e-4e9f-a7ce-73c3faab521c@postgrespro.ru> <922623af-872e-4cdc-9238-129b18bee42d@postgrespro.ru> <0ed2cf02-e655-6ec7-a4fe-52fd8d572f65@gmail.com> From: Alexander Lakhin 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 30.04.2024 13:20, Alexander Korotkov wrote: > On Tue, Apr 30, 2024 at 9:00 AM Alexander Lakhin wrote: >> I've discovered another failure, introduced by d3d55ce57. >> Please try the following: >> CREATE TABLE t (a int unique, b float); >> SELECT * FROM t NATURAL JOIN LATERAL >> (SELECT * FROM t t2 TABLESAMPLE SYSTEM (t.b)) t2; > I think we should just forbid SJE in case when relations to be merged > have cross-references with lateral vars. The draft patch for this is > attached. I'd like to ask Alexander to test it, Richard and Andrei to > review it. Thank you! Beside LATERAL vars, it seems that SJR doesn't play well with TABLESAMPLE in general. For instance: CREATE TABLE t (a int unique); INSERT INTO t SELECT * FROM generate_series (1,100); SELECT COUNT(*) FROM (SELECT * FROM t TABLESAMPLE BERNOULLI(1)) t1  NATURAL JOIN (SELECT * FROM t TABLESAMPLE BERNOULLI(100)) t2; returned 100, 100, 100 for me, though with enable_self_join_removal = off, I got 4, 0, 1... Best regards, Alexander