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 1qt7vA-00AwQ5-Oy for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Oct 2023 14:53:16 +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 1qt7v7-0059FP-Pf for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Oct 2023 14:53:14 +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 1qt7v7-0059Dw-B1 for pgsql-hackers@lists.postgresql.org; Wed, 18 Oct 2023 14:53:14 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qt7v4-0016Z4-7E for pgsql-hackers@lists.postgresql.org; Wed, 18 Oct 2023 14:53:12 +0000 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-32d569e73acso5984972f8f.1 for ; Wed, 18 Oct 2023 07:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1697640788; x=1698245588; 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=90ksXWViHFR2HiutWstY19yQbwc8VQPocJae+6hFDbc=; b=L5rX+2iW2UOMZTDGt3zZXyu971LPE7ufN78OCtkHclxdWj7nW2tZA/BiSUoOOEDs2g eklFY651aHUt9JPjhVq/WeLEGK68OBmyr8kUYEUWbIhuKt36CTlkSqA2YmkleNKgrlxv 7oAu1Zrv6EmDtq1r7K0ThCzyieSF7VQIx9Q1Ezcdq2ss0fyJy0l5Eg5sMoVekjM7dtHW b8DJ2DxeKW5iIN46les+cXSo77to+TJUtjUH4OjVl8cGcLVz/L8nOJzdgbnL3EulYQhf nOmJfN13Z9QURYGXFo2Bfh7G3XcZFWomadva/MF/x6BkcRGqCtrXag1/gaKvskKgTWnO +0LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697640788; x=1698245588; 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=90ksXWViHFR2HiutWstY19yQbwc8VQPocJae+6hFDbc=; b=prFbrMHGNF+vCCehCI+uPqrGAT010V/QJvgy9Km3vWlPQbbjFn1+Bnsg5aDkdLk5Xk //SPYD6HHSmnk0aQ7IXSIwqxQRcIazLJfVjcmxG/ZZTgB0YC6D7e9uKNH9syi4UXQCEj DiQ2z+mKRfXc/oCdu3HutTvbCJwrQRBkaHrQeoMgecHw8qT5rtZVi2XrxRPqYNJ7W6BE fYL+S6k3xc58ZUIbzY4uWsuQIbLgBPNkaYmlDTlJ7a2RttvBFvBCx8JuuGrT74JX3UKe HXSbC0JDFfCD0FXePgLshGbJOhi98VAts/WHXn17Hsd1GQ4vU3XwXTShQ26ECNoHtRKG NI4g== X-Gm-Message-State: AOJu0YycjM2lvyUmyEDX9IVtSqlqKhpnH4kK87e/awFqF0CA+N8NxWKb cCuMGg4kRxWMZroOb5x4QgUSeg== X-Google-Smtp-Source: AGHT+IFhKS4pWkqGo92yBYfgjkmMKKyqYygb3MILjbtsXXDEdeYPVqCLz4QWsMCh9dPmHIweaUJUmg== X-Received: by 2002:a5d:4990:0:b0:32d:aff1:58e6 with SMTP id r16-20020a5d4990000000b0032daff158e6mr5036624wrq.54.1697640787679; Wed, 18 Oct 2023 07:53:07 -0700 (PDT) Received: from [10.137.0.17] (srv1.mobissw.com. [89.235.0.226]) by smtp.gmail.com with ESMTPSA id l1-20020a5d6741000000b003200c918c81sm2273620wrw.112.2023.10.18.07.53.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Oct 2023 07:53:07 -0700 (PDT) Message-ID: <575aa0fe-311d-d3d0-1f1a-abb1b9665a63@enterprisedb.com> Date: Wed, 18 Oct 2023 16:53:05 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: BRIN minmax multi - incorrect distance for infinite timestamp/date Content-Language: en-US To: Ashutosh Bapat Cc: Dean Rasheed , PostgreSQL Hackers References: <14c205c6-fbf4-d2f2-3ea8-a7d5c5d63180@enterprisedb.com> <75de1895-3bd9-242e-cf13-3f5b0281e676@enterprisedb.com> <31400145-6f71-a6b2-e371-048a8e1584ef@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/18/23 12:47, Ashutosh Bapat wrote: > On Wed, Oct 18, 2023 at 1:55 AM Tomas Vondra > wrote: >> >> Hi, >> >> Here's a couple cleaned-up patches fixing the various discussed here. >> I've tried to always add a regression test demonstrating the issue >> first, and then fix it in the next patch. > > It will be good to commit the test changes as well. > I do plan to commit them, ofc. I was just explaining why I'm adding the tests first, and then fixing the issue in a separate commit. >> >> In particular, this deals with these issues: >> >> 1) overflows in distance calculation for large timestamp values (0002) > > I could reduce the SQL for timestamp overflow test to just > -- test overflows during CREATE INDEX with extreme timestamp values > CREATE TEMPORARY TABLE brin_timestamp_test(a TIMESTAMPTZ); > > SET datestyle TO iso; > > INSERT INTO brin_timestamp_test VALUES > ('4713-01-01 00:00:30 BC'), > ('294276-12-01 00:00:01'); > > CREATE INDEX ON brin_timestamp_test USING brin (a > timestamptz_minmax_multi_ops) WITH (pages_per_range=1); > > I didn't understand the purpose of adding 60 odd values to the table. > It didn't tell which of those values triggers the overflow. Minimal > set above is much easier to understand IMO. Using a temporary table > just avoids DROP TABLE statement. But I am ok if you want to use > non-temporary table with DROP. > > Code changes in 0002 look fine. Do we want to add a comment "cast to a > wider datatype to avoid overflow"? Or is that too explicit? > > The code changes fix the timestamp issue but there's a diff in case of > I did use that many values to actually force "compaction" and merging of points into ranges. We only keep 32 values per page range, so with 2 values we'll not build a range. You're right it may still trigger the overflow (we probably still calculate distances, I didn't realize that), but without the compaction we can't check the query plans. However, I agree 60 values may be a bit too much. And I realized we can reduce the count quite a bit by using the values_per_range option. We could set it to 8 (which is the minimum). >> >> 2) incorrect subtraction in distance for date values (0003) > > The test case for date brin index didn't crash though. Even after > applying 0003 patch. The reason why date subtraction can't overflow is > a bit obscure. PostgreSQL doesn't allow dates beyond 4714-12-31 BC > because of the code below > #define IS_VALID_DATE(d) \ > ((DATETIME_MIN_JULIAN - POSTGRES_EPOCH_JDATE) <= (d) && \ > (d) < (DATE_END_JULIAN - POSTGRES_EPOCH_JDATE)) > This prevents the lower side to be well within the negative int32 > overflow threshold and we always subtract higher value from the lower > one. May be good to elaborate this? A later patch does use float 8 > casting eliminating "any" possibility of overflow. So the comment may > not be necessary after squashing all the changes. > Not sure what you mean by "crash". Yes, it doesn't hit an assert, because there's none when calculating distance for date. It however should fail in the query plan check due to the incorrect order of subtractions. Also, the commit message does not claim to fix overflow. In fact, it says it can't overflow ... >> >> 3) incorrect distance for infinite date/timestamp values (0005) > > The tests could use a minimal set of rows here too. > > The code changes look fine and fix the problem seen with the tests alone. > OK >> >> 4) failing distance for extreme interval values (0007) > > I could reproduce the issue with a minimal set of values > -- test handling of overflow for interval values > CREATE TABLE brin_interval_test(a INTERVAL); > > INSERT INTO brin_interval_test VALUES > ('177999985 years'), > ('-178000000 years'); > > CREATE INDEX ON brin_interval_test USING brin (a > interval_minmax_multi_ops) WITH (pages_per_range=1); > DROP TABLE brin_interval_test; > > The code looks fine and fixed the issue seen with the test. > > We may want to combine various test cases though. Like the test adding > infinity and extreme values could be combined. Also the number of > values it inserts in the table for the reasons stated above. > I prefer not to do that. I find it more comprehensible to keep the tests separate / testing different things. If the tests were expensive to setup or something like that, that'd be a different situation. >> >> All the problems except "2" have been discussed earlier, but this seems >> a bit more serious than the other issues, as it's easier to hit. It >> subtracts the values in the opposite order (smaller - larger), so the >> distances are negated. Which means we actually merge the values from the >> most distant ones, and thus are "guaranteed" to build very a very >> inefficient summary. People with multi-minmax indexes on "date" columns >> probably will need to reindex. >> > > Right. Do we highlight that in the commit message so that the person > writing release notes picks it up from there? > Yes, I think I'll mention what impact each issue can have on indexes. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company