Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fn5ur-00012v-SU for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Aug 2018 17:37:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fn5uq-0006KG-2m for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Aug 2018 17:37:04 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fn5up-0006K9-OR for pgsql-hackers@lists.postgresql.org; Tue, 07 Aug 2018 17:37:03 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fn5un-0002Yi-0l for pgsql-hackers@postgresql.org; Tue, 07 Aug 2018 17:37:02 +0000 Received: from bruce by momjian.us with local (Exim 4.84_2) (envelope-from ) id 1fn5ul-0003jW-Vh; Tue, 07 Aug 2018 13:36:59 -0400 Date: Tue, 7 Aug 2018 13:36:59 -0400 From: Bruce Momjian To: kes-kes@yandex.ru, PostgreSQL-development Subject: Re: Typo in doc or wrong EXCLUDE implementation Message-ID: <20180807173659.GD7297@momjian.us> References: <153121527691.1408.5686988620817799073@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <153121527691.1408.5686988620817799073@wrigleys.postgresql.org> User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This email was sent to docs, but I think it is a hackers issue. The person is asking why exclusion constraints aren't marked as UNIQUE indexes that can be used for referential integrity. I think the reason is that non-equality exclusion constraints, like preventing overlap, but don't uniquely identify a specific value, and I don't think we want to auto-UNIQUE just for equality exclusion constraints. --------------------------------------------------------------------------- On Tue, Jul 10, 2018 at 09:34:36AM +0000, PG Doc comments form wrote: > The following documentation comment has been logged on the website: > > Page: https://www.postgresql.org/docs/10/static/sql-createtable.html > Description: > > Hi. > > https://www.postgresql.org/docs/current/static/sql-createtable.html#sql-createtable-exclude > If all of the specified operators test for equality, this is equivalent to a > UNIQUE constraint > > Exclusion constraints are implemented using an index > > > ALTER TABLE person > add constraint person_udx_person_id2 > EXCLUDE USING gist ( > person_id WITH = > ) > ; > > tucha=> ALTER TABLE "person_x_person" ADD CONSTRAINT > "person_x_person_fk_parent_person_id" > tucha-> FOREIGN KEY ("parent_person_id") > tucha-> REFERENCES "person" ("person_id") > tucha-> ON DELETE CASCADE ON UPDATE NO ACTION DEFERRABLE; > ERROR: there is no unique constraint matching given keys for referenced > table "person" > > because gist does not support unique indexes, I try with 'btree' > > > ALTER TABLE person > add constraint person_udx_person_id2 > EXCLUDE USING btree ( > person_id WITH = > ) > ; > > \d person > ... > "person_udx_person_id2" EXCLUDE USING btree (person_id WITH =) > > tucha=> ALTER TABLE "person_x_person" ADD CONSTRAINT > "person_x_person_fk_parent_person_id" > tucha-> FOREIGN KEY ("parent_person_id") > tucha-> REFERENCES "person" ("person_id") > tucha-> ON DELETE CASCADE ON UPDATE NO ACTION DEFERRABLE; > ERROR: there is no unique constraint matching given keys for referenced > table "person" > > Why postgres does not add unique flag. Despite on: "this is equivalent to a > UNIQUE constraint" > I thought it should be: > "person_udx_person_id2" UNIQUE EXCLUDE USING btree (person_id WITH =) > > PS. > > For example, you can specify a constraint that no two rows in the table > contain overlapping circles (see Section 8.8) by using the && operator. > > Also I expect that this: > ALTER TABLE person > add constraint person_udx_person_id > EXCLUDE USING gist ( > person_id WITH =, > tstzrange(valid_from, valid_till, '[)' ) WITH && > ) > > also should raise UNIQUE flag for exclusion thus we can use it in FK -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + As you are, so once was I. As I am, so you will be. + + Ancient Roman grave inscription +