Re: inet-type sequence - Mailing list pgsql-general

From Dawid Kuroczko
Subject Re: inet-type sequence
Date
Msg-id 758d5e7f05012914136f05f8d9@mail.gmail.com
Whole thread Raw
In response to inet-type sequence  ("Andrey V. Semyonov" <wilfre@mail.ru>)
List pgsql-general
On Sat, 29 Jan 2005 22:24:46 +0300, Andrey V. Semyonov <wilfre@mail.ru> wrote:
> Hi there.
> How do I create a sequence of type inet for automatic assignment an
> IP/32 to a new row?
> Neither nextval() nor CREATE SEQUENCE seem for me to work with type inet
> (PostgreSQL 8.0.0, pgAdmin III v 1.2.0).
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend
>

First of all you need a way to convert bigint to inet, say, something
like:

CREATE OR REPLACE FUNCTION bigint_to_inet(i bigint) RETURNS inet AS $$
   BEGIN RETURN ((i / 16777216)||'.'||(i % 16777216 / 65536)||'.'||(i
% 65536 / 256)||'.'||(i % 256))::inet; END;
$$ LANGUAGE plpgsql IMMUTABLE;

then create a sequence:

CREATE SEQUENCE foo_inetaddrs_seq START 192*16777216::bigint+168*65536;
CREATE TABLE foo (
     inetaddrs inet default
bigint_to_inet(netval('foo_inetaddrs_seq')) PRIMARY KEY,
---     rest with whatever...
);

Something like this?

pgsql-general by date:

Previous
From: PFC
Date:
Subject: Re: Splitting queries across servers
Next
From: Thomas Hallgren
Date:
Subject: Re: Moving from Sybase to PostgreSQL - Stored Procedures