Insert on conflict return false otherwise true in single query in stored function
You can handle exceptions.

create table test (id int primary key, data text);

insert into test values (1,'a'), (2,'b'), (3, 'c');

create or replace function test_error(_id int, _data text)
returns boolean as $res$
declare res boolean;
begin
    res = true;
    begin
        -- uncomment to check other exception 
        --select 1 / 0;
        insert into test values (_id, _data);
    exception
    when unique_violation then
        res = false;
    when others then
        raise exception 'Other exception';
    end;
    return res;
end;
$res$ language plpgsql;




select test_error(4, 'd');

| test_error |
| :--------- |
| t          |


If you try to insert a duplicate key:

select test_error(1, 'n');

| test_error |
| :--------- |
| f          |


db&lt;>fiddle here