Re: Bug of ALTER TABLE DROP CONSTRAINT

Lists: pgsql-hackers
From: "Jacky Leng" <lengjianquan(at)163(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-02 07:25:21
Message-ID: gr1p91$flr$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Considering the following sequence:
create table t(a int primary key);
alter table t drop constraint t_pkey;
insert into t values(null);
ERROR: null value in column "a" violates not-null constraint

My question is, why "null" is not allowed to be inserted after primary key
constraint has been dropped.


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Jacky Leng <lengjianquan(at)163(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-02 11:55:15
Message-ID: 603c8f070904020455k47229181h58a629ab9b7b3623@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Apr 2, 2009 at 3:25 AM, Jacky Leng <lengjianquan(at)163(dot)com> wrote:
> Considering the following sequence:
>    create table t(a int primary key);
>    alter table t drop constraint t_pkey;
>    insert into t values(null);
>    ERROR:  null value in column "a" violates not-null constraint
>
> My question is, why "null" is not allowed to be inserted after primary key
> constraint has been dropped.

Making a column into the primary key forces the column to NOT NULL.
You'll need to DROP NOT NULL separately.

It's probably possible to beat on the code hard enough to fix this,
but I'm not really sure there's much point, since the situation is
rare and the workaround is easy.

...Robert


From: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Jacky Leng <lengjianquan(at)163(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-02 12:24:48
Message-ID: a301bfd90904020524q25197c22o2e69a87cb0f1180@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

> > Considering the following sequence:
> > create table t(a int primary key);
> > alter table t drop constraint t_pkey;
> > insert into t values(null);
> > ERROR: null value in column "a" violates not-null constraint
> >
> > My question is, why "null" is not allowed to be inserted after primary
> key
> > constraint has been dropped.
>
> Making a column into the primary key forces the column to NOT NULL.
> You'll need to DROP NOT NULL separately.
>
> It's probably possible to beat on the code hard enough to fix this,

Yeah it will be a matter of finding the affected column entries and invoking
the removal of the not null entry from their corresponding pg_attribute
rows.

> but I'm not really sure there's much point, since the situation is
> rare and the workaround is easy.
>

Yeah and it is documented already. Although it is not obvious immediately
that a not-null attribute gets tagged onto the involved columns separately
for primary, unique-not-null types of constraints.

Regards,
Nikhils
--
http://www.enterprisedb.com


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>
Cc: Jacky Leng <lengjianquan(at)163(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-02 13:05:05
Message-ID: 603c8f070904020605s482786fata34aaa7880df5489@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Apr 2, 2009 at 8:24 AM, Nikhil Sontakke
<nikhil(dot)sontakke(at)enterprisedb(dot)com> wrote:
> Hi,
>>
>> > Considering the following sequence:
>> >    create table t(a int primary key);
>> >    alter table t drop constraint t_pkey;
>> >    insert into t values(null);
>> >    ERROR:  null value in column "a" violates not-null constraint
>> >
>> > My question is, why "null" is not allowed to be inserted after primary
>> > key
>> > constraint has been dropped.
>>
>> Making a column into the primary key forces the column to NOT NULL.
>> You'll need to DROP NOT NULL separately.
>>
>> It's probably possible to beat on the code hard enough to fix this,
>
> Yeah it will be a matter of finding the affected column entries and invoking
> the removal of the not null entry from their corresponding pg_attribute
> rows.

Actually it's more complicated than that. You'd need to remember
whether or not the NOT NULL was added when the primary key was added,
or whether it was there before, and only drop it if it wasn't there
before.

...Robert


From: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Jacky Leng <lengjianquan(at)163(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-02 13:17:48
Message-ID: a301bfd90904020617p20d41a1ag63b266825f707546@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

> >>
> >> Making a column into the primary key forces the column to NOT NULL.
> >> You'll need to DROP NOT NULL separately.
> >>
> >> It's probably possible to beat on the code hard enough to fix this,
> >
> > Yeah it will be a matter of finding the affected column entries and
> invoking
> > the removal of the not null entry from their corresponding pg_attribute
> > rows.
>
> Actually it's more complicated than that. You'd need to remember
> whether or not the NOT NULL was added when the primary key was added,
> or whether it was there before, and only drop it if it wasn't there
> before.
>

Hmm, and maybe that is the reason why this is not clubbed with the removal
of the primary constraint. Otherwise it seems to be a matter of decompiling
the conkey and generating AT_DropNotNull nodes for the involved columns
within ATPrepCmd and the rest should happen automatically. So I guess we can
let it be.

Regards,
Nikhils
--
http://www.enterprisedb.com


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>, Jacky Leng <lengjianquan(at)163(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-02 14:01:18
Message-ID: 19768.1238680878@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> Actually it's more complicated than that. You'd need to remember
> whether or not the NOT NULL was added when the primary key was added,
> or whether it was there before, and only drop it if it wasn't there
> before.

We've discussed before the idea that NOT NULL constraints should be
explicitly represented in pg_constraint, just like general CHECK
constraints (this would allow them to be named, have sane inheritance
behavior, etc). If we had that, then pg_attribute.attnotnull could
indicate the OR of "there is a NOT NULL on this column" and "there is
a pkey constraint on this column", and you'd just have to recompute it
properly after dropping either kind of constraint.

Not happening for 8.4, but maybe someday someone will get around to it.

regards, tom lane


From: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-03 09:45:22
Message-ID: a301bfd90904030245g1a8e113cu17ec902de3fe9ad6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

> We've discussed before the idea that NOT NULL constraints should be
> explicitly represented in pg_constraint, just like general CHECK
> constraints (this would allow them to be named, have sane inheritance
> behavior, etc). If we had that, then pg_attribute.attnotnull could
> indicate the OR of "there is a NOT NULL on this column" and "there is
> a pkey constraint on this column", and you'd just have to recompute it
> properly after dropping either kind of constraint.
>
> Not happening for 8.4, but maybe someday someone will get around to it.
>

Warrants an entry in the TODO items list:

* make NOT NULL constraints have pg_constraint entries, just like CHECK
constraints

Regards,
Nikhils
--
http://www.enterprisedb.com


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-07 14:45:13
Message-ID: 200904071445.n37EjD015354@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Nikhil Sontakke wrote:
> Hi,
>
>
> > We've discussed before the idea that NOT NULL constraints should be
> > explicitly represented in pg_constraint, just like general CHECK
> > constraints (this would allow them to be named, have sane inheritance
> > behavior, etc). If we had that, then pg_attribute.attnotnull could
> > indicate the OR of "there is a NOT NULL on this column" and "there is
> > a pkey constraint on this column", and you'd just have to recompute it
> > properly after dropping either kind of constraint.
> >
> > Not happening for 8.4, but maybe someday someone will get around to it.
> >
>
> Warrants an entry in the TODO items list:
>
> * make NOT NULL constraints have pg_constraint entries, just like CHECK
> constraints

This is now a TODO item (I just updated the description):

Store the constraint names of NOT NULL constraints

Currently NOT NULL constraints are stored in pg_attribute without
any designation of their origins, e.g. primary keys. One manifest
problem is that dropping a PRIMARY KEY constraint does not remove the
NOT NULL constraint designation.

* http://archives.postgresql.org/message-id/19768.1238680878@sss.pgh.pa.us

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-07 14:53:39
Message-ID: 1935.1239116019@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bruce Momjian <bruce(at)momjian(dot)us> writes:
> Nikhil Sontakke wrote:
>> Warrants an entry in the TODO items list:
>>
>> * make NOT NULL constraints have pg_constraint entries, just like CHECK
>> constraints

> This is now a TODO item (I just updated the description):

> Store the constraint names of NOT NULL constraints

I was intending to do that yesterday, but lost interest after
discovering how many duplicate, obsolete, and/or mutually contradictory
TODO entries there are related to constraints. That needs to be cleaned
up and consolidated sometime.

Also, what is wrong with the precise statement of the TODO item that
Nikhil gave? The one you gave would encourage someone to waste time on
a 100% wrong implementation (like adding a constraint name column to
pg_attribute).

regards, tom lane


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug of ALTER TABLE DROP CONSTRAINT
Date: 2009-04-07 21:15:01
Message-ID: 200904072115.n37LF1g16071@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane wrote:
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
> > Nikhil Sontakke wrote:
> >> Warrants an entry in the TODO items list:
> >>
> >> * make NOT NULL constraints have pg_constraint entries, just like CHECK
> >> constraints
>
> > This is now a TODO item (I just updated the description):
>
> > Store the constraint names of NOT NULL constraints
>
> I was intending to do that yesterday, but lost interest after
> discovering how many duplicate, obsolete, and/or mutually contradictory
> TODO entries there are related to constraints. That needs to be cleaned
> up and consolidated sometime.
>
> Also, what is wrong with the precise statement of the TODO item that
> Nikhil gave? The one you gave would encourage someone to waste time on
> a 100% wrong implementation (like adding a constraint name column to
> pg_attribute).

I felt the original wording would be unclear in explaining the problem
behavior; you are right the original wording was clearer for correcting
the problem. This updated wording works for both audiences, I think:

Move NOT NULL constraint information to pg_constraint

Currently NOT NULL constraints are stored in pg_attribute without
any designation of their origins, e.g. primary keys. One manifest
problem is that dropping a PRIMARY KEY constraint does not remove the
NOT NULL constraint designation.

*
http://archives.postgresql.org/message-id/19768.1238680878@sss.pgh.pa.us

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +