Inserting into lots of tables at once

I'm using postgreSQL and I want to insert into about 5 different tables where the returning data of an insert into a table is required in another table, and so on and so forth. E.g. if I insert data into table A, I need the returning id to insert into table B, and the returning id of that for table C, etc. And in some of the tables, I need to sometimes insert multiple rows.

I intend to wrap the entire thing up into a singular function rather than having 5 different queries because I want the entire thing to fail if one insert fails. And having separate queries doesn't allow for this.

Now, I know how to achieve this, but my question is, is this safe to do or are there any issues with this method, given that I have 1000s of users.


Using a function will work well, just as running several INSERT statements in a single transaction or a stack of CTEs using INSERT INTO ... SELECT ... RETURNING ....