Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TPdz3-0002vK-JF for pgsql-bugs@postgresql.org; Sat, 20 Oct 2012 18:41:17 +0000 Received: from mail-pa0-f46.google.com ([209.85.220.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TPdyy-0005ea-SB for pgsql-bugs@postgresql.org; Sat, 20 Oct 2012 18:41:16 +0000 Received: by mail-pa0-f46.google.com with SMTP id hz1so1078024pad.19 for ; Sat, 20 Oct 2012 11:41:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:subject:from:to:cc:date:in-reply-to:references :content-type:x-mailer:content-transfer-encoding:mime-version :x-gm-message-state; bh=gL24Ba7Cqi0sIylqYUc9Dvxf8Od6ARpWtQtCSydPdG8=; b=a33srJQK2+nTfI0Dy7pYDarn6etlQhdGUb4U9T/r2LmF/0Sqot4SVuXVxXmGQoCYZ5 62WLhlUSvKmmH/e8+cvha4MjAwIXuRRwtH42wyqpG3UIms6pfOGMUHIbpnKFq/9rDmO+ djttMDtDCCVxtv36x5x3LW3uqpOBPLFB3UvG0/tFT3HnB6O6dUUzQMiCzs5UV+3Rs5pP RiXPXEh1b3AXQFXxIRnVV9EEi5fFb/gpQUF6xxpuR2kYzXXa61D2ycx2vt41ry2U//F+ BGYMzl9JjjKCUQU2BtL0E6cCfVNUOKMwbpE7GO038zg5rNAXOmZP3AhNOngyKBWUGOer CLtg== Received: by 10.66.85.40 with SMTP id e8mr14029482paz.64.1350758471576; Sat, 20 Oct 2012 11:41:11 -0700 (PDT) Received: from [192.168.1.105] (c-69-181-249-16.hsd1.ca.comcast.net. [69.181.249.16]) by mx.google.com with ESMTPS id kr4sm3163491pbc.76.2012.10.20.11.41.10 (version=SSLv3 cipher=OTHER); Sat, 20 Oct 2012 11:41:10 -0700 (PDT) Message-ID: <1350758503.14401.18.camel@jdavis> Subject: Re: BUG #7525: Deferred unique index with predicate From: Jeff Davis To: rikard.pavelic@zg.htnet.hr Cc: pgsql-bugs@postgresql.org Date: Sat, 20 Oct 2012 11:41:43 -0700 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.2.2- Content-Transfer-Encoding: 7bit Mime-Version: 1.0 X-Gm-Message-State: ALoCoQmhDQHmueNMrmbXCIv+2sjFAE7t6WabVr/0o7HK91Wx0Iim58ZhN5tgEyQvUjiJswiXEFb/ X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201210/195 X-Sequence-Number: 34495 On Sat, 2012-09-08 at 12:40 +0000, rikard.pavelic@zg.htnet.hr wrote: > The following bug has been logged on the website: > > Bug reference: 7525 > Logged by: Rikard Pavelic > Email address: rikard.pavelic@zg.htnet.hr > PostgreSQL version: 9.1.2 > Operating system: Windows 7 > Description: > > Is there a way in Postgres to create a unique constraint with predicate or > an unique index which is deferred? > > Depesz said not yet. > http://www.depesz.com/2009/08/11/waiting-for-8-5-deferrable-uniqueness/ Both features exist. http://www.postgresql.org/docs/9.2/static/sql-createtable.html CREATE TABLE xyz(i int, j int, unique (i) deferrable); To create a unique index with a predicate, do: CREATE UNIQUE INDEX xyz_uniq_idx ON xyz(j) WHERE j > 10; However, it can't both be deferrable and have a predicate. In order to do that, you need to use an exclusion constraint: http://www.postgresql.org/docs/9.2/static/sql-createtable.html#SQL-CREATETABLE-EXCLUDE To make it equivalent to UNIQUE, set all operators to "=", e.g.: CREATE TABLE xyz(i int, exclude (i WITH =) where (i > 10) deferrable); Regards, Jeff Davis