title: Variable substitution in PL/pgSQL
body: <p>I have a select statement that is generated dynamically based on the supplied parameter. The problem is that postgresql always says:</p>
  
  <p><code>argument of WHERE must be type boolean, not type character varying</code> no matter what the parameter is. Did I miss anything?</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION getuid(name character varying) RETURNS integer AS $$
      DECLARE 
          statement varchar;
          uid integer;
      BEGIN
          IF ($1 = '') THEN
              statement := 'TRUE';
              statement := CAST(statement AS BOOLEAN);
          ELSE
              statement := 'users.keywords ILIKE''' || '%' || $1 || '%''';
          END IF;
          SELECT INTO uid id FROM users WHERE "statement";
          RETURN uid;
      END;
  $$ LANGUAGE plpgsql
  </code></pre>