CREATE OR REPLACE FUNCTION ptab_upd_trig()
RETURNS TRIGGER AS $$
DECLARE
tablename text;
a int;
b int;
c int;
BEGIN
	a = extract(year from OLD.a) - 1900 + 1;
	b = ceil(OLD.b / 7::float);
	if (b > 40) then b = 40; end if;
	c = ceil(OLD.c / 50::float);
	if (c > 2) then c = 2; end if;
	tablename = 'ptab_' || a || '_' || b;
	tablename = tablename || '_' || c;
	EXECUTE 'DELETE from ' || tablename || ' WHERE b = $1 and c = $2' USING OLD.b, OLD.c;

	a = extract(year from NEW.a) - 1900 + 1;
	b = ceil(NEW.b / 7::float);
	if (b > 40) then b = 40; end if;
	c = ceil(NEW.c / 50::float);
	if (c > 2) then c = 2; end if;
	tablename = 'ptab_' || a || '_' || b;
	tablename = tablename || '_' || c;
	EXECUTE 'INSERT INTO ' || tablename || ' SELECT $1.*' USING NEW;
	RETURN NULL;
END; $$ LANGUAGE plpgsql;
