Fix netmask handling in inet_minmax_multi_ops

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
This commit is contained in:
Tomas Vondra 2023-03-20 09:51:50 +01:00
parent 0b51d423e9
commit e858312683
3 changed files with 15 additions and 2 deletions

View 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));

View 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

View 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