Re: inet increment with int

Lists: pgsql-hackers
From: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
To: pgsql-hackers(at)postgresql(dot)org
Subject: inet increment with int
Date: 2005-09-05 18:25:01
Message-ID: 20050905182501.GS8469@quartz.itdept.newn.cam.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Ilya Kovalenko posted some code at in a thread starting at

http://archives.postgresql.org/pgsql-hackers/2005-04/msg00417.php

which lead to the TODO item:

* Allow INET + INT4 to increment the host part of the address, or
throw an error on overflow

I think that the naively coded function attached does what is needed, e.g.,

CREATE OR REPLACE FUNCTION inet_inc(inet, int4)
RETURNS inet
AS '/tmp/inet.so','inet_inc'
LANGUAGE C STRICT;

CREATE OPERATOR + (
leftarg = inet,
rightarg = int4,
procedure = inet_inc
);

test=# select '192.168.0.1/24'::inet + 300;
ERROR: Increment (300) too big for network (/24)
test=# select '192.168.0.1/24'::inet + 254;
?column?
------------------
192.168.0.255/24
(1 row)

test=# select '192.168.0.1/24'::inet + 255;
ERROR: Increment (255) takes address (192.168.0.1) out of its network (/24)
test=# select '192.168.0.1/24'::inet + -2;
ERROR: Increment (-2) takes address (192.168.0.1) out of its network (/24)
test=# select '255.255.255.254/0'::inet + 2;
ERROR: Increment (2) takes address (255.255.255.254) out of its network (/0)

and just for fun:

create table list (
host inet
);

insert into list values ('192.168.0.1/24');
insert into list values ('192.168.0.2/24');
insert into list values ('192.168.0.4/24');
insert into list values ('192.168.0.5/24');
insert into list values ('192.168.0.6/24');
insert into list values ('192.168.0.8/24');
insert into list values ('192.168.0.9/24');
insert into list values ('192.168.0.10/24');
insert into list values ('192.168.1.1/24');
insert into list values ('192.168.1.3/24');

select host+1 from list
where host+1 <<= '192.168.1.0/24'
and not exists
( select 1
from list
where host=host+1
and host << '192.168.1.0/24' )
limit 1;

If you agree that this is the right thing, I can code it less
naively, (Ilya rightly uses ntohl/htonl), create the operator's
commutator, provide a patch which makes it a built-in, and some
obvious documentation.

Cheers,

Patrick

Attachment Content-Type Size
inet.c text/plain 1.9 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-05 19:02:55
Message-ID: 3625.1125946975@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk> writes:
> * Allow INET + INT4 to increment the host part of the address, or
> throw an error on overflow

> I think that the naively coded function attached does what is needed, e.g.,

What happened to the IPv6 case? Also, I think you need to reject CIDR
inputs.

regards, tom lane


From: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-05 19:10:16
Message-ID: 20050905191016.GV8469@quartz.itdept.newn.cam.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Sep 05, 2005 at 03:02:55PM -0400, Tom Lane wrote:
> Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk> writes:
> > * Allow INET + INT4 to increment the host part of the address, or
> > throw an error on overflow
>
> > I think that the naively coded function attached does what is needed, e.g.,
>
> What happened to the IPv6 case?

My take on the thread is that the IPv6 case doesn't make sense, and the
int8 part was dropped from the TODO.

> Also, I think you need to reject CIDR inputs.

OK

Patrick


From: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-06 12:12:50
Message-ID: 20050906121250.GB560@quartz.itdept.newn.cam.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Sep 05, 2005 at 08:10:16PM +0100, Patrick Welche wrote:
> On Mon, Sep 05, 2005 at 03:02:55PM -0400, Tom Lane wrote:
> > Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk> writes:
> > > * Allow INET + INT4 to increment the host part of the address, or
> > > throw an error on overflow
> >
> > > I think that the naively coded function attached does what is needed, e.g.,
> >
> > What happened to the IPv6 case?
>
> My take on the thread is that the IPv6 case doesn't make sense, and the
> int8 part was dropped from the TODO.
>
> > Also, I think you need to reject CIDR inputs.
>
> OK

Now with:

test=# select '192.168.0.0/24'::inet + 1;
ERROR: Trying to increment a network (192.168.0.0/24) rather than a host
test=# select '192.168.0.1/24'::inet + -1;
ERROR: Increment returns a network (192.168.0.0/24) rather than a host

Cheers,

Patrick

