pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
A question on using CIDR datatype for both ipv6 and ipv4 address
2+ messages / 2 participants
[nested] [flat]

* A question on using CIDR datatype for both ipv6 and ipv4 address
@ 2011-07-28 23:29  Dhaval Shah <dhaval.shah.m@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Dhaval Shah @ 2011-07-28 23:29 UTC (permalink / raw)
  To: pgsql-admin

I need some help in understanding why this is not working:

I have created a cidr_test table with datatypes cidr, varchar and bigint.,

rwdb=# \d cidr_test;
             Table "public.cidr_test"
    Column    |         Type          | Modifiers
--------------+-----------------------+-----------
 ip_as_cidr   | cidr                  |
 ip_as_text   | character varying(40) |
 ip_as_number | bigint                |

And populated the table with the following values:

rwdb=# select * from cidr_test;
       ip_as_cidr       |     ip_as_text     | ip_as_number
------------------------+--------------------+--------------
 ::ffff:192.0.2.128/128 | ::ffff:192.0.2.128 |   3221226112
 ::ffff:192.0.2.125/128 | ::ffff:192.0.2.125 |   3221226109
 ::ffff:192.0.2.126/128 | ::ffff:192.0.2.126 |   3221226110
 ::ffff:192.0.2.127/128 | ::ffff:192.0.2.127 |   3221226111
 192.0.2.124/32         | 192.0.2.124        |   3221226108
(5 rows)


Note that the 5th row, is an IPV4 address in IPV4 format unlike others
which are in IPV6 format.

When I run the following query:

select * from cidr_test where inet(ip_as_cidr) >= inet '::ffff:192.0.2.124/128';

       ip_as_cidr       |     ip_as_text     | ip_as_number
------------------------+--------------------+--------------
 ::ffff:192.0.2.128/128 | ::ffff:192.0.2.128 |   3221226112
 ::ffff:192.0.2.125/128 | ::ffff:192.0.2.125 |   3221226109
 ::ffff:192.0.2.126/128 | ::ffff:192.0.2.126 |   3221226110
 ::ffff:192.0.2.127/128 | ::ffff:192.0.2.127 |   3221226111
(4 rows)

The results are different from the following:

select * from cidr_test where inet(ip_as_cidr) >= inet '192.0.2.124/32';

       ip_as_cidr       |     ip_as_text     | ip_as_number
------------------------+--------------------+--------------
 ::ffff:192.0.2.128/128 | ::ffff:192.0.2.128 |   3221226112
 ::ffff:192.0.2.125/128 | ::ffff:192.0.2.125 |   3221226109
 ::ffff:192.0.2.126/128 | ::ffff:192.0.2.126 |   3221226110
 ::ffff:192.0.2.127/128 | ::ffff:192.0.2.127 |   3221226111
 192.0.2.124/32         | 192.0.2.124        |   3221226108
(5 rows)


Let me know why I am not getting similar results when the RHS is an
IPV4 in IPV6 format vs, the RHS in IPV4 format.

Thanks in advance!



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: A question on using CIDR datatype for both ipv6 and ipv4 address
@ 2011-07-28 23:58  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Dhaval Shah <dhaval.shah.m@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2011-07-28 23:58 UTC (permalink / raw)
  To: Dhaval Shah <dhaval.shah.m@gmail.com>; +Cc: pgsql-admin

Dhaval Shah <dhaval.shah.m@gmail.com> writes:
> Let me know why I am not getting similar results when the RHS is an
> IPV4 in IPV6 format vs, the RHS in IPV4 format.

AFAIR, the inet/cidr types consider IPv4 to be an independent set of
values that sort in front of all IPv6 values.  The notion that IPv4
might be embedded in some part of the IPv6 space is not something that
that code is aware of.  If it did try to do that, you'd have
'::ffff:192.0.2.124' comparing as equal to '192.0.2.124', which would
be okay for some purposes but not all.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2011-07-28 23:58 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-07-28 23:29 A question on using CIDR datatype for both ipv6 and ipv4 address Dhaval Shah <dhaval.shah.m@gmail.com>
2011-07-28 23:58 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox