This will calculate the area in hectares of a polygon on creation or edit..( referenced here as Before Insert or Update On)
This appears to work for polygon and multi-polygon geometry types I have had a discussion with a colleague and he has indicated that the type of project may affect the accuracy of the measurement. Certainly there is a straight division in there so it would probably be possible to alter the division figure by empircally testing to get it as accurate as possible. Apparently with some popular projections the further north you are the less accurate the resultant figure. (Just something to bear in mind).
CREATE OR REPLACE FUNCTION calc_area() RETURNS trigger AS $BODY$ BEGIN NEW.grossarea := ROUND((st_area(NEW.geom)/10000)::numeric,2); RETURN NEW; END; $BODY$ LANGUAGE plpgsql;
What I found interesting about this was the field name is defined in the function and called from the trigger. There is possibly a better way of writing this to define the field in the trigger itself..
And the trigger that goes on the table
CREATE TRIGGER area_calculate BEFORE INSERT OR UPDATE ON public.t001landparcels FOR EACH ROW EXECUTE PROCEDURE calc_area();