Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pQ9dP-0008BT-QI for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Feb 2023 16:18:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9dO-0006In-Kj for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Feb 2023 16:18:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pQ9dO-0006Ie-A3 for pgsql-bugs@lists.postgresql.org; Thu, 09 Feb 2023 16:18:54 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pQ9dL-0005X2-Ix for pgsql-bugs@lists.postgresql.org; Thu, 09 Feb 2023 16:18:53 +0000 Received: by mail-ej1-x632.google.com with SMTP id jg8so7933676ejc.6 for ; Thu, 09 Feb 2023 08:18:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=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=VEWGAPNQ8KMjbpgYTpcKmXES93GFrFYgyIQ584STK4E=; b=Nr5zDQkiaajBq3hsjrznHfCO/Cg6psqhrdxnQ/jKLjGSMiQCA8JIfsdzoFoRa7Z3oo mL1d16+Ey3E91zfK1+A43eajcxhch8MHAoeTkQAACDSQfNgPu0sTttLBtrMiMq72CfN8 f6L6bxQCeL0sakBMRsr1vFSs4NaoaJ1PQR1dfo0jCnkT4Bjkd4SVHRX7nT6AQDO+ELv4 nNWKLRpTJMss7y0Xwt4b8BoJx/fkGaAbDXD+vwvX4vRkPl8y8OD0BCnTrc86EH8Hej84 qcVIiVWnzdU4eOQV6Ey+Kz4D0PZn75JA7MD1fRin5keKAKUn223tQwA35JXOWWn4n+++ o0Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=VEWGAPNQ8KMjbpgYTpcKmXES93GFrFYgyIQ584STK4E=; b=hGNGQcwAZpu+rGN3badSkYYfO3Iyr7svLLJiehHNtQrgQ1XTOLIRLFHEv/GKWE/Nrd PLNwIOzSTxFEV9kkO5N9IyWCFXw6LKhtka6JTUXU/I2zBcpd09TuoZjfduv2qqhHt+LQ jAd5HO5roMuPWxTs4OG66O3JSJrlxEzSxIapglKgl6E7uBVRsenE0TYWmK1rIMpHzedo dcqB3GUu91tHGgQegj20ePna3sQ684TIVbVmt8Rz6TEvo9Z/BnXsDqiGliIPMnO6zYEe RUW7AVJoUw8LYsR6UNST3v7ZGiKwurERSY88u4zwP7WUEK732f5q5tCzoI/y6T/peF8T ZMLQ== X-Gm-Message-State: AO0yUKX62J8p3IaVfgHbOxqCNBgIIH/h56dtv4isYVFBFbznuVk8jZt9 3vUOZ6BD8gTOqRU4rDnsZz0zqeleX9FrE1UWt5fGd7nQF6nlQmQz0EmO3LZKTEIsiBPcAMFIDx8 n1gn2SABz9XddZuRK4bfICTlNbhWFjTdBYFnxeBFZHeWcC7WAHwqA6dn0ENJYdvi8N7W1WB3OXe 4My95EfAOKYmPx9dmQVf7T6lBL/lyctSj3JID1V/rhfumi+6zRv/MojuwmjnbNrQDi9hhlaOlV9 Ceuuw1f/n8eB/0Yya3YpoeWj33XA28OksVND8NiZ48Lzibx X-Google-Smtp-Source: AK7set/cDLSa10gBWlMwTI0bi61LaG854on1QXX3/I1tGEjiuqRYdmN3FBjh2avTG+NWztZCBOBzIA== X-Received: by 2002:a17:907:724e:b0:8af:2fa1:5ae5 with SMTP id ds14-20020a170907724e00b008af2fa15ae5mr5200867ejc.53.1675959530515; Thu, 09 Feb 2023 08:18:50 -0800 (PST) Received: from [10.137.0.17] (nat-106.starnet.cz. [178.255.168.106]) by smtp.gmail.com with ESMTPSA id c10-20020a170906340a00b008857fe10c5csm1035179ejb.126.2023.02.09.08.18.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Feb 2023 08:18:50 -0800 (PST) Content-Type: multipart/mixed; boundary="------------4l6AnDx0ONEyDv8uXO3qUCB3" Message-ID: <5a8acd6d-79ba-1017-e1df-46dd948347f1@enterprisedb.com> Date: Thu, 9 Feb 2023 17:18:47 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: BUG #17774: Assert triggered on brin_minmax_multi.c Content-Language: en-US To: John Naylor , Tom Lane Cc: Dmitry Dolgov <9erthalion6@gmail.com>, tharakan@gmail.com, pgsql-bugs@lists.postgresql.org References: <17774-c6f3e36dd4471e67@postgresql.org> <20230208100658.mz2kkeruoma6iqtw@ddolgov.remote.csb> <3485884.1675868581@sss.pgh.pa.us> <20230208153313.fym7at3ubgrec67a@ddolgov.remote.csb> <3492169.1675872316@sss.pgh.pa.us> From: Tomas Vondra In-Reply-To: X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------4l6AnDx0ONEyDv8uXO3qUCB3 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 2/9/23 04:53, John Naylor wrote: > > On Wed, Feb 8, 2023 at 11:05 PM Tom Lane > wrote: >> >> Dmitry Dolgov <9erthalion6@gmail.com > > writes: >> >> On Wed, Feb 08, 2023 at 10:03:01AM -0500, Tom Lane wrote: >> >> I'd believe this argument more readily if the calculation weren't being >> >> done in float arithmetic.  Since it is, you're at the mercy of roundoff >> >> error ... and that small negative delta could certainly pass for >> >> roundoff error. >> >> > Hmm...yeah, good point. In both the reproducer I've posted and the >> > backtrace from the thread the delta is indeed rather small. >> >> I bet also it only fails when dealing with IPv6 addresses. >> With 32-bit IPv4 addresses, a float8 would have enough mantissa >> bits that the calculation wouldn't become imprecise. > > Addresses from different families are treated as a distance of one, so I > don't think inserting a single IPv6 address would get this far, and in > fact I still get a crash in an empty table by taking out the IPv6 > address from Dmitry's example: > > insert into brin_test values('127.0.0.1/0' ); > insert into brin_test values('0.0.0.0/12' ); > > Adding some debug calls shows it starts off negative from the start: > > =# insert into brin_test values('127.0.0.1/0' ); > INSERT 0 1 > =#  insert into brin_test values('0.0.0.0/12' ); > NOTICE:  idx: 3 before div: -1.000000 > NOTICE:  idx: 3 after div: -0.003906 > NOTICE:  idx: 2 before div: -0.003906 > NOTICE:  idx: 2 after div: -0.000015 > NOTICE:  idx: 1 before div: -0.000015 > NOTICE:  idx: 1 after div: -0.000000 > NOTICE:  idx: 0 before div: -0.000000 > NOTICE:  idx: 0 after div: -0.000000 > > ...so something else is wrong here but I haven't dug deeper yet. > I believe the bug is pretty trivial - the code applies the netmask incorrectly, so that with 127.0.0.1/0 it ends with 0.0.0.1, and because it assumes 0.0.0.1 < 0.0.0.0 it ends with negative delta. In particular, the issue is that the code does this: lena = ip_bits(ipa); -- 0 len = ip_addrsize(ipa); -- 4 for (for (i = 0; i < len; i++) { nbits = lena - (i * 8); ... mask = (0xFF << (8 - nbits)); ... } But for 127.0.0.1/0 we get lena=0, so for i>0 nbits gets negative, and the shift is probably going to do something silly (not sure what exactly, but AFAICS it's undefined behavior). Attached is a fixup that resolves this failure for me. I need to look a bit closer if there are some other issues (e.g. with the float rounding errors, etc.). Good thing is this is this won't break anything without the assert - we may pick incorrect ranges to merge, so the index is a bit less efficient, but still valid/correct. -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------4l6AnDx0ONEyDv8uXO3qUCB3 Content-Type: text/x-patch; charset=UTF-8; name="brin-inet-fixup.patch" Content-Disposition: attachment; filename="brin-inet-fixup.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2FjY2Vzcy9icmluL2JyaW5fbWlubWF4X211bHRp LmMgYi9zcmMvYmFja2VuZC9hY2Nlc3MvYnJpbi9icmluX21pbm1heF9tdWx0aS5jCmluZGV4 IGFjNjcwZmQwMmQ3Li5iZTAzZDYxYzRjYSAxMDA2NDQKLS0tIGEvc3JjL2JhY2tlbmQvYWNj ZXNzL2JyaW4vYnJpbl9taW5tYXhfbXVsdGkuYworKysgYi9zcmMvYmFja2VuZC9hY2Nlc3Mv YnJpbi9icmluX21pbm1heF9tdWx0aS5jCkBAIC0yMzY0LDE0ICsyMzY0LDE0IEBAIGJyaW5f bWlubWF4X211bHRpX2Rpc3RhbmNlX2luZXQoUEdfRlVOQ1RJT05fQVJHUykKIAkJdW5zaWdu ZWQgY2hhciBtYXNrOwogCQlpbnQJCQluYml0czsKIAotCQluYml0cyA9IGxlbmEgLSAoaSAq IDgpOworCQluYml0cyA9IE1heCgwLCBsZW5hIC0gKGkgKiA4KSk7CiAJCWlmIChuYml0cyA8 IDgpCiAJCXsKIAkJCW1hc2sgPSAoMHhGRiA8PCAoOCAtIG5iaXRzKSk7CiAJCQlhZGRyYVtp XSA9IChhZGRyYVtpXSAmIG1hc2spOwogCQl9CiAKLQkJbmJpdHMgPSBsZW5iIC0gKGkgKiA4 KTsKKwkJbmJpdHMgPSBNYXgoMCwgbGVuYiAtIChpICogOCkpOwogCQlpZiAobmJpdHMgPCA4 KQogCQl7CiAJCQltYXNrID0gKDB4RkYgPDwgKDggLSBuYml0cykpOwo= --------------4l6AnDx0ONEyDv8uXO3qUCB3--