CREATE OR REPLACE FUNCTION food600.m_max(
	pridajsa integer,
	riadok character varying)
    RETURNS integer
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE mimax integer default 0;
	naslosa integer default 0;
	pridaj integer default $1;
BEGIN
IF riadok='c_dokl' THEN
	IF pridajsa=0 THEN
		pridaj:=10;
	ELSE
		pridaj:=pridajsa;
	END IF;
ELSE
	IF pridajsa=0 THEN
		pridaj:=1;
	ELSE
		pridaj:=pridajsa;
	END IF;
END IF;
WHILE mimax=0 LOOP
/*	IF GET_LOCK("maxima",5)=1 THEN*/
		SELECT COUNT(*) INTO naslosa FROM food600.maxima WHERE maxima.soubor=riadok;
		IF naslosa=0 THEN 
			INSERT INTO food600.maxima (soubor,i_max) VALUES(riadok,pridaj);
			mimax:=pridaj;
		ELSE
			SELECT i_max INTO naslosa FROM food600.maxima WHERE maxima.soubor=riadok FOR UPDATE;
			mimax:=naslosa+pridaj;
			UPDATE food600.maxima SET i_max=mimax WHERE maxima.soubor=riadok;
		END IF;
/*	END IF;*/
END LOOP;
/*DO RELEASE_LOCK("maxima");*/
RETURN mimax;
END
$BODY$;
