title: How do I trigger or call a function on INSERT to one table based on result of another table?
body: <p>I know how to do this using SQL Server or Sybase, but can't seem to get it right using Postgres 9.4. I have a FUNCTION that is working as intended:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION "public"."welcome"(u_id INT DEFAULT 1234) 
    RETURNS "void" 
    AS $$
    INSERT INTO my_table_1 (title,body,category,user_id,created_at)
    VALUES ('Welcome!', '&lt;some text&gt;','general',u_id,now())
    $$ LANGUAGE sql
  </code></pre>
  
  <p>This FUNCTION works as expected when called as <code>SELECT welcome(1234);</code></p>
  
  <p>However, what I'm trying to do is call or trigger this FUNCTION based on the condition <code>AFTER</code> a new user gets inserted into another table and that user is the first and only user:</p>
  
  <pre><code>INSERT INTO my_table_2 (user_id,name,...) VALUES (5678,'John Doe',...);
  </code></pre>
  
  <p>and the following condition is met:</p>
  
  <pre><code>SELECT COUNT(*) from my_table_2 WHERE &lt;my conditions are met&gt;;
  </code></pre>
  
  <p>returns exactly and only <code>1</code></p>
  
  <p>So, symantically, I'm trying to accomplish:</p>
  
  <p><code>call TRIGGER welcome(1234) AFTER INSERT into my_table_2 where my conditions are met</code></p>
  
  <p>I've seen some examples, and don't quite understand the <code>CREATE FUNCTION x() AS TRIGGER</code> syntax, and it seems that Postgres is steering me in this direction, but examples are lacking in my case.  Help! (and thanks in advance!)</p>