title: What is the difference between prepared statements and SQL or PL/pgSQL functions, in terms of their purpose?
body: <p>In PostgreSQL, what is the difference between a prepared statement and a SQL or PL/pgSQL function, in terms of their purposes, advantages and disadvantages? When shall we use which?</p>
  
  <p>In this very simple example, do they work the same, correct? </p>
  
  <pre><code>CREATE TABLE foo (id INT, name VARCHAR(80)); 
  
  CREATE FUNCTION myfunc1(INT, VARCHAR(80)) RETURNS void AS ' 
  INSERT INTO foo VALUES ($1, $2);
  ' LANGUAGE SQL; 
  
  SELECT myfunc1(3, 'ben');
  
  CREATE FUNCTION myfunc2(INT, VARCHAR(80)) RETURNS void AS ' 
  BEGIN
  INSERT INTO foo VALUES ($1, $2);
  END' LANGUAGE plpgsql; 
  
  SELECT myfunc2(3, 'ben');
  
  PREPARE fooplan (INT, VARCHAR(80)) AS
      INSERT INTO foo VALUES($1, $2);
  PREPARE
  
  EXECUTE fooplan(3, 'ben');
  </code></pre>