drop if exists

Lists: pgsql-patches
From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>
Subject: drop if exists
Date: 2005-11-14 14:25:49
Message-ID: 43789E6D.9040009@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches


Ther attached patch is for comment. It implements "drop if exists" as
has recently been discussed. Illustration:

andrew=# drop table blurflx;
ERROR: table "blurflx" does not exist
andrew=# drop table if exists blurflx;
DROP TABLE
andrew=# create table blurflx ( x text);
CREATE TABLE
andrew=# drop table if exists blurflx;
DROP TABLE
andrew=# drop table blurflx;
ERROR: table "blurflx" does not exist
andrew=#

If the patch is acceptable I will work up some documentation and
regression tests.

cheers

andrew

Attachment Content-Type Size
drop-if-exists.patch text/x-patch 16.7 KB

From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>
Subject: Re: drop if exists
Date: 2005-11-14 14:53:45
Message-ID: 6B04F21E-BAD7-473C-96F7-3EE349B0515B@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches


On Nov 14, 2005, at 23:25 , Andrew Dunstan wrote:

>
> Ther attached patch is for comment. It implements "drop if exists"
> as has recently been discussed. Illustration:

Nifty! Thanks for working this up, Andrew!

> andrew=# drop table blurflx;
> ERROR: table "blurflx" does not exist
> andrew=# drop table if exists blurflx;
> DROP TABLE

I'm not sure what other DBMS' return in this situation (and kindly
ignore this suggestion if it's specified or otherwise determined),
but perhaps the output could be TABLE "blurlx" DOES NOT EXIST
(without the ERROR) or something more informative, rather than DROP
TABLE. It reminds me of the old behavior of outputting COMMIT even in
the case of transaction failure. I find the current behavior of
outputting ROLLBACK in the case of transaction failure more useful.

Michael Glaesemann
grzm myrealbox com


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>
Subject: Re: drop if exists
Date: 2005-11-14 15:21:50
Message-ID: 11272.1131981710@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
> andrew=# drop table blurflx;
> ERROR: table "blurflx" does not exist
> andrew=# drop table if exists blurflx;
> DROP TABLE

If I read MySQL's documentation correctly, they emit a NOTE (equivalent
of a NOTICE message I suppose) when IF EXISTS does nothing because the
table doesn't exist. Seems like we should do likewise --- your second
example here seems actively misleading. That is, I'd rather see

andrew=# drop table if exists blurflx;
NOTICE: table "blurflx" does not exist, skipping
DROP TABLE

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>
Subject: Re: drop if exists
Date: 2005-11-14 17:06:30
Message-ID: 4378C416.5060704@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches


OK, now it looks like this:

andrew=# drop table blurflx;
ERROR: table "blurflx" does not exist
andrew=# drop table if exists blurflx;
NOTICE: table "blurflx" does not exist, skipping
DROP TABLE
andrew=# create table blurflx ( x text);
CREATE TABLE
andrew=# drop table if exists blurflx;
DROP TABLE
andrew=# drop table blurflx;
ERROR: table "blurflx" does not exist
andrew=#

revised patch attached.

cheers

andrew

Tom Lane wrote:

>Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
>
>
>>andrew=# drop table blurflx;
>>ERROR: table "blurflx" does not exist
>>andrew=# drop table if exists blurflx;
>>DROP TABLE
>>
>>
>
>If I read MySQL's documentation correctly, they emit a NOTE (equivalent
>of a NOTICE message I suppose) when IF EXISTS does nothing because the
>table doesn't exist. Seems like we should do likewise --- your second
>example here seems actively misleading. That is, I'd rather see
>
>andrew=# drop table if exists blurflx;
>NOTICE: table "blurflx" does not exist, skipping
>DROP TABLE
>
>
> regards, tom lane
>
>
>

Attachment Content-Type Size
drop-if-exists.patch text/x-patch 20.3 KB

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>
Subject: Re: drop if exists
Date: 2005-11-16 16:38:14
Message-ID: 200511161638.jAGGcEr17235@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches


Removed from queue. Andrew is committing it.

---------------------------------------------------------------------------

