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 1l211k-000454-Da for pgsql-hackers@arkaria.postgresql.org; Wed, 20 Jan 2021 00:07:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l211j-0000VL-A9 for pgsql-hackers@arkaria.postgresql.org; Wed, 20 Jan 2021 00:07:11 +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 1l211i-0000VD-UF for pgsql-hackers@lists.postgresql.org; Wed, 20 Jan 2021 00:07:11 +0000 Received: from mail-ed1-x529.google.com ([2a00:1450:4864:20::529]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l211f-0007Ol-2t for pgsql-hackers@lists.postgresql.org; Wed, 20 Jan 2021 00:07:09 +0000 Received: by mail-ed1-x529.google.com with SMTP id p22so23655738edu.11 for ; Tue, 19 Jan 2021 16:07:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=PVtpz5M/rV840UjSz3A6b8tUfRADeVAfNsqrMXHxwIU=; b=XTQ5NTCvUj2QQuSrmCSn9jLPM/bFU/OuXP28L6Zi5x6yDQS8H4tzYlPER9GGnpJH8t x2yxdTiRvNRltJeMQJyPPVe+OH2/lxzbwAIYrM9c4v6+eIIp0tYCK9vHxHhgw8iy9HWs USr4ywgh4e8dqoes/4MIfqcMwosUbgzCYtWfBDCEsVxKAEaS60LNBIX6Y0TQQiZGy7wJ qG7hne2iV7ct6EftOaGDVqfJfEsRzlE5upFhid1PPjRxZ1RagudL15nio9en8xZdD4io MD+WZUTAYr01DF4yiafqwTxhfHKSmopdj7QI/UGg0wf4GnxfpaXfzVFaLSjz/IRYG5mW 7Cmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=PVtpz5M/rV840UjSz3A6b8tUfRADeVAfNsqrMXHxwIU=; b=LymdBaAW3g3FfOV67U1I+uLWPWKzfHXli8dbSv/ZdNz4H3av7wkw/Bs9wqHZYystme V130LOTH8PcMeldv7dU0eifQEgzKLZDFcVV/HhI+mNgiln2mChOWRBRZsaaaA5WV83mD 1aruw4cE15FAb0QE4ox0sSvLf8Ah+O76PPINn3yk5/CZzgxRcPNCqiNqQee3M//VI1c3 /43XIRNGaD1qXi2mhDG/mueBl30cWBhB13lJzq7WOvmu8kZH4g9EstUWsArMf/mwNiQ9 g1cHnqRY0x3E9ZX6z+M1RhWg3FTuRdSHfkPpaJB4hFTMjj29vu+8JoRDmnqPcJ4O05ad hCfg== X-Gm-Message-State: AOAM531eJ69eDxiElstFh4qqhYXSOFX7LWpks7ZnLLjRVu46VEi6zNrO 5URrCVhJSWbTjE/bOFk+u894MMNGUSs2lAiNJkvqeFrjC0vxE7cVNHvl81Xy+E1F6tfWixvG0h5 8TuCQ4ElsPChpzXA3i4bA4bonJwIYUnYg1kP/WxlaXjGpMoqjmkZTAVWK1Av6dAEctOKhb0RZt5 qsIOYjb98BK+ugUtfTHUzCB4d1AWNjFmCmps4uNbFeuuwLanoJrCvLN9/bq27PGa29o+uBHI43W XtnSp3CeFkcMGISW6E7RtgV9eyB5WvOiHwc+kaj3t4bC98YUGqTGA== X-Google-Smtp-Source: ABdhPJxwiX/o+bs4toTqU1U7RxAiENIAjWCq5kVZTbKiah7BSv3diEH0suGqHthmDCsgeLM01kl27w== X-Received: by 2002:aa7:cfd7:: with SMTP id r23mr5222556edy.298.1611101224934; Tue, 19 Jan 2021 16:07:04 -0800 (PST) Received: from [10.137.0.20] (ip-86-49-253-127.net.upcbroadband.cz. [86.49.253.127]) by smtp.gmail.com with ESMTPSA id g2sm137606ejk.108.2021.01.19.16.07.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 19 Jan 2021 16:07:03 -0800 (PST) Subject: Re: WIP: BRIN multi-range indexes To: John Naylor Cc: PostgreSQL Hackers References: <20201001222127.GA24173@alvherre.pgsql> <160434032777.4948.12542847889193566385.pgcf@coridan.postgresql.org> <20201102232545.zw4xir577gq55ogj@development> <88b3831d-8afa-4135-a420-27b12bf9fae4@enterprisedb.com> <1fdcbd2a-ff0f-fb81-d227-22279d5cbcb7@enterprisedb.com> <061b24c0-87eb-51a7-17d6-e1ba8c59fd23@enterprisedb.com> <8a40b7e4-357e-b0c2-e8b7-72001993fc32@enterprisedb.com> From: Tomas Vondra Message-ID: Date: Wed, 20 Jan 2021 01:07:00 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit 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: Precedence: bulk On 1/19/21 9:44 PM, John Naylor wrote: > On Tue, Jan 12, 2021 at 1:42 PM Tomas Vondra > > > wrote: > > I suspect it'd due to minmax having to decide which "ranges" to merge, > > which requires repeated sorting, etc. I certainly don't dare to claim > > the current algorithm is perfect. I wouldn't have expected such big > > difference, though - so definitely worth investigating. > > It seems that monotonically increasing (or decreasing) values in a table > are a worst case scenario for multi-minmax indexes, or basically, unique > values within a range. I'm guessing it's because it requires many passes > to fit all the values into a limited number of ranges. I tried using > smaller pages_per_range numbers, 32 and 8, and that didn't help. > > Now, with a different data distribution, using only 10 values that > repeat over and over, the results are muchs more sympathetic to multi-minmax: > > insert into iot (num, create_dt) > select random(), '2020-01-01 0:00'::timestamptz + (x % 10 || ' > seconds')::interval > from generate_series(1,5*365*24*60*60) x; > > create index cd_single on iot using brin(create_dt); > 27.2s > > create index cd_multi on iot using brin(create_dt > timestamptz_minmax_multi_ops); > 30.4s > > create index cd_bt on iot using btree(create_dt); > 61.8s > > Circling back to the monotonic case, I tried running a simple perf > record on a backend creating a multi-minmax index on a timestamptz > column and these were the highest non-kernel calls: > +   21.98%    21.91%  postgres         postgres            [.] > FunctionCall2Coll > +    9.31%     9.29%  postgres         postgres            [.] > compare_combine_ranges > +    8.60%     8.58%  postgres         postgres            [.] qsort_arg > +    5.68%     5.66%  postgres         postgres            [.] > brin_minmax_multi_add_value > +    5.63%     5.60%  postgres         postgres            [.] timestamp_lt > +    4.73%     4.71%  postgres         postgres            [.] > reduce_combine_ranges > +    3.80%     0.00%  postgres         [unknown]           [.] > 0x0320016800040000 > +    3.51%     3.50%  postgres         postgres            [.] timestamp_eq > > There's no one place that's pathological enough to explain the 4x > slowness over traditional BRIN and nearly 3x slowness over btree when > using a large number of unique values per range, so making progress here > would have to involve a more holistic approach. > Yeah. This very much seems like the primary problem is in how we build the ranges incrementally - with monotonic sequences, we end up having to merge the ranges over and over again. I don't know what was the structure of the table, but I guess it was kinda narrow (very few columns), which exacerbates the problem further, because the number of rows per range will be way higher than in real-world. I do think the solution to this might be to allow more values during batch index creation, and only "compress" to the requested number at the very end (when serializing to on-disk format). There are a couple additional comments about possibly replacing sequential scan with a binary search, that could help a bit too. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company