Re: searching cidr/inet arrays - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: searching cidr/inet arrays
Date
Msg-id 20050425202406.GA3289@winnie.fuhr.org
Whole thread Raw
In response to searching cidr/inet arrays  (Rob Casson <rob.casson@gmail.com>)
Responses UPDATE WITH ORDER BY  (Rodrigo Carvalhaes <grupos@carvalhaes.net>)
List pgsql-sql
On Mon, Apr 25, 2005 at 02:46:37PM -0400, Rob Casson wrote:
>
> i'm having trouble figuring out how to search in inet arrays....its
> been a long time since i used postgres array support, so i may just be
> bone-headed......
> 
> how can i determine if a given ip address is contained in the subnet
> declaration inside an array?
> 
>     {134.53.25.0/24,134.53.0.0/16}
>     {134.53.24.0/24}
> 
> i'd like to see which rows match an ip of, say, 134.53.24.2.....

See "Row and Array Comparisons" in the "Functions and Operators"
chapter of the documentation.  The following works in 7.4 and later:

CREATE TABLE foo (   id    serial PRIMARY KEY,   nets  cidr[] NOT NULL
);

INSERT INTO foo (nets) VALUES ('{134.53.25.0/24,134.53.0.0/16}');
INSERT INTO foo (nets) VALUES ('{134.53.24.0/24}');

SELECT * FROM foo WHERE '134.53.24.2' << ANY (nets);id |              nets              
----+-------------------------------- 1 | {134.53.25.0/24,134.53.0.0/16} 2 | {134.53.24.0/24}
(2 rows)

SELECT * FROM foo WHERE '134.53.100.2' << ANY (nets);id |              nets              
----+-------------------------------- 1 | {134.53.25.0/24,134.53.0.0/16}
(1 row)

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/


pgsql-sql by date:

Previous
From: Rob Casson
Date:
Subject: searching cidr/inet arrays
Next
From: Michael Fuhr
Date:
Subject: Re: Coming from Oracle SQL