Adding integers ( > 8 bytes) to an inet - Mailing list pgsql-general

From Kristian Larsson
Subject Adding integers ( > 8 bytes) to an inet
Date
Msg-id 20090908135825.GL47859@spritelink.se
Whole thread Raw
Responses Re: Adding integers ( > 8 bytes) to an inet  (Martin Gainty <mgainty@hotmail.com>)
Re: Adding integers ( > 8 bytes) to an inet  (Alvaro Herrera <alvherre@commandprompt.com>)
List pgsql-general
Hello!

I'm having some trouble with the inet data type and its
operators. Right now I'm relying on operations such as

kll=# SELECT '10.0.0.0/24'::inet + (2^(32-24))::integer;
  ?column?
-------------
 10.0.1.0/24
(1 row)

to get the "next" available /24. This works all fine and dandy
for IPv4 since I'll never go beyond what an integer has to offer.
Expanding my application to IPv6 will on the other hand cause me
some trouble since 2^128 won't fit in an integer and not in a
bigint either. I tried numeric;

kll=> SELECT '2000::/48'::inet + (2^(128-96))::numeric(100);
ERROR: operator does NOT exist: inet + numeric
LINE 1: SELECT '2000::/48'::inet + (2^(128-96))::numeric(100);
                                 ^
HINT: No operator matches the given name AND argument type(s). You might need TO ADD explicit type casts.


And poking in pg_operator / pg_type seems to confirm this:

nils=# SELECT (SELECT typname FROM pg_type WHERE typelem=oprleft), oprname, (SELECT typname FROM pg_type WHERE
typelem=oprright)FROM pg_operator WHERE oprleft=(SELECT typelem FROM pg_type WHERE typname='_inet') AND oprname='+'; 
 ?column? | oprname | ?column?
----------+---------+----------
 _inet    | +       | _int8
(1 row)

I could hack together some kluge to loop through, but it all
becomes quite ugly after a while and I would rather see some way
to add a numeric.

Am I doing it the wrong way? Bug?
What to do?

Kind regards,
   Kristian.

--
Kristian Larsson                                        KLL-RIPE
+46 704 264511                          kll@spritelink.net

pgsql-general by date:

Previous
From: Markus Pscheidt
Date:
Subject: Re: Fail to start Postgres on Ubuntu kernel 2.6.28-15-generic
Next
From: BlackMage
Date:
Subject: Order By Date Question