Skip site navigation (1) Skip section navigation (2)

Re: inet-type sequence

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: "Andrey V(dot) Semyonov" <wilfre(at)mail(dot)ru>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: inet-type sequence
Date: 2005-01-29 21:47:02
Message-ID: 20050129214702.GA30278@winnie.fuhr.org (view raw or flat)
Thread:
Lists: pgsql-general
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/

In response to

pgsql-general by date

Next:From: PFCDate: 2005-01-29 22:09:44
Subject: Re: Splitting queries across servers
Previous:From: MaxDate: 2005-01-29 21:44:46
Subject: Re: Splitting queries across servers

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group