How to fan out rows with a specified range of values? I have a table which essentially contains 3 columns: ID, FIRST, LAST. They're all integers.

I'm wondering if there's a way to use generate_series() to get a query result where for each row in the original table, there are many rows containing numbers between FIRST and LAST?

E.g. for a row with data (42, 5, 8) the output contains rows (42, 5), (42, 6), (42, 7) and (42, 8).
This can be achieved using generate_series()

with test_data (id, "first", "last") as (
   values (42, 5, 8), 
          (43, 6, 9)
)
select td.id, g.nr
from test_data td
  cross join lateral generate_series(td."first", td."last", 1) as g(nr)
order by td.id, g.nr;


Online example: https://rextester.com/TWE47868