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 1pVxGf-0007gT-PY for pgsql-performance@arkaria.postgresql.org; Sat, 25 Feb 2023 16:19:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pVxFf-0003PC-5v for pgsql-performance@arkaria.postgresql.org; Sat, 25 Feb 2023 16:18:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pVxFe-0003P1-Re for pgsql-performance@lists.postgresql.org; Sat, 25 Feb 2023 16:18:22 +0000 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pVxFc-0007V3-9R for pgsql-performance@postgresql.org; Sat, 25 Feb 2023 16:18:21 +0000 Received: by mail-wr1-x430.google.com with SMTP id j2so2118738wrh.9 for ; Sat, 25 Feb 2023 08:18:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; 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=0w8hweVaKHTHhoDE4LNYbxHPVlcaa3DBWGaD0+Q9C88=; b=F58xFty54RJTCNGs76sYs6paudaqZVPWQWQMS3uTHGuOjLV8mATtLfUtQC0K9b2sFN X0nogNlocro0jbcqXC1c6pKA++vUDSP5KqV4+PwfDbuJopRhbq6ONFpzIsnAiUEW3yY8 hpfuTNbnaLvNbTtqGXHpcTLDtaq+fDlcWCPjlokTITrMWmIAbdevPwcI5SqT+yjyWva8 6UvUAZJFnbwuBgctgzFkazvMAywv8YszSSwT/2c/4KyopyD5uV1kl40/jTYK6WBMNH0i 9wjS/K4jgXCxbyHJqahYticZ7oz+Y5lN947gTXswACc3Y7aPKWWM5f8LzfKNk9VhGCaO bmHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=0w8hweVaKHTHhoDE4LNYbxHPVlcaa3DBWGaD0+Q9C88=; b=D4qwSVQLYZ1b/AWkySsZRyqvoKBJT4r595qgfZbSG93wr9OpNgHGsZ+W3Dh6o7ajTD cdDwEfiBSGOsc1imdFcAx1YL9WDCqzwbBN3exT0jmYSPuTMYOZkQslL9WKD9wtO5G0hP Y3cmYkxlhdMm9y94AaiZX6ZpAjloLvB2hJSz+Wsyzy+WOxHjFOSRbhHRdNOsAWWWwWkO MFU8INtademfXNUpQ0qygBZX+BIMYmduvmU+NeHopgGr5bu/jruvC/TpSGRf8xgwbdYI WLfd5GxM18xfqnsefqwbgyFQZQpglKYULgqHFtTTpro+jemSQzI5+hJdTR9BPyKOAD1d nAwg== X-Gm-Message-State: AO0yUKXd3Vms2Up5+Enbn6aTqKiQEfghcj7EmN6mYDaLzODfe/m+yuhJ uZR4Doxa3MLbF+Q/dXdtpPZvbPrzF1lIuayg1aQpdZUIbO+zrNpjiA2sQ4foQ+E0pxdb3C3SJ1d oOSlBfZK6Hyo9UBu9RyIPfR0EJwQmGi1pgxWPIJJ0SXA226d74BwNZhfDq1EMtxJUk0lC3OBJlD B3ytERYM/X7CB729oNAn90DmEp80UM04/ZVk9jX3MVNnTXv0Y= X-Google-Smtp-Source: AK7set+eqR2gsT5+pgfYrXFOmSmtS4SnAyl2DE9VucL9LHqFdQS0xn8cjVXuUdx3lIgmUaoMW3CytA== X-Received: by 2002:a05:6000:1cd:b0:2c5:8575:c37 with SMTP id t13-20020a05600001cd00b002c585750c37mr17144199wrx.66.1677341898594; Sat, 25 Feb 2023 08:18:18 -0800 (PST) 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 l3-20020a05600012c300b002c5694aef92sm2151998wrx.21.2023.02.25.08.18.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Feb 2023 08:18:18 -0800 (PST) Message-ID: <68fde90b-bb47-1b75-f9a7-32faa7dbb8a1@enterprisedb.com> Date: Sat, 25 Feb 2023 17:18:16 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: BRIN index worse than sequential scan for large search set To: Justin Pryzby , Mickael van der Beek Cc: pgsql-performance@postgresql.org References: <20230224171944.GB1653@telsasoft.com> <20230224203715.GE1653@telsasoft.com> Content-Language: en-US From: Tomas Vondra In-Reply-To: <20230224203715.GE1653@telsasoft.com> 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 FWIW I don't think the randomness per se is the main issue. The main problem is that this results in a loop of bitmap index scans, with 20k loops. This is made worse by the block-range nature of BRIN indexes, resulting in many more heap block accesses. The table has ~80k pages, but the bitmapscan plan reads ~2559362. That can easily happen if each loop matches 100 ranges (out of 700), each having 128 pages. Making the ranges smaller should help to minimize the amount of pages read unnecessarily, but the loops are an issue. And the same range may be scanned again and again, if the range is consistent with multiple values. Interestingly enough, this is the kind of queries / plans I thought about a couple weeks ago, which made me to look at SK_SEARCHARRAY support for BRIN indexes. Imagine you did rewrite the query to something like: SELECT * FROM test_brin WHERE id IN (... lilerals ...); That would only scan the table one, reducing the number of heap pages it has to access. The drawback is that we still have to build the bitmap, and without the SK_SEARCHARRAY support we just scan the index for each element again. So better. With the SK_SEARCHARRAY patch [1] this is further optimized, and for me the query runs a bit faster than seqscan (not by much, and it depend on how the data is cached). At least with pages_per_range=1. Of course, this won't help unless the query can be rewritten like this. At least not currently, but I wonder if we could invent some sort of "pushdown" that'd derive an array of values and push it down into a parameterized path at once (instead of doing that for each value in a loop). regards [1] https://commitfest.postgresql.org/42/4187/ -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company