add additional options to CREATE TABLE ... AS

Lists: pgsql-patches
From: Kris Jurka <books(at)ejurka(dot)com>
To: pgsql-patches(at)postgresql(dot)org
Subject: add additional options to CREATE TABLE ... AS
Date: 2006-02-14 19:32:36
Message-ID: Pine.BSO.4.63.0602141425560.8341@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches


This patch adds most of the options available for regular CREATE TABLE
syntax to the CREATE TABLE x AS SELECT ... and AS EXECUTE ...
Specifically this allows specification of on commit behavior for temp
tables and tablespaces for regular tables to these two statements.
Additionally with/without oids is now available for the EXECUTE variant.
Currently you still cannot specify inheritance attributes with these
commands, but this seems like a more complicated task.

Kris Jurka

Attachment Content-Type Size
create-table-options.patch text/plain 32.1 KB

From: Neil Conway <neilc(at)samurai(dot)com>
To: Kris Jurka <books(at)ejurka(dot)com>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: add additional options to CREATE TABLE ... AS
Date: 2006-02-14 20:27:12
Message-ID: 1139948832.31672.15.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On Tue, 2006-02-14 at 14:32 -0500, Kris Jurka wrote:
> This patch adds most of the options available for regular CREATE TABLE
> syntax to the CREATE TABLE x AS SELECT ... and AS EXECUTE ...
> Specifically this allows specification of on commit behavior for temp
> tables and tablespaces for regular tables to these two statements.

The implementation is pretty ugly -- it clutters ExecuteStmt and Query
with fields that really do not belong there. Per previous discussion, I
think it would be better to refactor the CREATE TABLE AS implementation
to be essentially a CREATE TABLE followed by a INSERT ... SELECT.

(That's not necessarily a reason to reject the patch, but the patch does
increase the benefit of performing that refactoring.)

A few cosmetic comments:

typedef struct ExecuteStmt
{
NodeTag type;
char *name;
RangeVar *into;
ContainsOids intocontainsoids;
bool intohasoids;
OnCommitAction intooncommit;
char *intotablespacename;
List *params;
} ExecuteStmt;

I think we ought to use either camel-case or underscore characters to
separate words.

parser/analyze.c, circa 1822:

if (stmt->intoTableSpaceName)
qry->intoTableSpaceName = pstrdup(stmt->intoTableSpaceName);
else
qry->intoTableSpaceName = NULL;

You can omit the "else", as makeNode() zeroes all the fields of the new
node. (You could argue that leaving the assignment is more readable, but
I personally don't think so: this behavior of makeNode() is used in a
several places in the backend.)

-Neil


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Kris Jurka <books(at)ejurka(dot)com>, pgsql-patches(at)postgresql(dot)org
Subject: Re: add additional options to CREATE TABLE ... AS
Date: 2006-02-14 20:38:48
Message-ID: 22300.1139949528@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Neil Conway <neilc(at)samurai(dot)com> writes:
> The implementation is pretty ugly -- it clutters ExecuteStmt and Query
> with fields that really do not belong there. Per previous discussion, I
> think it would be better to refactor the CREATE TABLE AS implementation
> to be essentially a CREATE TABLE followed by a INSERT ... SELECT.

I kinda wonder why bother at all. I don't see any good reason why
people shouldn't issue two statements.

>> if (stmt->intoTableSpaceName)
>> qry->intoTableSpaceName = pstrdup(stmt->intoTableSpaceName);
>> else
>> qry->intoTableSpaceName = NULL;

> You can omit the "else", as makeNode() zeroes all the fields of the new
> node.

For that matter, why not just

qry->intoTableSpaceName = stmt->intoTableSpaceName;

There's no need for the string-copy operation here, is there?

regards, tom lane


From: Kris Jurka <books(at)ejurka(dot)com>
To: pgsql-patches(at)postgresql(dot)org
Subject: Re: add additional options to CREATE TABLE ... AS
Date: 2006-02-14 21:10:30
Message-ID: Pine.BSO.4.63.0602141610020.13106@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On Tue, 14 Feb 2006, Kris Jurka wrote:

>
> This patch adds most of the options available for regular CREATE TABLE syntax
> to the CREATE TABLE x AS SELECT ... and AS EXECUTE ...

Here's the doc changes for this.

Kris Jurka

Attachment Content-Type Size
create-as-doc.patch text/plain 4.8 KB

From: Kris Jurka <books(at)ejurka(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Neil Conway <neilc(at)samurai(dot)com>, pgsql-patches(at)postgresql(dot)org
Subject: Re: add additional options to CREATE TABLE ... AS
Date: 2006-02-14 22:30:36
Message-ID: Pine.BSO.4.63.0602141722160.27266@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On Tue, 14 Feb 2006, Tom Lane wrote:

> I kinda wonder why bother at all. I don't see any good reason why
> people shouldn't issue two statements.
>

Well if you don't know what the resulting columns are going to be that
could be difficult. There are a number of reasons why this patch is an
improvement.

1) People have requested this feature:
http://archives.postgresql.org/pgsql-bugs/2005-11/msg00163.php
http://archives.postgresql.org/pgsql-bugs/2004-03/msg00186.php

2) The SQL spec requires ON COMMIT for CREATE TEMP TABLE AS SELECT.

3) The unification of EXECUTE and SELECT options actually simplifies the
grammar by removing the WithOidsAs production hack.

Kris Jurka


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Neil Conway <neilc(at)samurai(dot)com>, Kris Jurka <books(at)ejurka(dot)com>, pgsql-patches(at)postgresql(dot)org
Subject: Re: add additional options to CREATE TABLE ... AS
Date: 2006-02-15 17:38:51
Message-ID: 1140025131.12131.66.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On Tue, 2006-02-14 at 15:38 -0500, Tom Lane wrote:
> Neil Conway <neilc(at)samurai(dot)com> writes:
> > The implementation is pretty ugly -- it clutters ExecuteStmt and Query
> > with fields that really do not belong there. Per previous discussion, I
> > think it would be better to refactor the CREATE TABLE AS implementation
> > to be essentially a CREATE TABLE followed by a INSERT ... SELECT.
>
> I kinda wonder why bother at all. I don't see any good reason why
> people shouldn't issue two statements.

The good reason is that CREATE plus INSERT SELECT is extremely bug prone
and very annoying to have to code SQL that way. CTAS is sent from on
high, IMHO, even without the performance optimisation.

Best Regards, Simon Riggs


From: Neil Conway <neilc(at)samurai(dot)com>
To: Kris Jurka <books(at)ejurka(dot)com>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: add additional options to CREATE TABLE ... AS
Date: 2006-02-18 20:28:09
Message-ID: 1140294489.2615.0.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On Tue, 2006-02-14 at 14:32 -0500, Kris Jurka wrote:
> This patch adds most of the options available for regular CREATE TABLE
> syntax to the CREATE TABLE x AS SELECT ... and AS EXECUTE ...

Barring any objections, I'll apply this later today or tomorrow.

-Neil


From: Neil Conway <neilc(at)samurai(dot)com>
To: Kris Jurka <books(at)ejurka(dot)com>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: add additional options to CREATE TABLE ... AS
Date: 2006-02-19 00:05:02
Message-ID: 1140307502.2615.6.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On Tue, 2006-02-14 at 14:32 -0500, Kris Jurka wrote:
> This patch adds most of the options available for regular CREATE TABLE
> syntax to the CREATE TABLE x AS SELECT ... and AS EXECUTE ...

Applied to HEAD, including the documentation updates posted separately.
Thanks for the patch.

-Neil