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 1ok43i-0004cQ-Vl for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Oct 2022 13:52:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ok42i-0001Q4-CX for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Oct 2022 13:51:04 +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 1ok42h-0001Pv-KL for pgsql-hackers@lists.postgresql.org; Sun, 16 Oct 2022 13:51:04 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ok42c-0004Is-GE for pgsql-hackers@lists.postgresql.org; Sun, 16 Oct 2022 13:51:02 +0000 Received: by mail-ed1-x52b.google.com with SMTP id m15so12655262edb.13 for ; Sun, 16 Oct 2022 06:50:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=content-transfer-encoding:in-reply-to:references:to:from :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=U3tfroLeaOa1sDJUCS3182U2M6dRlECPXBsnvJWZ8sU=; b=IErDawddsTLTBqT+zEgjs3KSTSQf5bjMyA5wuNgVERDYjzUri3EHp9Ax2OKlXmajjr n+PWVOr0b6ghct0PIwkxZ7KxRE/0QR+d4e1zcgKF2xufjJBOlvcyMg+AXOV77RUzytih SGC+tkWO1rDg9tGKUTd/K1xhx9oAHuCacimLxlIG/TER0E/obvX+sBevpQ683oR4Hrwy /R2Cl4v5RoisfPYbcO5ZdgGMNPlTl4PUxO4RX73dIBVNv6vqDzanuFcFhK2jRtJepJA6 Dp8KsGaeSTUNASoGuIZXzRijs041WyDNNpbY7fuKPwihg8zdhx7G8rS/fhv1xbNd9+Ii rwLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:references:to:from :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=U3tfroLeaOa1sDJUCS3182U2M6dRlECPXBsnvJWZ8sU=; b=JnQZlBzP82niSaxfN29+tQufj6e4VHqI0eRgx8JUWaqEc19+VhM/L4zakhHYXMbeS+ Olm30rCj9+eKX6DCU25UPdmC+DLHk5qYaGHJUODSJF9ykoscHQx7Ksz8eMGJt7IuD6Rr GeJgsSch6YwQrylmpS4UFUPe6kY8TxYLX0hcHuhUcuEF6wjg2+MXYSSiu3pR91NMlHgq 6M0HhUsj92M5duB4BlgkFEEgPS8puKXiIKZbMAuuGh0e7K+gsk822pMMdLOdeAnkx/ns OIQd9HAfhpy6pTSj+3QB1YR1lFuReyW3Ie8JkRhbAbC1Reb3+wFn3enlzidAINLbVzHo q91Q== X-Gm-Message-State: ACrzQf12HsVuH2H3+1ALR05+L0ZSKE5b3kxmTsd51lQQKhMlaXQEBnec DsTzuW8upVuG6pMh1RLpbcnbDc8+AYCNxG5+oInJH3UTB8Wj0IA4srS4G2HAf3nXBkknDX+niWm pMNjlO/FakuTs/S4ctCMsJPEaZYHjjirlXCemm8rbsf1E/ag8wJhEebXeN3xZSxyX32L3gElnJd vB8VjUjGlT3md5LX6GEwas4Kz6c/8yBvSmhn71o2VxBqnvotuhfQ8m+P11qBSARWIpzYwwmZ4EG FaEvduDnM3s+o44pved+X4KplU36YOy6I/CzM1P95557D+VY7ZNkS3LKtqfGq8auZh0lIzXleQP jBer9QlLg+DOA9koYgCeWYi/iBg1Gnrwpxn3Ldp4oDx46MQYyM4Z11TIKQ== X-Google-Smtp-Source: AMsMyM6SP8lltjCFkqM+PLR7JiyjB/hjlfaLLJ4xu4bbWRXD0P/+4oUKXiuAqpM6cMuUSBCh3jZciw== X-Received: by 2002:aa7:de10:0:b0:458:e101:fe54 with SMTP id h16-20020aa7de10000000b00458e101fe54mr6103436edv.80.1665928255403; Sun, 16 Oct 2022 06:50:55 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-162.bb.vodafone.cz. [86.49.228.162]) by smtp.gmail.com with ESMTPSA id m3-20020a17090679c300b0077909095acasm4654977ejo.143.2022.10.16.06.50.54 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 16 Oct 2022 06:50:54 -0700 (PDT) Message-ID: <1a7c2ff5-a855-64e9-0272-1f9947f8a558@enterprisedb.com> Date: Sun, 16 Oct 2022 15:50:53 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 Subject: Re: PATCH: Using BRIN indexes for sorted output Content-Language: en-US From: Tomas Vondra To: PostgreSQL Hackers References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 On 10/15/22 14:33, Tomas Vondra wrote: > Hi, > > ... > > There's a bunch of issues with this initial version of the patch, > usually described in XXX comments in the relevant places.6) > > ... I forgot to mention one important issue in my list yesterday, and that's memory consumption. The way the patch is coded now, the new BRIN support function (brin_minmax_ranges) produces information about *all* ranges in one go, which may be an issue. The worst case is 32TB table, with 1-page BRIN ranges, which means ~4 billion ranges. The info is an array of ~32B structs, so this would require ~128GB of RAM. With the default 128-page ranges, it's still be ~1GB, which is quite a lot. We could have a discussion about what's the reasonable size of BRIN ranges on such large tables (e.g. building a bitmap on 4 billion ranges is going to be "not cheap" so this is likely pretty rare). But we should not introduce new nodes that ignore work_mem, so we need a way to deal with such cases somehow. The easiest solution likely is to check this while planning - we can check the table size, calculate the number of BRIN ranges, and check that the range info fits into work_mem, and just not create the path when it gets too large. That's what we did for HashAgg, although that decision was unreliable because estimating GROUP BY cardinality is hard. The wrinkle here is that counting just the range info (BrinRange struct) does not include the values for by-reference types. We could use average width - that's just an estimate, though. A more comprehensive solution seems to be to allow requesting chunks of the BRIN ranges. So that we'd get "slices" of ranges and we'd process those. So for example if you have 1000 ranges, and you can only handle 100 at a time, we'd do 10 loops, each requesting 100 ranges. This has another problem - we do care about "overlaps", and we can't really know if the overlapping ranges will be in the same "slice" easily. The chunks would be sorted (for example) by maxval. But there can be a range with much higher maxval (thus in some future slice), but very low minval (thus intersecting with ranges in the current slice). Imagine ranges with these minval/maxval values, sorted by maxval: [101,200] [201,300] [301,400] [150,500] and let's say we can only process 2-range slices. So we'll get the first two, but both of them intersect with the very last range. We could always include all the intersecting ranges into the slice, but what if there are too many very "wide" ranges? So I think this will need to switch to an iterative communication with the BRIN index - instead of asking "give me info about all the ranges", we'll need a way to - request the next range (sorted by maxval) - request the intersecting ranges one by one (sorted by minval) Of course, the BRIN side will have some of the same challenges with tracking the info without breaking the work_mem limit, but I suppose it can store the info into a tuplestore/tuplesort, and use that instead of plain in-memory array. Alternatively, it could just return those, and BrinSort would use that. OTOH it seems cleaner to have some sort of API, especially if we want to support e.g. minmax-multi opclasses, that have a more complicated concept of "intersection". regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company