Thread: ADD COLUMN with PRIMARY KEY bug (was: I think this is a BUG?)

ADD COLUMN with PRIMARY KEY bug (was: I think this is a BUG?)

From
"Brendan Jurd"
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

The attached patch resolves the bug reported by Kaloyan Iliev [1] on -general.

The problem occurred when executing ALTER TABLE ... ADD COLUMN ...
PRIMARY KEY with no default clause, on a table with rows already
present.  The NOT NULL constraint was not enforced.  The bug has been
observed on (at least) 8.2, 8.3 and CVS HEAD.

The fix was to force evaluation of the NOT NULL constraint in
ATExecAddColumn in all cases where the parser indicated that the
column ought to be NOT NULL.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: http://getfiregpg.org

iD8DBQFIENpl5YBsbHkuyV0RApP6AKDdnZAA0LjSzh9XdiQt7K/cx4YOcQCgydHr
6BYSD2kVX2DP7LYgDHvzNdE=
=Yrmp
-----END PGP SIGNATURE-----

On Fri, Apr 25, 2008 at 3:46 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>  alter table t1 add column f2 int not null;
>
>  transformAlterTableStmt will produce an AT_AddColumn subcommand
>  containing a ColumnDef with is_not_null = false, followed by an
>  AT_SetNotNull subcommand.  But for
>

I realised that there's no reason for preparing a separate SetNotNull
subcommand anymore, now that ATExecAddColumn takes care of enforcing
the constraint, so I removed this special case.

I've also added a regression test for ADD COLUMN PRIMARY KEY.

Cheers,
BJ

[1] http://archives.postgresql.org/message-id/481040A2.7090203@digsys.bg

Attachment

Re: ADD COLUMN with PRIMARY KEY bug (was: I think this is a BUG?)

From
Tom Lane
Date:
"Brendan Jurd" <direvus@gmail.com> writes:
> I realised that there's no reason for preparing a separate SetNotNull
> subcommand anymore, now that ATExecAddColumn takes care of enforcing
> the constraint, so I removed this special case.

This part seems to me to be code beautification, not a bug-fix, and
shouldn't be back-patched (particularly in view of the fact that we
haven't tested the change all that much --- there might be other
places depending on the old behavior).

Will take care of this.

            regards, tom lane

Re: ADD COLUMN with PRIMARY KEY bug (was: I think this is a BUG?)

From
Tom Lane
Date:
"Brendan Jurd" <direvus@gmail.com> writes:
> The attached patch resolves the bug reported by Kaloyan Iliev [1] on -general.

Applied as two separate patches (bug fix and inessential cleanup).
The bug turns out to date back to the original addition of support for
ALTER ... ADD COLUMN ... DEFAULT/NOT NULL in 8.0.

            regards, tom lane