CREATE OR REPLACE FUNCTION timestamp_update() 
RETURNS trigger AS
$$
declare
v_upd_string text;
v_sqlstate text;
v_message text;
v_context text;

BEGIN
	v_upd_string := 'UPDATE ' || TG_TABLE_SCHEMA || '.' || TG_TABLE_NAME;
	
	IF (TG_OP = 'INSERT') THEN
		v_upd_string := v_upd_string || ' SET create_timestamp = ''' || now() || ''', ';
		v_upd_string := v_upd_string || ' update_timestamp = ''' || now() || ''' where syspk = ' || new.syspk;
	ELSEIF (TG_OP = 'UPDATE') THEN
		v_upd_string := v_upd_string || ' SET update_timestamp = ''' || now() || ''' where syspk = ' || old.syspk;
	END IF;
	
	execute (v_upd_string);
	
	RETURN new;
	
	EXCEPTION WHEN OTHERS THEN
		GET STACKED DIAGNOSTICS
		v_sqlstate = returned_sqlstate,
		v_message = message_text,
		v_context = pg_exception_context;
		RETURN NULL;
END
$$  LANGUAGE plpgsql;