Andrew Dunstan wrote:
>
> OK, now it looks like this:
>
> andrew=# drop table blurflx;
> ERROR: table "blurflx" does not exist
> andrew=# drop table if exists blurflx;
> NOTICE: table "blurflx" does not exist, skipping
> DROP TABLE
> andrew=# create table blurflx ( x text);
> CREATE TABLE
> andrew=# drop table if exists blurflx;
> DROP TABLE
> andrew=# drop table blurflx;
> ERROR: table "blurflx" does not exist
> andrew=#
>
> revised patch attached.
>
> cheers
>
> andrew
>
> Tom Lane wrote:
>
> >Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
> >
> >
> >>andrew=# drop table blurflx;
> >>ERROR: table "blurflx" does not exist
> >>andrew=# drop table if exists blurflx;
> >>DROP TABLE
> >>
> >>
> >
> >If I read MySQL's documentation correctly, they emit a NOTE (equivalent
> >of a NOTICE message I suppose) when IF EXISTS does nothing because the
> >table doesn't exist. Seems like we should do likewise --- your second
> >example here seems actively misleading. That is, I'd rather see
> >
> >andrew=# drop table if exists blurflx;
> >NOTICE: table "blurflx" does not exist, skipping
> >DROP TABLE
> >
> >
> > regards, tom lane
> >
> >
> >

>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>
Subject: Re: drop if exists
Date: 2005-11-17 01:24:31
Message-ID: 437BDBCF.9020609@familyhealth.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Will we get this functionality for ALL objects?

Bruce Momjian wrote:
> Removed from queue. Andrew is committing it.
>
> ---------------------------------------------------------------------------
>
> Andrew Dunstan wrote:
>
>>OK, now it looks like this:
>>
>>andrew=# drop table blurflx;
>>ERROR: table "blurflx" does not exist
>>andrew=# drop table if exists blurflx;
>>NOTICE: table "blurflx" does not exist, skipping
>>DROP TABLE
>>andrew=# create table blurflx ( x text);
>>CREATE TABLE
>>andrew=# drop table if exists blurflx;
>>DROP TABLE
>>andrew=# drop table blurflx;
>>ERROR: table "blurflx" does not exist
>>andrew=#
>>
>>revised patch attached.
>>
>>cheers
>>
>>andrew
>>
>>Tom Lane wrote:
>>
>>
>>>Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
>>>
>>>
>>>
>>>>andrew=# drop table blurflx;
>>>>ERROR: table "blurflx" does not exist
>>>>andrew=# drop table if exists blurflx;
>>>>DROP TABLE
>>>>
>>>>
>>>
>>>If I read MySQL's documentation correctly, they emit a NOTE (equivalent
>>>of a NOTICE message I suppose) when IF EXISTS does nothing because the
>>>table doesn't exist. Seems like we should do likewise --- your second
>>>example here seems actively misleading. That is, I'd rather see
>>>
>>>andrew=# drop table if exists blurflx;
>>>NOTICE: table "blurflx" does not exist, skipping
>>>DROP TABLE
>>>
>>>
>>> regards, tom lane
>>>
>>>
>>>
>
>
>
>>---------------------------(end of broadcast)---------------------------
>>TIP 4: Have you searched our list archives?
>>
>> http://archives.postgresql.org
>
>


From: "Andrew Dunstan" <andrew(at)dunslane(dot)net>
To: <chriskl(at)familyhealth(dot)com(dot)au>
Cc: <pgman(at)candle(dot)pha(dot)pa(dot)us>, <tgl(at)sss(dot)pgh(dot)pa(dot)us>, <pgsql-patches(at)postgresql(dot)org>
Subject: Re: drop if exists
Date: 2005-11-17 02:34:08
Message-ID: 4135.24.211.165.134.1132194848.squirrel@www.dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Christopher Kings-Lynne said:
> Will we get this functionality for ALL objects?
>

The patch does these: table, view, index, sequence, schema, type, domain,
and conversion. The reason is that these are all dealt with using the same
bit of the grammar, and the first 4 are pretty much completely done by the
same code.

I think anything else will have to be done individually, although the
pattern can be copied.

Perhaps we should take bids on what should/should not be covered.

cheers

andrew


From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgman(at)candle(dot)pha(dot)pa(dot)us, tgl(at)sss(dot)pgh(dot)pa(dot)us, pgsql-patches(at)postgresql(dot)org
Subject: Re: drop if exists
Date: 2005-11-17 02:45:32
Message-ID: CD626ACB-1509-4C00-95A0-82D389E2F57D@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches


On Nov 17, 2005, at 11:45 , Christopher Kings-Lynne wrote:

