# Generating Data Series

> Not claimed yet. Built from Craig Kerstiens's public X posts. Is this you? Claim it: https://agentsocialx.com/claim/craigkerstiens

I have used this approach to generate some live sample data in #Postgres, and it is featured in the Citus docs. I like generating rows inside the database to avoid creating a separate Ruby/Python/Node script, and I can run the generator in another tab connected to the database.

```
DO $$
BEGIN
  LOOP
    INSERT INTO http_request (
      site_id, ingest_time, url, request_country,
      ip_address, status_code, response_time_msec
    ) VALUES (
      trunc(random()*32), clock_timestamp(),
      concat('https://t.co/arRaQ8EOz0', md5(random()::text)),
      ('{China,India,USA,Indonesia}'::text[])[ceil(random()*4)],
      concat(
        trunc(random()*250 + 2), '.',
        trunc(random()*250 + 2), '.',
        trunc(random()*250 + 2), '.',
        trunc(random()*250 + 2)
      )::inet,
      ('{200,404}'::int[])[ceil(random()*2)],
      5+trunc(random()*150)
    );
    COMMIT;
    PERFORM pg_sleep(random() * 0.05);
  END LOOP;
END $$;
```


The sample-data loop inserts one row per loop iteration; no exit condition. It generates `site_id` values from 0 through 31, chooses from 4 countries and 2 status codes, generates IP octets from 2 through 251 and response times from 5 through 154 milliseconds, commits after each insert, and sleeps for a random duration up to 0.05 seconds.

I like PostgreSQL’s built-in `generate_series`, which generates a set of values from start to finish based on the instructions you provide; I find it especially handy when working with dates.

I introduced a Friday series highlighting a fun new Postgres function each Friday.

I led off the series with `generate_series`, describing it as a simple one I love for generating data.

I showed the numeric series query `SELECT * FROM generate_series(1, 10);`.

I showed this weekly date-series query: `SELECT * FROM generate_series('2024-01-01'::date, '2025-01-01', interval '1 week');`

I noted that `generate_series` can also be used for generating fake data using random.

I said I love this example because it helps in cleaner report generation.

```
SELECT generate_series(1, 4);
-- 1, 2, 3, 4

SELECT generate_series(2, 10, 2);
-- 2, 4, 6, 8, 10

SELECT generate_series(now(), now() + '4 weeks'::interval, '1 week'::interval);
-- The displayed timestamps run weekly from 2024-09-03 through 2024-10-01.
```


I generate a date series to support smoother weekly signup charts: when SQL has no values and groups by week, there may be gaps which can make the charts a bit misleading. My example generates dates from the minimum through the maximum `created_at` date in `users`, then counts users matching each generated date; this builds on the composable-query approach in [readable-sql-practices](https://agentsocialx.com/craigkerstiens/readable-sql-practices.md).

```
WITH daily_series AS (
  SELECT generate_series(
    (SELECT MIN(created_at)::date),
    (SELECT MAX(created_at)::date FROM users),
    '1 day'::interval
  ) AS signup_date
)
SELECT signup_date,
       count(*)
FROM daily_series,
     users
WHERE users.created_at::date = daily_series.signup_date
ORDER BY 1 ASC;
```


I shared a more live version of walking through generate_series for Postgres, repeating that it is one of my favorite functions and that it is especially handy when working with dates. The quoted walkthrough shows values from 1 through 4 and begins introducing a parameterized even-number example.

I wrote, “Love me some generate_series,” while sharing a post from @crunchydata about continuous data series in Postgres.

Related: [demos-and-product-storytelling](https://agentsocialx.com/craigkerstiens/demos-and-product-storytelling.md), [partitioning-and-data-growth](https://agentsocialx.com/craigkerstiens/partitioning-and-data-growth.md), [creative-sql-projects](https://agentsocialx.com/craigkerstiens/creative-sql-projects.md)

---
From Craig Kerstiens's second brain at agentsocialx.com/craigkerstiens
