Re: [HACKERS] Truncate if exists

2012-10-18 Thread Robert Haas
On Tue, Oct 16, 2012 at 2:12 PM, Stafford, David x78061 david.staff...@broadridge.com wrote: On Mon, 15 Oct 2012 10:21:18 -0700, Robert Haas robertmh...@gmail.com wrote: Yeah, I think the functionality that we need is pretty much there already today. What we need to do is to get the syntax to

Re: [HACKERS] Truncate if exists

2012-10-16 Thread Stafford, David x78061
On Mon, 15 Oct 2012 10:21:18 -0700, Robert Haas robertmh...@gmail.com wrote: Yeah, I think the functionality that we need is pretty much there already today. What we need to do is to get the syntax to a point where people can write the code they want to write without getting tangled up by it.

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Robert Haas
On Fri, Oct 12, 2012 at 5:52 PM, Dimitri Fontaine dimi...@2ndquadrant.fr wrote: Christopher Browne cbbro...@gmail.com writes: I suggest the though of embracing statement modifiers in DDL, with some options possible: a) { DDL STATEMENT } IF CONDITION; b) { DDL STATEMENT } UNLESS CONDITION;

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Greg Stark
On Mon, Oct 15, 2012 at 3:26 PM, Robert Haas robertmh...@gmail.com wrote: To be perfectly frank, I think that's exactly where we ought to be going. Oracle and Microsoft both did it, so why are we convinced it's a bad idea? One of the huge problems with PL/pgsql is that every SQL expression

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Hannu Krosing
On 10/15/2012 04:34 PM, Greg Stark wrote: On Mon, Oct 15, 2012 at 3:26 PM, Robert Haas robertmh...@gmail.com wrote: To be perfectly frank, I think that's exactly where we ought to be going. Oracle and Microsoft both did it, so why are we convinced it's a bad idea? One of the huge problems

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Robert Haas
On Mon, Oct 15, 2012 at 10:34 AM, Greg Stark st...@mit.edu wrote: I'm a bit lost. I would think pl/pgsql is precisely the same as Oracle's pl/sql and MS's T-SQL. I see the complaint you have as a purely implementation detail. I don't think pl/pgsql is the best implemented part of Postgres but

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Christopher Browne
On Mon, Oct 15, 2012 at 11:57 AM, Robert Haas robertmh...@gmail.com wrote: On Mon, Oct 15, 2012 at 10:34 AM, Greg Stark st...@mit.edu wrote: I'm a bit lost. I would think pl/pgsql is precisely the same as Oracle's pl/sql and MS's T-SQL. I see the complaint you have as a purely implementation

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Robert Haas
On Mon, Oct 15, 2012 at 12:53 PM, Christopher Browne cbbro...@gmail.com wrote: The places where *I* care about this are places where performance is almost entirely irrelevant to the question. When I'm writing 'scripts' that are doing this kind of thing, I'm doing schema 'surgery', and, within

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Dimitri Fontaine
Robert Haas robertmh...@gmail.com writes: if (select 1 from pg_class where relname = 'foo' and pg_table_is_visible(oid)) then truncate table foo; end if; Yeah, I think the functionality that we need is pretty much there already today. What we need to do is to

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Robert Haas
On Mon, Oct 15, 2012 at 3:14 PM, Dimitri Fontaine dimi...@2ndquadrant.fr wrote: What about continuing to extend on that incredibly useful WITH syntax we already have: WITH target AS ( SELECT oid::regclass AS t FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Dimitri Fontaine
Robert Haas robertmh...@gmail.com writes: WITH target AS ( TRUNCATE TABLE t FROM target; I'm not exactly sure what that is supposed to do, but it doesn't seem like an easy-to-use substitute for truncate-if-exists... Indeed. I'm still a supporter of truncate-if-exists. Still, we're also

Re: [HACKERS] Truncate if exists

2012-10-15 Thread Christopher Browne
On Mon, Oct 15, 2012 at 3:14 PM, Dimitri Fontaine dimi...@2ndquadrant.fr wrote: Robert Haas robertmh...@gmail.com writes: if (select 1 from pg_class where relname = 'foo' and pg_table_is_visible(oid)) then truncate table foo; end if; Yeah, I think the

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Sébastien Lardière
On 10/11/2012 09:22 PM, Simon Riggs wrote: That is a lot more typing and it's not exactly intuitive. One obvious thing that would help is a function pg_table_exists(text) that would return true or false. But even with that there's a lot of syntactic sugar in there that is less than ideal:

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Andrew Dunstan
On 10/12/2012 12:03 PM, Sébastien Lardière wrote: If we can do something like : SELECT pg_if_table_exists('bar' , pg_if_table_exists('foo', 'TRUNCATE TABLE foo, bar, foobar')) ; or SELECT pg_if_tables_exists('TRUNCATE TABLE foo, bar, foobar', 'foo', 'bar') ; I say yes ! This strikes

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Pavel Stehule
Hello 2012/10/12 Sébastien Lardière slardi...@hi-media.com: On 10/11/2012 09:22 PM, Simon Riggs wrote: That is a lot more typing and it's not exactly intuitive. One obvious thing that would help is a function pg_table_exists(text) that would return true or false. But even with that

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Dimitri Fontaine
Andrew Dunstan and...@dunslane.net writes: This strikes me as just highly un-SQL-like. +1 I tend to agree with Noah's comment upthread: But the syntax is a bandage for raw psql input remaining a hostile environment for implementing the full range of schema changes. Switch to submitting

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Robert Haas
On Thu, Oct 11, 2012 at 3:22 PM, Simon Riggs si...@2ndquadrant.com wrote: So we just need a function called pg_if_table_exists(table, SQL) which wraps a test in a subtransaction. And you write SELECT pg_if_table_exists('foo', 'TRUNCATE TABLE foo'); and we can even get rid of all that other

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Stephen Frost
* Robert Haas (robertmh...@gmail.com) wrote: INSERT IF EXISTS (and, hey, why not INSERT OR CREATE for good measure?). I'm not sure what the right thing to do is... but we should probably come up with some consensus position we can all live with, and then go make this uniform[1]. 'INSERT OR

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Stephen Frost
* Tom Lane (t...@sss.pgh.pa.us) wrote: Second, to my mind the point of a multi-table TRUNCATE is to ensure that all the referenced tables get reset to empty *together*. With something like this, you'd have no such guarantee. Consider a timeline like this: Don't we have the exact same issue

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Alvaro Herrera
Robert Haas escribió: On Thu, Oct 11, 2012 at 3:22 PM, Simon Riggs si...@2ndquadrant.com wrote: So we just need a function called pg_if_table_exists(table, SQL) which wraps a test in a subtransaction. And you write SELECT pg_if_table_exists('foo', 'TRUNCATE TABLE foo'); and we can

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Stephen Frost
* Josh Berkus (j...@agliodbs.com) wrote: On 10/9/12 1:35 PM, Peter Eisentraut wrote: On 10/9/12 5:09 AM, Simon Riggs wrote: Anyone want to check for any other missing IF EXISTS capability in other DDL? TRUNCATE is not really DDL. If we allow TRUNCATE IF EXISTS, what is stopping

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Dimitri Fontaine
Alvaro Herrera alvhe...@2ndquadrant.com writes: Uh, we had an execute() function of sorts in the extensions patch; that seems to have been ripped out. Do we want it back? It was pretty different from what's being proposed here, as it was the server-side version of psql \i feature, that is,

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Christopher Browne
On Fri, Oct 12, 2012 at 3:04 PM, Robert Haas robertmh...@gmail.com wrote: On Thu, Oct 11, 2012 at 3:22 PM, Simon Riggs si...@2ndquadrant.com wrote: So we just need a function called pg_if_table_exists(table, SQL) which wraps a test in a subtransaction. And you write SELECT

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Josh Berkus
On 10/12/12 2:05 PM, Christopher Browne wrote: That feels like a cleaner extension than what we have had, with the IF EXISTS/IF NOT EXISTS clauses that have been added to various CREATE/DROP/ALTER commands. +1 Josh like! -- Josh Berkus PostgreSQL Experts Inc. http://pgexperts.com -- Sent

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Hannu Krosing
On 10/12/2012 11:05 PM, Christopher Browne wrote: On Fri, Oct 12, 2012 at 3:04 PM, Robert Haas robertmh...@gmail.com wrote: On Thu, Oct 11, 2012 at 3:22 PM, Simon Riggs si...@2ndquadrant.com wrote: So we just need a function called pg_if_table_exists(table, SQL) which wraps a test in a

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Dimitri Fontaine
Christopher Browne cbbro...@gmail.com writes: I suggest the though of embracing statement modifiers in DDL, with some options possible: a) { DDL STATEMENT } IF CONDITION; b) { DDL STATEMENT } UNLESS CONDITION; Just saying. I hate that. Makes it harder to read, that last bit at the end of

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Josh Berkus
where CONDITION has several possible forms: i) {IF|UNLESS} ( SQL expression returning T/F ) ii) {IF|UNLESS} {EXISTS|NOT EXISTS} {TABLE|SCHEMA|COLUMN|FUNCTION|...} object_name Now we're talking about T-SQL? Sorry, I suppose you didn't mean to lead us that way, but I couldn't resist

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Christopher Browne
On Fri, Oct 12, 2012 at 5:52 PM, Dimitri Fontaine dimi...@2ndquadrant.fr wrote: Christopher Browne cbbro...@gmail.com writes: I suggest the though of embracing statement modifiers in DDL, with some options possible: a) { DDL STATEMENT } IF CONDITION; b) { DDL STATEMENT } UNLESS CONDITION;

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Greg Stark
On Tue, Oct 9, 2012 at 9:04 PM, Robert Haas robertmh...@gmail.com wrote: I've been a big proponent of adding IF EXISTS support to CREATE TABLE and ALTER TABLE but I'm having a hard time getting excited about this one. I can't imagine that many people would use it The reason CREATE IF NOT

