8

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.

Erwin Brandstetter
  • 185,527
  • 28
  • 463
  • 633
Faheem Mitha
  • 1,049
  • 2
  • 12
  • 18

2 Answers2

12

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:

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

Erwin Brandstetter
  • 185,527
  • 28
  • 463
  • 633
2

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.

ETL
  • 988
  • 6
  • 18