Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Consider a situation where one need to perform a bunch of essentially identical operations, with the only variable being the name of some object.

In my case, I need to drop some schemas, all of the form ceu_shard_test_merge_*, to use shell globbing terminology. So, conceptually, this can be written as

DROP SCHEMA ceu_shard_test_merge_* CASCADE;

by analogy with a Unix shell.

Of course this command doesn't work, so how can one do this with a single command? My understanding is that this cannot be done portably. I'm using PostgreSQL 8.4, but methods for more recent versions of PG are fine too.

It would be nice if the solution had a dry run or dummy option, so one could see what commands were going to be run before actually running them. Perhaps a way to just print the commands?

Also, an indication of how to deal with more general patterns than the example given would be nice.

share|improve this question
add comment

2 Answers

This query on the system catalog creates the necessary DDL scipt:

SELECT string_agg(format('DROP SCHEMA %I CASCADE;', nspname), E'\n')
FROM   pg_namespace
WHERE  nspname LIKE 'ceu_shard_test_merge_%';

Note the use of format() to escape identifiers if necessary.

format() requires PostgreSQL 9.1+.
Replace with quote_ident() in older versions.

string_agg() requires PostgreSQL 9.0+.
Replace with array_to_string(array_agg(...), E'\n') in older versions.

For Postgres 8.4, that would be:

SELECT array_to_string(
          array_agg('DROP SCHEMA ' || quote_ident(nspname) || ' CASCADE;')
        , E'\n')
FROM ...

Returns:

DROP SCHEMA ceu_shard_test_merge_1 CASCADE;
DROP SCHEMA ceu_shard_test_merge_2 CASCADE;
...

Which you can inspect before executing.

You can put it all into a DO command for automatic execution or create a function for repeated use. Consider this closely related answer for a complete code example:
Bug in PL/pgSQL function creation

Or this related answer on SO.

share|improve this answer
    
Thanks very much for the detailed answer, Erwin. The alternatives when using more recent versions of PG are particularly interesting to me. Can you give an example when escaping identifiers would be necessary? Would special characters or spaces in the schema name qualify? –  Faheem Mitha Apr 28 '13 at 15:52
    
@FaheemMitha: All of these qualify: upper case letters, white space, reserved words, special characters. Consider this worst case: CREATE SCHEMA "; DELETE FROM accounts; --". But careful with that! More on SQL injection here. –  Erwin Brandstetter Apr 28 '13 at 17:05
    
Thanks for the clarification, Erwin. –  Faheem Mitha Apr 28 '13 at 17:17
add comment

You could write a procedure with the EXECUTE and build the statement in the procedure by looping over the list of schema which match your query from the information_schema tables.

Then you can call your procedure from psql or other passing it your criteria such as 'ceu_shard_test_merge_%'.

You could have a parameter to do it or just dry run and instead of execute then it could output the statements or something along those lines.

share|improve this answer
    
Thanks for the tips, ETL. –  Faheem Mitha Apr 28 '13 at 8:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.