Re: searching cidr/inet arrays

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Rob Casson <rob(dot)casson(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: searching cidr/inet arrays
Date: 2005-04-25 20:24:06
Message-ID: 20050425202406.GA3289@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Fuhr 2005-04-25 21:36:00 Re: Coming from Oracle SQL
Previous Message Rob Casson 2005-04-25 18:46:37 searching cidr/inet arrays