CIDR/INET improvements

Lists: pgsql-hackerspgsql-patches
From: Joachim Wieland <joe(at)mcknight(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: CIDR/INET improvements
Date: 2006-01-07 13:02:48
Message-ID: 20060107130248.GA2666@mcknight.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

The TODO list contains some items concerning the CIDR/INET datatype.

* %Prevent INET cast to CIDR if the unmasked bits are not zero, or
zero the bits

I added a function for this cast (which zeroes the bits) but then the
opr_sanity regression test failed because there is a cast from INET -> CIDR
but the other way round is considered to be binary compatible.

Actually both types are not binary compatible, since they have a
type component that is either 0 or 1, depending on whether it is of type
INET or CIDR.

If nobody objects, I'll send in a patch that includes a cast function for
the other way round as well.

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

Once at it I wonder how much arithmetic these types need?
What about functions to get/set a specific byte, for example:

inet_get_byte('192.168.1.1'::inet, 0) returns 1
inet_get_byte('192.168.1.1'::inet, 1) returns 1
inet_get_byte('192.168.1.1'::inet, 2) returns 168
inet_get_byte('192.168.1.1'::inet, 3) returns 192

and inet_set_byte('192.168.1.1'::inet, 3, 128) returns '128.168.1.1'

Which other functions have been missing here in the past?

Joachim


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joachim Wieland <joe(at)mcknight(dot)de>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: CIDR/INET improvements
Date: 2006-01-07 17:50:23
Message-ID: 29560.1136656223@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Joachim Wieland <joe(at)mcknight(dot)de> writes:
> Actually both types are not binary compatible, since they have a
> type component that is either 0 or 1, depending on whether it is of type
> INET or CIDR.

The whole question of the relationship of those types really needs to be
looked at more carefully. We've got this schizophrenic idea that they
sometimes are the same type and sometimes are not. ISTM that either
they are the same type (and having a bit within the data is reasonable)
or they are distinct types (in which case the bit within the data should
be redundant). I'm not sure which is better.

I think the reason why things are as they are right now is to avoid
needing a pile of redundant-seeming pg_proc entries, eg you'd need
both abbrev(inet) and abbrev(cidr) if you were taking a hard line
about them being different types.

You can *not* just throw in a cast that removes the bit without breaking
many of those functions for the CIDR case. For instance abbrev behaves
differently depending on the state of the bit:

regression=# select abbrev(cidr '10.1.0.0/16');
abbrev
---------
10.1/16
(1 row)

regression=# select abbrev(inet '10.1.0.0/16');
abbrev
-------------
10.1.0.0/16
(1 row)

> What about functions to get/set a specific byte, for example:

I would vote against adding any such thing in the absence of any strong
demand for it. I think functions that expose the underlying data just
encourage people to write IPv4-only code. If you can't define and use
the function in a way that handles both IPv4 and IPv6, you probably
shouldn't have it.

regards, tom lane


From: Joachim Wieland <joe(at)mcknight(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: CIDR/INET improvements
Date: 2006-01-07 19:18:12
Message-ID: 20060107191812.GA3415@mcknight.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Sat, Jan 07, 2006 at 12:50:23PM -0500, Tom Lane wrote:
> Joachim Wieland <joe(at)mcknight(dot)de> writes:
> > Actually both types are not binary compatible, since they have a
> > type component that is either 0 or 1, depending on whether it is of type
> > INET or CIDR.

> The whole question of the relationship of those types really needs to be
> looked at more carefully. We've got this schizophrenic idea that they
> sometimes are the same type and sometimes are not. ISTM that either
> they are the same type (and having a bit within the data is reasonable)
> or they are distinct types (in which case the bit within the data should
> be redundant). I'm not sure which is better.

What about doing both? ;-)

We could create a few wrapper functions that call the functions that are
there right now. That way there is no need to duplicate the code with the
actual functionality. The outside world sees different types and the
function can distinguish between both if it needs to.

Joachim


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Joachim Wieland <joe(at)mcknight(dot)de>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] CIDR/INET improvements
Date: 2006-01-24 04:15:47
Message-ID: 200601240415.k0O4FlH16767@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches


I looked into this, and it seems the easiest solution is to just call
network() if a cidr-cast value is output and the value is actually an
inet value internally.

