Re: CREATE SCHEMA IF NOT EXISTS

Lists: pgsql-hackers
From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-15 17:50:00
Message-ID: F849F722-BF5D-40B7-8333-E94D6DC5C52C@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hackers,

Is there any reason not to add $subject? Would it be difficult? Would save me having to write a DO statement every time I needed it (which in migration scripts is fairly often).

Thanks,

David


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-15 18:31:27
Message-ID: CAFcNs+pxpBraYkxAd6nsoN1GgLobXKrEK=D=_D53bHfX-Q3qAg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/15 David E. Wheeler <david(at)justatheory(dot)com>

> Hackers,
>
> Is there any reason not to add $subject? Would it be difficult?

Looking to the source code I think this feature isn't hard to implement...
I'm writing a little path to do that and I'll send soon...

> Would save me having to write a DO statement every time I needed it (which
> in migration scripts is fairly often).
>
>
I understand your difficulty.

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: fabriziomello(at)gmail(dot)com
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-15 20:10:43
Message-ID: 8EA726F8-9C89-49C9-AC60-204933DD7677@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Aug 15, 2012, at 11:31 AM, Fabrízio de Royes Mello wrote:

>> Is there any reason not to add $subject? Would it be difficult?
>
> Looking to the source code I think this feature isn't hard to implement... I'm writing a little path to do that and I'll send soon...

Cool, thanks!

David


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-16 17:36:32
Message-ID: CAFcNs+p9cFGX9YFir-Cp3jOc8HvfWxMRcLr7uyc_kia5b1NhZw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/15 David E. Wheeler <david(at)justatheory(dot)com>

> On Aug 15, 2012, at 11:31 AM, Fabrízio de Royes Mello wrote:
>
> >> Is there any reason not to add $subject? Would it be difficult?
> >
> > Looking to the source code I think this feature isn't hard to
> implement... I'm writing a little path to do that and I'll send soon...
>
> Cool, thanks!
>
>
The attached patch implement this feature:

CREATE SCHEMA [ IF NOT EXISTS ] schema_name [ AUTHORIZATION user_name ] [
schema_element [ ... ] ]
CREATE SCHEMA [ IF NOT EXISTS ] AUTHORIZATION user_name [ schema_element [
... ] ]

Now, PostgreSQL don't trow an error if we use "IF NOT EXISTS" in "CREATE
SCHEMA" statement.

So, I don't know the next steps...

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

Attachment Content-Type Size
create_schema_if_not_exists.patch application/octet-stream 4.5 KB

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: fabriziomello(at)gmail(dot)com
Cc: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-16 17:43:09
Message-ID: 502D312D.30804@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 08/16/2012 01:36 PM, Fabrízio de Royes Mello wrote:
>
>
> 2012/8/15 David E. Wheeler <david(at)justatheory(dot)com
> <mailto:david(at)justatheory(dot)com>>
>
> On Aug 15, 2012, at 11:31 AM, Fabrízio de Royes Mello wrote:
>
> >> Is there any reason not to add $subject? Would it be difficult?
> >
> > Looking to the source code I think this feature isn't hard to
> implement... I'm writing a little path to do that and I'll send
> soon...
>
> Cool, thanks!
>
>
> The attached patch implement this feature:
>
> CREATE SCHEMA [ IF NOT EXISTS ] schema_name [ AUTHORIZATION user_name
> ] [ schema_element [ ... ] ]
> CREATE SCHEMA [ IF NOT EXISTS ] AUTHORIZATION user_name [
> schema_element [ ... ] ]
>
> Now, PostgreSQL don't trow an error if we use "IF NOT EXISTS" in
> "CREATE SCHEMA" statement.
>
> So, I don't know the next steps...

Please see
<http://wiki.postgresql.org/wiki/Developer_FAQ#I_have_developed_a_patch.2C_what_next.3F>

cheers

andrew


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: fabriziomello(at)gmail(dot)com
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-16 17:49:43
Message-ID: 32D10431-F1A9-4F98-A23F-1D2CC15EFABB@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Aug 16, 2012, at 10:36 AM, Fabrízio de Royes Mello wrote:

> The attached patch implement this feature:
>
> CREATE SCHEMA [ IF NOT EXISTS ] schema_name [ AUTHORIZATION user_name ] [ schema_element [ ... ] ]
> CREATE SCHEMA [ IF NOT EXISTS ] AUTHORIZATION user_name [ schema_element [ ... ] ]
>
> Now, PostgreSQL don't trow an error if we use "IF NOT EXISTS" in "CREATE SCHEMA" statement.
>
> So, I don't know the next steps...

Awesome, thanks! Please add it to the next CommitFest:

https://commitfest.postgresql.org/action/commitfest_view?id=15

Best,

David


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-16 17:55:03
Message-ID: CAFcNs+qzGu5rxh4nw6Qyy4v3ZQ7BUA9VcBGf-UVKY=x2w1+dbw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/16 David E. Wheeler <david(at)justatheory(dot)com>

> On Aug 16, 2012, at 10:36 AM, Fabrízio de Royes Mello wrote:
>
> > The attached patch implement this feature:
> >
> > CREATE SCHEMA [ IF NOT EXISTS ] schema_name [ AUTHORIZATION user_name ]
> [ schema_element [ ... ] ]
> > CREATE SCHEMA [ IF NOT EXISTS ] AUTHORIZATION user_name [ schema_element
> [ ... ] ]
> >
> > Now, PostgreSQL don't trow an error if we use "IF NOT EXISTS" in "CREATE
> SCHEMA" statement.
> >
> > So, I don't know the next steps...
>
> Awesome, thanks! Please add it to the next CommitFest:
>
> https://commitfest.postgresql.org/action/commitfest_view?id=15
>
>
Patch added to CommitFest:

