Fix netmask handling in inet_minmax_multi_ops
authorTomas Vondra <tomas.vondra@postgresql.org>
Mon, 20 Mar 2023 08:51:50 +0000 (09:51 +0100)
committerTomas Vondra <tomas.vondra@postgresql.org>
Mon, 20 Mar 2023 09:24:14 +0000 (10:24 +0100)
When calculating distance in brin_minmax_multi_distance_inet(), the
netmask was applied incorrectly. This results in (seemingly) incorrect
ordering of values, triggering an assert.

For builds without asserts this is mostly harmless - we may merge other
ranges, possibly resulting in slightly less efficient index. But it's
still correct and the greedy algorithm doesn't guarantee optimality
anyway.

Backpatch to 14, where minmax-multi indexes were introduced.

Reported by Dmitry Dolgov, investigation and fix by me.

Reported-by: Dmitry Dolgov
Backpatch-through: 14
Discussion: https://postgr.es/m/17774-c6f3e36dd4471e67@postgresql.org

src/backend/access/brin/brin_minmax_multi.c
src/test/regress/expected/brin_multi.out
src/test/regress/sql/brin_multi.sql

index 0ace6035beb880a7685c2d2ba1757b0c2c4abf0b..cdf78cdecccc23edf449c47076b1c85ea2685fa4 100644 (file)
@@ -2364,14 +2364,14 @@ brin_minmax_multi_distance_inet(PG_FUNCTION_ARGS)
                unsigned char mask;
                int                     nbits;
 
-               nbits = lena - (i * 8);
+               nbits = Max(0, lena - (i * 8));
                if (nbits < 8)
                {
                        mask = (0xFF << (8 - nbits));
                        addra[i] = (addra[i] & mask);
                }
 
-               nbits = lenb - (i * 8);
+               nbits = Max(0, lenb - (i * 8));
                if (nbits < 8)
                {
                        mask = (0xFF << (8 - nbits));
index fbbfb6710ce2c8ef4aca3ac74fc2f74ca1d20681..861a06ef8ca6dea3a25bd00ecbaf3bb995a25320 100644 (file)
@@ -351,6 +351,12 @@ VACUUM brintest_multi;  -- force a summarization cycle in brinidx
 insert into public.brintest_multi (float4col) values (real 'nan');
 insert into public.brintest_multi (float8col) values (real 'nan');
 UPDATE brintest_multi SET int8col = int8col * int4col;
+-- Test handling of inet netmasks with inet_minmax_multi_ops
+CREATE TABLE brin_test_inet (a inet);
+CREATE INDEX ON brin_test_inet USING brin (a inet_minmax_multi_ops);
+INSERT INTO brin_test_inet VALUES ('127.0.0.1/0');
+INSERT INTO brin_test_inet VALUES ('0.0.0.0/12');
+DROP TABLE brin_test_inet;
 -- Tests for brin_summarize_new_values
 SELECT brin_summarize_new_values('brintest_multi'); -- error, not an index
 ERROR:  "brintest_multi" is not an index
index 49f26072ec6e1a5b1a3870be104922d8dc64e09d..070455257c0e8bec073a4116129f0fea3556ef8e 100644 (file)
@@ -357,6 +357,13 @@ insert into public.brintest_multi (float8col) values (real 'nan');
 
 UPDATE brintest_multi SET int8col = int8col * int4col;
 
+-- Test handling of inet netmasks with inet_minmax_multi_ops
+CREATE TABLE brin_test_inet (a inet);
+CREATE INDEX ON brin_test_inet USING brin (a inet_minmax_multi_ops);
+INSERT INTO brin_test_inet VALUES ('127.0.0.1/0');
+INSERT INTO brin_test_inet VALUES ('0.0.0.0/12');
+DROP TABLE brin_test_inet;
+
 -- Tests for brin_summarize_new_values
 SELECT brin_summarize_new_values('brintest_multi'); -- error, not an index
 SELECT brin_summarize_new_values('tenk1_unique1'); -- error, not a BRIN index