-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathpopulation.sql
139 lines (121 loc) · 3.73 KB
/
population.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
DROP DATABASE IF EXISTS "rinhadb";
CREATE DATABASE "rinhadb";
\c rinhadb;
CREATE UNLOGGED TABLE clients (
id INT UNIQUE NOT NULL,
limite INT,
balance INT,
PRIMARY KEY(id)
);
CREATE UNLOGGED TABLE transactions1 (
id SERIAL,
valor INT NOT NULL,
tipo VARCHAR(10),
descricao VARCHAR(10) NOT NULL,
realizada_em timestamptz NULL
);
CREATE UNLOGGED TABLE transactions2 (
id SERIAL,
client_id INT,
valor INT NOT NULL,
tipo VARCHAR(10),
descricao VARCHAR(10) NOT NULL,
realizada_em timestamptz NULL
);
CREATE UNLOGGED TABLE transactions3 (
id SERIAL,
client_id INT,
valor INT NOT NULL,
tipo VARCHAR(10),
descricao VARCHAR(10) NOT NULL,
realizada_em timestamptz NULL
);
CREATE UNLOGGED TABLE transactions4 (
id SERIAL,
client_id INT,
valor INT NOT NULL,
tipo VARCHAR(10),
descricao VARCHAR(10) NOT NULL,
realizada_em timestamptz NULL
);
CREATE UNLOGGED TABLE transactions5 (
id SERIAL,
client_id INT,
valor INT NOT NULL,
tipo VARCHAR(10),
descricao VARCHAR(10) NOT NULL,
realizada_em timestamptz NULL
);
INSERT INTO "clients"("id", "limite", "balance") VALUES
(1, 100000, 0),
(2, 80000, 0),
(3, 1000000, 0),
(4, 10000000, 0),
(5, 500000, 0);
CREATE OR REPLACE FUNCTION FindClient(idClient INT)
RETURNS clients
LANGUAGE sql
AS $$
SELECT * FROM clients WHERE id = idClient FOR UPDATE;
$$
;
CREATE OR REPLACE FUNCTION add_transaction(idClient INT, valor INT, tipo CHAR, descricao VARCHAR(10))
RETURNS INT
LANGUAGE 'plpgsql'
AS $$
DECLARE
novo_saldo INT;
BEGIN
IF tipo = 'c' THEN
UPDATE clients SET balance = balance + valor WHERE id = idClient
RETURNING balance INTO novo_saldo;
ELSE
UPDATE clients SET balance = balance - valor WHERE id = idClient AND balance - valor >= limite * -1
RETURNING balance INTO novo_saldo;
END IF;
IF novo_saldo IS NOT NULL THEN
case idClient
WHEN 1 THEN
INSERT INTO transactions1(valor, tipo, descricao, realizada_em)
VALUES (valor, tipo, descricao, now());
WHEN 2 THEN
INSERT INTO transactions2(valor, tipo, descricao, realizada_em)
VALUES (valor, tipo, descricao, now());
WHEN 3 THEN
INSERT INTO transactions3(valor, tipo, descricao, realizada_em)
VALUES (valor, tipo, descricao, now());
WHEN 4 THEN
INSERT INTO transactions4(valor, tipo, descricao, realizada_em)
VALUES (valor, tipo, descricao, now());
ELSE
INSERT INTO transactions5(valor, tipo, descricao, realizada_em)
VALUES (valor, tipo, descricao, now());
END CASE;
END IF;
RETURN novo_saldo;
END
$$;
-- Esquentando o banco pra diminuir o tempo inicial das requisicoes
-- Referencia do pq isso funciona https://littlekendra.com/2016/11/25/why-is-my-query-faster-the-second-time-it-runs-dear-sql-dba-episode-23/
SELECT * FROM clients;
SELECT * FROM transactions1;
SELECT * FROM transactions2;
SELECT * FROM transactions3;
SELECT * FROM transactions4;
SELECT * FROM transactions5;
SELECT * FROM add_transaction(1, 10, 'c', 'primeira');
SELECT * FROM transactions1;
SELECT * FROM add_transaction(2, 10, 'c', 'primeira');
SELECT * FROM transactions2;
SELECT * FROM add_transaction(3, 10, 'c', 'primeira');
SELECT * FROM transactions3;
SELECT * FROM add_transaction(4, 10, 'c', 'primeira');
SELECT * FROM transactions4;
SELECT * FROM add_transaction(5, 10, 'c', 'primeira');
SELECT * FROM transactions5;
DELETE FROM transactions1;
DELETE FROM transactions2;
DELETE FROM transactions3;
DELETE FROM transactions4;
DELETE FROM transactions5;
UPDATE clients SET balance=0;