https://commitfest.postgresql.org/action/patch_view?id=907

Thanks,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello


From: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
To: fabriziomello(at)gmail(dot)com
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-17 00:44:22
Message-ID: CAHHcreo7jTvFdiHkLXTuHjF9MiQT5DjtXcvMWqVZ-yz11wyBog@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/16 Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>:
> The attached patch implement this feature:
>
> CREATE SCHEMA [ IF NOT EXISTS ] schema_name [ AUTHORIZATION user_name ] [
> schema_element [ ... ] ]
> CREATE SCHEMA [ IF NOT EXISTS ] AUTHORIZATION user_name [ schema_element [
> ... ] ]
>
> Now, PostgreSQL don't trow an error if we use "IF NOT EXISTS" in "CREATE
> SCHEMA" statement.

I started testing this, but I didn't see regression tests for it.
Could you write them?.

Best.
--
Dickson S. Guedes
mail/xmpp: guedes(at)guedesoft(dot)net - skype: guediz
http://guedesoft.net - http://www.postgresql.org.br


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-17 13:16:30
Message-ID: CAFcNs+qW7DeNV7+PcBD9HzZnm5TZsNhTVXhbRR9xurP-kFG6pA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/16 Dickson S. Guedes <listas(at)guedesoft(dot)net>

> 2012/8/16 Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>:
> > The attached patch implement this feature:
> >
> > CREATE SCHEMA [ IF NOT EXISTS ] schema_name [ AUTHORIZATION user_name ] [
> > schema_element [ ... ] ]
> > CREATE SCHEMA [ IF NOT EXISTS ] AUTHORIZATION user_name [ schema_element
> [
> > ... ] ]
> >
> > Now, PostgreSQL don't trow an error if we use "IF NOT EXISTS" in "CREATE
> > SCHEMA" statement.
>
> I started testing this, but I didn't see regression tests for it.
> Could you write them?.
>
>
The attached patch contains regression tests for it.

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

Attachment Content-Type Size
create_schema_if_not_exists_v2.patch application/octet-stream 5.7 KB

From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-17 13:42:26
Message-ID: CAFcNs+rbqWHVXhPhB8bNi3jBkMAKoAL6U0Fa28ivvgp-N6X+bw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/17 Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>

>
> I started testing this, but I didn't see regression tests for it.
>> Could you write them?.
>>
>>
> The attached patch contains regression tests for it.
>
>
Please, don't consider de last patch (v2) because I make a little mistake
on "create_schema.sgml" structure. The attached patch fix that.

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

Attachment Content-Type Size
create_schema_if_not_exists_v3.patch application/octet-stream 5.7 KB

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: fabriziomello <fabriziomello(at)gmail(dot)com>
Cc: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-17 13:46:17
Message-ID: 1345211124-sup-9139@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Fabrízio de Royes Mello's message of vie ago 17 09:16:30 -0400 2012:

> The attached patch contains regression tests for it.

I think you're missing support in copyfuncs.c and equalfuncs.c for the
new field in the node.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-17 13:59:58
Message-ID: CAFcNs+paWTdTFe6T6Fnme52Eyr6BdgOgGfnJaumPKzo1VgSMRQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/17 Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>

> Excerpts from Fabrízio de Royes Mello's message of vie ago 17 09:16:30
> -0400 2012:
>
> > The attached patch contains regression tests for it.
>
> I think you're missing support in copyfuncs.c and equalfuncs.c for the
> new field in the node.
>
>
You're completely right, thanks...

The attached patch add support for the new field in the node in
"copyfuncs.c" and "equalfuncs.c".

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

Attachment Content-Type Size
create_schema_if_not_exists_v4.patch application/octet-stream 6.5 KB

From: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
To: fabriziomello(at)gmail(dot)com
Cc: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-17 14:37:25
Message-ID: CAHHcreoqKcUqR3oTycNMcv-4beVRGRgbXAZqzqt6ndCaBE6QyA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/17 Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>:
>
> 2012/8/17 Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
>>
>> Excerpts from Fabrízio de Royes Mello's message of vie ago 17 09:16:30
>> -0400 2012:
>>
>> > The attached patch contains regression tests for it.
>>
>> I think you're missing support in copyfuncs.c and equalfuncs.c for the
>> new field in the node.
>
> You're completely right, thanks...
>
> The attached patch add support for the new field in the node in
> "copyfuncs.c" and "equalfuncs.c".

Maybe I'm missing something but shouldn't it being handled in extension.c too?

[]s
--
Dickson S. Guedes
mail/xmpp: guedes(at)guedesoft(dot)net - skype: guediz
http://guedesoft.net - http://www.postgresql.org.br


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-17 21:22:16
Message-ID: CAFcNs+qmQGnvV4e6R3UXyrN9Y4jcnFmno3yJLJ2XBEMgmk4j8w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/17 Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>