Attachment Content-Type Size
inet.c text/plain 2.5 KB

From: Andrew - Supernews <andrew+nonews(at)supernews(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-07 02:48:00
Message-ID: slrndhsl70.1vfu.andrew+nonews@trinity.supernews.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2005-09-06, Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk> wrote:
> Now with:
>
> test=# select '192.168.0.0/24'::inet + 1;
> ERROR: Trying to increment a network (192.168.0.0/24) rather than a host

What possible justification is there for this behaviour?

> test=# select '192.168.0.1/24'::inet + -1;
> ERROR: Increment returns a network (192.168.0.0/24) rather than a host

While I suspect I know where this idea came from, it is equally boneheaded
since it is making completely unwarranted assumptions about how inet
values are being used.

--
Andrew, Supernews
http://www.supernews.com - individual and corporate NNTP services


From: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
To: andrew(at)supernews(dot)com
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-07 12:35:59
Message-ID: 20050907123559.GC7193@quartz.itdept.newn.cam.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Sep 07, 2005 at 02:48:00AM -0000, Andrew - Supernews wrote:
> On 2005-09-06, Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk> wrote:
> > Now with:
> >
> > test=# select '192.168.0.0/24'::inet + 1;
> > ERROR: Trying to increment a network (192.168.0.0/24) rather than a host
>
> What possible justification is there for this behaviour?
>
> > test=# select '192.168.0.1/24'::inet + -1;
> > ERROR: Increment returns a network (192.168.0.0/24) rather than a host
>
> While I suspect I know where this idea came from, it is equally boneheaded
> since it is making completely unwarranted assumptions about how inet
> values are being used.

So, back to original version?

Comments anyone?

Patrick


From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-07 13:37:42
Message-ID: 20050907133742.GC33154@colo.samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Patrick Welche wrote:
>Comments anyone?

Is incrementing an inet address a valid thing to do, or is its
meaning too open to interpretation?

How about either a pair of functions, one for incrementing the
network and another for the host, or a combined function that allows
you to work with both parts in one go?

Sam


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-16 10:36:56
Message-ID: 200509161036.j8GAauG21822@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


This has been saved for the 8.2 release:

http://momjian.postgresql.org/cgi-bin/pgpatches_hold

---------------------------------------------------------------------------

Patrick Welche wrote:
> Ilya Kovalenko posted some code at in a thread starting at
>
> http://archives.postgresql.org/pgsql-hackers/2005-04/msg00417.php
>
> which lead to the TODO item:
>
> * Allow INET + INT4 to increment the host part of the address, or
> throw an error on overflow
>
> I think that the naively coded function attached does what is needed, e.g.,
>
> CREATE OR REPLACE FUNCTION inet_inc(inet, int4)
> RETURNS inet
> AS '/tmp/inet.so','inet_inc'
> LANGUAGE C STRICT;
>
> CREATE OPERATOR + (
> leftarg = inet,
> rightarg = int4,
> procedure = inet_inc
> );
>
> test=# select '192.168.0.1/24'::inet + 300;
> ERROR: Increment (300) too big for network (/24)
> test=# select '192.168.0.1/24'::inet + 254;
> ?column?
> ------------------
> 192.168.0.255/24
> (1 row)
>
> test=# select '192.168.0.1/24'::inet + 255;
> ERROR: Increment (255) takes address (192.168.0.1) out of its network (/24)
> test=# select '192.168.0.1/24'::inet + -2;
> ERROR: Increment (-2) takes address (192.168.0.1) out of its network (/24)
> test=# select '255.255.255.254/0'::inet + 2;
> ERROR: Increment (2) takes address (255.255.255.254) out of its network (/0)
>
> and just for fun:
>
> create table list (
> host inet
> );
>
> insert into list values ('192.168.0.1/24');
> insert into list values ('192.168.0.2/24');
> insert into list values ('192.168.0.4/24');
> insert into list values ('192.168.0.5/24');
> insert into list values ('192.168.0.6/24');
> insert into list values ('192.168.0.8/24');
> insert into list values ('192.168.0.9/24');
> insert into list values ('192.168.0.10/24');
> insert into list values ('192.168.1.1/24');
> insert into list values ('192.168.1.3/24');
>
> select host+1 from list
> where host+1 <<= '192.168.1.0/24'
> and not exists
> ( select 1
> from list
> where host=host+1
> and host << '192.168.1.0/24' )
> limit 1;
>
>
>
> If you agree that this is the right thing, I can code it less
> naively, (Ilya rightly uses ntohl/htonl), create the operator's
> commutator, provide a patch which makes it a built-in, and some
> obvious documentation.
>
> Cheers,
>
> Patrick

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-16 15:19:13
Message-ID: 20050916151913.GK574@quartz.itdept.newn.cam.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Sep 16, 2005 at 06:36:56AM -0400, Bruce Momjian wrote:
>
> This has been saved for the 8.2 release:

It isn't actually a patch for application yet ;-) It is the function in
a state that is easy to test. I take it that as I have basically had
no comments back, I will just go ahead and make a patch for the
function as a built-in...

Cheers,

Patrick


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2005-09-16 16:57:20
Message-ID: 200509161657.j8GGvKw22849@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Patrick Welche wrote:
> On Fri, Sep 16, 2005 at 06:36:56AM -0400, Bruce Momjian wrote:
> >
> > This has been saved for the 8.2 release:
>
> It isn't actually a patch for application yet ;-) It is the function in
> a state that is easy to test. I take it that as I have basically had
> no comments back, I will just go ahead and make a patch for the
> function as a built-in...

Right.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2006-04-30 02:24:48
Message-ID: 200604300224.k3U2OmR12348@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


FYI, 8.2 will have this and more based on this applied patch:

Add INET/CIDR operators: and, or, not, plus int8, minus int8, and inet
minus inet.

Stephen R. van den Berg

---------------------------------------------------------------------------

Patrick Welche wrote:
> Ilya Kovalenko posted some code at in a thread starting at
>
> http://archives.postgresql.org/pgsql-hackers/2005-04/msg00417.php
>
> which lead to the TODO item:
>
> * Allow INET + INT4 to increment the host part of the address, or
> throw an error on overflow
>
> I think that the naively coded function attached does what is needed, e.g.,
>
> CREATE OR REPLACE FUNCTION inet_inc(inet, int4)
> RETURNS inet
> AS '/tmp/inet.so','inet_inc'
> LANGUAGE C STRICT;
>
> CREATE OPERATOR + (
> leftarg = inet,
> rightarg = int4,
> procedure = inet_inc
> );
>
> test=# select '192.168.0.1/24'::inet + 300;
> ERROR: Increment (300) too big for network (/24)
> test=# select '192.168.0.1/24'::inet + 254;
> ?column?
> ------------------
> 192.168.0.255/24
> (1 row)
>
> test=# select '192.168.0.1/24'::inet + 255;
> ERROR: Increment (255) takes address (192.168.0.1) out of its network (/24)
> test=# select '192.168.0.1/24'::inet + -2;
> ERROR: Increment (-2) takes address (192.168.0.1) out of its network (/24)
> test=# select '255.255.255.254/0'::inet + 2;
> ERROR: Increment (2) takes address (255.255.255.254) out of its network (/0)
>
> and just for fun:
>
> create table list (
> host inet
> );
>
> insert into list values ('192.168.0.1/24');
> insert into list values ('192.168.0.2/24');
> insert into list values ('192.168.0.4/24');
> insert into list values ('192.168.0.5/24');
> insert into list values ('192.168.0.6/24');
> insert into list values ('192.168.0.8/24');
> insert into list values ('192.168.0.9/24');
> insert into list values ('192.168.0.10/24');
> insert into list values ('192.168.1.1/24');
> insert into list values ('192.168.1.3/24');
>
> select host+1 from list
> where host+1 <<= '192.168.1.0/24'
> and not exists
> ( select 1
> from list
> where host=host+1
> and host << '192.168.1.0/24' )
> limit 1;
>
>
>
> If you agree that this is the right thing, I can code it less
> naively, (Ilya rightly uses ntohl/htonl), create the operator's
> commutator, provide a patch which makes it a built-in, and some
> obvious documentation.
>
> Cheers,
>
> Patrick

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster

--
Bruce Momjian http://candle.pha.pa.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet increment with int
Date: 2006-05-01 12:55:08
Message-ID: 20060501125508.GA28452@quartz.itdept.newn.cam.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Apr 29, 2006 at 10:24:48PM -0400, Bruce Momjian wrote:
>
> FYI, 8.2 will have this and more based on this applied patch:
>
> Add INET/CIDR operators: and, or, not, plus int8, minus int8, and inet
> minus inet.

I know, I'm already using it :-)

Thanks,

Patrick