Re: inet/cidr type comparisons

From: Alex Pilosov <alex(at)pilosoft(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inet/cidr type comparisons
Date: 2001-06-11 17:16:01
Message-ID: Pine.BSO.4.10.10106111218170.16686-100000@spider.pilosoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

This behaviour makes harder to use index to optimize a << b

What I have right now is rewriting a <<= b to use index plan :
(a >= network(b)) && ( a <= broadcast(b) )

However, that breaks down, since (for example)

if a=10.1.2.3/32 and b = 10.1.2.0/24, broadcast(b) will be 10.1.2.255/24,
but 10.1.2.255/24 is considered to be less than 10.1.2.3/32...

I can work around this, however, the concept just didn't make sense to me,
but I see that for some people it does, so I'll live with it :)

So what I'm going to do then is to make a function set_masklen(inet|cidr,
int4) which would take an existing address and return a new value with
changed masklen.

Also, I'd like to create casting functions from varchar to inet/cidr,
since they are missing. Functions I'm writing:

varchar_inet(varchar)
varchar_cidr(varchar)
varchar_inet(varchar, int4)
varchar_cidr(varchar, int4)

(the last two variants will take masklen as a separate argument)

Does this look good? Actually, what's more advisable for these functions,
doing conversions from varchar or doing it from text?

Apologies for asking so many questions, but I'd like a sanity check before
proceeding :)

Thanks
-alex

On Mon, 11 Jun 2001, Tom Lane wrote:

> Alex Pilosov <alex(at)pilosoft(dot)com> writes:
> > I noticed current wierd behaviour of a less/greater than comparisons of
> > things involving inet/cidr:
>
> > 10.1.2.3/8 is considered to be less than 10.0.0.0/32
>
> And what's wrong with that? Essentially this comes from the conclusion
> that 10/8 is less than 10.0.0.0/32, which I have no problem with.
>
> > To me, this makes no sense. I think b and c should be transposed, and
> > netmask comparison must be only used as a tiebreaker when the values are
> > the same otherwise (such as, when comparing 10.1.2.3/8 and 10.1.2.3/32).
>
> That would break the rule that network part is major sort key and host
> part is minor sort key, which I think is useful behavior.
>
> > For type cidr, same thing applies: currently, 10.1.2.0/24 is considered to
> > be less than 10.0.0.0/8.
>
> It is?
>
> regression=# select '10.1.2.0/24'::cidr < '10.0.0.0/8'::cidr;
> ?column?
> ----------
> f
> (1 row)
>
>
> regards, tom lane
>
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message mlw 2001-06-11 17:21:14 OID Wrap
Previous Message Keith G. Murphy 2001-06-11 16:28:09 Re: something smells bad