CREATE OR REPLACE FUNCTION food600.semafor_zapni(
	ip_hod character DEFAULT 0,
	ip_kto character DEFAULT ''::bpchar,
	OUT op_hod character)
    RETURNS character
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE li_semafor integer default 0;
li_pocet integer default 0;
li_stav boolean ;

BEGIN
select count(*) into li_pocet from food600.semafor where n_semafor=ip_hod;
--insert into food600.semafor(n_semafor) values (ip_hod);
IF li_pocet=0 THEN
 insert into food600.semafor(n_semafor) values (ip_hod);	
ELSE
END IF;

select min(i_semafor) into li_semafor from food600.semafor where n_semafor=ip_hod;

select pg_try_advisory_lock(li_semafor) into li_stav;

if li_stav THEN
 op_hod = '1';
 update food600.semafor set kedy = now(),kto = ip_kto where i_semafor = li_semafor;
else
 op_hod = '0';
END IF;

END
$BODY$;
