Re: conditional dropping of columns/constraints

Lists: pgsql-hackers
From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: conditional dropping of columns/constraints
Date: 2009-05-04 14:10:49
Message-ID: 49FEF769.60504@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

Would a patch adding 'IF EXISTS' support to:
- ALTER TABLE ... DROP COLUMN
- ALTER TABLE ... DROP CONSTRAINT
possibly be accepted?

Having it makes the annoying task of writing/testing of schema-upgrade
scripts a bit easier.

Andres


From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-04 16:14:53
Message-ID: 49FF147D.3030703@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 05/04/2009 04:10 PM, Andres Freund wrote:
> Would a patch adding 'IF EXISTS' support to:
> - ALTER TABLE ... DROP COLUMN
> - ALTER TABLE ... DROP CONSTRAINT
> possibly be accepted?
>
> Having it makes the annoying task of writing/testing of schema-upgrade
> scripts a bit easier.
Oh, and to any other sensible place given there are more?

Andres


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-04 17:34:41
Message-ID: 603c8f070905041034i113aa3do686ae24222936720@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, May 4, 2009 at 10:10 AM, Andres Freund <andres(at)anarazel(dot)de> wrote:
> Would a patch adding 'IF EXISTS' support to:
> - ALTER TABLE ... DROP COLUMN
> - ALTER TABLE ... DROP CONSTRAINT
> possibly be accepted?
>
> Having it makes the annoying task of writing/testing of schema-upgrade
> scripts a bit easier.

Can't speak for the committers, but I've wished for this a time or two myself.

...Robert


From: Chris Browne <cbbrowne(at)acm(dot)org>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-04 19:21:10
Message-ID: 874ow0ekt5.fsf@dba2.int.libertyrms.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

robertmhaas(at)gmail(dot)com (Robert Haas) writes:
> On Mon, May 4, 2009 at 10:10 AM, Andres Freund <andres(at)anarazel(dot)de> wrote:
>> Would a patch adding 'IF EXISTS' support to:
>> - ALTER TABLE ... DROP COLUMN
>> - ALTER TABLE ... DROP CONSTRAINT
>> possibly be accepted?
>>
>> Having it makes the annoying task of writing/testing of schema-upgrade
>> scripts a bit easier.
>
> Can't speak for the committers, but I've wished for this a time or two myself.

For constraints, it's easy enough to treat that as idempotent; it's no
big deal to drop and re-add a constraint.

For columns, I'd *much* more frequently be interested in
ALTER TABLE ... ADD COLUMN IF NOT EXISTS ...

Note that this is distinctly NOT the same as:
ALTER TABLE ... DROP COLUMN IF EXISTS ...
ALTER TABLE ... ADD COLUMN ...
--
(format nil "~S(at)~S" "cbbrowne" "linuxdatabases.info")
http://linuxdatabases.info/info/lisp.html
Signs of a Klingon Programmer - 10. "A TRUE Klingon Warrior does not
comment his code!"


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Chris Browne <cbbrowne(at)acm(dot)org>
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-05 12:41:27
Message-ID: 200905051541.27889.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Monday 04 May 2009 22:21:10 Chris Browne wrote:
> For constraints, it's easy enough to treat that as idempotent; it's no
> big deal to drop and re-add a constraint.

