Thread: Re: [SQL] An easy question about creating a primary key

Re: [SQL] An easy question about creating a primary key

From
"Christopher Kings-Lynne"
Date:
> I think you've forgotten your own work, Chris.
>
> regression=# create table foo (bar int not null);
> CREATE
> regression=# alter table foo add primary key (bar);
> NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index
> 'foo_pkey' for table 'foo'
> CREATE
> regression=#

Bizarre.  That patch was never committed.  If you check
src/backend/commands/command.c and search for 'CONSTR_' you'll notice that
the CONSTR_UNIQUE function I implemented is there, but CONSTR_PRIMARY is
definitely not being handled.  (I'm looking at the 7.2b2 source code)

Chris



Re: [SQL] An easy question about creating a primary key

From
Tom Lane
Date:
"Christopher Kings-Lynne" <chriskl@familyhealth.com.au> writes:
>> I think you've forgotten your own work, Chris.
>> 
>> regression=# create table foo (bar int not null);
>> CREATE
>> regression=# alter table foo add primary key (bar);
>> NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index
>> 'foo_pkey' for table 'foo'
>> CREATE
>> regression=#

> Bizarre.  That patch was never committed.  If you check
> src/backend/commands/command.c and search for 'CONSTR_' you'll notice that
> the CONSTR_UNIQUE function I implemented is there, but CONSTR_PRIMARY is
> definitely not being handled.  (I'm looking at the 7.2b2 source code)

Hmm ... actually, I wonder whether that code in command.c isn't entirely
dead code.  I believe that as things stand, parser/analyze.c converts
UNIQUE and PRIMARY constraints into CREATE INDEX statements; the
constraint nodes themselves never make it past the parser.  It looks to
me like command.c only needs to handle CHECK constraints and foreign-key
constraints, cf transformAlterTableStmt().
        regards, tom lane


Re: [SQL] An easy question about creating a primary key

From
Tom Lane
Date:
Now that I look at it, I think I made the relevant changes in the
parser:

2001-10-11 20:07  tgl
* doc/src/sgml/ref/alter_table.sgml, src/backend/catalog/pg_type.c,src/backend/commands/command.c,
src/backend/parser/analyze.c,src/backend/tcop/utility.c,
src/include/commands/command.h,src/include/nodes/parsenodes.h,src/test/regress/expected/alter_table.out,src/test/regress/expected/foreign_key.out:
BreaktransformCreateStmt()into multiple routines and maketransformAlterStmt() use these routines, instead of having
lotsofduplicate (not to mention should-have-been-duplicate) code.  Addinga column with a CHECK constraint actually
worksnow, and the teststo reject unsupported DEFAULT and NOT NULL clauses actually firenow.  ALTER TABLE ADD PRIMARY
KEYworks, modulo having to havecreated the column(s) NOT NULL already.
 

I was mainly interested in eliminating the inconsistencies in parse-time
handling of CREATE TABLE and ALTER TABLE, and the ensuing bugs mentioned
in the commit log.  I didn't think much about the possibility that I was
obsoleting stuff in command.c, but maybe I did.
        regards, tom lane


Re: [SQL] An easy question about creating a primary key

From
"Christopher Kings-Lynne"
Date:
You know, that would explain a lot.  Since this was only done in October, I
wouldn't have noticed it.  And it explains why I couldn't get various
changes in my code to actually have any effect...

Oh well, it was a fun coding exercise ;)  Feel free to remove the ADD UNIQUE
stuff from command.c to see if the parser will handle it.  However, your
commit message also just implies that all you fixed was ADD PRIMARY KEY???

Chris

> -----Original Message-----
> From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
> Sent: Tuesday, 4 December 2001 10:40 AM
> To: Christopher Kings-Lynne
> Cc: Ligia Pimentel; Hackers
> Subject: Re: [SQL] An easy question about creating a primary key
>
>
> Now that I look at it, I think I made the relevant changes in the
> parser:
>
> 2001-10-11 20:07  tgl
>
>     * doc/src/sgml/ref/alter_table.sgml, src/backend/catalog/pg_type.c,
>     src/backend/commands/command.c, src/backend/parser/analyze.c,
>     src/backend/tcop/utility.c, src/include/commands/command.h,
>     src/include/nodes/parsenodes.h,
>     src/test/regress/expected/alter_table.out,
>     src/test/regress/expected/foreign_key.out: Break
>     transformCreateStmt() into multiple routines and make
>     transformAlterStmt() use these routines, instead of having lots of
>     duplicate (not to mention should-have-been-duplicate) code.  Adding
>     a column with a CHECK constraint actually works now, and the tests
>     to reject unsupported DEFAULT and NOT NULL clauses actually fire
>     now.  ALTER TABLE ADD PRIMARY KEY works, modulo having to have
>     created the column(s) NOT NULL already.
>
> I was mainly interested in eliminating the inconsistencies in parse-time
> handling of CREATE TABLE and ALTER TABLE, and the ensuing bugs mentioned
> in the commit log.  I didn't think much about the possibility that I was
> obsoleting stuff in command.c, but maybe I did.
>
>             regards, tom lane
>