WITH new_tbl_main( id ) AS (
INSERT INTO tbl_main ( institution_id ) VALUES ( 38)
RETURNING id )
INSERT INTO tbl_child1 ( tbl_main_id )
SELECT new_tbl_main.id FROM new_tbl_main
RETURNING tbl_main_id
INSERT INTO tbl_child1 ( tbl_main_id )
SELECT new_tbl_main.id FROM new_tbl_main
WITH new_tbl_main( id ) AS (
INSERT INTO tbl_main ( institution_id ) VALUES ( 38)
RETURNING id
), chi2 as (
INSERT INTO tbl_child1 ( tbl_main_id )
SELECT new_tbl_main.id FROM new_tbl_main
)
INSERT INTO tbl_child1 ( tbl_main_id )
SELECT new_tbl_main.id FROM new_tbl_main
RETURNING tbl_main_id