Patch for testing attached. Passes regression tests. By affecting only
the output you can internally cast back and forth and only output is
affected. However, if you load in a dump that was interally inet but
was dumped out as cidr-cast, you lose the unmasked bits.

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

Tom Lane wrote:
> Joachim Wieland <joe(at)mcknight(dot)de> writes:
> > Actually both types are not binary compatible, since they have a
> > type component that is either 0 or 1, depending on whether it is of type
> > INET or CIDR.
>
> The whole question of the relationship of those types really needs to be
> looked at more carefully. We've got this schizophrenic idea that they
> sometimes are the same type and sometimes are not. ISTM that either
> they are the same type (and having a bit within the data is reasonable)
> or they are distinct types (in which case the bit within the data should
> be redundant). I'm not sure which is better.
>
> I think the reason why things are as they are right now is to avoid
> needing a pile of redundant-seeming pg_proc entries, eg you'd need
> both abbrev(inet) and abbrev(cidr) if you were taking a hard line
> about them being different types.
>
> You can *not* just throw in a cast that removes the bit without breaking
> many of those functions for the CIDR case. For instance abbrev behaves
> differently depending on the state of the bit:
>
> regression=# select abbrev(cidr '10.1.0.0/16');
> abbrev
> ---------
> 10.1/16
> (1 row)
>
> regression=# select abbrev(inet '10.1.0.0/16');
> abbrev
> -------------
> 10.1.0.0/16
> (1 row)
>
>
> > What about functions to get/set a specific byte, for example:
>
> I would vote against adding any such thing in the absence of any strong
> demand for it. I think functions that expose the underlying data just
> encourage people to write IPv4-only code. If you can't define and use
> the function in a way that handles both IPv4 and IPv6, you probably
> shouldn't have it.
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>

--
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

Attachment Content-Type Size
unknown_filename text/plain 722 bytes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Joachim Wieland <joe(at)mcknight(dot)de>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] CIDR/INET improvements
Date: 2006-01-24 04:30:58
Message-ID: 13552.1138077058@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> Patch for testing attached.

This is an utterly bad idea, because it not only doesn't address the
problem (ie, confusion about whether inet and cidr are distinct types
or not), but it masks mistakes in that realm by hiding data on output.
It'll be almost impossible to debug situations where x is different
from y but they display the same.

regards, tom lane


From: Joachim Wieland <joe(at)mcknight(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] CIDR/INET improvements
Date: 2006-01-24 09:27:58
Message-ID: 20060124092758.GA24797@mcknight.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Mon, Jan 23, 2006 at 11:30:58PM -0500, Tom Lane wrote:
> Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> > Patch for testing attached.

> This is an utterly bad idea, because it not only doesn't address the
> problem (ie, confusion about whether inet and cidr are distinct types
> or not), but it masks mistakes in that realm by hiding data on output.
> It'll be almost impossible to debug situations where x is different
> from y but they display the same.

FWIW, I append the patch I've done a few weeks ago. It adds an inettocidr
cast function.
I updated it to comply to Bruce's recent "ip_type" -> "ip_is_cidr" change.

Joachim

--
Joachim Wieland joe(at)mcknight(dot)de
C/ Usandizaga 12 1°B ICQ: 37225940
20002 Donostia / San Sebastian (Spain) GPG key available

Attachment Content-Type Size
pg_inettocidr.3.diff text/plain 8.2 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joachim Wieland <joe(at)mcknight(dot)de>
Cc: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] CIDR/INET improvements
Date: 2006-01-24 15:11:07
Message-ID: 18590.1138115467@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Joachim Wieland <joe(at)mcknight(dot)de> writes:
> FWIW, I append the patch I've done a few weeks ago. It adds an inettocidr
> cast function.

I think we need to take two steps back and look at the larger picture:
the INET/CIDR situation is conceptually a mess and it's going to take
more than a localized change to clean it up.

I have some ideas about this and will try to post a proposal on -hackers
later today.

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joachim Wieland <joe(at)mcknight(dot)de>
Cc: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] CIDR/INET improvements
Date: 2006-01-26 02:37:06
Message-ID: 27062.1138243026@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Joachim Wieland <joe(at)mcknight(dot)de> writes:
> FWIW, I append the patch I've done a few weeks ago. It adds an inettocidr
> cast function.

I've incorporated this code into the INET cleanup patch just committed.
Thanks!

regards, tom lane