Plpgsql function basics

We previously learnt how to write the body of plpgsql. We can turn that into a function by replacing DO with AS.

Before AS we must write CREATE OR REPLACE FUNCTION then the function name with parameters. Then the type it RETURNS and the LANGUAGE we are using.

CREATE OR REPLACE FUNCTION something()
  RETURNS text
  LANGUAGE plpgsql
AS $body$
  ...
$body$;

If we use parameters we must define the name and then the type:

CREATE OR REPLACE FUNCTION something(mytext text)
  RETURNS text
  LANGUAGE plpgsql
AS $body$
BEGIN
  return mytext;
END;
$body$;

Call it via select something('hello').;

postgresql

Edit on github
comments powered by Disqus
Click me