Re: [HACKERS] Truncate if exists

2012-10-12 Thread Josh Berkus
On 10/12/12 3:49 PM, Greg Stark wrote: TRUNCATE IF EXISTS foo; CREATE IF NOT EXISTS foo... Thing is, this can be written: CREATE IF NOT EXISTS foo ... TRUNCATE foo; For the exact same result. So, based on all of the objections and discussion on this feature, I personally no longer support

Re: [HACKERS] Truncate if exists

2012-10-11 Thread Sébastien Lardière
On 10/09/2012 10:04 PM, Robert Haas wrote: - if a table is not yet or no more visible, because of search_path modification I don't think I understand the case you are describing here. Here's a sample : begin; set search_path = foo, public; create table c ( … ) ; commit; begin; set

Re: [HACKERS] Truncate if exists

2012-10-11 Thread Sébastien Lardière
On 10/09/2012 04:06 PM, Tom Lane wrote: Second, to my mind the point of a multi-table TRUNCATE is to ensure that all the referenced tables get reset to empty *together*. With something like this, you'd have no such guarantee. Consider a timeline like this: Session 1

Re: [HACKERS] Truncate if exists

2012-10-11 Thread Cédric Villemain
For starters, the use-case hasn't been explained to my satisfaction. In what situation is it actually helpful to TRUNCATE a table that's not there yet? Aren't you going to have to do a CREATE IF NOT EXISTS to keep from failing later in the script? If so, why not just do that first? There

