Christopher B. Browne's Home Page
cbbrowne@acm.org

1.123. tableaddkey( text )

Function Properties

PLPGSQLtext
tableAddKey (tab_fqname) - if the table has not got a column of the form _Slony-I_<clustername>_rowID, then add it as a bigint, defaulted to nextval() for a sequence created for the cluster.
    declare
    	p_tab_fqname	alias for $1;
    	v_tab_fqname_quoted	text default '';
    	v_attkind		text default '';
    	v_attrow		record;
    	v_have_serial	bool default 'f';
    begin
    	v_tab_fqname_quoted := slon_quote_input(p_tab_fqname);
    	--
    	-- Loop over the attributes of this relation
    	-- and add a "v" for every user column, and a "k"
    	-- if we find the Slony-I special serial column.
    	--
    	for v_attrow in select PGA.attnum, PGA.attname
    			from "pg_catalog".pg_class PGC,
    			    "pg_catalog".pg_namespace PGN,
    				"pg_catalog".pg_attribute PGA
    			where slon_quote_brute(PGN.nspname) || '.' ||
    			    slon_quote_brute(PGC.relname) = v_tab_fqname_quoted
    				and PGN.oid = PGC.relnamespace
    				and PGA.attrelid = PGC.oid
    				and not PGA.attisdropped
    				and PGA.attnum > 0
    			order by attnum
    	loop
    		if v_attrow.attname = '_Slony-I_schemadoc_rowID' then
    		    v_attkind := v_attkind || 'k';
    			v_have_serial := 't';
    		else
    			v_attkind := v_attkind || 'v';
    		end if;
    	end loop;
    	
    	--
    	-- A table must have at least one attribute, so not finding
    	-- anything means the table does not exist.
    	--
    	if not found then
    		raise exception 'Slony-I: tableAddKey(): table % not found', v_tab_fqname_quoted;
    	end if;
    
    	--
    	-- If it does not have the special serial column, we
    	-- have to add it. This will be only half way done.
    	-- The function to add the table to the set must finish
    	-- these definitions with NOT NULL and UNIQUE after
    	-- updating all existing rows.
    	--
    	if not v_have_serial then
    		execute 'lock table ' || v_tab_fqname_quoted ||
    			' in access exclusive mode';
    		execute 'alter table only ' || v_tab_fqname_quoted ||
    			' add column "_Slony-I_schemadoc_rowID" bigint;';
    		execute 'alter table only ' || v_tab_fqname_quoted ||
    			' alter column "_Slony-I_schemadoc_rowID" ' ||
    			' set default "pg_catalog".nextval(''sl_rowid_seq'');';
    
    		v_attkind := v_attkind || 'k';
    	end if;
    
    	--
    	-- Return the resulting Slony-I attkind
    	--
    	return v_attkind;
    end;

Google

If this was useful, let others know by an Affero rating

Contact me at cbbrowne@acm.org