Re: Bug in ALTER COLUMN SET DATA TYPE ?

Lists: pgsql-hackers
From: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-02 11:52:07
Message-ID: CABOikdOgMONK1q8adowZQw3PVVv+m2cbgGRAkBLCbKYfLdXfJw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

I noticed this behavior on master and it seems like a bug to me:

postgres=# CREATE TABLE test (a float check (a > 10.2));
CREATE TABLE
postgres=# CREATE TABLE test_child() INHERITS(test);
CREATE TABLE
postgres=# ALTER TABLE test ALTER COLUMN a SET DATA TYPE numeric;

ERROR: constraint must be added to child
tables too

The error message seems unintended. Sure, we are changing the data type of
a column which has a constraint on it. The ALTER TABLE mechanism would drop
and recreate that constraint after changing the data type of the column.

The ATAddCheckConstraint() function checks if the "recurse" flag is passed
(basically check against ONLY clause). If the flag is not passed and the
table has children, it raises the above mentioned exception. This is right
for a normal ADD CONSTRAINT operation because we don't want to allow
constraint addition ONLY on the parent table unless there are no child
tables currently on the parent. But when constraint is being re-added as a
side-effect of another ALTER TABLE command, we shouldn't really be raising
an exception because ATPrepCmd() would have expanded to child tables and
there would appropriate commands in the work queue to recreate constraints
on all the child tables as well.

So I think we need to teach ATAddCheckConstraint() to not do this check if
its being called from AT_PASS_OLD_INDEX of ALTER TABLE.

I can work up a patch if we are in agreement that this indeed is a bug and
the approach that I mentioned for fixing this is also right. Comments ?

Thanks,
Pavan


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-02 14:41:14
Message-ID: 26992.1351867274@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com> writes:
> I noticed this behavior on master and it seems like a bug to me:

> postgres=# CREATE TABLE test (a float check (a > 10.2));
> CREATE TABLE
> postgres=# CREATE TABLE test_child() INHERITS(test);
> CREATE TABLE
> postgres=# ALTER TABLE test ALTER COLUMN a SET DATA TYPE numeric;
> ERROR: constraint must be added to child tables too

Interestingly, this works in 8.3 and earlier ... but it fails as
described in all later versions. So we broke it quite some time back.
It would be good to identify exactly what change broke it.

regards, tom lane


From: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-02 15:07:14
Message-ID: CABOikdMq4RFeHXSjEdaWEWcq+tU9Lxu07QYqaQTB3R7ntvRwag@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 2, 2012 at 8:11 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com> writes:
> > I noticed this behavior on master and it seems like a bug to me:
>
> > postgres=# CREATE TABLE test (a float check (a > 10.2));
> > CREATE TABLE
> > postgres=# CREATE TABLE test_child() INHERITS(test);
> > CREATE TABLE
> > postgres=# ALTER TABLE test ALTER COLUMN a SET DATA TYPE numeric;
> > ERROR: constraint must be added to child tables too
>
> Interestingly, this works in 8.3 and earlier ... but it fails as
> described in all later versions. So we broke it quite some time back.
> It would be good to identify exactly what change broke it.
>
>
Hmm.. I haven't yet tested on other branches. But I'm surprised that you
find it broken on so much old branches. "git annotate" suggests that the
offending error message was added by commit
09ff76fcdb275769ac4d1a45a67416735613d04b and that commit is dated Apr 20,
2012

Thanks,
Pavan


From: Nikhil Sontakke <nikkhils(at)gmail(dot)com>
To: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-02 15:16:23
Message-ID: CANgU5ZcQohB8ykOptq9ehLWonf6wVBy2xX7WyHPbYyPpN+bxEQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> > postgres=# CREATE TABLE test (a float check (a > 10.2));
>> > CREATE TABLE
>> > postgres=# CREATE TABLE test_child() INHERITS(test);
>> > CREATE TABLE
>> > postgres=# ALTER TABLE test ALTER COLUMN a SET DATA TYPE numeric;
>> > ERROR: constraint must be added to child tables too
>>
>> Interestingly, this works in 8.3 and earlier ... but it fails as
>> described in all later versions. So we broke it quite some time back.
>> It would be good to identify exactly what change broke it.
>>
>>
> Hmm.. I haven't yet tested on other branches. But I'm surprised that you
> find it broken on so much old branches. "git annotate" suggests that the
> offending error message was added by commit
> 09ff76fcdb275769ac4d1a45a67416735613d04b and that commit is dated Apr 20,
> 2012
>
>

