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

8.22. altertableaddtriggers(integer)

Function Properties

PLPGSQLinteger
alterTableAddTriggers(tab_id) Adds the log and deny access triggers to a replicated table.
    declare
    	p_tab_id			alias for $1;
    	v_no_id				int4;
    	v_tab_row			record;
    	v_tab_fqname		text;
    	v_tab_attkind		text;
    	v_n					int4;
    	v_trec	record;
    	v_tgbad	boolean;
    begin
    	-- ----
    	-- Grab the central configuration lock
    	-- ----
    	lock table sl_config_lock;
    
    	-- ----
    	-- Get our local node ID
    	-- ----
    	v_no_id := getLocalNodeId('_schemadoc');
    
    	-- ----
    	-- Get the sl_table row and the current origin of the table. 
    	-- ----
    	select T.tab_reloid, T.tab_set, T.tab_idxname, 
    			S.set_origin, PGX.indexrelid,
    			slon_quote_brute(PGN.nspname) || '.' ||
    			slon_quote_brute(PGC.relname) as tab_fqname
    			into v_tab_row
    			from sl_table T, sl_set S,
    				"pg_catalog".pg_class PGC, "pg_catalog".pg_namespace PGN,
    				"pg_catalog".pg_index PGX, "pg_catalog".pg_class PGXC
    			where T.tab_id = p_tab_id
    				and T.tab_set = S.set_id
    				and T.tab_reloid = PGC.oid
    				and PGC.relnamespace = PGN.oid
    				and PGX.indrelid = T.tab_reloid
    				and PGX.indexrelid = PGXC.oid
    				and PGXC.relname = T.tab_idxname
    				for update;
    	if not found then
    		raise exception 'Slony-I: alterTableAddTriggers(): Table with id % not found', p_tab_id;
    	end if;
    	v_tab_fqname = v_tab_row.tab_fqname;
    
    	v_tab_attkind := determineAttKindUnique(v_tab_row.tab_fqname, 
    						v_tab_row.tab_idxname);
    
    	execute 'lock table ' || v_tab_fqname || ' in access exclusive mode';
    
    	-- ----
    	-- Create the log and the deny access triggers
    	-- ----
    	execute 'create trigger "_schemadoc_logtrigger"' || 
    			' after insert or update or delete on ' ||
    			v_tab_fqname || ' for each row execute procedure logTrigger (' ||
                                   pg_catalog.quote_literal('_schemadoc') || ',' || 
    				pg_catalog.quote_literal(p_tab_id::text) || ',' || 
    				pg_catalog.quote_literal(v_tab_attkind) || ');';
    
    	execute 'create trigger "_schemadoc_denyaccess" ' || 
    			'before insert or update or delete on ' ||
    			v_tab_fqname || ' for each row execute procedure ' ||
    			'denyAccess (' || pg_catalog.quote_literal('_schemadoc') || ');';
    
    	perform alterTableConfigureTriggers (p_tab_id);
    	return p_tab_id;
    end;

Google

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

Contact me at cbbrowne@acm.org