|
| 1 | +-- ========================================================= |
| 2 | +-- Random Test Data Generator for PostgreSQL Backup Testing |
| 3 | +-- ========================================================= |
| 4 | + |
| 5 | +-- Optional: speed up bulk inserts |
| 6 | +SET synchronous_commit = OFF; |
| 7 | +SET maintenance_work_mem = '512MB'; |
| 8 | + |
| 9 | +-- ========================================================= |
| 10 | +-- Drop existing tables (safe reset) |
| 11 | +-- ========================================================= |
| 12 | + |
| 13 | +DROP TABLE IF EXISTS order_items; |
| 14 | +DROP TABLE IF EXISTS orders; |
| 15 | +DROP TABLE IF EXISTS products; |
| 16 | +DROP TABLE IF EXISTS customers; |
| 17 | + |
| 18 | +-- ========================================================= |
| 19 | +-- Create tables |
| 20 | +-- ========================================================= |
| 21 | + |
| 22 | +CREATE TABLE customers ( |
| 23 | + id BIGSERIAL PRIMARY KEY, |
| 24 | + first_name TEXT NOT NULL, |
| 25 | + last_name TEXT NOT NULL, |
| 26 | + email TEXT NOT NULL, |
| 27 | + created_at TIMESTAMP NOT NULL DEFAULT now() |
| 28 | +); |
| 29 | + |
| 30 | +CREATE TABLE products ( |
| 31 | + id BIGSERIAL PRIMARY KEY, |
| 32 | + name TEXT NOT NULL, |
| 33 | + price NUMERIC(10,2) NOT NULL, |
| 34 | + created_at TIMESTAMP NOT NULL DEFAULT now() |
| 35 | +); |
| 36 | + |
| 37 | +CREATE TABLE orders ( |
| 38 | + id BIGSERIAL PRIMARY KEY, |
| 39 | + customer_id BIGINT NOT NULL REFERENCES customers(id), |
| 40 | + order_date TIMESTAMP NOT NULL, |
| 41 | + status TEXT NOT NULL |
| 42 | +); |
| 43 | + |
| 44 | +CREATE TABLE order_items ( |
| 45 | + id BIGSERIAL PRIMARY KEY, |
| 46 | + order_id BIGINT NOT NULL REFERENCES orders(id), |
| 47 | + product_id BIGINT NOT NULL REFERENCES products(id), |
| 48 | + quantity INT NOT NULL, |
| 49 | + unit_price NUMERIC(10,2) NOT NULL |
| 50 | +); |
| 51 | + |
| 52 | +-- ========================================================= |
| 53 | +-- Insert customers |
| 54 | +-- Adjust the number in generate_series for scale |
| 55 | +-- ========================================================= |
| 56 | + |
| 57 | +INSERT INTO customers (first_name, last_name, email, created_at) |
| 58 | +SELECT |
| 59 | + 'First' || gs, |
| 60 | + 'Last' || gs, |
| 61 | + 'user' || gs || '@example.com', |
| 62 | + NOW() - (random() * interval '365 days') |
| 63 | +FROM generate_series(1, 100000) AS gs; |
| 64 | + |
| 65 | +-- ========================================================= |
| 66 | +-- Insert products |
| 67 | +-- ========================================================= |
| 68 | + |
| 69 | +INSERT INTO products (name, price, created_at) |
| 70 | +SELECT |
| 71 | + 'Product ' || gs, |
| 72 | + ROUND((random() * 500 + 5)::numeric, 2), |
| 73 | + NOW() - (random() * interval '365 days') |
| 74 | +FROM generate_series(1, 5000) AS gs; |
| 75 | + |
| 76 | +-- ========================================================= |
| 77 | +-- Insert orders |
| 78 | +-- ========================================================= |
| 79 | + |
| 80 | +INSERT INTO orders (customer_id, order_date, status) |
| 81 | +SELECT |
| 82 | + (random() * 99999 + 1)::BIGINT, |
| 83 | + NOW() - (random() * interval '365 days'), |
| 84 | + (ARRAY['pending','shipped','delivered','cancelled'])[floor(random()*4)+1] |
| 85 | +FROM generate_series(1, 300000); |
| 86 | + |
| 87 | +-- ========================================================= |
| 88 | +-- Insert order items |
| 89 | +-- Each order gets 1–5 items |
| 90 | +-- ========================================================= |
| 91 | + |
| 92 | +INSERT INTO order_items (order_id, product_id, quantity, unit_price) |
| 93 | +SELECT |
| 94 | + o.id, |
| 95 | + (random() * 4999 + 1)::BIGINT, |
| 96 | + (random() * 4 + 1)::INT, |
| 97 | + ROUND((random() * 500 + 5)::numeric, 2) |
| 98 | +FROM orders o |
| 99 | +CROSS JOIN LATERAL generate_series(1, (random()*4 + 1)::INT); |
| 100 | + |
| 101 | +-- ========================================================= |
| 102 | +-- Indexes (important for realistic backup size) |
| 103 | +-- ========================================================= |
| 104 | + |
| 105 | +CREATE INDEX idx_orders_customer ON orders(customer_id); |
| 106 | +CREATE INDEX idx_order_items_order ON order_items(order_id); |
| 107 | +CREATE INDEX idx_order_items_product ON order_items(product_id); |
| 108 | + |
| 109 | +-- ========================================================= |
| 110 | +-- Analyze for realistic planner stats |
| 111 | +-- ========================================================= |
| 112 | + |
| 113 | +ANALYZE; |
| 114 | + |
| 115 | +-- ========================================================= |
| 116 | +-- Summary |
| 117 | +-- ========================================================= |
| 118 | + |
| 119 | +SELECT |
| 120 | + (SELECT count(*) FROM customers) AS customers, |
| 121 | + (SELECT count(*) FROM products) AS products, |
| 122 | + (SELECT count(*) FROM orders) AS orders, |
| 123 | + (SELECT count(*) FROM order_items) AS order_items; |
0 commit comments