Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qr4KQ-002QEt-HE for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Oct 2023 22:38:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qr4KO-00BQCS-2L for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Oct 2023 22:38:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qr4KN-00BQCK-NU for pgsql-hackers@lists.postgresql.org; Thu, 12 Oct 2023 22:38:48 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qr4KJ-000FIx-CR for pgsql-hackers@lists.postgresql.org; Thu, 12 Oct 2023 22:38:47 +0000 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-4065dea9a33so15698585e9.3 for ; Thu, 12 Oct 2023 15:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1697150322; x=1697755122; darn=lists.postgresql.org; h=cc:subject:from:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=PIL/i1lT7RnTzbECz113wh3EIrvxBCeqyz4LYKpA8/Y=; b=NlldyCJSiw158T3X5nRVqOO5wrSG8Hli2N3MBMceCpSHlMWgcJD7N/dYaUQCAtd0Y2 XeSaRVBo1PKLHRKklkFQbgE106APrIbVAVuieuOUDMd5DPgiz4Kuc3LGrWXWqRE2Cntr mTSJ932xYk4CepgrY4gwC70tJxmcuhL7fopmBYB535lEtllq8DYm9p1adl+y4HBT00ex CsTgsqAeJART0nQJ3SrtkNHPmpHSM03DuVIV4NEwGVBNnFKvPmtB4PFsBBt+eBb8LwxV D+WzUuGSoX4F9MRQQLm5/PKPCuIxFEeTzy7SdRBN2TWjbmicmiwTYHyvkNmlNz+2y2sZ 0sTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697150322; x=1697755122; h=cc:subject:from:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PIL/i1lT7RnTzbECz113wh3EIrvxBCeqyz4LYKpA8/Y=; b=Sd0Nhb02QWlFuGilIwq4yWXtmPQpb9Ga/3YbP+wQ/8w11FWwR0j9PdkAsBr1qiD6wg pAOh4PKBKgMG5kELFDoqN9atgsrx1uww+CC6Zvnk/ggexbdcy6vrz/NJkQPcG8gf1Nss Oe7haHwQf1Nob9DDcwFxjnJ6ORcKkn234MagznVNEdB2gfCFONeJDn91njj3b1CVsbiY 2yJd8o8AVMDz8lA8ndP4vsafKwJ5VD8YfWZRfnSuAc/c37wlS6IsqJ7wY2kk6Oegxxx9 DHvpdw6ge2ycHLZAlArxPvOutjp9VcpXyu5b5CXY/nu4sRi74mHYDdU9xdZMlhaNgr96 dnAA== X-Gm-Message-State: AOJu0Yzyio3wSbobYxQ+tHxkJoVfRCPkgBQ5zsbZvkZqqVBPMNfjf+EU P4QQfWbHbpErb408s1INP/0HC89i4KaNvbci8/Tm0gN3WUchfBxq8SHPXo+X1Lab5wLP727KVJP reSxoTodZ4vBDD2YhtPU1quQCyLcD9U16xtb4FtwCVKuCbQl9977OeucapdRq0qYujUsrPdpzX/ 8lReJwqnzQHERZIdcho+Zysr25OA== X-Google-Smtp-Source: AGHT+IFFiDZfYxH/ymn4nZMYWHGPEqRoV8YXoJy8JoKXhCdf67t1GRehaw85liGTFbUvZpr8hdRRPQ== X-Received: by 2002:adf:d1cf:0:b0:32c:d0e0:3e71 with SMTP id b15-20020adfd1cf000000b0032cd0e03e71mr9642435wrd.38.1697150322120; Thu, 12 Oct 2023 15:38: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 g8-20020a5d5408000000b0031c5b380291sm19289371wrv.110.2023.10.12.15.38.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Oct 2023 15:38:41 -0700 (PDT) Content-Type: multipart/mixed; boundary="------------w83OiYWIK7THWmr0Iqp8XmOq" Message-ID: Date: Fri, 13 Oct 2023 00:38:40 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Content-Language: en-US To: PostgreSQL Hackers From: Tomas Vondra Subject: BRIN minmax multi - incorrect distance for infinite timestamp/date Cc: Ashutosh Bapat List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------w83OiYWIK7THWmr0Iqp8XmOq Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, Ashutosh Bapat reported me off-list a possible issue in how BRIN minmax-multi calculate distance for infinite timestamp/date values. The current code does this: if (TIMESTAMP_NOT_FINITE(dt1) || TIMESTAMP_NOT_FINITE(dt2)) PG_RETURN_FLOAT8(0); so means infinite values are "very close" to any other value, and thus likely to be merged into a summary range. That's exactly the opposite of what we want to do, possibly resulting in inefficient indexes. Consider this example create table test (a timestamptz) with (fillfactor=50); insert into test select (now() + ((10000 * random())::int || ' seconds')::interval) from generate_series(1,1000000) s(i); update test set a = '-infinity'::timestamptz where random() < 0.01; update test set a = 'infinity'::timestamptz where random() < 0.01; explain (analyze, timing off, costs off) select * from test where a = '2024-01-01'::timestamptz; QUERY PLAN ------------------------------------------------------------------------------ Bitmap Heap Scan on test (actual rows=0 loops=1) Recheck Cond: (a = '2024-01-01 00:00:00+01'::timestamp with time zone) Rows Removed by Index Recheck: 680662 Heap Blocks: lossy=6024 -> Bitmap Index Scan on test_a_idx (actual rows=60240 loops=1) Index Cond: (a = '2024-01-01 00:00:00+01'::timestamp with time zone) Planning Time: 0.075 ms Execution Time: 106.871 ms (8 rows) Clearly, large part of the table gets scanned - this happens because when building the index, we end up with ranges like this: [-infinity,a,b,c,...,x,y,z,infinity] and we conclude that distance for [-infinity,a] is 0, and we combine these values into a range. And the same for [z,infinity]. But we should do exactly the opposite thing - never merge those. Attached is a patch fixing this, with which the plan looks like this: QUERY PLAN ------------------------------------------------------------------------------ Bitmap Heap Scan on test (actual rows=0 loops=1) Recheck Cond: (a = '2024-01-01 00:00:00+01'::timestamp with time zone) -> Bitmap Index Scan on test_a_idx (actual rows=0 loops=1) Index Cond: (a = '2024-01-01 00:00:00+01'::timestamp with time zone) Planning Time: 0.289 ms Execution Time: 9.432 ms (6 rows) Which seems much better. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------w83OiYWIK7THWmr0Iqp8XmOq Content-Type: text/x-patch; charset=UTF-8; name="brin-infinity-fix.patch" Content-Disposition: attachment; filename="brin-infinity-fix.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2FjY2Vzcy9icmluL2JyaW5fbWlubWF4X211bHRp LmMgYi9zcmMvYmFja2VuZC9hY2Nlc3MvYnJpbi9icmluX21pbm1heF9tdWx0aS5jCmluZGV4 IGY4YjJhM2Y5YmMuLmM4Nzc1YzI3NGUgMTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL2FjY2Vz cy9icmluL2JyaW5fbWlubWF4X211bHRpLmMKKysrIGIvc3JjL2JhY2tlbmQvYWNjZXNzL2Jy aW4vYnJpbl9taW5tYXhfbXVsdGkuYwpAQCAtMjA4NCw4ICsyMDg0LDE0IEBAIGJyaW5fbWlu b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