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 1qIx7p-0002tu-5U for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jul 2023 20:04:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qIx7o-0003hW-49 for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jul 2023 20:04:48 +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 1qIx7n-0003hN-P6 for pgsql-hackers@lists.postgresql.org; Mon, 10 Jul 2023 20:04:47 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qIx7k-003L6C-5r for pgsql-hackers@lists.postgresql.org; Mon, 10 Jul 2023 20:04:46 +0000 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-51cff235226so9785862a12.0 for ; Mon, 10 Jul 2023 13:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1689019482; x=1691611482; 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=LuU/rZoxoHBSDYqUee9EKVNwVoPmsWzbzBq90pTGa0g=; b=MPyK6fNgkUs9bMquoPTNG0GbTXzlneyCRzv2eO9tKAM33cvE8ynpM22EUf3hacSnUg IzeiWCbke74u/BJn7KVMVVZkHUt5CZZLBTFiOorO7WpOVZXLI1AtUnhx1/LecB2wFJf2 nuybVJaQyeM1xuioqLFJBoKnTaIijBirZTnDdZambT021eUorRzhrNx3VqsUVOuJZQsi 4LL/Etxg1w6M1jlTtNfhDrxF3py7E3Wimc84/Zr5uznnYvlAsqkS3WqmneWzl+sWEoUS Q5IUD1PZbw48y/FTHgVirTAfSf5s6AYCgVGmZNGlnnm/N0TrXw8sdZEbIB8tb0TI+eFM od0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689019482; x=1691611482; 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=LuU/rZoxoHBSDYqUee9EKVNwVoPmsWzbzBq90pTGa0g=; b=GzWSUY2RUpmBYhLeOqt6AF8KXxhIPM8OCjbTTNXfi60pEX5znlrVGiBmRiTH1Z4fne pFkiXDfn4eZjrA/G8ZHoZNm5q5HXLpIHe1p1SccLekYAgF6b+U8WzEyPSQ79Ji75WqfS I3iYyVSX5E0WBH83p4Cx3drVgHJnw7VdRPsWjhePdnNDgEbACIM2yx1MeD4Kbjb41N8G Se91+0xQ2YHu2Co6IYabq333c3k6AzM6KcegFI/jp3rIApBbt0cbuSGECl8SpnOad/RG Bu3+Ag8RWy+nu4emXDdh/tOTD1SvElQXKrGp7xSX7fwEDeoltukmlUvk6P2jkuHcK6lA Sr4Q== X-Gm-Message-State: ABy/qLZviam9JHSdMUN7CS5uR3+Vzd1gHui9Fhi2HQvKz/sH6WX7q4Tf Y4Z16yzTPDSJ7Bx6cqWnYJVxlg== X-Google-Smtp-Source: APBJJlGezwNUuR49E/U1h+x1EWGYZRzsZ86XHe2Em5O6of1CQbUGELCEqbROt/bxa9lamqfWp91o1Q== X-Received: by 2002:a05:6402:3591:b0:51e:5bd5:fe7e with SMTP id y17-20020a056402359100b0051e5bd5fe7emr4453531edc.17.1689019482539; Mon, 10 Jul 2023 13:04:42 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id t20-20020a05640203d400b0051de38eb925sm112187edw.93.2023.07.10.13.04.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Jul 2023 13:04:41 -0700 (PDT) Message-ID: <5c92f99a-e83e-c1b3-2c47-d75857a70b73@enterprisedb.com> Date: Mon, 10 Jul 2023 22:04:40 +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 To: Matthias van de Meent Cc: Justin Pryzby , Andres Freund , Greg Stark , PostgreSQL Hackers References: <1607e037-8c5b-1626-9f98-496f067b9eb5@enterprisedb.com> <4e679562-fd9a-33a3-ce71-cd7a27525f8e@enterprisedb.com> <20230216161016.GV1653@telsasoft.com> <9f73f798-1dac-c75d-d834-aa012ba13b65@enterprisedb.com> <8f1374d1-6f4b-ce99-84eb-ffa852c0dadb@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> Content-Language: en-US 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/10/23 18:18, Matthias van de Meent wrote: > On Mon, 10 Jul 2023 at 17:09, Tomas Vondra > wrote: >> On 7/10/23 14:38, Matthias van de Meent wrote: >>> Kind of. For single-dimensional opclasses (minmax, minmax_multi) we >>> only need to extract the normal min/max values for ASC/DESC sorts, >>> which are readily available in the summary. But for multi-dimensional >>> and distance searches (nearest neighbour) we need to calculate the >>> distance between the indexed value(s) and the origin value to compare >>> the summary against, and the order would thus be asc/desc on distance >>> - a distance which may not be precisely represented by float types - >>> thus 'relative order' with its own order operation. >>> >> >> Can you give some examples of such data / queries, and how would it >> leverage the BRIN sort stuff? > > Order by distance would be `ORDER BY box <-> '(1, 2)'::point ASC`, and > the opclass would then decide that `<->(box, point) ASC` means it has > to return the closest distance from the point to the summary, for some > measure of 'distance' (this case L2, <#> other types, etc.). For DESC, > that would return the distance from `'(1,2)'::point` to the furthest > edge of the summary away from that point. Etc. > Thanks. >> For distance searches, I imagine this as data indexed by BRIN inclusion >> opclass, which creates a bounding box. We could return closest/furthest >> point on the bounding box (from the point used in the query). Which >> seems a bit like a R-tree ... > > Kind of; it would allow us to utilize such orderings without the > expensive 1 tuple = 1 index entry and without scanning the full table > before getting results. No tree involved, just a sequential scan on > the index to allow some sketch-based pre-sort on the data. Again, this > would work similar to how GiST's internal pages work: each downlink in > GiST contains a summary of the entries on the downlinked page, and > distance searches use a priority queue where the priority is the > distance of the opclass-provided distance operator - lower distance > means higher priority. Yes, that's roughly how I understood this too - a tradeoff that won't give the same performance as GiST, but much smaller and cheaper to maintain. > For BRIN, we'd have to build a priority queue > for the whole table at once, but presorting table sections is part of > the design of BRIN sort, right? Yes, that's kinda the whole point of BRIN sort. > >> But I have no idea what would this do for multi-dimensional searches, or >> what would those searches do? How would you sort such data other than >> lexicographically? Which I think is covered by the current BRIN Sort, >> because the data is either stored as multiple columns, in which case we >> use the BRIN on the first column. Or it's indexed using BRIN minmax as a >> tuple of values, but then it's sorted lexicographically. > > Yes, just any BRIN summary that allows distance operators and the like > should be enough MINMAX is easy to understand, and box inclusion are > IMO also fairly easy to understand. > True. If minmax is interpreted as inclusion with a simple 1D points, it kinda does the same thing. (Of course, minmax work with data types that don't have distances, but there's similarity.) >>>> I haven't really thought about geometric types, just about minmax and >>>> minmax-multi. It's not clear to me what the benefit for these types be. >>>> I mean, we can probably sort points lexicographically, but is anyone >>>> doing that in queries? It seems useless for order by distance. >>> >>> Yes, that's why you would sort them by distance, where the distance is >>> generated by the opclass as min/max distance between the summary and >>> the distance's origin, and then inserted into the tuplesort. >>> >> >> OK, so the query says "order by distance from point X" and we calculate >> the min/max distance of values in a given page range. > > Yes, and because it's BRIN that's an approximation, which should > generally be fine. > 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? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company