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 1qKL5L-000LBD-E8 for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Jul 2023 15:51:59 +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 1qKL5J-00430C-Pn for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Jul 2023 15:51:57 +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 1qKL5J-004304-Fo for pgsql-hackers@lists.postgresql.org; Fri, 14 Jul 2023 15:51:57 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qKL5B-000RpN-Pv for pgsql-hackers@lists.postgresql.org; Fri, 14 Jul 2023 15:51:56 +0000 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-3fba8e2aa52so20579585e9.1 for ; Fri, 14 Jul 2023 08:51:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1689349907; x=1691941907; 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=l4kNexnAqFfduWU+e4jbC81Eu+lViLQLn/yNVnSKTy4=; b=JSyeJgJOz0++2QLRjxRcyY0oawMl/zMPqx0qSDnfwfABaq9OpntO1Jd3389fyHLtsr 0wI7/GiM/0XG3zHXNv6f+b0ZQke9CierYVsIALuu7fFy3DvaDybQtgmsOcxZyOwYjX6J i+f+/8h/AQGxAiMF5K9OO21lHnqfBFdtsJpVnFTkrmlOmMN+jafRg29JnWdqfszMXKz/ lpdQTDAWHag/RW59dGFTS+WNPD/YEiPb6xxosT9ABILKBUw/4gY3jwrsbvm3/NfZ8EAT 9GmckAo3U88EhmvMnsfp1nS+nTFGUboLB7Yr0GR2Yk7IvZ0fWXbnxoAvNwy2aBxTV9NF FL1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689349907; x=1691941907; 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=l4kNexnAqFfduWU+e4jbC81Eu+lViLQLn/yNVnSKTy4=; b=TtG0MDUS8cZiH2s057NaIbgylC2bEqyZ8T+Jxh6YhabPA0WE3XCk+TcQ65W14Ykyur e0mWAAab1nGbcW+NMm0UNJ8HrOKoRH3vQSVdzvXSuD9cgJKbDaR9vLwXMRTOXOG/nSxu KsouQ0uk3DARkkhuGoZpcxAh80gApz5IKwCEmhNHgBuqaLIsaZpgDWdPMP2hqvYbIJyD JFyQ91vnIJ/VM8TvQIC/86unAfccM9CAFfQkeLRQS6Znb11QCRPe/O7vhx5vIc/M2ZCc q/AiwRmCh31AibQyF2Q8T1WZNmxgBWM98R6IPlMhQ1idGbGX3JwfTC5ktb+C94eU76C8 d/cA== X-Gm-Message-State: ABy/qLaIlOuJCBC1xarATngddDgIIVh8I0R9QtpoP2EBlnjPz6I1gcLy qlFBxR5evs6MFi8aTFz3Y8D6cA== X-Google-Smtp-Source: APBJJlFhnonicStdE+7UYTN8QQjWpcM4Q+DsooMZl0kI/5XuLXb17JT9tfzxX7lpUWZrop+uQ5qDRw== X-Received: by 2002:a7b:c049:0:b0:3fc:e1:24b5 with SMTP id u9-20020a7bc049000000b003fc00e124b5mr4304560wmc.23.1689349907663; Fri, 14 Jul 2023 08:51:47 -0700 (PDT) Received: from [10.137.0.17] (static-84-42-175-93.bb.vodafone.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id z13-20020a7bc7cd000000b003fc05b89e5bsm1685776wmk.34.2023.07.14.08.51.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 14 Jul 2023 08:51:47 -0700 (PDT) Message-ID: <79133b5e-5244-4059-1d4a-3748ad4a19bc@enterprisedb.com> Date: Fri, 14 Jul 2023 17:51:45 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: PATCH: Using BRIN indexes for sorted output Content-Language: en-US To: Matthias van de Meent Cc: Justin Pryzby , Andres Freund , Greg Stark , PostgreSQL Hackers References: <1607e037-8c5b-1626-9f98-496f067b9eb5@enterprisedb.com> <30f37aae-57fa-e7ab-9444-7b82969ed68f@enterprisedb.com> <825f648f-2a31-1870-942f-03fba2b6b3cf@enterprisedb.com> <0c331bf8-0cd3-e9fe-ae81-9e3d68f1f95c@enterprisedb.com> <1992ff4f-bf0e-3ad0-a859-cf054f6abc74@enterprisedb.com> <5c92f99a-e83e-c1b3-2c47-d75857a70b73@enterprisedb.com> <065bb749-71ee-6bd2-7a4b-1088cb26a31a@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/14/23 16:42, Matthias van de Meent wrote: > On Fri, 14 Jul 2023 at 16:21, Tomas Vondra > wrote: >> >> >> >> >> On 7/11/23 13:20, Matthias van de Meent wrote: >>> On Mon, 10 Jul 2023 at 22:04, Tomas Vondra >>> wrote: >>>> Approximation in what sense? My understanding was we'd get a range of >>>> distances that we know covers all rows in that range. So the results >>>> should be accurate, no? >>> >>> The distance is going to be accurate only to the degree that the >>> summary can produce accurate distances for the datapoints it >>> represents. That can be quite imprecise due to the nature of the >>> contained datapoints: a summary of the points (-1, -1) and (1, 1) will >>> have a minimum distance of 0 to the origin, where the summary (-1, 0) >>> and (-1, 0.5) would have a much more accurate distance of 1. >> >> Ummm, I'm probably missing something, or maybe my mental model of this >> is just wrong, but why would the distance for the second summary be more >> accurate? Or what does "more accurate" mean? >> >> Is that about the range of distances for the summary? For the first >> range the summary is a bounding box [(-1,1), (1,1)] so all we know the >> points may have distance in range [0, sqrt(2)]. While for the second >> summary it's [1, sqrt(1.25)]. > > Yes; I was trying to refer to the difference between what results you > get from the summary vs what results you get from the actual > datapoints: In this case, for finding points which are closest to the > origin, the first bounding box has a less accurate estimate than the > second. > OK. I think regular minmax indexes have a similar issue with non-distance ordering, because we don't know if the min/max values are still in the page range (or deleted/updated). >>> The point I was making is that the summary can only approximate the >>> distance, and that approximation is fine w.r.t. the BRIN sort >>> algoritm. >>> >> >> I think as long as the approximation (whatever it means) does not cause >> differences in results (compared to not using an index), it's OK. > I haven't written any code yet, but I think if we don't try to find the exact min/max distances for the summary (e.g. by calculating the closest point exactly) but rather "estimates" that are guaranteed to bound the actual min/max, that's good enough for the sorting. For the max, this probably is not an issue, as we can just calculate distance for the corners and use a maximum of that. At least with reasonable euclidean distance ... in 2D I'm imagining the bounding box summary as a rectangle, with the "max distance" being a minimum radius of a circle containing it (the rectangle). For min we're looking for the largest radius not intersecting with the box, which seems harder to calculate I think. However, now that I'm thinking about it - don't (SP-)GiST indexes already do pretty much exactly this? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company