Search Postgresql Archives

Re: inet-type sequence

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Jan 29, 2005 at 02:35:06PM -0700, Michael Fuhr wrote:

> CREATE FUNCTION bigint2inet(bigint) RETURNS inet AS '
> BEGIN
>     RETURN cidr(''0x'' || lpad(to_hex($1), 8, ''0''));
> END;
> ' LANGUAGE plpgsql IMMUTABLE STRICT;

I should point out that the above function is intended only as a
trivial example.  It's designed for IPv4 address and will have
problems when the bigint value exceeds 2^32 - 1 (4294967295):

SELECT bigint2inet((2^32 - 1)::bigint);
   bigint2inet   
-----------------
 255.255.255.255
(1 row)

SELECT bigint2inet((2^32)::bigint);
 bigint2inet 
-------------
 16.0.0.0
(1 row)

A more robust implementation is left as an exercise for the reader.

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux