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 1qsOBf-007pyQ-9u for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Oct 2023 14:03:15 +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 1qsOBc-001G5D-SQ for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Oct 2023 14:03:13 +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 1qsOBc-001G54-Ix for pgsql-hackers@lists.postgresql.org; Mon, 16 Oct 2023 14:03:13 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qsOBa-000lHg-0b for pgsql-hackers@lists.postgresql.org; Mon, 16 Oct 2023 14:03:11 +0000 Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-32d9effe314so2477208f8f.3 for ; Mon, 16 Oct 2023 07:03:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1697464988; x=1698069788; 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=bg0PcpLDIvwGEGXcmqeGQxCUPVdMQ5JknvhEfioEHXc=; b=cW6d5IrtZOHVXT2mPQq29GhIKHCh3gBfC9xA3e1nDrc9JA8S1S7eO4FdTQWem3l5l0 q3EaQMwSXEet2xdQEMs6m22fnA8N5D7Bb4Jz+eOoZVcU8siXCx1WPe4yhkdTceqvpLzR gNBk+auDSQhvdS0q285vA0RSEA3G2zd1dV5FKMpv87bSzKDO0zQ/MYLT4Uby7Xi9tutu XxNB7AGiqD6fM4vehv4RlP6fY4yM6lVlP7HM/YWbTNQHn7M2GwmLoKeIMx64bNPbINSD +GlKQQfI3B/T2QNNo84VfKivIdx/h+04S9dfElkW+y+051D9VCjRCRVmus7VKw4Fgqs+ FvUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697464988; x=1698069788; 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=bg0PcpLDIvwGEGXcmqeGQxCUPVdMQ5JknvhEfioEHXc=; b=Gbc21FWkIzXg38xYyqPdzwK849ZDwCl+zJC0MxvMj4LW1INH0kvpboGLcZ6hpZHZOL 0CKiGHPwGf+hyewIR2a88D6pRzBwJAI/Pe0jCu6JF8XpwFpbLo8ATJ0FwU6msWA2dJT5 n3vCsStkfs/rI3j9bTVXRwpCwwc8o/3EdFRP/7H5deikpbH7nYJrCnIoMGktAaLDZV3I aQ/SDBf8CmYTGDnC0UMoMX9hH4dX7tw22a/mnVpMjcBy/Lq5ButbL3lW1ahrkbH+LwA4 qKRkXz1j1QptSD/O9m+B2HvS/XJsTLLar2K7jvsqu9b6yrNu3SOmg1puwfavrPX05Rle aULg== X-Gm-Message-State: AOJu0YwqbNnHoD/fhnvjZnq9BXOxQj+Ovqln2gWkBo5cb3JjkM46Go9K 3HSBfwp3RL0pXlZK2t0w4y6jjg== X-Google-Smtp-Source: AGHT+IHSXtTr2o98e5DhrviLBitGEs38D8J17VAdzlLfe5QzPmzVI6TJiNy5n6D/WAAWJgrp141YCw== X-Received: by 2002:a05:6000:3c1:b0:329:6bdc:5a60 with SMTP id b1-20020a05600003c100b003296bdc5a60mr25171668wrg.12.1697464987755; Mon, 16 Oct 2023 07:03:07 -0700 (PDT) Received: from [10.137.0.17] (srv1.mobissw.com. [89.235.0.226]) by smtp.gmail.com with ESMTPSA id bn28-20020a056000061c00b0032da7454ebesm5404143wrb.79.2023.10.16.07.03.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Oct 2023 07:03:07 -0700 (PDT) Message-ID: Date: Mon, 16 Oct 2023 16:03:12 +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 To: Ashutosh Bapat , Dean Rasheed Cc: PostgreSQL Hackers References: <14c205c6-fbf4-d2f2-3ea8-a7d5c5d63180@enterprisedb.com> <75de1895-3bd9-242e-cf13-3f5b0281e676@enterprisedb.com> Content-Language: en-US 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/16/23 11:25, Ashutosh Bapat wrote: > Thanks Tomas for bringing this discussion to hackers. > > > On Fri, Oct 13, 2023 at 8:58 PM Dean Rasheed wrote: >> >> On Fri, 13 Oct 2023 at 13:17, Tomas Vondra >> wrote: >>> >>> I do plan to backpatch this, yes. I don't think there are many people >>> affected by this (few people are using infinite dates/timestamps, but >>> maybe the overflow could be more common). >>> > > The example you gave is missing CREATE INDEX command. Is it "create > index test_idx_a on test using brin(a);" Ah, you're right - apologies. FWIW when testing I usually use 1-page ranges, to possibly hit more combinations / problems. More importantly, it needs to specify the opclass, otherwise it'll use the default minmax opclass which does not use distance at all: create index test_idx_a on test using brin (a timestamptz_minmax_multi_ops) with (pages_per_range=1); > > Do already create indexes have this issue? Do they need to rebuilt > after upgrading? > Yes, existing indexes will have inefficient ranges. I'm not sure we want to push people to reindex everything, the issue seem somewhat unlikely in practice. >> >> OK, though I doubt that such values are common in practice. >> >> There's also an overflow problem in >> brin_minmax_multi_distance_interval() though, and I think that's worse >> because overflows there throw "interval out of range" errors, which >> can prevent index creation or inserts. >> >> There's a patch (0009 in [1]) as part of the infinite interval work, >> but that just uses interval_mi(), and so doesn't fix the >> interval-out-of-range errors, except for infinite intervals, which are >> treated as special cases, which I don't think is really necessary. >> > > Right. I used interval_mi() to preserve the finite value behaviour as > is. But ... > >> I think this should be rewritten to compute delta from ia and ib >> without going via an intermediate Interval value. I.e., instead of >> computing "result", just do something like >> >> dayfraction = (ib->time % USECS_PER_DAY) - (ia->time % USECS_PER_DAY); >> days = (ib->time / USECS_PER_DAY) - (ia->time / USECS_PER_DAY); >> days += (int64) ib->day - (int64) ia->day; >> days += ((int64) ib->month - (int64) ia->month) * INT64CONST(30); >> >> then convert to double precision as it does now: >> >> delta = (double) days + dayfraction / (double) USECS_PER_DAY; >> > > Given Tomas's explanation of how these functions are supposed to work, > I think your suggestions is better. > > I was worried that above calculations may not produce the same result > as the current code when there is no error because modulo and integer > division are not distributive over subtraction. But it looks like > together they behave as normal division which is distributive over > subtraction. I couldn't find an example where that is not true. > > Tomas, you may want to incorporate this in your patch. If not, I will > incorporate it in my infinite interval patchset in [1]. > I'd rather keep it as separate patch, although maybe let's deal with it separately from the larger patches. It's a bug, and having it in a patch set that adds a feature does not seem like a good idea (or maybe I don't understand what the other thread does, I haven't looked very closely). regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company