Re: Truncate if exists - Mailing list pgsql-hackers

From Robert Haas
Subject Re: Truncate if exists
Date
Msg-id CA+TgmobQ8dpgRObHXh1mp=B3b5L-nJrWz=8oSttvNZXGOkK=Ag@mail.gmail.com
Whole thread Raw
In response to Re: Truncate if exists  (Simon Riggs <simon@2ndQuadrant.com>)
Responses Re: Truncate if exists  (Alvaro Herrera <alvherre@2ndquadrant.com>)
Re: Truncate if exists  (Christopher Browne <cbbrowne@gmail.com>)
List pgsql-hackers
On Thu, Oct 11, 2012 at 3:22 PM, Simon Riggs <simon@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 DDL crud that's been added....
>
> and we can have pg_if_table_not_exists() also.

You could make this more composable by having pg_table_exists() and
pg_execute_sql_from_string().  Then you can write: SELECT CASE WHEN
pg_table_exists(...) THEN pg_execute_sql_from_string(...) END.  And if
you want the if-not-exists case then just stick a NOT in there.  And
if you want a more complicated condition, you can easily write that as
well.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: Deprecating RULES
Next
From: Robert Haas
Date:
Subject: Re: September 2012 commitfest