insert ... returning where X hi i have an issue im trying to solve using the following to do a insert statement

begin;
with t1 as (values (2),(3),(4))

,in1 as (insert into table123 (id,val,num)
select nextval('table123_id_seq'),t1.val,42
from t1
on conflict do nothing returning *)

,ins2 as (insert into table456 (id,num,fkey_id)
select nextval('table234_id_seq'),333, (select table123.id from table123 where val = 2)
from in1
on conflict do nothing returning *)
;commit;


what I get is 3 entries into table456 where I want only one

is there a clever way to do returning where X or something similar to avoid the duplication?

currently i workround just divide the insert through 2 queries ins1a with val=2 and ins1b with t1.v
There's an awful lot to be said for keeping things simple: 

begin;

insert into table123 ( id, val, num ) values ( nextval('table123seq'), 2, 42 ); 
insert into table123 ( id, val, num ) values ( nextval('table123seq'), 3, 42 ); 
insert into table123 ( id, val, num ) values ( nextval('table123seq'), 4, 42 ); 

insert into table456 ( id, num, fkey_id ) 
   select next( 'table546seq' ), 333, id 
   from table123 
   where table213.num = 2 ; 

select * from table456 ; 
commit ; 


Alternatively, make your id columns auto-increment and forget all about them entirely. 

If that's not what you're after, then a plain-English description of the Problem you're trying to solve - not the difficulties with your current attempt at solving it - would be a good place to start.