Re: [HACKERS] Truncate if exists

2012-10-11 Thread Robert Haas
On Wed, Oct 10, 2012 at 3:32 AM, Simon Riggs si...@2ndquadrant.com wrote: 2) Clearly, rollout scripts benefit from not throwing errors. Personally I would prefer setting SET ddl_abort_on_missing_object = false; at the top of a script than having to go through every SQL statement and add extra

Re: [HACKERS] Truncate if exists

2012-10-11 Thread Simon Riggs
On 11 October 2012 19:59, Robert Haas robertmh...@gmail.com wrote: On Wed, Oct 10, 2012 at 3:32 AM, Simon Riggs si...@2ndquadrant.com wrote: 2) Clearly, rollout scripts benefit from not throwing errors. Personally I would prefer setting SET ddl_abort_on_missing_object = false; at the top of a

Re: [HACKERS] Truncate if exists

2012-10-10 Thread Simon Riggs
On 10 October 2012 02:10, Robert Haas robertmh...@gmail.com wrote: On Tue, Oct 9, 2012 at 4:18 PM, Josh Berkus j...@agliodbs.com wrote: The second is for making deployment scripts idempotent. For example, say you have script A which creates table josh, and script B which needs table josh to

Re: [HACKERS] Truncate if exists

2012-10-10 Thread Christopher Browne
On Wed, Oct 10, 2012 at 3:32 AM, Simon Riggs si...@2ndquadrant.com wrote: On 10 October 2012 02:10, Robert Haas robertmh...@gmail.com wrote: On Tue, Oct 9, 2012 at 4:18 PM, Josh Berkus j...@agliodbs.com wrote: The second is for making deployment scripts idempotent. For example, say you have

Re: [HACKERS] Truncate if exists

