When working with partitions, there is often a need to delete all partitions at once.
However
DROP TABLE tablename*
Does not work.
So I faced this problem today. I loaded my server db through pgadmin3 and did it that way. Tables are sorted alphabetically so shift and click followed by delete works well.
I've always felt way more comfortable creating a sql script I can review and test before I run it than relying on getting the plpgsql just right so it doesn't blow away my database. Something simple in bash that selects the tablenames from the catalog, then creates the drop statements for me. So for 8.4.x you'd get this basic query:
SELECT 'drop table '||n.nspname ||'.'|| c.relname||';' as "Name"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','S','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid);
Which you can add a where clause to. (where c.relname ilike 'bubba%'
)
Output looks like this:
Name
-----------------------
drop table public.a1;
drop table public.a2;
So, save that to a .sql file and run it with psql -f filename.sql
Use a comma separated list:
DROP TABLE foo, bar, baz;
If you realy need a footgun, this one will do it's job:
CREATE OR REPLACE FUNCTION footgun(IN _schema TEXT, IN _parttionbase TEXT)
RETURNS void
LANGUAGE plpgsql
AS
$$
DECLARE
row record;
BEGIN
FOR row IN
SELECT
table_schema,
table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema = _schema
AND
table_name ILIKE (_parttionbase || '%')
LOOP
EXECUTE 'DROP TABLE ' || quote_ident(row.table_schema) || '.' || quote_ident(row.table_name);
RAISE INFO 'Dropped table: %', quote_ident(row.table_schema) || '.' || quote_ident(row.table_name);
END LOOP;
END;
$$;
SELECT footgun('public', 'tablename');
Using linux command line tools, it can be done this way:
psql -d mydb -P tuples_only=1 -c '\dt' | cut -d '|' -f 2 | paste -sd "," | sed 's/ //g' | xargs -I{} echo psql -d mydb -c "drop table {};"
NOTE: The last echo is there because I couldn't find a way to put quotes around the drop command, so you need to copy and paste the output and add the quotes yourself.
If anyone can fix that minor issue, that'd be awesome sauce.
I used this.
echo "select 'drop table '||tablename||';' from pg_tables where tablename like 'name%'" | \
psql -U postgres -d dbname -t | \
psql -U postgres -d dbname
Substitute in appropriate values for dbname
and name%
.
Disclosure: this answer is meant for Linux users.
I would add some more specific instructions to what @prongs said:
\dt
can support wildcards: so you can run \dt myPrefix*
for example, to select only the tables you want to drop;CTRL-SHIFT-DRAG
to select then CTRL-SHIFT-C
to copy the text;vim
, go to INSERT MODE
and paste the tables with CTRL-SHIFT-V
;ESC
, then run :%s/[ ]*\n/, /g
to translate it to comma-separated list, then you can paste it (excluding the last comma) in DROP TABLE % CASCADE
.