history.sql30 lines, 1.6 KB
| 1 | -- Two years of history, like a small production database: a deposit every |
| 2 | -- two minutes, a million entries. One deposit in five goes to a busy shop, |
| 3 | -- the wallet in :wallet, the rest to a thousand other customers. For |
| 4 | -- development only, as the superuser. |
| 5 | BEGIN; |
| 6 | -- Our balance triggers would check a million rows one at a time, at COMMIT. |
| 7 | -- These balance by construction, and the reconciliation checks every one of |
| 8 | -- them anyway: so for this transaction only, no triggers. |
| 9 | SET LOCAL session_replication_role = replica; |
| 10 | |
| 11 | INSERT INTO accounts (kind, owner_id, name, currency, normal_balance) |
| 12 | SELECT 'wallet', gen_random_uuid(), 'USD wallet', 'USD', 'credit' FROM generate_series(1, 1000); |
| 13 | |
| 14 | CREATE TEMP TABLE history ON COMMIT DROP AS |
| 15 | SELECT gen_random_uuid() AS tx, now() - n * interval '2 minutes' AS at, 100 + n % 9900 AS amount, |
| 16 | CASE WHEN n % 5 = 0 THEN :'wallet'::uuid ELSE ids[1 + n % cardinality(ids)] END AS wallet |
| 17 | FROM generate_series(525600, 1, -1) AS n, |
| 18 | (SELECT array_agg(id) AS ids FROM accounts WHERE kind = 'wallet' AND id <> :'wallet') AS customers; |
| 19 | |
| 20 | INSERT INTO transactions (id, description, created_at) SELECT tx, 'deposit', at FROM history; |
| 21 | INSERT INTO entries (transaction_id, account_id, direction, amount, currency, created_at) |
| 22 | SELECT tx, e.account, e.direction, amount, 'USD', at |
| 23 | FROM history, |
| 24 | (SELECT id FROM accounts WHERE kind = 'system' AND name = 'settlement' AND currency = 'USD') AS s, |
| 25 | LATERAL (VALUES (s.id, 'debit'::entry_direction), (wallet, 'credit')) AS e (account, direction) |
| 26 | ORDER BY at; |
| 27 | COMMIT; |
| 28 | |
| 29 | -- Fresh statistics for the planner, and a visibility map for index-only scans. |
| 30 | VACUUM ANALYZE accounts, transactions, entries; |