forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.sql
76 lines (60 loc) · 2 KB
/
boot.sql
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
-- Create transactions table
CREATE UNLOGGED TABLE transaction (
customer_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
type CHAR(1) NOT NULL,
description TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Create customer table
CREATE UNLOGGED TABLE customer (
id INTEGER NOT NULL,
balance BIGINT NOT NULL,
credit INTEGER NOT NULL
);
-- Create default customer balance
INSERT INTO
"customer" ("id", "balance", "credit")
VALUES
(1, 0, 100000);
INSERT INTO
"customer" ("id", "balance", "credit")
VALUES
(2, 0, 80000);
INSERT INTO
"customer" ("id", "balance", "credit")
VALUES
(3, 0, 1000000);
INSERT INTO
"customer" ("id", "balance", "credit")
VALUES
(4, 0, 10000000);
INSERT INTO
"customer" ("id", "balance", "credit")
VALUES
(5, 0, 500000);
-- create indexes
CREATE INDEX idx_transaction_createdat_1 ON transaction (customer_id, created_at DESC) WHERE customer_id = 1;
CREATE INDEX idx_transaction_createdat_2 ON transaction (customer_id, created_at DESC) WHERE customer_id = 2;
CREATE INDEX idx_transaction_createdat_3 ON transaction (customer_id, created_at DESC) WHERE customer_id = 3;
CREATE INDEX idx_transaction_createdat_4 ON transaction (customer_id, created_at DESC) WHERE customer_id = 4;
CREATE INDEX idx_transaction_createdat_5 ON transaction (customer_id, created_at DESC) WHERE customer_id = 5;
-- Create reconcile balance trigger
CREATE
OR REPLACE FUNCTION reconcile_customer_balance () RETURNS TRIGGER LANGUAGE PLPGSQL AS $$ DECLARE cbalance INT; ccredit INT;
BEGIN
SELECT balance, credit FROM customer c WHERE id = NEW.customer_id INTO cbalance, ccredit;
IF NEW.type = 'd' THEN
NEW.amount = NEW.amount * -1;
END IF;
IF cbalance+NEW.amount+ccredit < 0 THEN
RAISE EXCEPTION 'no credit';
END IF;
UPDATE customer SET balance = balance + NEW.amount WHERE id = NEW.customer_id;
RETURN NEW;
END;
$$;
CREATE TRIGGER reconcile_customer_balance
AFTER
INSERT
ON transaction FOR EACH ROW EXECUTE PROCEDURE reconcile_customer_balance();