HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Dropping a group of schemas with similar name patterns

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
groupwithdroppingpatternsschemasnamesimilar

Problem

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.

Solution

This query on the system catalog generates the necessary DDL script:

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.

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;
...


Inspect the result before executing in turn.

You can wrap all into a DO command for automatic execution or create a function for repeated use. Consider this closely related code example:

  • Bug in PL/pgSQL function creation



Or this related answer on SO.
Output in psql

To address @Stew's comment: display unadorned text as result in psql with the \pset meta-command:

\pset format unaligned
\pset tuples_only


Or use the short commands \x \a

Code Snippets

SELECT string_agg(format('DROP SCHEMA %I CASCADE;', nspname), E'\n')
FROM   pg_namespace
WHERE  nspname LIKE 'ceu_shard_test_merge_%';
SELECT array_to_string(
          array_agg('DROP SCHEMA ' || quote_ident(nspname) || ' CASCADE;')
        , E'\n')
FROM ...
DROP SCHEMA ceu_shard_test_merge_1 CASCADE;
DROP SCHEMA ceu_shard_test_merge_2 CASCADE;
...
\pset format unaligned
\pset tuples_only

Context

StackExchange Database Administrators Q#41087, answer score: 11

Revisions (0)

No revisions yet.