How do I insert a record and put the ID into an existing record in another table? I want to insert a record in one table and save the resuting ID in a record I already have in another table. Which sounds easy.

Simplistic example: 

create table aaa (id serial primary key, foo int);
create table foos (id serial primary key, foo int, aaa_id int);


Imagine foos has rows which I want to insert in aaa.  But I want a record of aaa.id stored in foos.aaa_id. 

Heres some code that definitely does not work:

with rows as (
    insert into aaa (foo) 
        select foo from foos 
        returning foos.id as foos_id, aaa.id as aaa_id 
)
update foos
    set aaa_id = (select aaa_id from rows where foos_id = foos.id);


I can't find a way of getting the "returning" clause to return anything other than records from the table it inserted.

Surely there must be a simple way to do this?
You can use DO block:

DO $$
DECLARE
    rows CURSOR FOR
        SELECT * FROM foos;
BEGIN
    FOR r IN rows LOOP
        WITH _ins AS (
            INSERT INTO aaa (foo) 
            VALUES (r.foo)
            RETURNING aaa.id AS aaa_id 
        )
        UPDATE foos
            SET aaa_id = (SELECT aaa_id FROM _ins)
            WHERE id = r.id;
    END LOOP;
END$$;