Mea Culpa. I did indeed add that error message. But it's strange when Tom
says that this is broken since 8.3!

Regards,
Nikhils
--
StormDB - http://www.stormdb.com
The Database Cloud
Postgres-XC Support and Service


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Nikhil Sontakke <nikkhils(at)gmail(dot)com>
Cc: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-02 15:30:22
Message-ID: 28050.1351870222@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Nikhil Sontakke <nikkhils(at)gmail(dot)com> writes:
>> Hmm.. I haven't yet tested on other branches. But I'm surprised that you
>> find it broken on so much old branches. "git annotate" suggests that the
>> offending error message was added by commit
>> 09ff76fcdb275769ac4d1a45a67416735613d04b and that commit is dated Apr 20,
>> 2012

> Mea Culpa. I did indeed add that error message. But it's strange when Tom
> says that this is broken since 8.3!

In the 8.4 branch, the error is coming from here:

regression=# \set VERBOSITY verbose
regression=# ALTER TABLE test ALTER COLUMN a TYPE numeric;
ERROR: 42P16: constraint must be added to child tables too
LOCATION: ATAddCheckConstraint, tablecmds.c:4467

which appears to have been added in commit cd902b331. I think the
commit Pavan is looking at just moved the logic around.

regards, tom lane


From: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Nikhil Sontakke <nikkhils(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-02 15:39:00
Message-ID: CABOikdOZPfh932+0myqwS4LPvN0w+-JH6CRMAaJr=zZOxoevKQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 2, 2012 at 9:00 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

>
>
> In the 8.4 branch, the error is coming from here:
>
> regression=# \set VERBOSITY verbose
> regression=# ALTER TABLE test ALTER COLUMN a TYPE numeric;
> ERROR: 42P16: constraint must be added to child tables too
> LOCATION: ATAddCheckConstraint, tablecmds.c:4467
>
> which appears to have been added in commit cd902b331. I think the
> commit Pavan is looking at just moved the logic around.
>

I also though that for a moment, but the commit that I mentioned did not
move that error message from somewhere else. I also tested by resetting
to commit 1f0363001166ef6a43619846e44cfb9dbe7335ed (previous to the
offending commit) and don't see any error.

But since you're seeing it, may it was taken out by some other older commit
and then added back again. Will look more into it

Thanks,
Pavan


From: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Nikhil Sontakke <nikkhils(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-02 15:58:34
Message-ID: CABOikdPZMxoDPYfdiojPRHBtoYkokLC1q2r4AvzGETfFdCTPXw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 2, 2012 at 9:09 PM, Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>wrote:

>
>
> I also though that for a moment, but the commit that I mentioned did not
> move that error message from somewhere else. I also tested by resetting
> to commit 1f0363001166ef6a43619846e44cfb9dbe7335ed (previous to the
> offending commit) and don't see any error.
>
> But since you're seeing it, may it was taken out by some other older
> commit and then added back again. Will look more into it
>
>
Here is more trace:

The error message was first added by:
commit cd902b331dc4b0c170e800441a98f9213d98b46b
Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Date: Fri May 9 23:32:05 2008 +0000

It was then removed by:
commit 61d81bd28dbec65a6b144e0cd3d0bfe25913c3ac
Author: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Date: Mon Dec 5 15:10:18 2011 -0300

And then again added back by:
commit 09ff76fcdb275769ac4d1a45a67416735613d04b
Author: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Date: Fri Apr 20 23:46:20 2012 -0300

So coming back to the issue, do you think it's a good idea to teach
ATAddCheckConstraint() that the call is coming from a late phase of ALTER
TABLE ?

Thanks,
Pavan


From: Nikhil Sontakke <nikkhils(at)gmail(dot)com>
To: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-03 04:51:22
Message-ID: CANgU5Zf4ESYyC6g2BqeSpCx6q5J7-Q+t-Lc1abFcUUytEYJ-Nw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> So coming back to the issue, do you think it's a good idea to teach
> ATAddCheckConstraint() that the call is coming from a late phase of ALTER
> TABLE ?
>
> +1

You mentioned AT_PASS_OLD_INDEX in your original mail, but I guess that you
meant that we should check for AT_PASS_OLD_CONSTR and then not raise an
error?

Regards,
Nikhils
--
StormDB - http://www.stormdb.com
The Database Cloud
Postgres-XC Support and Service


From: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
To: Nikhil Sontakke <nikkhils(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-03 04:55:13
Message-ID: CABOikdOzm6s+wKqk_76aXGiWGRgM=4_t16=bbTWSw5JPG4jYvw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Nov 3, 2012 at 10:21 AM, Nikhil Sontakke <nikkhils(at)gmail(dot)com> wrote:

>
>
>> So coming back to the issue, do you think it's a good idea to teach
>> ATAddCheckConstraint() that the call is coming from a late phase of ALTER
>> TABLE ?
>>
>> +1
>
> You mentioned AT_PASS_OLD_INDEX in your original mail, but I guess that
> you meant that we should check for AT_PASS_OLD_CONSTR and then not raise
> an error?
>
>
Yeah, I meant AT_PASS_OLD_CONSTR.

Thanks,
Pavan


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
Cc: Nikhil Sontakke <nikkhils(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-04 23:11:03
Message-ID: 25112.1352070663@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com> writes:
> On Sat, Nov 3, 2012 at 10:21 AM, Nikhil Sontakke <nikkhils(at)gmail(dot)com> wrote:
>>>> So coming back to the issue, do you think it's a good idea to teach
>>>> ATAddCheckConstraint() that the call is coming from a late phase of ALTER
>>>> TABLE ?

>>> +1

>> You mentioned AT_PASS_OLD_INDEX in your original mail, but I guess that
>> you meant that we should check for AT_PASS_OLD_CONSTR and then not raise
>> an error?

> Yeah, I meant AT_PASS_OLD_CONSTR.

It's clear that we need to pass down the information that this action is
coming from re-creation of a check constraint, but I think the above
proposal for how to do it is pretty wrong-headed. The pass structure
has never meant anything for semantics of individual AlterTable actions;
it's only used to make sure those actions are done in an appropriate
order. Furthermore, the pass number isn't available where it would be
needed --- you'd have to pass it down through several levels of
subroutine that don't have another reason to care about it.

I'm inclined to think the cleanest solution is to add another value of
enum AlterTableType, perhaps "AT_ReAddConstraint", to signal that we're
executing a re-add; and then add another bool parameter to
ATExecAddConstraint to tell the latter not to complain if child tables
exist. This is more in line with pre-existing coding choices such as
the use of AT_AddConstraintRecurse.

regards, tom lane


From: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Nikhil Sontakke <nikkhils(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-05 09:12:42
Message-ID: CABOikdPzwimryLtnN52arpMAuLL7NBfSAY0XHpNzCcM3nWrdEQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Nov 5, 2012 at 4:41 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

>
>
> It's clear that we need to pass down the information that this action is
> coming from re-creation of a check constraint, but I think the above
> proposal for how to do it is pretty wrong-headed.

Yeah, I only meant that we need to teach ATExecAddConstraint that its being
called from the specific pass of ALTER TABLE and wanted to get agreement on
that. I hadn't thought about any particular implementation. So your
proposal below looks absolutely fine and clean.

>
> I'm inclined to think the cleanest solution is to add another value of
> enum AlterTableType, perhaps "AT_ReAddConstraint", to signal that we're
> executing a re-add; and then add another bool parameter to
> ATExecAddConstraint to tell the latter not to complain if child tables
> exist. This is more in line with pre-existing coding choices such as
> the use of AT_AddConstraintRecurse.
>

Please see attached patch which does what you suggested above. May be it
needs a little more commentary to record why we made this specific change.
Please let me know if you think so and want me to do that.

Thanks,
Pavan

Attachment Content-Type Size
alter-type-readd-constraint.patch application/octet-stream 5.4 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com>
Cc: Nikhil Sontakke <nikkhils(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in ALTER COLUMN SET DATA TYPE ?
Date: 2012-11-05 18:37:54
Message-ID: 21853.1352140674@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavan Deolasee <pavan(dot)deolasee(at)gmail(dot)com> writes:
> Please see attached patch which does what you suggested above. May be it
> needs a little more commentary to record why we made this specific change.
> Please let me know if you think so and want me to do that.

Applied with some cosmetic adjustments and addition of a regression
test.

regards, tom lane