How do I end function when X
You can simplify to:

CREATE OR REPLACE FUNCTION pg_temp.insert_stuff(_a1 text, _a2 text, _a3 int)
  RETURNS TABLE (id_i int, id_j int) AS
$func$
BEGIN 
   IF EXISTS (SELECT FROM table_1 WHERE a12 = _a1||a2) THEN  -- see below
      RETURN QUERY
      SELECT -1, NULL;
      -- RAISE EXCEPTION 'Some informative msg here'  -- alternative?
   ELSE
      RETURN QUERY
      SELECT insert_here(_a1, _a2)
           , insert_there(_a3, _a2);
   END IF;
END
$func$  LANGUAGE plpgsql;


Note the clearer (and faster) IF EXISTS. See:


PL/pgSQL checking if a row exists


Either variant might fail "unexpectedly" if either input parameter or the column a12 can be NULL! You would have to define desired behavior.

Subtle difference: Since I replaced your CTE construct with a simple SELECT, the function always returns at least one now, even if either nested function (insert_here() or insert_there()) returns no row. Your version would return nothing in this case, which may be a sneaky bug if either of the nested functions can return no row.

I am assuming Postgres 10 or later (you didn't declare). Else, depending on the undeclared nature of the nested functions, you might need to consider:


What is the expected behaviour for multiple set-returning functions in select clause?