X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5007152A32 for ; Thu, 7 Apr 2005 21:30:12 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 01255-05 for ; Thu, 7 Apr 2005 20:30:02 +0000 (GMT) Received: from mariner.sharechive.com (h-67-102-69-10.snfccasy.covad.net [67.102.69.10]) by svr1.postgresql.org (Postfix) with ESMTP id 6324B5353D for ; Thu, 7 Apr 2005 21:30:00 +0100 (BST) Received: from localhost.localdomain (craiga-linux.sharechive.com [10.0.0.19]) by mariner.sharechive.com (8.12.10/8.12.10) with ESMTP id j37KTrFM022609 for ; Thu, 7 Apr 2005 13:29:54 -0700 Received: from localhost.localdomain (craiga-linux [127.0.0.1]) by localhost.localdomain (8.12.5/8.12.5) with ESMTP id j37KOra7003946; Thu, 7 Apr 2005 13:24:53 -0700 Received: (from craiga@localhost) by localhost.localdomain (8.12.5/8.12.5/Submit) id j37KOr7K003944; Thu, 7 Apr 2005 13:24:53 -0700 Date: Thu, 7 Apr 2005 13:24:53 -0700 From: Craig Addleman To: pgsql-sql@postgresql.org Subject: Re: DROP TYPE without error? Message-ID: <20050407202453.GA3821@craiga-linux.sharechive.com> Mail-Followup-To: Craig Addleman , pgsql-sql@postgresql.org References: <6C0CF58A187DA5479245E0830AF84F4208057C@poweredge.attiksystem.ch> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6C0CF58A187DA5479245E0830AF84F4208057C@poweredge.attiksystem.ch> User-Agent: Mutt/1.4i X-Virus-Scanned: ClamAV 0.83/813/Thu Apr 7 03:20:51 2005 on mariner.sharechive.com X-Virus-Status: Clean X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200504/141 X-Sequence-Number: 21034 I was confronted with a similar problem. I have several scripts which create or modify schemas, and each run in a single transaction. So, dropping a non-existent TYPE will produce a show-stopping error. I wrote this function, and others for various database objects: CREATE OR REPLACE FUNCTION dba_droptype(varchar) RETURNS boolean AS ' DECLARE p_type ALIAS FOR $1; v_exists boolean; BEGIN SELECT INTO v_exists TRUE WHERE EXISTS( SELECT 1 FROM pg_type WHERE typname = p_type::name); IF v_exists THEN RAISE NOTICE ''Dropping TYPE %'', p_type; EXECUTE ''DROP TYPE '' || p_type || '' CASCADE''; END IF; RETURN FOUND; END; ' LANGUAGE 'plpgsql'; COMMENT ON FUNCTION dba_droptype(varchar) IS ' Usage: SELECT dba_drop_type(type_name) Checks for existence of a type and drops it if found. Implements DROP TYPE CASCADE; if a function or other object depends on the type, that object will also be dropped. Returns TRUE if successful, returns FALSE if type is not found.'; -- Craig Addleman DBA ShareChive LLC * Philippe Lang [2005-04-06 05:59]: > Hi, > > Since it is not possible to use CREATE OR REPLACE TYPE, is there a way > of using DROP TYPE on a non-existing type, without causing the entire > script to abort? It may sound crazy to ask for this, but it could be > really useful in my case, where Pl/Pgsql and Pl/Perl code is being > generated automatically, based on data found in a database. > > Thanks > > ----------------- > Philippe Lang > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org