failoverset_int(integer, integer, integer, bigint)

6.65. failoverset_int(integer, integer, integer, bigint)

Function Properties

Language: PLPGSQL

Return Type: integer

FUNCTION failoverSet_int (failed_node, backup_node, set_id, wait_seqno) Finish failover for one set.

declare
	p_failed_node		alias for $1;
	p_backup_node		alias for $2;
	p_set_id			alias for $3;
	p_wait_seqno		alias for $4;
	v_row				record;
	v_last_sync			int8;
begin
	-- ----
	-- Grab the central configuration lock
	-- ----
	lock table sl_config_lock;

	-- ----
	-- Change the origin of the set now to the backup node.
	-- On the backup node this includes changing all the
	-- trigger and protection stuff
	-- ----
	if p_backup_node = getLocalNodeId('_schemadoc') then
		delete from sl_setsync
				where ssy_setid = p_set_id;
		delete from sl_subscribe
				where sub_set = p_set_id
					and sub_receiver = p_backup_node;
		update sl_set
				set set_origin = p_backup_node
				where set_id = p_set_id;

		for v_row in select * from sl_table
				where tab_set = p_set_id
				order by tab_id
		loop
			perform alterTableConfigureTriggers(v_row.tab_id);
		end loop;
		insert into sl_event
				(ev_origin, ev_seqno, ev_timestamp,
				ev_snapshot, 
				ev_type, ev_data1, ev_data2, ev_data3, ev_data4)
				values
				(p_backup_node, "pg_catalog".nextval('sl_event_seq'), CURRENT_TIMESTAMP,
				pg_catalog.txid_current_snapshot(),
				'ACCEPT_SET', p_set_id::text,
				p_failed_node::text, p_backup_node::text,
				p_wait_seqno::text);
	else
		delete from sl_subscribe
				where sub_set = p_set_id
					and sub_receiver = p_backup_node;
		update sl_set
				set set_origin = p_backup_node
				where set_id = p_set_id;
	end if;

	-- update sl_node
	-- 	   set no_active=false WHERE 
	-- 	   no_id=p_failed_node;

	-- Rewrite sl_listen table
	perform RebuildListenEntries();

	-- ----
	-- If we are a subscriber of the set ourself, change our
	-- setsync status to reflect the new set origin.
	-- ----
	if exists (select true from sl_subscribe
			where sub_set = p_set_id
				and sub_receiver = getLocalNodeId(
						'_schemadoc'))
	then
		delete from sl_setsync
				where ssy_setid = p_set_id;

		select coalesce(max(ev_seqno), 0) into v_last_sync
				from sl_event
				where ev_origin = p_backup_node
					and ev_type = 'SYNC';
		if v_last_sync > 0 then
			insert into sl_setsync
					(ssy_setid, ssy_origin, ssy_seqno,
					ssy_snapshot, ssy_action_list)
					select p_set_id, p_backup_node, v_last_sync,
					ev_snapshot, NULL
					from sl_event
					where ev_origin = p_backup_node
						and ev_seqno = v_last_sync;
		else
			insert into sl_setsync
					(ssy_setid, ssy_origin, ssy_seqno,
					ssy_snapshot, ssy_action_list)
					values (p_set_id, p_backup_node, '0',
					'1:1:', NULL);
		end if;
				
	end if;

	return p_failed_node;
end;