title: Create a temp table (if not exists) for use into a custom procedure
body: <p>I'm trying to get the hang of using temp tables:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION test1(user_id BIGINT) RETURNS BIGINT AS
  $BODY$
  
  BEGIN
    create temp table temp_table1
    ON COMMIT DELETE ROWS
  
    as SELECT table1.column1, table1.column2 
    FROM table1
    INNER JOIN -- ............
  
    if exists (select * from temp_table1) then
      -- work with the result
      return 777;
    else 
      return 0;
    end if;
  
  END;
  $BODY$
  LANGUAGE plpgsql;
  </code></pre>
  
  <p>I want the row <code>temp_table1</code> to be deleted immediately or as soon as possible, that's why I added <code>ON COMMIT DELETE ROWS</code>. Obviously, I got the error:</p>
  
  <pre><code>ERROR:  relation "temp_table1" already exists
  </code></pre>
  
  <p>I tried to add <code>IF NOT EXISTS</code> but I couldn't, I simply couldn't find working example of it that would be the I'm looking for.</p>
  
  <p>Your suggestions? </p>