PostgreSQL Returning id into array

The values expression is invalid, set-returning functions (i.e. unnest) are not allowed there. The CTE does not need an into clause inside but a select statement that follows it. Try

WITH webpage_rows(wr_id) AS
(
  INSERT INTO webpages (domain_id, title, slug)
  select query_domain_id, unnest(query_page_titles), unnest(query_page_slugs)
  returning id
)
select array_agg(wr_id) into new_webpage_id from webpage_rows;

Unrelated but I do not think that having a variable and return table column by the same name is a good idea.


Try this :

  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 -- id is to be replaced by the column of the table webpages and which must be aggregated into the array new_webpage_id
  )
SELECT array_agg(new_webpage_id)
  INTO new_webpage_id
  FROM webpage_rows