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 1pTSIr-00011j-Kh for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Feb 2023 18:51:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pTSIq-0001Ti-EY for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Feb 2023 18:51:20 +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 1pTSIq-0001TZ-1P for pgsql-hackers@lists.postgresql.org; Sat, 18 Feb 2023 18:51:20 +0000 Received: from mail-il1-x12c.google.com ([2607:f8b0:4864:20::12c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pTSIi-00014o-Ca for pgsql-hackers@lists.postgresql.org; Sat, 18 Feb 2023 18:51:18 +0000 Received: by mail-il1-x12c.google.com with SMTP id t2so429740ilf.1 for ; Sat, 18 Feb 2023 10:51:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=AcwyvVQnpQf4Pts442dcE5vIKRzJd+WAv0ZPcBPncqY=; b=3JPRlEbG6uFgsRYxX5mWbA3Ub2RQntWICyHdFZ/rdZ535v9pqct5WxALb1ww1/wSJ/ n9j1pFTkKASVddvwSJbsMwgfszMtllY01X9QcQ2ArT2jmJynAwOZpuivqGIkUmmAF4/7 XexpkR0jj7lj/kTAJvCUwt1YFJofvVkM/WB9QF1rJ8uybmORECI+d2sCDkpwF6yW+Iot Vivt8kyX7IIKJep6VI3VlZdMxRL64AN1PXrvPOcKsd+AzjAIT10MuDb7hOBi4jVTgQiI +4nInetICnz/1CH3IUzf55fuDMZbtooEkiuj5bN8kINIiAVMaeuqfudj7bHFTVUzYMzh CQzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AcwyvVQnpQf4Pts442dcE5vIKRzJd+WAv0ZPcBPncqY=; b=w9iDUS2E7zk+kXimCljCfF6PXP7A825UT1ZDEOn1hoSELKrTeWRxTr/+57Jk+xDMLl PGysIT3SQdpdXRz4mYT6OWBUb5oxXNrPN/KwTnJgNlffJgN7GOTmvDi/vltai6jxvt4S COMMqoAj1VYxrCCSS8a91c6tUm3anZX/7pwcJpY4msjIECmWofu2m0pIzAULEU5uhnbX CI0dIPv9TX/8/jipfKCpWWrmbi5R1GRExE7pRR+Ti3oYDTNstr7ePlp9C5yvZGvZ7WK3 ukpCK2pqIC95S0HzCzoI5UoHIU22sK96wnLVZ4DpvQxHZ7UwVLNCUvTLA/V1ZH1GqZzw X8DA== X-Gm-Message-State: AO0yUKUS4GYcdXwRGUFTkBcEmv8+2EdYBl+GZiX9DxcUet2QxxCinrRQ SJ9v1m3K2RPRdIy990qzIFwuLA== X-Google-Smtp-Source: AK7set/yJNlC5aIwspwEcCF8PL1gwHhpFDF6p+KLDwVCPrhqi8JYA0ymlFUW0rlU0FZ2gla2CnvCiA== X-Received: by 2002:a92:7d0f:0:b0:315:5651:3317 with SMTP id y15-20020a927d0f000000b0031556513317mr367821ilc.14.1676746271367; Sat, 18 Feb 2023 10:51:11 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id u22-20020a02aa96000000b003b7d56593d6sm2495581jai.160.2023.02.18.10.51.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 10:51:10 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 570E58017F4; Sat, 18 Feb 2023 12:51:09 -0600 (CST) Date: Sat, 18 Feb 2023 12:51:09 -0600 From: Justin Pryzby To: Tomas Vondra Cc: Andres Freund , Greg Stark , Matthias van de Meent , Zhihong Yu , pgsql-hackers@lists.postgresql.org Subject: Re: PATCH: Using BRIN indexes for sorted output Message-ID: <20230218185109.GH1653@telsasoft.com> References: <87964cda-e440-8759-2838-bc4628072a18@enterprisedb.com> <14568731-ac91-ce2d-1477-0e2504c72775@enterprisedb.com> <20221206185143.ivy3afwmkhloksgv@awork3.anarazel.de> <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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8f1374d1-6f4b-ce99-84eb-ffa852c0dadb@enterprisedb.com> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Are (any of) these patches targetting v16 ? typos: ar we - we are? morestly - mostly interstect - intersect > + * XXX We don't sort the bins, so just do binary sort. For large number of values > + * this might be an issue, for small number of values a linear search is fine. "binary sort" is wrong? > + * only half of there ranges, thus 1/2. This can be extended to randomly half of *these* ranges ? > From 7b3307c27b35ece119feab4891f03749250e454b Mon Sep 17 00:00:00 2001 > From: Tomas Vondra > Date: Mon, 17 Oct 2022 18:39:28 +0200 > Subject: [PATCH 01/11] Allow index AMs to build and use custom statistics I think the idea can also apply to btree - currently, correlation is considered to be a property of a column, but not an index. But that fails to distinguish between a freshly built index, and an index with out of order heap references, which can cause an index scan to be a lot more expensive. I implemented per-index correlation stats way back when: https://www.postgresql.org/message-id/flat/20160524173914.GA11880%40telsasoft.com See also: https://www.postgresql.org/message-id/14438.1512499811@sss.pgh.pa.us With my old test case: Index scan is 3x slower than bitmap scan, but index scan is costed as being cheaper: postgres=# explain analyze SELECT * FROM t WHERE i>11 AND i<55; Index Scan using t_i_idx on t (cost=0.43..21153.74 rows=130912 width=8) (actual time=0.107..222.737 rows=128914 loops=1) postgres=# SET enable_indexscan =no; postgres=# explain analyze SELECT * FROM t WHERE i>11 AND i<55; Bitmap Heap Scan on t (cost=2834.28..26895.96 rows=130912 width=8) (actual time=16.830..69.860 rows=128914 loops=1) If it's clustered, then the index scan is almost twice as fast, and the costs are more consistent with the associated time. The planner assumes that the indexes are freshly built... postgres=# CLUSTER t USING t_i_idx ; postgres=# explain analyze SELECT * FROM t WHERE i>11 AND i<55; Index Scan using t_i_idx on t (cost=0.43..20121.74 rows=130912 width=8) (actual time=0.084..117.549 rows=128914 loops=1) -- Justin