Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id ECE6018E38D7 for ; Thu, 12 Jan 2012 08:41:40 -0400 (AST) Received: from mail-ww0-f42.google.com ([74.125.82.42]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RlJyM-0001iM-2o for pgsql-bugs@postgresql.org; Thu, 12 Jan 2012 12:41:39 +0000 Received: by wgbdt14 with SMTP id dt14so144333wgb.1 for ; Thu, 12 Jan 2012 04:41:23 -0800 (PST) Received: by 10.180.93.193 with SMTP id cw1mr5830668wib.5.1326372083827; Thu, 12 Jan 2012 04:41:23 -0800 (PST) Received: from [192.168.1.104] (75-17-113-92.pool.ukrtel.net. [92.113.17.75]) by mx.google.com with ESMTPS id q7sm258207wix.5.2012.01.12.04.41.21 (version=SSLv3 cipher=OTHER); Thu, 12 Jan 2012 04:41:22 -0800 (PST) Date: Thu, 12 Jan 2012 14:41:23 +0200 From: Pavel Golub Reply-To: Pavel Golub Organization: Microolap X-Priority: 3 (Normal) Message-ID: <1108742574.20120112144123@gf.microolap.com> To: r d CC: pgsql-bugs@postgresql.org Subject: Re: Weird message when creating PK constraint named like table In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201201/100 X-Sequence-Number: 32167 Hello, r. You wrote: rd> When I do this rd> CREATE TABLE "T1" rd> ( rd> "T1_ID" bigint NOT NULL, rd> CONSTRAINT "T1" PRIMARY KEY ("T1_ID" ) rd> ); rd> I get the following message: rd> NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "T1" for table "T1" rd> ERROR: relation "T1" already exists rd> ********** Error ********** rd> ERROR: relation "T1" already exists rd> SQL state: 42P07 rd> It does NOT create either the table or the constraint, and the rd> message is confusing because there is no relation by that name. rd> The SQLSTATE 42P07 is described in the manual as only as "table rd> undefined", and it is not clear if the intent is to allow or rd> disallow the creation of a constraint called the same as the rd> table in Postgresql. Oracle 11g allows this, but my feeling is that rd> doing this should not be allowed, just as Postgresql handles it. rd> I am complaining about the confusing error message which IMO is rd> off-topic, not about how the DB handles this. rd> Seen on Postgresql 9.1.2 Linux 64bit. The quick answer is PRIMARY KEY constraint always has underlying system index with the same name. Thus to implement CREATE statement above PostgreSQL should create table with the name “T1″ and the index with the same name. This is impossible, because tables and indexes are stored in the same system catalog pg_class (they share the same namespace). That is where ambiguity appears. The same is true for UNIQUE constraint. On the other hand you may freely create CHECK constraint under such conditions: CREATE TABLE "T1" ( "T1_ID" bigint NOT NULL, CONSTRAINT "T1" CHECK ("T1_ID" > 0 ) ); -- With best wishes, Pavel mailto:pavel@gf.microolap.com