tags:

views:

66

answers:

2

From Googling, I'm pretty sure the answer to this is "no", but anyway.

Question: is it possible either to delete all rows from all tables with a single command in postgres (without destroying the database), or to cascade a delete in postgres?

If not - wow.

A: 

is it possible either to delete all rows from all tables with a single command in postgres

You are right, the answer is NO

You can define cascading foreign keys that will delete all referencing rows if the "parent" is deleted. But that is an attribute of the foreign key, nothing you can specify with the DELETE statement

if not - wow.

What's that supposed to mean?

On second thought: what are you trying to achieve?

I have the suspicion that you are trying to "reset" e.g. a test database. In that case the PostgreSQL approach would be:

  • Create a database that contains everything (tables, views, indexes etc) you need in a new database (call it e.g. my_template)
  • To reset your current test-DB, do a DROP DATABASE testdb and then re-create the test database using CREATE DATABASE testdb TEMPLATE my_template

The newly created testdb will have all tables defined in my_template. That is probably a lot faster than deleting all rows.

a_horse_with_no_name
You're right, that's exactly what I was trying to do, and I was getting frustrated that postgres didn't have a simple way to clear data quickly. I'll create a template - thanks for the help.
AP257
As you seem to expect a "DELETE all FROM everything" type of statement I'm wondering why? No database that I know of supports this.
a_horse_with_no_name
Well, what I'd really like is a DELETE CASCADE. In the absence of that, DELETE all FROM everything would be an acceptable substitute. But not having either is a bit of a pain!
AP257
So which database has a DELETE CASCADE command?
a_horse_with_no_name
+1  A: 

You could write a stored procedure that selects all tables in all schemas and then does a DELETE CASCADE or TRUNCATE on these tables. It's just a few lines of pl/pgsql-code, not a big deal.

Edit: This will do the trick, but be carefull! :

CREATE OR REPLACE FUNCTION truncate_all() RETURNS void LANGUAGE plpgsql AS
$$
DECLARE
    row record;
    query   text;
BEGIN
    query := 'SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema NOT IN(''pg_catalog'', ''information_schema'') AND table_type = ''BASE TABLE''';

    FOR row IN EXECUTE query LOOP
        EXECUTE 'TRUNCATE ' || quote_ident(row.table_schema) || '.' || quote_ident(row.table_name) || ' CASCADE;';
    END LOOP;

    RETURN;
END;
$$;

-- execute:
SELECT truncate_all();
Frank Heikens
+1: [TRUNCATE](http://www.postgresql.org/docs/8.1/interactive/sql-truncate.html) would be my choice - not a fan of `DELETE CASCADE`, too easy to screw up with fat fingering...
OMG Ponies
The solution will only work if the database has no foreign keys defined which would only be true for very simple database. But the function could be enhanced to first drop all existing constraints, and then truncate all tables.
a_horse_with_no_name
No it's not, that's where CASCADE comes in. Just test it and you will see. http://www.postgresql.org/docs/current/static/sql-truncate.html
Frank Heikens
A cool, didn't know about that one ;) Thanks for pointing that out
a_horse_with_no_name
`TRUNCATE CASCADE` is on one hand one of the scariest commands in existence... but quite applicable here.
araqnid
Yep, it's sort of a nuclear warhead for your database. :(
Frank Heikens