I am trying to create a function in postgreSQL where I need to input multiple rows, and upon insert, I need to get the id of each row so that I can use it later in the function to be inserted into another table.
The function I have below is partially what I am working with. I have wrapped it with a WITH clause so that I can use the webpage_ids later.
create or replace function insert_webpages (query_domain_id int, query_page_titles text[], query_page_slugs text[])
returns table (new_webpage_id int[]) as
$$
DECLARE new_webpage_id int[];
BEGIN
WITH webpage_rows AS (
INSERT INTO webpages (domain_id, title, slug)
VALUES (query_domain_id, unnest(query_page_titles), unnest(query_page_slugs))
RETURNING id into new_webpage_id
)
...
The problem that I am currently facing is that I know that the last line does not input the all the ids into the new_webpage_id array.
So how do I fix it such that it inputs each id of the newly inputed row into the new_webpage_id array?