Adding integers ( > 8 bytes) to an inet

From: Kristian Larsson <kristian(at)spritelink(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Adding integers ( > 8 bytes) to an inet
Date: 2009-09-08 13:58:25
Message-ID: 20090908135825.GL47859@spritelink.se
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

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(at)spritelink(dot)net

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bill Moran 2009-09-08 14:29:19 Re: Order By Date Question
Previous Message BlackMage 2009-09-08 13:29:28 Order By Date Question

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2009-09-08 14:14:13 Re: Time-based Releases WAS: 8.5 release timetable, again
Previous Message Alvaro Herrera 2009-09-08 13:38:55 Re: CREATE LIKE INCLUDING COMMENTS and STORAGES