pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: emilu@encs.concordia.ca
Cc: pgsql-sql@postgresql.org
Subject: Re: drop table where tableName like 'backup_2007%' ?
Date: Mon, 31 Mar 2008 11:55:14 -0400
Message-ID: <28893.1206978914@sss.pgh.pa.us> (raw)
In-Reply-To: <47F0F9BB.3060706@encs.concordia.ca>
References: <47F0F9BB.3060706@encs.concordia.ca>

Emi Lu <emilu@encs.concordia.ca> writes:
> Is there a command to drop tables whose name begins a specific string?

No.  The standard answer to this type of problem is to write a little
plpgsql function that scans the appropriate catalog and issues commands
constructed with EXECUTE.

	for r in select relname from pg_class where relname like 'backup_2007%'
	loop
		execute 'DROP TABLE ' || quote_ident(r);
	end loop;

Note that the above is overly simplistic --- it doesn't pay attention
to schemas, for example.

Some people prefer to just print out the constructed commands into a
file, so they can eyeball them before actually executing them.

			regards, tom lane



view thread (3+ messages)  latest in thread

Message-ID: <28893.1206978914@sss.pgh.pa.us>
Permalink:  ../28893.1206978914@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/28893.1206978914@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, emilu@encs.concordia.ca
  Subject: Re: drop table where tableName like 'backup_2007%' ?
  In-Reply-To: <28893.1206978914@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox