PL/PgSQL has a different structure. You can't just copy an SQL function body, declare it language plpgsql
, and expect it to work.
Run this standalone and you'll see:
regress=> CREATE SCHEMA test;
CREATE SCHEMA
regress=> CREATE OR REPLACE FUNCTION test.testXUnitStyle() RETURNS SETOF TEXT AS $$
SELECT ok(4 * 5 = 20, 'math should work in postgres');
$$ LANGUAGE plpgsql;
ERROR: syntax error at or near "SELECT"
LINE 2: SELECT ok(4 * 5 = 20, 'math should work in postgres');
^
If you read the manual for Pl/PgSQL you'll quickly understand why: PL/PgSQL has overall struture of [DECLARE ...] BEGIN ... END;
. You must also use PERFORM
to run a query and discard the result, or RETURN QUERY
if you wish to return the result. So you'd rewrite your code in PL/PgSQL as:
CREATE OR REPLACE FUNCTION test.testXUnitStyle() RETURNS SETOF TEXT AS $$
BEGIN
RETURN QUERY SELECT ok(4 * 5 = 20, 'math should work in postgres');
END;
$$ LANGUAGE plpgsql;