liveBook · Manning

Generate_series function

As explained in Just Use Postgres!

The generate_series function is a PostgreSQL set-returning function that produces sequential numeric or timestamp values over a specified range.

generate_series is a PostgreSQL set-returning function for producing sequential integers or timestamps across an inclusive range. Its optional step defaults to 1. section 1.5

Generate mock stock trades

SELECT id, random(1,10) AS buyer_id, (array['AAPL','F','DASH'])[random(1,3)] AS symbol, random(1,20) AS order_quantity, round(random(10.00,20.00), 2) AS bid_price, now() AS order_time FROM generate_series(1,1000) AS id; Use the generated rows directly in an INSERT to populate a trades table with sample data. section 1.5

Listing 1.7 Inserting 1,000 sample trades
INSERT INTO trades (id, buyer_id, symbol, 
    order_quantity, bid_price, order_time)
SELECT
    id,  
    random(1,10) as buyer_id,
    (array['AAPL','F','DASH'])[random(1,3)] as symbol,
    random(1,20) as order_quantity,
    round(random(10.00,20.00), 2) as bid_price,
    now() as order_time
FROM generate_series(1,1000) AS id;

Shows a complete INSERT query that combines generated identifiers with random buyers, symbols, quantities, prices, and timestamps. section 1.5

How Just Use Postgres! covers it

How deep it goes
Defines integer ranges, aliases output columns, and demonstrates bulk insertion, while only noting timestamp support.
Examples
Uses integer sequences, aliased IDs, random buyers, stock symbols, and 1,000 generated trade rows.
What you can do after
Apply it via SQL to create sequential values and populate tables with varied sample records.
How it connects
Connects set-returning functions with SQL queries, PostgreSQL tables, random values, arrays, and mock data.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.