1.76. logswitch_weekly( )

Function Properties

Language: PLPGSQL

Return Type: integer

logswitch_weekly() Ensure a logswitch is done at least weekly

DECLARE
	v_now			timestamp;
	v_now_dow		int4;
	v_auto_dow		int4;
	v_auto_time		time;
	v_auto_ts		timestamp;
	v_lastrun		timestamp;
	v_laststart		timestamp;
	v_days_since	int4;
BEGIN
	-- ----
	-- Check that today is the day to run at all
	-- ----
	v_auto_dow := registry_get_int4(
			'logswitch_weekly.dow', 0);
	v_now := "pg_catalog".now();
	v_now_dow := extract (DOW from v_now);
	if v_now_dow <> v_auto_dow then
		perform registry_set_timestamp(
				'logswitch_weekly.lastrun', v_now);
		return 0;
	end if;

	-- ----
	-- Check that the last run of this procedure was before and now is
	-- after the time we should automatically switch logs.
	-- ----
	v_auto_time := registry_get_text(
			'logswitch_weekly.time', '02:00');
	v_auto_ts := current_date + v_auto_time;
	v_lastrun := registry_get_timestamp(
			'logswitch_weekly.lastrun', 'epoch');
	if v_lastrun >= v_auto_ts or v_now < v_auto_ts then
		perform registry_set_timestamp(
				'logswitch_weekly.lastrun', v_now);
		return 0;
	end if;

	-- ----
	-- This is the moment configured in dow+time. Check that the
	-- last logswitch was done more than 2 days ago.
	-- ----
	v_laststart := registry_get_timestamp(
			'logswitch.laststart', 'epoch');
	v_days_since := extract (days from (v_now - v_laststart));
	if v_days_since < 2 then
		perform registry_set_timestamp(
				'logswitch_weekly.lastrun', v_now);
		return 0;
	end if;

	-- ----
	-- Fire off an automatic logswitch
	-- ----
	perform logswitch_start();
	perform registry_set_timestamp(
			'logswitch_weekly.lastrun', v_now);
	return 1;
END;