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 1qXpj8-0008Vc-Vv for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Aug 2023 21:12:50 +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 1qXpj7-000pq3-CU for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Aug 2023 21:12:49 +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 1qXpj7-000ppt-2e for pgsql-bugs@lists.postgresql.org; Sun, 20 Aug 2023 21:12:48 +0000 Received: from mout.kundenserver.de ([212.227.17.10]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qXpiz-0002mG-Py for pgsql-bugs@lists.postgresql.org; Sun, 20 Aug 2023 21:12:48 +0000 Received: from vmpopsmtp.local ([185.212.53.230]) by mrelayeu.kundenserver.de (mreue106 [213.165.67.113]) with ESMTPSA (Nemesis) id 1MEVmm-1qV96z1up8-00G4Xy; Sun, 20 Aug 2023 23:12:39 +0200 Received: from mackerio.local (pakerio.local [192.168.2.101]) by vmpopsmtp.local (Postfix) with ESMTPS id 8000114035C; Sun, 20 Aug 2023 23:12:38 +0200 (CEST) X-Footer: cGhhcm1hcHAuZGU= Received: from wsmpbm2.fritz.box ([10.0.8.53]) (authenticated user james.inform@pharmapp.de) by mackerio.local (Kerio Connect 9.2.7 patch 3) with ESMTPSA (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)); Sun, 20 Aug 2023 23:12:59 +0200 Subject: Re: BUG #18060: Left joining rows using random() function in join condition doesn't work as expected. To: David Rowley Cc: Tom Lane , pgsql-bugs@lists.postgresql.org References: <18060-e59408b5655979ed@postgresql.org> <2548808.1692547941@sss.pgh.pa.us> From: James Inform Message-ID: <48e87ec7-4371-3524-79d0-18439640ddc0@pharmapp.de> Date: Sun, 20 Aug 2023 23:12:35 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:52.0) Gecko/20100101 PostboxApp/7.0.60 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Provags-ID: V03:K1:ZGhjmwl3dTdD08hAjLWVq0Ey3Vndo3MO3977dDDwr3WQ9L+q00e L7lIPv8f51sUJ4qAot5YHUo1Y/jYhYbFkZbDaWKPOEfUEt9VHDrFtikmoMN/+lYbOohD34X 1/YJblFl7LHITSmFQrsARV0FW+pw57xEwJDx8HzvEN7alzkgnQUBhInLzDwzjAG9OM9QGL1 jIdPw3cZ+y9IMxAtTqtOg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:AkXm0vVVv8o=;8QlWYYtrUp/1PsEmTm7UOhLP/gh Alg4cz/o/TIaKSZnJsff7Px98uEBNAePYnw9Qc23pHtw45WN3zibjkFI6CzMTE0Jz8lBf6uZy 9o1mbfUv//QyW008L06xXaL7kmezFsWZHm9dYkIGNKsPjBdSzTCkOOZqJ7wcVJJKJdGYfkFiA /JefbLXKcemNKGchAeM9xNapArWJsKwq7hZc8lSKC2fdFi++UMfozTAxkt8og6lkaoiBBPJnS LOKXgz7QZwMVCakE/TdyQYTEvgToJGFTS+c/xPFopSdyAYITlGAInX0ZsPyGhXXHBqn8KRUeS IA6zD+iDhsgzkXQRyVrlQs/7WWBpYtv+m3nTGYyFuwPkgekB91GCo46NsGRuZgh33ZZ3thEd6 kdtYoYwr/hWAU4g3xQNwZc4tmrC5cycSYmHTs8s5zKIQSvJxKGycGa77u9ObQmW1ADXHgb3M0 eakfnEMk0k6ewzPFSKspP2SHsiZRizkY+YWN0MDhzehDInKMCFhChqp20kLM0sbi+9eH66rzf BSw4rJ0StGr2gv/X14LqNaSxPNVUT1hdpoyd+RVQdqjeOzUGb85NJI4/VfZxUdGjGdzdgNldT 34PB3+gcJKHtZLM2JilmNbc86JnL2PCMrWyTzIwKSiisxeZ2IXeueirF+m0JiZQ2OTjBVF+kG sNuNkTl2qi1fX7l02UlRnHDcMx697cwbheGHNx7Z6q3DzvOI5928N6AvCrYVKZK/RotB94dks k1ySyCzfXQ/RfcE1awJouEb5ydcias2kkzwmZ9gRZoxHvVa+uYrz73tAVagGDbFeD4AJ55xw5 lUzM68BLx0J37KzgRpHph8OQBJ2V0xZ/zKy2fwVOz44wZhc3CZ/XRVRD7M+cj2XKw8mwUeG90 CHYNAiRN9Zk5cdg== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thank you David for this detailled explanation. Makes things very clear. David Rowley schrieb am 20.08.23 um 23:04: > On Mon, 21 Aug 2023 at 06:26, James Inform wrote: >> Just looked at the results again. >> >> They look strange. When I execute the query multiple times I get three >> kinds of results: >> - NO match on all columns >> - THE SAME match on all columns >> - THREE DIFFERENT columns that are repeated for all the 1000 rows. >> >> With your explanation there should be randomly assigned rows appearing, >> but the seems to be a pattern. At least more than those three different >> ones. >> >> Have you executed my example and looked at the results? >> If not, please give it a try. Nothing looks random there. > You might expect that the "link_id = 1 + (random()*500)::int8" is > evaluated as a join qual because you put it in the ON clause, but > PostgreSQL will distribute these quals to the lowest location that > they can be evaluated. Since the only column that's mentioned in your > join expression belongs to the "link" table, then the qual is > evaluated at the scan level for that relation. You'll notice this if > you look at the EXPLAIN output. > > The reason you sometimes get no matches is simply that on that > execution of the query, the random number didn't happen to line up > with any of the particular link_ids on any of the scanned tuples. > > The reason you sometimes get the same match is that 1 tuple happened > to match the random number during the scan, and that tuple was joined > to 1000 times on your effective clauseless join. > > The reason you see three different columns being matches it is that 3 > tuples happened to match your random expression during the scan and > the clauseless join joined all three, resulting in 3000 rows rather > than 1000 rows in the final output. > > The link table is only scanned once due to the Material node in the > Nested Loop join. If you did: SET enable_material = off; then the > scan would be performed once per row in the "source" table. That > would mean the random() function would be executed 1 million times > instead of 1 thousand times. > > It might take you a while, but if you tried enough times, all the > planets would align and "link_id = 1 + (random()*500)::int8" would > happen to match all tuples during the scan. The query would then > return 1 million rows. > > PostgreSQL wouldn't have pushed your ON qual down to the scan level if > you'd included some column from the "source" table in the expression. > I'm not sure what good it'd do you, but you'd see different results > using something like "link_id = source_id * 0 + 1 + > (random()*5)::int8" (note the multiplication by 0) > > David