Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 93D9D9FB1EF for ; Sat, 27 Jan 2007 07:58:32 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 79615-02 for ; Sat, 27 Jan 2007 07:58:28 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from proton.planetargon.com (proton.planetargon.com [198.145.37.35]) by postgresql.org (Postfix) with ESMTP id 605F59FA4CA for ; Sat, 27 Jan 2007 07:58:28 -0400 (AST) Received: from [172.16.1.106] (unknown [61.197.227.146]) by proton.planetargon.com (Postfix) with ESMTP id 476618D7188; Sat, 27 Jan 2007 03:58:26 -0800 (PST) In-Reply-To: <20070127075505.GA8035@wolff.to> References: <45B9CCC6.4000109@fuzzy.cz> <20070127075505.GA8035@wolff.to> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <51AAD09D-F90D-480F-A93E-0C57246415D7@seespotcode.net> Cc: Tomas Vondra , pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: implementing (something like) UNIQUE constraint using PL/pgSQL Date: Sat, 27 Jan 2007 20:58:24 +0900 To: Bruno Wolff III X-Mailer: Apple Mail (2.752.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/147 X-Sequence-Number: 27299 On Jan 27, 2007, at 16:55 , Bruno Wolff III wrote: > On Fri, Jan 26, 2007 at 10:41:26 +0100, > Tomas Vondra wrote: >> >> in our application we need to implement a constraint that enforces >> 'at >> most N rows with this value', that is we have a table with 'flag' >> column >> and for each value there should be at most 10 rows (for example, the >> exact number does not matter). > > Another approach is to add a instance number column and constrain that > value to be between 1 and 10. And make value and instance number > unique. > You'll need to do a bit more work when inserting new rows than normal > (to find a free instance number). This should be very robust against > getting in a bad state. > If you go with enforcing the condition with a trigger you need to > be careful > about simultaneous inserts and visibility. This may have already been mentioned upthread, but this sounds like a special case of the SQL queues I saw on Greg Sabino Mullane's blog: http://people.planetpostgresql.org/greg/index.php?/archives/89- Implementing-a-queue-in-SQL-Postgres-version.html Perhaps you could tweak that to serve your needs. Michael Glaesemann grzm seespotcode net