CREATE OR REPLACE FUNCTION fun_test(v_id int, v_remark text) RETURNS integer AS$BODY$declare o_id int;begin perform 1 from foo where id=v_id and remark = v_remark ;if not found then insert into foo(remark) values (v_remark) returning id into o_id;return o_id;else update foo set id=v_id, remark=v_remark where id=v_id and remark = v_remark returning id into o_id;return o_id ;end if;exception when others thenraise exception 'exec fun fun_test error,PLZ contact DBA!';return 999;end;$BODY$ LANGUAGE plpgsql VOLATILE COST 100;ALTER FUNCTION fun_test(int, text) OWNER TO postgres;参考: http://my.oschina.net/Kenyon/blog/54376 http://my.oschina.net/Kenyon/blog/108303