At first you need to declare language :
$ createlang plpgsql database
Then connect on postgres :
database=# CREATE FUNCTION a_function () RETURNS int4 AS '
database'# DECLARE
database'# an_integer int4;
database'# BEGIN
database'# an_integer := 10 * 10;
database'# return an_integer;
database'# END;
database'# ' LANGUAGE 'plpgsql';
Your first function is now created. You can use it like this :
SELECT a_function() AS output;
Here an exemple of a function :
CREATE OR REPLACE FUNCTION b_function() RETURNS SETOF TEXT AS '
DECLARE
element_id int4;
elmt_id0 int4;
elmt_id1 int4;
element record;
output text;
cur_element CURSOR for SELECT * FROM element;
BEGIN
OPEN cur_element;
LOOP
FETCH cur_element INTO element_id, elmt_id0, elmt_id1;
IF NOT FOUND THEN
EXIT;
END IF;
DELETE FROM alpha WHERE element_id=element_id;
DELETE FROM country WHERE element_id=element_id;
FOR element in SELECT * FROM element WHERE elmt_id=elmt_id0 OR elmt_id=elmt_id1 LOOP
output := element.element_home_dir || element.element_uri;
return next output;
END LOOP;
END LOOP;
CLOSE cur_element;
RETURN;
END;
' LANGUAGE 'plpgsql';
SELECT * from b_function() GROUP BY b_function;