> Excerpts from Dickson S. Guedes's message of vie ago 17 10:37:25 -0400
> 2012:
> > 2012/8/17 Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>:
> > >
> > > 2012/8/17 Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
> > >>
> > >> Excerpts from Fabrízio de Royes Mello's message of vie ago 17 09:16:30
> > >> -0400 2012:
> > >>
> > >> > The attached patch contains regression tests for it.
> > >>
> > >> I think you're missing support in copyfuncs.c and equalfuncs.c for the
> > >> new field in the node.
> > >
> > > You're completely right, thanks...
> > >
> > > The attached patch add support for the new field in the node in
> > > "copyfuncs.c" and "equalfuncs.c".
> >
> > Maybe I'm missing something but shouldn't it being handled in
> extension.c too?
>
> Please be more explicit? I don't know what you mean.
>
>
Returning conversation to the list.

I think he talked about this piece of code:

diff --git a/src/backend/commands/extension.c
b/src/backend/commands/extension.c
index 8512cdb..e359a9c 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -1374,6 +1374,7 @@ CreateExtension(CreateExtensionStmt *stmt)
csstmt->schemaname = schemaName;
csstmt->authid = NULL; /* will be created
by current user */
csstmt->schemaElts = NIL;
+ csstmt->if_not_exists = false;
CreateSchemaCommand(csstmt, NULL);

