(9.1) btree_gist support for searching on "not equals"

Lists: pgsql-hackers
From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>, Teodor Sigaev <teodor(at)sigaev(dot)ru>
Subject: (9.1) btree_gist support for searching on "not equals"
Date: 2010-05-21 20:47:36
Message-ID: 1274474857.27379.101.camel@jdavis-laptop
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


This patch adds support to btree_gist for searching on <> ("not
equals").

This allows an interesting use of exclusion constraints:

Say you have a table:

create table zoo
(
cage int,
animal text,
exclude using gist (cage with =, animal with <>)
);

That will permit you to add as many zebras as you want to a given cage,
and as many lions as you want to another cage, but will not allow you to
mix zebras and lions in the same cage.

It also allows you to enforce the constraint that only one tuple exists
in a table by doing something like:

create table a
(
i int,
exclude using gist (i with <>),
unique (i)
);

Regards,
Jeff Davis

Attachment Content-Type Size
btree-gist-ne.patch text/x-patch 7.7 KB

From: Marko Tiikkaja <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org, Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>, Teodor Sigaev <teodor(at)sigaev(dot)ru>
Subject: Re: (9.1) btree_gist support for searching on "not equals"
Date: 2010-05-21 22:02:46
Message-ID: 4BF70306.6080309@cs.helsinki.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 5/21/10 11:47 PM +0300, Jeff Davis wrote:
> It also allows you to enforce the constraint that only one tuple exists
> in a table by doing something like:
>
> create table a
> (
> i int,
> exclude using gist (i with<>),
> unique (i)
> );

FWIW, this is achievable a lot more easily:
CREATE UNIQUE INDEX "a_single_row" ON a ((1));

Regards,
Marko Tiikkaja


From: Takahiro Itagaki <itagaki(dot)takahiro(at)oss(dot)ntt(dot)co(dot)jp>
To: Marko Tiikkaja <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi>
Cc: Jeff Davis <pgsql(at)j-davis(dot)com>, pgsql-hackers(at)postgresql(dot)org, Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>, Teodor Sigaev <teodor(at)sigaev(dot)ru>
Subject: Re: (9.1) btree_gist support for searching on "not equals"
Date: 2010-05-25 01:03:22
Message-ID: 20100525100322.CF3F.52131E4D@oss.ntt.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Marko Tiikkaja <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi> wrote:

> On 5/21/10 11:47 PM +0300, Jeff Davis wrote:
> > It also allows you to enforce the constraint that only one tuple exists
> > in a table by doing something like:
> >
> > create table a
> > (
> > i int,
> > exclude using gist (i with<>),
> > unique (i)
> > );

+1. I've not read the code, but it might be considerable that we can
abort index scans if we find a first index entry for "i". While we must
scan all candidates for "WHERE i <> ?", but we can abort for the constraint
case because we know existing values are all the same.

> FWIW, this is achievable a lot more easily:
> CREATE UNIQUE INDEX "a_single_row" ON a ((1));

The former exclusion constraint means "one same value for all rows",
but your alternative means "a_single_row", right?

Regards,
---
Takahiro Itagaki
NTT Open Source Software Center


From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Marko Tiikkaja <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi>
Cc: pgsql-hackers(at)postgresql(dot)org, Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>, Teodor Sigaev <teodor(at)sigaev(dot)ru>
Subject: Re: (9.1) btree_gist support for searching on "not equals"
Date: 2010-05-25 03:25:18
Message-ID: 1274757918.3342.4.camel@jdavis
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, 2010-05-22 at 01:02 +0300, Marko Tiikkaja wrote:
> On 5/21/10 11:47 PM +0300, Jeff Davis wrote:
> > It also allows you to enforce the constraint that only one tuple exists
> > in a table by doing something like:
> >
> > create table a
> > (
> > i int,
> > exclude using gist (i with<>),
> > unique (i)
> > );
>
> FWIW, this is achievable a lot more easily:
> CREATE UNIQUE INDEX "a_single_row" ON a ((1));
>

Yes, you're right. Also, neither of us accounted for NULLs, so I suppose
a NOT NULL is necessary as well.

I think the original case (same values only) is potentially useful
enough that we should support it.

Regards,
Jeff Davis


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: Marko Tiikkaja <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi>, pgsql-hackers(at)postgresql(dot)org, Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>, Teodor Sigaev <teodor(at)sigaev(dot)ru>
Subject: Re: (9.1) btree_gist support for searching on "not equals"
Date: 2010-05-25 03:39:23
Message-ID: AANLkTimi-euOZ-LfSccbcRpq0VJwOMRl1xZ0MNoptqsO@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, May 24, 2010 at 11:25 PM, Jeff Davis <pgsql(at)j-davis(dot)com> wrote:
> I think the original case (same values only) is potentially useful
> enough that we should support it.

+1.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise Postgres Company