title: execute insert statement only if a certain condition is met
body: <p>I'd like to run an insert statement only if a certain condition is met. Is there a way to do that in pure sql?</p>
  
  <p>An example of what I'd like to do in pl/pgsql:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION set_search_lock (
      userid     IN   integer,
  )
      RETURNS boolean AS $body$
  
  
  DECLARE
  BEGIN
  
      PERFORM 'A' FROM LOCK_TABLE WHERE USERID = userid LIMIT 1;
  
      IF NOT FOUND THEN
          INSERT INTO LOCK_TABLE (USERID) VALUES (userid);
          RETURN true;
      ELSE
          RETURN false;
      END IF;
  
  
  END;
  $body$
  LANGUAGE PLPGSQL;
  </code></pre>