X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BE327D98B3 for ; Tue, 27 Sep 2005 11:04:15 -0300 (ADT) 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 84680-03 for ; Tue, 27 Sep 2005 14:04:04 +0000 (GMT) Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by svr1.postgresql.org (Postfix) with ESMTP id 99565D9921 for ; Tue, 27 Sep 2005 11:04:05 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id BAC6C31F51; Tue, 27 Sep 2005 16:04:08 +0200 (MET DST) From: Chris Browne X-Newsgroups: pgsql.sql Subject: Re: add column if doesn't exist Date: Tue, 27 Sep 2005 09:34:07 -0400 Organization: cbbrowne Computing Inc Lines: 65 Message-ID: <60d5muk52o.fsf@dba2.int.libertyrms.com> References: <200509270216.05141.peter_e@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Complaints-To: usenet@news.hub.org User-Agent: Gnus/5.1007 (Gnus v5.10.7) XEmacs/21.4.17 (Jumbo Shrimp, linux) Cancel-Lock: sha1:FLaF5gGVknFVvu2LZr8xko1dzV0= To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.093 required=5 tests=[AWL=0.093] X-Spam-Level: X-Archive-Number: 200509/266 X-Sequence-Number: 22813 bmetcalf@nortel.com ("Brandon Metcalf") writes: > p == peter_e@gmx.net writes: > > p> Brandon Metcalf wrote: > p> > Is there a way to check for the existence of a column in a table > p> > other than, say, doing a SELECT on that column name and checking the > p> > output? > > p> SELECT * FROM information_schema.columns; > > p> Customize to taste. > > > Yes, that's what I'm looking for. Thanks. > > Now, is there a way to mix PostgreSQL commands and SQL and do > something like > > ALTER TABLE foo ADD COLUMN bar WHERE EXISTS(SELECT * FROM > information_schema.columns WHERE ...) > > ? I set up a stored procedure to do this for Slony-I... Replace @NAMESPACE@ with your favorite namespace, and slon_quote_brute can likely be treated as an identity function unless you use silly namespace names :-). create or replace function @NAMESPACE@.add_missing_table_field (text, text, text, text) returns bool as ' DECLARE p_namespace alias for $1; p_table alias for $2; p_field alias for $3; p_type alias for $4; v_row record; v_query text; BEGIN select 1 into v_row from pg_namespace n, pg_class c, pg_attribute a where @NAMESPACE@.slon_quote_brute(n.nspname) = p_namespace and c.relnamespace = n.oid and @NAMESPACE@.slon_quote_brute(c.relname) = p_table and a.attrelid = c.oid and @NAMESPACE@.slon_quote_brute(a.attname) = p_field; if not found then raise notice ''Upgrade table %.% - add field %'', p_namespace, p_table, p_field; v_query := ''alter table '' || p_namespace || ''.'' || p_table || '' add column ''; v_query := v_query || p_field || '' '' || p_type || '';''; execute v_query; return ''t''; else return ''f''; end if; END;' language plpgsql; comment on function @NAMESPACE@.add_missing_table_field (text, text, text, text) is 'Add a column of a given type to a table if it is missing'; -- output = ("cbbrowne" "@" "ntlug.org") http://cbbrowne.com/info/sgml.html "The surest sign that intelligent life exists elsewhere in the universe is that it has never tried to contact us." -- Calvin and Hobbes