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