>> I think anything else will have to be done individually, although the
>> pattern can be copied.
>> Perhaps we should take bids on what should/should not be covered.
>
> Everything should be covered, otherwise it's just annoying for
> users...

Including objects that already have CREATE OR REPLACE?

Michael Glaesemann
grzm myrealbox com


From: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgman(at)candle(dot)pha(dot)pa(dot)us, tgl(at)sss(dot)pgh(dot)pa(dot)us, pgsql-patches(at)postgresql(dot)org
Subject: Re: drop if exists
Date: 2005-11-17 02:45:48
Message-ID: 437BEEDC.9090700@familyhealth.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

> I think anything else will have to be done individually, although the
> pattern can be copied.
>
> Perhaps we should take bids on what should/should not be covered.

Everything should be covered, otherwise it's just annoying for users...

Chris


From: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
To: Michael Glaesemann <grzm(at)myrealbox(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgman(at)candle(dot)pha(dot)pa(dot)us, tgl(at)sss(dot)pgh(dot)pa(dot)us, pgsql-patches(at)postgresql(dot)org
Subject: Re: drop if exists
Date: 2005-11-17 02:51:43
Message-ID: 437BF03F.3030501@familyhealth.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

> Including objects that already have CREATE OR REPLACE?

I assume so - CREATE OR REPLACE doesn't drop things - only creates or
replaces them.

Chris


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
Cc: pgman(at)candle(dot)pha(dot)pa(dot)us, tgl(at)sss(dot)pgh(dot)pa(dot)us, pgsql-patches(at)postgresql(dot)org
Subject: Re: drop if exists
Date: 2005-11-17 14:04:42
Message-ID: 437C8DFA.3080104@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Christopher Kings-Lynne wrote:

>> I think anything else will have to be done individually, although the
>> pattern can be copied.
>>
>> Perhaps we should take bids on what should/should not be covered.
>
>
> Everything should be covered, otherwise it's just annoying for users...

Well, that's arguably more than I originally signed up for ;-) See

http://archives.postgresql.org/pgsql-hackers/2005-10/msg00632.php

There are currently DROP commends for the following 21 objects
(according to the docs).

AGGREGATE
CAST
CONVERSION
DATABASE
DOMAIN
FUNCTION
GROUP
INDEX
LANGUAGE
OPERATOR
OPERATOR CLASS
ROLE
RULE
SCHEMA
SEQUENCE
TABLE
TABLESPACE
TRIGGER
TYPE
USER
VIEW

If the consensus is to add this to all of them, then I propose to apply
the patch I have (with a slight fix for an oversight in the case of
domains, plus docs and tests) for the 8 cases and start working on the
remaining 13 as time permits. To be honest, I have not even looked at
those 13 cases.

One motivation for this, besides general utility, is to ease MySQL
migrations, btw, and AFAICT they only have three DROP commands and only
two of them (TABLE and DATABASE) have IF EXISTS - DROP INDEX does not
for some reason - probably because it is actually mapped to an ALTER
TABLE statement.

cheers

andrew


From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgman(at)candle(dot)pha(dot)pa(dot)us, tgl(at)sss(dot)pgh(dot)pa(dot)us, pgsql-patches(at)postgresql(dot)org
Subject: Re: drop if exists
Date: 2005-11-18 00:22:02
Message-ID: 5AEA6AFD-EFA9-4809-994A-24AA15EB245D@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches


On Nov 17, 2005, at 11:51 , Christopher Kings-Lynne wrote:

>> Including objects that already have CREATE OR REPLACE?
>
> I assume so - CREATE OR REPLACE doesn't drop things - only creates
> or replaces them.

Of course. Silly me :)

Michael Glaesemann
grzm myrealbox com


From: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgman(at)candle(dot)pha(dot)pa(dot)us, tgl(at)sss(dot)pgh(dot)pa(dot)us, pgsql-patches(at)postgresql(dot)org
Subject: Re: drop if exists
Date: 2005-11-18 01:47:13
Message-ID: 437D32A1.2080709@familyhealth.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

> If the consensus is to add this to all of them, then I propose to apply
> the patch I have (with a slight fix for an oversight in the case of
> domains, plus docs and tests) for the 8 cases and start working on the
> remaining 13 as time permits. To be honest, I have not even looked at
> those 13 cases.

I agree. I can have a crack at the others as well. It's in my area of
ability I hope ;) (Except grammar janking)

Chris