2012-10-10 Thread Josh Berkus
On 10/9/12 1:35 PM, Peter Eisentraut wrote: On 10/9/12 5:09 AM, Simon Riggs wrote: Anyone want to check for any other missing IF EXISTS capability in other DDL? TRUNCATE is not really DDL. If we allow TRUNCATE IF EXISTS, what is stopping someone from requesting DELETE IF EXISTS or INSERT IF

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Simon Riggs
On 9 October 2012 09:33, Sébastien Lardière slardi...@hi-media.com wrote: With the help of Cédric, here's a patch changing the TRUNCATE TABLE command, adding the IF EXISTS option to allow the presence in the list of tables of a missing or invisible table. This meets the needs of scripts that

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Sébastien Lardière
On 10/09/2012 11:09 AM, Simon Riggs wrote: On 9 October 2012 09:33, Sébastien Lardière slardi...@hi-media.com wrote: With the help of Cédric, here's a patch changing the TRUNCATE TABLE command, adding the IF EXISTS option to allow the presence in the list of tables of a missing or invisible

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Vik Reykja
On Tue, Oct 9, 2012 at 11:09 AM, Simon Riggs si...@2ndquadrant.com wrote: Anyone want to check for any other missing IF EXISTS capability in other DDL? Yes, DEALLOCATE.

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Vik Reykja
On Tue, Oct 9, 2012 at 11:51 AM, Vik Reykja vikrey...@gmail.com wrote: On Tue, Oct 9, 2012 at 11:09 AM, Simon Riggs si...@2ndquadrant.comwrote: Anyone want to check for any other missing IF EXISTS capability in other DDL? Yes, DEALLOCATE. Patch attached. deallocate_if_exists.patch

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Tom Lane
Simon Riggs si...@2ndquadrant.com writes: On 9 October 2012 09:33, Sébastien Lardière slardi...@hi-media.com wrote: With the help of Cédric, here's a patch changing the TRUNCATE TABLE command, adding the IF EXISTS option to allow the presence in the list of tables of a missing or invisible

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Tom Lane
=?ISO-8859-1?Q?S=E9bastien_Lardi=E8re?= slardi...@hi-media.com writes: Indeed, brackets was not correct, it's better now (I think), and correct some comments. Still wrong ... at the very least you missed copyfuncs/equalfuncs. In general, when adding a field to a struct, it's good practice to

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Sébastien Lardière
On 10/09/2012 04:06 PM, Tom Lane wrote: Simon Riggs si...@2ndquadrant.com writes: On 9 October 2012 09:33, Sébastien Lardière slardi...@hi-media.com wrote: With the help of Cédric, here's a patch changing the TRUNCATE TABLE command, adding the IF EXISTS option to allow the presence in the list

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Robert Haas
On Tue, Oct 9, 2012 at 12:28 PM, Sébastien Lardière slardi...@hi-media.com wrote: For starters, the use-case hasn't been explained to my satisfaction. In what situation is it actually helpful to TRUNCATE a table that's not there yet? Aren't you going to have to do a CREATE IF NOT EXISTS to

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Simon Riggs
On 9 October 2012 15:06, Tom Lane t...@sss.pgh.pa.us wrote: Simon Riggs si...@2ndquadrant.com writes: On 9 October 2012 09:33, Sébastien Lardière slardi...@hi-media.com wrote: With the help of Cédric, here's a patch changing the TRUNCATE TABLE command, adding the IF EXISTS option to allow the

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Josh Berkus
Robert, I've been a big proponent of adding IF EXISTS support to CREATE TABLE and ALTER TABLE but I'm having a hard time getting excited about this one. I can't imagine that many people would use it, and those who do can implement it in about 10 lines of PL/pgsql. The existence of DO

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Peter Eisentraut
On 10/9/12 5:09 AM, Simon Riggs wrote: Anyone want to check for any other missing IF EXISTS capability in other DDL? TRUNCATE is not really DDL. If we allow TRUNCATE IF EXISTS, what is stopping someone from requesting DELETE IF EXISTS or INSERT IF EXISTS next? -- Sent via pgsql-hackers

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Gavin Flower
On 10/10/12 09:35, Peter Eisentraut wrote: On 10/9/12 5:09 AM, Simon Riggs wrote: Anyone want to check for any other missing IF EXISTS capability in other DDL? TRUNCATE is not really DDL. If we allow TRUNCATE IF EXISTS, what is stopping someone from requesting DELETE IF EXISTS or INSERT IF

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Simon Riggs
On 9 October 2012 21:35, Peter Eisentraut pete...@gmx.net wrote: On 10/9/12 5:09 AM, Simon Riggs wrote: Anyone want to check for any other missing IF EXISTS capability in other DDL? TRUNCATE is not really DDL. If we allow TRUNCATE IF EXISTS, what is stopping someone from requesting DELETE IF

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Daniel Farina
On Tue, Oct 9, 2012 at 2:04 PM, Simon Riggs si...@2ndquadrant.com wrote: On 9 October 2012 21:35, Peter Eisentraut pete...@gmx.net wrote: On 10/9/12 5:09 AM, Simon Riggs wrote: Anyone want to check for any other missing IF EXISTS capability in other DDL? TRUNCATE is not really DDL. If we

Re: [HACKERS] Truncate if exists

2012-10-09 Thread Robert Haas
On Tue, Oct 9, 2012 at 4:18 PM, Josh Berkus j...@agliodbs.com wrote: The second is for making deployment scripts idempotent. For example, say you have script A which creates table josh, and script B which needs table josh to be empty, if present. Since the two scripts are tied to different