In PG functions, the whole body of the function is enclosed in apostrophes, so you have to escape then within the function body.
It can get pretty nasty, so I wrote a little table about that when I revamped the PL/pgSQL documentation and wrote the oracle PL/SQL porting guide that are part of the PostgreSQL documentation:
http://www.postgresql.org/docs/view.php?version=7.3&idoc=0&file=plpgsql.html
http://www.postgresql.org/docs/view.php?version=7.3&idoc=0&file=plpgsql-porting.html
-Roberto