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

8.77. logswitch_start()

Function Properties

PLPGSQLinteger
logswitch_start() Initiate a log table switch if none is in progress
    DECLARE
    	v_current_status	int4;
    BEGIN
    	-- ----
    	-- Grab the central configuration lock to prevent race conditions
    	-- while changing the sl_log_status sequence value.
    	-- ----
    	lock table sl_config_lock;
    
    	-- ----
    	-- Get the current log status.
    	-- ----
    	select last_value into v_current_status from sl_log_status;
    
    	-- ----
    	-- status = 0: sl_log_1 active, sl_log_2 clean
    	-- Initiate a switch to sl_log_2.
    	-- ----
    	if v_current_status = 0 then
    		perform "pg_catalog".setval('sl_log_status', 3);
    		perform registry_set_timestamp(
    				'logswitch.laststart', now()::timestamp);
    		raise notice 'Slony-I: Logswitch to sl_log_2 initiated';
    		return 2;
    	end if;
    
    	-- ----
    	-- status = 1: sl_log_2 active, sl_log_1 clean
    	-- Initiate a switch to sl_log_1.
    	-- ----
    	if v_current_status = 1 then
    		perform "pg_catalog".setval('sl_log_status', 2);
    		perform registry_set_timestamp(
    				'logswitch.laststart', now()::timestamp);
    		raise notice 'Slony-I: Logswitch to sl_log_1 initiated';
    		return 1;
    	end if;
    
    	raise exception 'Previous logswitch still in progress';
    END;

Google
Contact me at cbbrowne@acm.org