/*

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

Attachment Content-Type Size
create_schema_if_not_exists_v5.patch application/octet-stream 7.0 KB

From: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
To: fabriziomello(at)gmail(dot)com
Cc: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>, david(at)justatheory(dot)com
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-28 02:01:19
Message-ID: CAHHcreqw-r+=xTh2+T6XpxiO-SAvZgq7aWSQJ99BPkJt6Vx59Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello,

I reviewed this v5 of patch:

- https://commitfest.postgresql.org/action/patch_view?id=907

The patch is small and implements a new syntax to CREATE SCHEMA
that allow the creation of a schema be skipped when IF NOT EXISTS is
used.

It was applied to 483c2c1071c45e275782d33d646c3018f02f9f94 with
two hunks offset, was compiled without errors or new warnings and
pass all tests, even the tests that covers the expected results for it
self.

The docs was updated with the information about the expected behavior.

I tested against ambiguity, i. e. creating a schema with name 'if' and got the
expected results when try to create it if not exists.

Two questions:

- Should this patch implements others INEs like ADD COLUMN IF NOT EXISTS?
- Should pg_dump or pg_restore support some kind of flag to use a
CREATE SCHEMA IF NOT EXISTS ... instead CREATE SCHEMA ...?

Regards,
--
Dickson S. Guedes
mail/xmpp: guedes(at)guedesoft(dot)net -
skype/twitter: guediz ~ github.com/guedes
http://guedesoft.net ~ http://www.postgresql.org.br

Attachment Content-Type Size
create_schema_if_not_exists_v5.patch application/octet-stream 7.0 KB

From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
Cc: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>, david(at)justatheory(dot)com
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-28 15:19:40
Message-ID: CAFcNs+rQT5gLg6z=j3yStquPUh7GdtpVQUGrJoEmig4Ez+FDFQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/27 Dickson S. Guedes <listas(at)guedesoft(dot)net>

>
> [...]
>
> Two questions:
>
> - Should this patch implements others INEs like ADD COLUMN IF NOT EXISTS?
>

If this feature is important I believe we must implement it.

Exists several "CREATE" statements without "IF NOT EXISTS" option too, so
we can discuss more about it and I can implement it in this patch or in
another.

> - Should pg_dump or pg_restore support some kind of flag to use a
> CREATE SCHEMA IF NOT EXISTS ... instead CREATE SCHEMA ...?
>
>
I can't imagine a use case for this feature... can you explain more about
your idea?

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: fabriziomello(at)gmail(dot)com
Cc: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-28 16:08:13
Message-ID: E9AA9A20-A0AD-4B87-9C9B-7B1D2A81E17E@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Aug 28, 2012, at 8:19 AM, Fabrízio de Royes Mello wrote:

>> - Should this patch implements others INEs like ADD COLUMN IF NOT EXISTS?
>
> If this feature is important I believe we must implement it.
>
> Exists several "CREATE" statements without "IF NOT EXISTS" option too, so we can discuss more about it and I can implement it in this patch or in another.

I don't think any of them have to be a part of *this* patch. There can be patches for each of them. Probably simpler that way.

But then, I am not like to code them, so WTF do I know? :-)

David


From: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: fabriziomello(at)gmail(dot)com, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-08-28 16:29:08
Message-ID: CAHHcreq30hQ3KB8zv+dNnAE3cL2HYuAQOQYxFLJNL3DVh+DtoQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/8/28 David E. Wheeler <david(at)justatheory(dot)com>:
> On Aug 28, 2012, at 8:19 AM, Fabrízio de Royes Mello wrote:
>
>>> - Should this patch implements others INEs like ADD COLUMN IF NOT EXISTS?
>>
>> If this feature is important I believe we must implement it.
>>
>> Exists several "CREATE" statements without "IF NOT EXISTS" option too, so we can discuss more about it and I can implement it in this patch or in another.
>
> I don't think any of them have to be a part of *this* patch. There can be patches for each of them. Probably simpler that way.

Yes, agreed. Other implementations should be in their own distinct
patches. BTW, it could be interesting search the archive for past
discussions about CINE.

I changed the status of this patch to "Ready for Committer".

Regards,
--
Dickson S. Guedes
mail/xmpp: guedes(at)guedesoft(dot)net -
skype/twitter: guediz ~ github.com/guedes
http://guedesoft.net ~ http://www.postgresql.org.br


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
Cc: fabriziomello(at)gmail(dot)com, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>, david(at)justatheory(dot)com
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-09-21 17:59:36
Message-ID: 27616.1348250376@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Dickson S. Guedes" <listas(at)guedesoft(dot)net> writes:
> I reviewed this v5 of patch:

> - https://commitfest.postgresql.org/action/patch_view?id=907

> The patch is small and implements a new syntax to CREATE SCHEMA
> that allow the creation of a schema be skipped when IF NOT EXISTS is
> used.

I don't believe this has been thought through nearly carefully enough.
If CREATE SCHEMA created a schema and nothing more, then the proposed
implementation would probably be fine. But per spec, CREATE SCHEMA
can specify not only creating the schema but a whole bunch of objects
within the schema. As coded, if the schema exists then creation of
the specified sub-objects is just skipped, regardless of whether they
exist or not. I doubt that this is really sane behavior. Would the
principle of least astonishment dictate that the IF NOT EXISTS option
apply implicitly to each sub-object as well? (If so, we'd have to
extend everything that can appear in OptSchemaEltList; most of those
commands don't have IF NOT EXISTS options today.)

This is somewhat connected to our previous arguments about whether CINE
ought to try to make any guarantees about whether the existing object is
at all like the object that would have been created if the statement
executed successfully. I realize that the existing statements that have
an INE option have entirely blown off this concern, but I still think
that it's a question that ought to be considered, particularly for
objects that have separately creatable sub-structure.

A possible compromise is to allow the IF NOT EXISTS option only without
a schema-element list, which I suspect is the only use-case David had in
mind to start with anyway.

The existing patch added the check in a pretty randomly chosen spot too,
with one bad consequence being that if the schema already exists then it
will fall out with the wrong user ID in effect, creating a security bug.
But I'm not entirely sure where to put the check instead. Should we put
it before or after the permissions checks --- that is, should IF NOT
EXISTS require that you would have had permission to create the schema?
Or, if the schema does exist, should we just call it good anyway? I'm
too lazy to look at how other INE options resolved this question, but it
seems like we ought to be consistent.

Also, the AUTHORIZATION clause of CREATE SCHEMA creates an aspect of all
this that doesn't exist for any other kind of CREATE command, namely
that the object might have been requested to be created under some other
user id. For instance, supposing that we were to go forward with trying
to create sub-objects, but the ownership of the existing schema is
different from what's implied or specified by CREATE SCHEMA, should the
sub-objects be (attempted to be) created as owned by that user instead?
Perhaps not, but I'm not at all sure.

regards, tom lane


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, fabriziomello(at)gmail(dot)com, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-09-21 18:06:25
Message-ID: FCC0A167-8637-48B4-9C6B-A4D2A1261B2A@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sep 21, 2012, at 10:59 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> I don't believe this has been thought through nearly carefully enough.
> If CREATE SCHEMA created a schema and nothing more, then the proposed
> implementation would probably be fine. But per spec, CREATE SCHEMA
> can specify not only creating the schema but a whole bunch of objects
> within the schema. As coded, if the schema exists then creation of
> the specified sub-objects is just skipped, regardless of whether they
> exist or not. I doubt that this is really sane behavior. Would the
> principle of least astonishment dictate that the IF NOT EXISTS option
> apply implicitly to each sub-object as well? (If so, we'd have to
> extend everything that can appear in OptSchemaEltList; most of those
> commands don't have IF NOT EXISTS options today.)

I had no idea about that functionality. Seems very strange.

> A possible compromise is to allow the IF NOT EXISTS option only without
> a schema-element list, which I suspect is the only use-case David had in
> mind to start with anyway.

Yes, true.

> The existing patch added the check in a pretty randomly chosen spot too,
> with one bad consequence being that if the schema already exists then it
> will fall out with the wrong user ID in effect, creating a security bug.
> But I'm not entirely sure where to put the check instead. Should we put
> it before or after the permissions checks --- that is, should IF NOT
> EXISTS require that you would have had permission to create the schema?
> Or, if the schema does exist, should we just call it good anyway? I'm
> too lazy to look at how other INE options resolved this question, but it
> seems like we ought to be consistent.

Agreed. But if it already exists, where does it currently die? ISTM that would be the point to check, if possible.

> Also, the AUTHORIZATION clause of CREATE SCHEMA creates an aspect of all
> this that doesn't exist for any other kind of CREATE command, namely
> that the object might have been requested to be created under some other
> user id. For instance, supposing that we were to go forward with trying
> to create sub-objects, but the ownership of the existing schema is
> different from what's implied or specified by CREATE SCHEMA, should the
> sub-objects be (attempted to be) created as owned by that user instead?
> Perhaps not, but I'm not at all sure.

I tend to think that if the schema exists, there should be no attempt to create the sub-objects. Seems the least astonishing to me.

Best,

David


From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, fabriziomello(at)gmail(dot)com, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-09-21 23:59:07
Message-ID: CAB7nPqRoK_bsoV1Y3tgWQa2LmuKRnJtmWDXj_vT7xrbN0VzMMg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Sep 22, 2012 at 3:06 AM, David E. Wheeler <david(at)justatheory(dot)com>wrote:

> On Sep 21, 2012, at 10:59 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> > I don't believe this has been thought through nearly carefully enough.
> > If CREATE SCHEMA created a schema and nothing more, then the proposed
> > implementation would probably be fine. But per spec, CREATE SCHEMA
> > can specify not only creating the schema but a whole bunch of objects
> > within the schema. As coded, if the schema exists then creation of
> > the specified sub-objects is just skipped, regardless of whether they
> > exist or not. I doubt that this is really sane behavior. Would the
> > principle of least astonishment dictate that the IF NOT EXISTS option
> > apply implicitly to each sub-object as well? (If so, we'd have to
> > extend everything that can appear in OptSchemaEltList; most of those
> > commands don't have IF NOT EXISTS options today.)
>
> I had no idea about that functionality. Seems very strange.
>
Isn't it this SQL?
CREATE SCHEMA foo CREATE TABLE aa (a int) CREATE TABLE bb (b int);
--
Michael Paquier
http://michael.otacoo.com


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-09-22 05:13:10
Message-ID: CAFcNs+rF=2=syvcx7FEOrsvDe8PnJA-bgvmZFY+p7Xv=Kiia1Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/9/21 David E. Wheeler <david(at)justatheory(dot)com>

> On Sep 21, 2012, at 10:59 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> > I don't believe this has been thought through nearly carefully enough.
> > If CREATE SCHEMA created a schema and nothing more, then the proposed
> > implementation would probably be fine. But per spec, CREATE SCHEMA
> > can specify not only creating the schema but a whole bunch of objects
> > within the schema. As coded, if the schema exists then creation of
> > the specified sub-objects is just skipped, regardless of whether they
> > exist or not. I doubt that this is really sane behavior. Would the
> > principle of least astonishment dictate that the IF NOT EXISTS option
> > apply implicitly to each sub-object as well? (If so, we'd have to
> > extend everything that can appear in OptSchemaEltList; most of those
> > commands don't have IF NOT EXISTS options today.)
>
> I had no idea about that functionality. Seems very strange.
>
>
I completely forgot this functionality. The example above is from our docs
[1]:

CREATE SCHEMA hollywood
CREATE TABLE films (title text, release date, awards text[])
CREATE VIEW winners AS
SELECT title, release FROM films WHERE awards IS NOT NULL;

The "CREATE SCHEMA" statement accepts another "CREATE" commands (CREATE
{TABLE | VIEW | INDEX | SEQUENCE | TRIGGER}), and the current patch do not
consider this options.

> A possible compromise is to allow the IF NOT EXISTS option only without
> > a schema-element list, which I suspect is the only use-case David had in
> > mind to start with anyway.
>
> Yes, true.
>
>
Ok.

> > The existing patch added the check in a pretty randomly chosen spot too,
> > with one bad consequence being that if the schema already exists then it
> > will fall out with the wrong user ID in effect, creating a security bug.
> > But I'm not entirely sure where to put the check instead. Should we put
> > it before or after the permissions checks --- that is, should IF NOT
> > EXISTS require that you would have had permission to create the schema?
> > Or, if the schema does exist, should we just call it good anyway? I'm
> > too lazy to look at how other INE options resolved this question, but it
> > seems like we ought to be consistent.
>
> Agreed. But if it already exists, where does it currently die? ISTM that
> would be the point to check, if possible.
>
>
I change the patch (attached) to skip only the schema creation and execute
others statements...

> > Also, the AUTHORIZATION clause of CREATE SCHEMA creates an aspect of all
> > this that doesn't exist for any other kind of CREATE command, namely
> > that the object might have been requested to be created under some other
> > user id. For instance, supposing that we were to go forward with trying
> > to create sub-objects, but the ownership of the existing schema is
> > different from what's implied or specified by CREATE SCHEMA, should the
> > sub-objects be (attempted to be) created as owned by that user instead?
> > Perhaps not, but I'm not at all sure.
>
> I tend to think that if the schema exists, there should be no attempt to
> create the sub-objects. Seems the least astonishing to me.
>
>
Why don't create sub-objects? I think the INE clause must affect only
"CREATE SCHEMA" statement, the others must be executed normally. We can
discuss more about it...

[1] http://www.postgresql.org/docs/9.2/interactive/sql-createschema.html

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

Attachment Content-Type Size
create_schema_if_not_exists_v6.patch application/octet-stream 9.6 KB

From: Volker Grabsch <vog(at)notjusthosting(dot)com>
To: "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>
Cc: fabriziomello(at)gmail(dot)com, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>, david(at)justatheory(dot)com
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-09-29 09:32:13
Message-ID: 20120929093213.GE16144@vlap
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Dickson S. Guedes schrieb:
> - https://commitfest.postgresql.org/action/patch_view?id=907
>
> The patch is small and implements a new syntax to CREATE SCHEMA
> that allow the creation of a schema be skipped when IF NOT EXISTS is
> used.
>
> [...]
>
> - Should this patch implements others INEs like ADD COLUMN IF NOT EXISTS?

If there's still a chance to improve the patch, I'd love to see
the following INEs implemented. Several real-world database
upgrade scripts would benefit from those:

1)
CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ] name ...

2)
ALTER TABLE ... ADD [ COLUMN ] [ IF NOT EXISTS ] column_name ...

3)
CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ IF NOT EXISTS ] [ name ] ON ...

Regards,
Volker

--
Volker Grabsch
---<<(())>>---


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>
Cc: fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>, david <david(at)justatheory(dot)com>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 19:08:16
Message-ID: 1349204729-sup-3710@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


The fundamental issue with this patch hasn't been answered sufficiently,
I think. Consider the following sequence of commands:

create schema if not exists foo create table first (a int);
create schema if not exists foo create table second (a int);

As far as I can see, with the patch as it currently stands, you would
end up with only table "first" in the schema, which seems very
surprising to me.

I think this needs more thought, and in any case it needs more
comprehensive regression test and documentation (i.e. at least the
examples ought to explain what would happen in such cases).

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Volker Grabsch <vog(at)notjusthosting(dot)com>
Cc: Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>, david <david(at)justatheory(dot)com>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 19:14:29
Message-ID: 1349204899-sup-5876@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Volker Grabsch's message of sáb sep 29 06:32:13 -0300 2012:
> Dickson S. Guedes schrieb:
> > - https://commitfest.postgresql.org/action/patch_view?id=907
> >
> > The patch is small and implements a new syntax to CREATE SCHEMA
> > that allow the creation of a schema be skipped when IF NOT EXISTS is
> > used.
> >
> > [...]
> >
> > - Should this patch implements others INEs like ADD COLUMN IF NOT EXISTS?
>
> If there's still a chance to improve the patch, I'd love to see
> the following INEs implemented. Several real-world database
> upgrade scripts would benefit from those:

I don't see that this patch is responsible for such new commands. If
you want them, feel free to submit separate patches for them (or have
someone else do it for you). But see the thread starting at
http://archives.postgresql.org/message-id/467881.79137.qm%40web27104.mail.ukl.yahoo.com

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 19:16:37
Message-ID: 6C98E3D5-F93F-4BED-B8CF-BBE2072AB2D3@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 2, 2012, at 12:08 PM, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> wrote:

> create schema if not exists foo create table first (a int);
> create schema if not exists foo create table second (a int);
>
>
> As far as I can see, with the patch as it currently stands, you would
> end up with only table "first" in the schema, which seems very
> surprising to me.

Yeah, I think the second should die. CINE should only work if there are no other objects created as part of the statement, IMHO.

Best,

David


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: David E(dot) Wheeler <david(at)justatheory(dot)com>
Cc: Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 19:30:05
Message-ID: 1349205863-sup-2245@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from David E. Wheeler's message of mar oct 02 16:16:37 -0300 2012:
> On Oct 2, 2012, at 12:08 PM, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> wrote:
>
> > create schema if not exists foo create table first (a int);
> > create schema if not exists foo create table second (a int);
> >
> >
> > As far as I can see, with the patch as it currently stands, you would
> > end up with only table "first" in the schema, which seems very
> > surprising to me.
>
> Yeah, I think the second should die. CINE should only work if there are no other objects created as part of the statement, IMHO.

Well, if that's the rationale then you end up with no schema foo at all
(i.e. both die), which seems even more surprising (though I admit it has
the advantage of being a simple rule to document.)

How about call this for precedent:

mkdir -p /tmp/foo/bar
mkdir -p /tmp/foo/baz

In this case you end up with directory "foo" and at least two subdirs in
it, bar and baz. This works even if /tmp/foo existed previously and
even if there was some other stuff in it.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 19:37:30
Message-ID: 59BF1E0D-ECB6-45DA-8020-EBB9CF605BF3@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 2, 2012, at 12:30 PM, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> wrote:

> How about call this for precedent:
>
> mkdir -p /tmp/foo/bar
> mkdir -p /tmp/foo/baz
>
> In this case you end up with directory "foo" and at least two subdirs in
> it, bar and baz. This works even if /tmp/foo existed previously and
> even if there was some other stuff in it.

Well, what about this, then?

create schema if not exists foo create table second (a int);
create schema if not exists foo create table second (b int);

David


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 19:48:33
Message-ID: 25156.1349207313@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> writes:
> Excerpts from David E. Wheeler's message of mar oct 02 16:16:37 -0300 2012:
>> On Oct 2, 2012, at 12:08 PM, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> wrote:
>>> create schema if not exists foo create table first (a int);
>>> create schema if not exists foo create table second (a int);
>>>
>>> As far as I can see, with the patch as it currently stands, you would
>>> end up with only table "first" in the schema, which seems very
>>> surprising to me.

>> Yeah, I think the second should die. CINE should only work if there are no other objects created as part of the statement, IMHO.

> Well, if that's the rationale then you end up with no schema foo at all
> (i.e. both die), which seems even more surprising (though I admit it has
> the advantage of being a simple rule to document.)

I think we should just disallow putting any contained objects in the
statement when IF NOT EXISTS is used. It's simple to understand, simple
to document and implement, and I think it covers all the sane use-cases
anyway.

regards, tom lane


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: David E(dot) Wheeler <david(at)justatheory(dot)com>
Cc: Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 19:52:15
Message-ID: 1349207179-sup-9461@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from David E. Wheeler's message of mar oct 02 16:37:30 -0300 2012:
> On Oct 2, 2012, at 12:30 PM, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> wrote:
>
> > How about call this for precedent:
> >
> > mkdir -p /tmp/foo/bar
> > mkdir -p /tmp/foo/baz
> >
> > In this case you end up with directory "foo" and at least two subdirs in
> > it, bar and baz. This works even if /tmp/foo existed previously and
> > even if there was some other stuff in it.
>
> Well, what about this, then?
>
> create schema if not exists foo create table second (a int);
> create schema if not exists foo create table second (b int);

Yes, exactly -- what about this case? This is precisely the reason we
don't have CREATE TABLE IF NOT EXISTS.

I don't know what the best answer is. Most people seem to think that
the answer ought to be that you end up with a single column second.a,
and the second command errors out.

So if you do this:

create schema if not exists foo create table first (a int);
create schema if not exists foo create table first (b int),
create table second (a int);

you end up with *only* the first table, because the second command
errors out when the first table is observed to exist.

Now, what if you were to do this instead:

create schema if not exists foo
create table if not exists first (a int);
create schema if not exists foo
create table if not exists first (b int),
create table if not exists second (a int);

The you end up with first.a and second.a.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 19:54:21
Message-ID: DFC5EB20-EF1B-493C-A442-2ABAC0C70347@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 2, 2012, at 12:48 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> I think we should just disallow putting any contained objects in the
> statement when IF NOT EXISTS is used. It's simple to understand, simple
> to document and implement, and I think it covers all the sane use-cases
> anyway.

+1

David


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 20:24:38
Message-ID: 506B4D86.10306@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 10/02/2012 03:48 PM, Tom Lane wrote:
> Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> writes:
>> Excerpts from David E. Wheeler's message of mar oct 02 16:16:37 -0300 2012:
>>> On Oct 2, 2012, at 12:08 PM, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> wrote:
>>>> create schema if not exists foo create table first (a int);
>>>> create schema if not exists foo create table second (a int);
>>>>
>>>> As far as I can see, with the patch as it currently stands, you would
>>>> end up with only table "first" in the schema, which seems very
>>>> surprising to me.
>>> Yeah, I think the second should die. CINE should only work if there are no other objects created as part of the statement, IMHO.
>> Well, if that's the rationale then you end up with no schema foo at all
>> (i.e. both die), which seems even more surprising (though I admit it has
>> the advantage of being a simple rule to document.)
> I think we should just disallow putting any contained objects in the
> statement when IF NOT EXISTS is used. It's simple to understand, simple
> to document and implement, and I think it covers all the sane use-cases
> anyway.
>
>

I thought we'd already agreed on this.

+1.

cheers

andrew


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, David E(dot) Wheeler <david(at)justatheory(dot)com>, Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>, fabriziomello <fabriziomello(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 20:38:20
Message-ID: 1349210181-sup-1239@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Andrew Dunstan's message of mar oct 02 17:24:38 -0300 2012:
>
> On 10/02/2012 03:48 PM, Tom Lane wrote:
> > Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> writes:

> >> Well, if that's the rationale then you end up with no schema foo at all
> >> (i.e. both die), which seems even more surprising (though I admit it has
> >> the advantage of being a simple rule to document.)
> > I think we should just disallow putting any contained objects in the
> > statement when IF NOT EXISTS is used. It's simple to understand, simple
> > to document and implement, and I think it covers all the sane use-cases
> > anyway.
>
> I thought we'd already agreed on this.

Well, it's not what the latest proposed patch implements.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 22:33:17
Message-ID: CAFcNs+pBnQZrcC7B33e7D+bFUgi-j0wNdxFhQqgg9AtbQKW9Mg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/10/2 Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>

> Excerpts from Andrew Dunstan's message of mar oct 02 17:24:38 -0300 2012:
> >
> > On 10/02/2012 03:48 PM, Tom Lane wrote:
> > > Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> writes:
>
> > >> Well, if that's the rationale then you end up with no schema foo at
> all
> > >> (i.e. both die), which seems even more surprising (though I admit it
> has
> > >> the advantage of being a simple rule to document.)
> > > I think we should just disallow putting any contained objects in the
> > > statement when IF NOT EXISTS is used. It's simple to understand,
> simple
> > > to document and implement, and I think it covers all the sane use-cases
> > > anyway.
> >
> > I thought we'd already agreed on this.
>
> Well, it's not what the latest proposed patch implements.
>
>
You're right... the latest proposed patch don't implements it.

I'll change the patch and send soon...

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello


From: Stirling Newberry <stirling(dot)newberry(at)xigenics(dot)net>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-02 22:58:12
Message-ID: A9B3E11F-8461-4F51-8DC8-8CFDA4E9D1D3@xigenics.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

This case points to a weakness in many programming languages, not having a clear ifof (if and only if) versus if construction.

The sane use case for create schema foo if not exists <object> is for building a database dynamically, where several points may be the first to put a table in a schema, and schemas should not be created if there are no objects. The create/search/drop design pattern having its own problems.

Thus the construction should default to one behavior, and have an option for the second.

e.g.

create schema foo if not exists (will not be done if foo existed)
create schema foo if not exists FORCE (will be done even if foo existed)

This would even allow for mixed e.g.

create schema foo if not exists (tables that should be created once and not again)
FORCE (objects routine will add if the schema does)

On Oct 2, 2012, at 6:33 PM, Fabrízio de Royes Mello wrote:

>
> 2012/10/2 Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
> Excerpts from Andrew Dunstan's message of mar oct 02 17:24:38 -0300 2012:
> >
> > On 10/02/2012 03:48 PM, Tom Lane wrote:
> > > Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> writes:
>
> > >> Well, if that's the rationale then you end up with no schema foo at all
> > >> (i.e. both die), which seems even more surprising (though I admit it has
> > >> the advantage of being a simple rule to document.)
> > > I think we should just disallow putting any contained objects in the
> > > statement when IF NOT EXISTS is used. It's simple to understand, simple
> > > to document and implement, and I think it covers all the sane use-cases
> > > anyway.
> >
> > I thought we'd already agreed on this.
>
> Well, it's not what the latest proposed patch implements.
>
>
> You're right... the latest proposed patch don't implements it.
>
> I'll change the patch and send soon...
>
> Regards,
>
> --
> Fabrízio de Royes Mello
> Consultoria/Coaching PostgreSQL
> >> Blog sobre TI: http://fabriziomello.blogspot.com
> >> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
> >> Twitter: http://twitter.com/fabriziomello
>


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-03 12:27:41
Message-ID: CAFcNs+oxozz7qN7ZwY3eASN+ktR6OuH3g914FzaRPQkYrm1Ysg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/10/2 Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>

>
> You're right... the latest proposed patch don't implements it.
>
> I'll change the patch and send soon...
>
>
What is more reasonable?
* show a syntax error or
* show a message that you can not use the INE with contained objects

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: fabriziomello <fabriziomello(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, David E(dot) Wheeler <david(at)justatheory(dot)com>, Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-03 12:55:37
Message-ID: 1349268926-sup-3495@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Fabrízio de Royes Mello's message of mié oct 03 09:27:41 -0300 2012:
> 2012/10/2 Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
>
> >
> > You're right... the latest proposed patch don't implements it.
> >
> > I'll change the patch and send soon...
> >
> >
> What is more reasonable?
> * show a syntax error or
> * show a message that you can not use the INE with contained objects

Second one.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-03 13:11:03
Message-ID: CAFcNs+qMMK83icUbH=+5NBYfSUR6pUcXUBWzrAtzBZsdweZmyA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/10/3 Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>

> Excerpts from Fabrízio de Royes Mello's message of mié oct 03 09:27:41
> -0300 2012:
> > 2012/10/2 Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
> >
> > >
> > > You're right... the latest proposed patch don't implements it.
> > >
> > > I'll change the patch and send soon...
> > >
> > >
> > What is more reasonable?
> > * show a syntax error or
> > * show a message that you can not use the INE with contained objects
>
> Second one.
>
>
Maybe something like this?

ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("IF NOT EXISTS cannot be used with schema elements"),
parser_errposition(@9)));

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: fabriziomello <fabriziomello(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, David E(dot) Wheeler <david(at)justatheory(dot)com>, Dickson S(dot) Guedes <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-03 16:07:38
Message-ID: 1349280385-sup-4902@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Fabrízio de Royes Mello's message of mié oct 03 10:11:03 -0300 2012:

> Maybe something like this?
>
> ereport(ERROR,
> (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
> errmsg("IF NOT EXISTS cannot be used with schema elements"),
> parser_errposition(@9)));

Seems reasonable, but where? Please submit a complete patch.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-03 17:29:38
Message-ID: CAFcNs+qQtEgNZF4eA9p0MS71+TFBQCanv36zYGr_sTeoPpELDQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/10/3 Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>

> Excerpts from Fabrízio de Royes Mello's message of mié oct 03 10:11:03
> -0300 2012:
>
> > Maybe something like this?
> >
> > ereport(ERROR,
> > (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
> > errmsg("IF NOT EXISTS cannot be used with schema elements"),
> > parser_errposition(@9)));
>
> Seems reasonable, but where? Please submit a complete patch.
>
>
The attached patch implements the behavior we've discussed.

If we use "IF NOT EXISTS" with schema elements then occurs an error like
this:

[local]:5432 fabrizio(at)fabrizio=# CREATE SCHEMA IF NOT EXISTS test_schema_1
CREATE TABLE abc (
a serial,
b int UNIQUE
);
ERROR: IF NOT EXISTS cannot be used with schema elements
LINE 1: CREATE SCHEMA IF NOT EXISTS test_schema_1
^
Time: 0,773 ms

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

Attachment Content-Type Size
create_schema_if_not_exists_v7.patch application/octet-stream 8.8 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: fabriziomello(at)gmail(dot)com
Cc: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-03 17:56:01
Message-ID: 17969.1349286961@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

=?ISO-8859-1?Q?Fabr=EDzio_de_Royes_Mello?= <fabriziomello(at)gmail(dot)com> writes:
> The attached patch implements the behavior we've discussed.

OK, I'll pick this up again, since we seem to have consensus on this
behavior.

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: fabriziomello(at)gmail(dot)com
Cc: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-03 23:49:14
Message-ID: 10320.1349308154@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

=?ISO-8859-1?Q?Fabr=EDzio_de_Royes_Mello?= <fabriziomello(at)gmail(dot)com> writes:
> The attached patch implements the behavior we've discussed.

Committed with some adjustments, notably repairing the
order-of-operations error I complained about before.

regards, tom lane


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: fabriziomello(at)gmail(dot)com, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, "Dickson S(dot) Guedes" <listas(at)guedesoft(dot)net>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA IF NOT EXISTS
Date: 2012-10-04 00:17:10
Message-ID: 6329A8AB-5583-463B-B6A0-6C7D2C2FF2B5@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 3, 2012, at 4:49 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Committed with some adjustments, notably repairing the
> order-of-operations error I complained about before.

Awesome, thanks!

David