Not if the constraint is a primary key, for example.


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Chris Browne <cbbrowne(at)acm(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-05 12:56:09
Message-ID: 4A003769.1050600@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Chris Browne wrote:
> For columns, I'd *much* more frequently be interested in
> ALTER TABLE ... ADD COLUMN IF NOT EXISTS ...
>
>
>

We have debated CREATE ... IF NOT EXISTS in the past, and there is no
consensus on what it should do, so we don't have it for any command.
That is quite a different case from what's being asked for, and the two
should not be conflated.

cheers

andrew


From: Andres Freund <andres(at)anarazel(dot)de>
To: Chris Browne <cbbrowne(at)acm(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-05 13:19:50
Message-ID: 4A003CF6.8030900@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi Chris,

On 05/04/2009 09:21 PM, Chris Browne wrote:
> robertmhaas(at)gmail(dot)com (Robert Haas) writes:
>> On Mon, May 4, 2009 at 10:10 AM, Andres Freund<andres(at)anarazel(dot)de> wrote:
>>> Would a patch adding 'IF EXISTS' support to:
>>> - ALTER TABLE ... DROP COLUMN
>>> - ALTER TABLE ... DROP CONSTRAINT
>>> possibly be accepted?
>> Can't speak for the committers, but I've wished for this a time or two myself.
> For constraints, it's easy enough to treat that as idempotent; it's no
> big deal to drop and re-add a constraint.
>
> For columns, I'd *much* more frequently be interested in
> ALTER TABLE ... ADD COLUMN IF NOT EXISTS ...
>
> Note that this is distinctly NOT the same as:
> ALTER TABLE ... DROP COLUMN IF EXISTS ...
> ALTER TABLE ... ADD COLUMN ...
Yes, I would like to have that myself - but this seems to open a way
much bigger can of worms.
Also the problem solved by both suggestions are not completely congruent
- so I thought better tackle the easier one first before starting a long
and arduous discussion...

Andres


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Chris Browne <cbbrowne(at)acm(dot)org>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-05 14:45:51
Message-ID: 603c8f070905050745k179a2b3ch5c4ec71f9d42b15c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, May 5, 2009 at 8:56 AM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
> We have debated CREATE ... IF NOT EXISTS in the past, and there is no
> consensus on what it should do, so we don't have it for any command. That is
> quite a different case from what's being asked for, and the two should not
> be conflated.

I must be missing something, because the semantics of CREATE ... IF
NOT EXISTS seem pretty well-defined to me, at least for any object
that has a name. Check whether that name is in use; if not, create
the object per the specified definition. Now for something like ALTER
TABLE ... ADD FOREIGN KEY I can see that there could be a problem.
That having been said, it's certain that CREATE IF NOT EXISTS is a
bigger foot-gun than DROP IF EXISTS, because after a succesful DROP IF
EXISTS the state of the object is known, whereas after CREATE IF NOT
EXISTS, it isn't (yes, it exists, but the definitions might not
match). Still, that seems no reason not to implement it. Right now,
I have a complex set of scripts which track the state of the database
to determine which DDL statements have already been applied.
Something like this would potentially simplify those scripts quite a
bit, so I'm much in favor.

On the other hand, I also agree with the point already made that these
are two different features, and we may as well focus on one at a time.

...Robert


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Chris Browne <cbbrowne(at)acm(dot)org>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-05 15:10:03
Message-ID: 17130.1241536203@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Tue, May 5, 2009 at 8:56 AM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>> We have debated CREATE ... IF NOT EXISTS in the past, and there is no
>> consensus on what it should do, so we don't have it for any command. That is
>> quite a different case from what's being asked for, and the two should not
>> be conflated.

> I must be missing something, because the semantics of CREATE ... IF
> NOT EXISTS seem pretty well-defined to me,

Please go read the prior threads (I think searching for "CINE" might
help, because we pretty shortly started abbreviating it like that).

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Chris Browne <cbbrowne(at)acm(dot)org>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-05 15:12:23
Message-ID: 4A005757.3000007@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas wrote:
> On Tue, May 5, 2009 at 8:56 AM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>
>> We have debated CREATE ... IF NOT EXISTS in the past, and there is no
>> consensus on what it should do, so we don't have it for any command. That is
>> quite a different case from what's being asked for, and the two should not
>> be conflated.
>>
>
> I must be missing something, because the semantics of CREATE ... IF
> NOT EXISTS seem pretty well-defined to me, at least for any object
> that has a name. Check whether that name is in use; if not, create
> the object per the specified definition.
>

And if it does exist but the definitions don't match? That's the issue
on which there has not been consensus. You apparently thing the command
should silently do nothing, but that's not what everyone thinks. (I have
no very strong feelings on the subject - I'm just explaining the issue.)

cheers

andrew


From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-06 16:32:17
Message-ID: 4A01BB91.3000401@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

On 05/04/2009 04:10 PM, Andres Freund wrote:
> Would a patch adding 'IF EXISTS' support to:
> - ALTER TABLE ... DROP COLUMN
> - ALTER TABLE ... DROP CONSTRAINT
> possibly be accepted?
A first version of a patch is attached:
- allows [ IF EXISTS ] for both, conditional dropping of columns and
constraints
- adds two tiny additions to the alter_table regression suite
- adds minimal documentation (my wording might be completely off)

As this is my first patch to PG I am happy with most sort of feedback.

Andres

Attachment Content-Type Size
drop-col-if-exists-v1.context.patch text/x-diff 19.2 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-06 21:43:00
Message-ID: 28529.1241646180@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Andres Freund <andres(at)anarazel(dot)de> writes:
> As this is my first patch to PG I am happy with most sort of feedback.

Please add your patch to the commit-fest queue here:
http://wiki.postgresql.org/wiki/CommitFestInProgress

Since we are still busy with 8.4 beta, it's unlikely that anyone will
take a close look until the next commit fest begins. FWIW, I took a
very fast look through the patch and thought it was at least touching
the right places, except I think you missed equalfuncs.c. (It'd be a
good idea to grep for all uses of AlterTableCmd struct to see if you
missed anything else.) I don't have time now to look closer or do any
testing.

regards, tom lane


From: Andres Freund <andres(at)anarazel(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-05-07 10:57:56
Message-ID: 4A02BEB4.7090600@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi Tom, hi all,

On 05/06/2009 11:43 PM, Tom Lane wrote:
> Andres Freund<andres(at)anarazel(dot)de> writes:
>> As this is my first patch to PG I am happy with most sort of feedback.
> Please add your patch to the commit-fest queue here:
> http://wiki.postgresql.org/wiki/CommitFestInProgress
Will do so, after this email has arrived.

> Since we are still busy with 8.4 beta, it's unlikely that anyone will
> take a close look until the next commit fest begins. FWIW, I took a
> very fast look through the patch and thought it was at least touching
> the right places, except I think you missed equalfuncs.c. (It'd be a
> good idea to grep for all uses of AlterTableCmd struct to see if you
> missed anything else.) I don't have time now to look closer or do any
> testing.
Ok, fixed that place (stupid me, I had seen that it is used there and
forgot about it) and found no other places.

Thanks,

Andres

Attachment Content-Type Size
0001-Feature-DROP-COLUMN-CONSTRAINT-IF-EXISTS.patch text/x-diff 0 bytes

From: Bernd Helmle <mailings(at)oopsware(dot)de>
To: Andres Freund <andres(at)anarazel(dot)de>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-07-09 21:59:20
Message-ID: 19D00A8C7947D873A1C26425@teje
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

--On Donnerstag, Mai 07, 2009 12:57:56 +0200 Andres Freund
<andres(at)anarazel(dot)de> wrote:

> Ok, fixed that place (stupid me, I had seen that it is used there and
> forgot about it) and found no other places.
>
> Thanks,
>
> Andres

Here is a slightly updated version of the patch. I did some (very minor)
editing on the wording in the docs and cleaned a merge conflict in
tablecmds.c. I saw no code issues so far, make check passes without errors.
Maybe someone with more bison skills can comment on the duplicated parser
stuff, but it seems consistent with the rest of the code.

--
Thanks

Bernd

Attachment Content-Type Size
drop_column_constraint_if_exists_reviewed.patch text/x-diff 19.6 KB

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Bernd Helmle <mailings(at)oopsware(dot)de>
Cc: Andres Freund <andres(at)anarazel(dot)de>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: conditional dropping of columns/constraints
Date: 2009-07-20 02:46:43
Message-ID: 4A63DA93.9080201@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bernd Helmle wrote:
>
> Here is a slightly updated version of the patch. I did some (very
> minor) editing on the wording in the docs and cleaned a merge conflict
> in tablecmds.c. I saw no code issues so far, make check passes without
> errors. Maybe someone with more bison skills can comment on the
> duplicated parser stuff, but it seems consistent with the rest of the
> code.

The duplicated stuff in the grammar is pretty much what had to be done
for other DROP IF EXISTS items to avoid shift/reduce conflicts, so it's
quite kosher.

patch committed.

Thanks.

andrew