BUG #19582: Query fails on mixed IPv4 IPv6 data when index added

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: gsaviane(at)gmail(dot)com
Subject: BUG #19582: Query fails on mixed IPv4 IPv6 data when index added
Date: 2026-07-27 16:18:59
Message-ID: 19582-b9e0113754eb1d62@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19582
Logged by: Giorgio Saviane
Email address: gsaviane(at)gmail(dot)com
PostgreSQL version: 17.10
Operating system: Ubuntu 22.04
Description:

The attached SQL code snippet shows how to reproduce the problem. A table
with generated mixed IPv4 and IPv6 data, when queried filtering by IP family
fails when a conditional index is being added to the table. I suspect the
planner pre-executes the query on some samples, applying the index condition
only partially.
This is the output I get when executing the SQL:

BEGIN
CREATE TABLE
INSERT 0 20000000
count
---------
8355840
(1 row)

CREATE INDEX
QUERY PLAN
-----------------------------------------------------------------------------------------------
Aggregate (cost=1913.76..1913.77 rows=1 width=8)
-> Bitmap Heap Scan on test_inet (cost=8.31..1912.51 rows=500 width=0)
Recheck Cond: (((addr & '255.0.0.0'::inet) = '1.0.0.0'::inet) AND
(family(addr) = 4))
-> Bitmap Index Scan on test_inet_expr_idx (cost=0.00..8.18
rows=500 width=0)
Index Cond: ((addr & '255.0.0.0'::inet) = '1.0.0.0'::inet)
(5 rows)

ERROR: cannot AND inet values of different sizes
ROLLBACK

=============== CODE HERE ==============
begin;

-- Create a table to host inet addresses
create table test_inet (
addr inet not null
);

-- Generate mixed IPv4 and IPv6 data.
-- The amout of data makes the difference.
-- Experimentally, lower than 13M it does not reproduce.
-- It might depend on server configuration
insert into test_inet(addr)
select (case
mod(s,2) when 0
then 'fe80:1::'::inet + s
else '1.1.0.0'::inet + s
end) from generate_series(1, 20000000) s;

-- Count filtered by IPv4 family and subnet.
-- Suceeds without an index
select count(*)
from test_inet
where family(addr) = 4
and addr & '255.0.0.0'::inet = '1.0.0.0';

-- Now create an index filtered only on IPv4 family
create index
on test_inet((addr & '255.0.0.0'::inet))
where family(addr) = 4;

-- The plan should show how subnet and family
-- conditions are split due to rechecking
explain select count(*)
from test_inet
where family(addr) = 4
and addr & '255.0.0.0'::inet = '1.0.0.0';

-- Same count filtered by IPv4 family and subnet fails with:
-- ERROR: cannot AND inet values of different sizes
select count(*)
from test_inet
where family(addr) = 4
and addr & '255.0.0.0'::inet = '1.0.0.0';

rollback;
=============== END CODE ==============

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Daniel Gustafsson 2026-07-27 17:36:06 Re: Race between datachecksum enablement and create table with the file_copy strategy
Previous Message Ayush Tiwari 2026-07-27 12:38:31 Re: BUG #19579: Wrong results regression