pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pavel Golub <pavel@microolap.com>
To: r d <rd0002@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Weird message when creating PK constraint named like table
Date: Thu, 12 Jan 2012 14:41:23 +0200
Message-ID: <1108742574.20120112144123@gf.microolap.com> (raw)
In-Reply-To: <CALtFtELrOJ-V0wuke++S7C1_XvDiJ9irBtnuecjLMq4d7zhhTg@mail.gmail.com>
References: <CALtFtELrOJ-V0wuke++S7C1_XvDiJ9irBtnuecjLMq4d7zhhTg@mail.gmail.com>

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




view thread (5+ messages)  latest in thread

Message-ID: <1108742574.20120112144123@gf.microolap.com>
Permalink:  ../1108742574.20120112144123@gf.microolap.com/
Also on:    postgresql.org/message-id/1108742574.20120112144123@gf.microolap.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: pavel@microolap.com, rd0002@gmail.com
  Subject: Re: Weird message when creating PK constraint named like table
  In-Reply-To: <1108742574.20120112144123@gf.microolap.com>
  References: <CALtFtELrOJ-V0wuke++S7C1_XvDiJ9irBtnuecjLMq4d7zhhTg@mail.gmail.com> <1108742574.20120112144123@gf.microolap.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox