You can do this in a single
insert ... select statement:
insert into form (versionnumber, name, state, timestamp) select coalesce(max(versionnumber), 0) + 1, $1, 1, now() from form where name = $1;
The logic is to search for the last version number of the form in the table (with a default of
0), and increment it.
$1 is the parameter to the query, that should be given the form name. I doubt that you need a trigger to do what you want.
CLICK HERE to find out more related problems solutions.