pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: Rob Casson <rob.casson@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: searching cidr/inet arrays
Date: Mon, 25 Apr 2005 14:24:06 -0600
Message-ID: <20050425202406.GA3289@winnie.fuhr.org> (raw)
In-Reply-To: <fddc0c5705042511464f94be68@mail.gmail.com>
References: <fddc0c5705042511464f94be68@mail.gmail.com>

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/



view thread (2+ messages)

Message-ID: <20050425202406.GA3289@winnie.fuhr.org>
Permalink:  ../20050425202406.GA3289@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20050425202406.GA3289@winnie.fuhr.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mike@fuhr.org, rob.casson@gmail.com
  Subject: Re: searching cidr/inet arrays
  In-Reply-To: <20050425202406.GA3289@winnie.fuhr.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox