Postgresql backend casts inet types to text, breaks IP operations and IPv6 lookups.
|Reported by:||eide||Owned by:||Erik Romijn|
|Component:||Database layer (models, ORM)||Version:||master|
|Severity:||Normal||Keywords:||ipv6 postgres inet dceu13|
|Cc:||Jernej Kos, mmitar@…, asandroq||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Ticket #708 describes a problem with LIKE operations on inet types in postgresql. The solution was to cast inet to text using the HOST() function.
But by casting inet to text none of the network operations in postgresql will work, and IPv6 lookups are pretty much broken. In the database I'm currently using, doing a HOST() on a IPv6 address will always produce a compressed URL. So if I'm checking against a fullsize address in django the lookup will fail, even though they are the same address.
Here's an example of what I'm talking about:
my_db=# CREATE TABLE my_ips (ip inet); CREATE TABLE ^ my_db=# INSERT INTO my_ips VALUES ('2001:0db8:0000:0000:0000:0000:0000:0001'); INSERT 0 1 my_db=# SELECT * FROM my_ips WHERE ip = '2001:0db8:0000:0000:0000:0000:0000:0001'; ip ------------- 2001:db8::1 (1 row) my_db=# SELECT * FROM my_ips WHERE ip = '2001:db8::1'; ip ------------- 2001:db8::1 (1 row)
So far so good, but when you throw HOST() into the picture, this happens:
my_db=# SELECT * FROM my_ips WHERE HOST(ip) = '2001:db8::1'; ip ------------- 2001:db8::1 (1 row) my_db=# SELECT * FROM my_ips WHERE HOST(ip) = '2001:0db8:0000:0000:0000:0000:0000:0001'; ip ---- (0 rows)
2001:db8::1 and 2001:0db8:0000:0000:0000:0000:0000:0001 are the same address, just displayed on different forms.
Currently I always make sure that I pass a compressed IP to the models with IPAddressFields. That does however assume that all postgresql databases will always return IPv6 addresses on the compressed form, and I do not know if that's correct.
The correct solution would be to not cast inet to text.
Also, the postgresql documentation on Network Address Functions and Operators states that:
The host, text, and abbrev functions are primarily intended to offer alternative display formats.
So using HOST() for lookups is acctually kind of wrong in the first place.
Change History (22)
comment:1 Changed 7 years ago by
|Patch needs improvement:||unset|
comment:5 follow-up: 6 Changed 7 years ago by
|Status:||closed → reopened|
|Version:||1.0 → 1.1|