Skip to content

Commit 2ab4053

Browse files
author
Evgeniy Khyst
committed
first commit
1 parent 729033c commit 2ab4053

10 files changed

+299
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,9 @@ The more indexes you have, the slower `INSERT` statements will become.
890890
Make sure you don't have an unused indexes.
891891
892892
1. Reset statistics counters to zeros
893-
```sql
894-
SELECT pg_stat_reset();
895-
```
893+
```sql
894+
SELECT pg_stat_reset();
895+
```
896896
897897
2. Let the system work for some time after resetting statistics.
898898

data.sql

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
INSERT INTO category (category_id, name)
2+
VALUES
3+
('940ff0d0-88b7-413e-ac43-8938bcdae665', 'Software Reuse'),
4+
('c5ccfb21-2f89-468a-aeac-645823f5fc42', 'Object-Oriented Software Design'),
5+
('d4747177-46f2-477a-b580-641890ae645b', 'Software Design & Engineering'),
6+
('963d44f4-ad3f-4086-9e05-83290242381b', 'Databases & Big Data');
7+
8+
INSERT INTO author (author_id, full_name)
9+
VALUES
10+
('def8af9d-1191-4199-9600-fe3f7825a742', 'Erich Gamma'),
11+
('2e77d8a6-0c53-4d7c-ac4d-94877a8d9759', 'Richard Helm'),
12+
('3e031ea1-47bb-4d1d-9e11-b4692f26c80e', 'Ralph Johnson'),
13+
('c9700131-3740-4fb6-a9ab-d77abd4da907', 'John Vlissides'),
14+
('f3a82350-925b-4c84-803f-adf5ecb6de0d', 'Robert Martin'),
15+
('63c79713-2ba1-4006-9c37-6f54af18782d', 'Eric Evans'),
16+
('b7644e96-cc68-48a7-b6ce-67aa91b52bdf', 'Martin Fowler'),
17+
('a32b8ccc-38d7-4f2f-9014-1d593f507dc2', 'Gregor Hohpe'),
18+
('43b7a06e-1b32-4e0a-b536-348d73d76cd5', 'Bobby Woolf'),
19+
('ed0cb2b8-b4a8-4ae7-882e-2e75d7791ef7', 'Pramod Sadalage'),
20+
('08014407-cce6-417a-b3de-7252da4f3344', 'Martin Kleppmann');
21+
22+
INSERT INTO publisher (publisher_id, name)
23+
VALUES
24+
('05a85ed5-e00d-49ad-a215-795dbfd87937', 'Addison-Wesley Professional'),
25+
('05f7bdc8-306c-4b9e-b37d-20593fcb70f5', 'Pearson'),
26+
('a080026e-51d8-4e88-ba05-063ce43a83af', 'O''Reilly Media');
27+
28+
INSERT INTO book (book_id, isbn, title, publication_date, publisher_id, rating)
29+
VALUES
30+
('9a58f19c-bf6a-4cd0-be4a-33fa33264473', '978-0201633610', 'Design Patterns: Elements of Reusable Object-Oriented Software', '1994-11-10', '05a85ed5-e00d-49ad-a215-795dbfd87937', 4.7),
31+
('f00b659b-95e6-47f6-af5f-ec25c4e38631', '978-0135974445', 'Agile Software Development, Principles, Patterns, and Practices', '2002-10-15', '05f7bdc8-306c-4b9e-b37d-20593fcb70f5', 4.6),
32+
('58b46e28-ec87-4776-b46d-180046780132', '978-0321125217', 'Domain-Driven Design: Tackling Complexity in the Heart of Software', '2003-08-20', '05a85ed5-e00d-49ad-a215-795dbfd87937', 4.5),
33+
('54c8cf28-e071-47ed-97e5-2002512509ea', '978-0321127426', 'Patterns of Enterprise Application Architecture', '2002-11-05', '05a85ed5-e00d-49ad-a215-795dbfd87937', 4.5),
34+
('aa8811c4-7f06-44f9-b934-a648d4d908c6', '978-0321200686', 'Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions', '2003-08-10', '05a85ed5-e00d-49ad-a215-795dbfd87937', 4.7),
35+
('f8a7f930-5b23-480b-bfea-7f77c5cb2db9', '978-0321826626', 'NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence', '2012-08-08', '05a85ed5-e00d-49ad-a215-795dbfd87937', 4.4),
36+
('aa327ea7-58ee-42dc-8c49-83f68bc50da7', '978-1449373320', 'Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems', '2017-04-18', 'a080026e-51d8-4e88-ba05-063ce43a83af', 4.8);
37+
38+
INSERT INTO book_category (book_id, category_id)
39+
VALUES
40+
('9a58f19c-bf6a-4cd0-be4a-33fa33264473', '940ff0d0-88b7-413e-ac43-8938bcdae665'),
41+
('9a58f19c-bf6a-4cd0-be4a-33fa33264473', 'c5ccfb21-2f89-468a-aeac-645823f5fc42'),
42+
('f00b659b-95e6-47f6-af5f-ec25c4e38631', 'c5ccfb21-2f89-468a-aeac-645823f5fc42'),
43+
('58b46e28-ec87-4776-b46d-180046780132', 'c5ccfb21-2f89-468a-aeac-645823f5fc42'),
44+
('54c8cf28-e071-47ed-97e5-2002512509ea', 'c5ccfb21-2f89-468a-aeac-645823f5fc42'),
45+
('aa8811c4-7f06-44f9-b934-a648d4d908c6', 'c5ccfb21-2f89-468a-aeac-645823f5fc42'),
46+
('f8a7f930-5b23-480b-bfea-7f77c5cb2db9', '963d44f4-ad3f-4086-9e05-83290242381b'),
47+
('aa327ea7-58ee-42dc-8c49-83f68bc50da7', 'd4747177-46f2-477a-b580-641890ae645b');
48+
49+
INSERT INTO book_author (book_id, author_id)
50+
VALUES
51+
('9a58f19c-bf6a-4cd0-be4a-33fa33264473', 'def8af9d-1191-4199-9600-fe3f7825a742'),
52+
('9a58f19c-bf6a-4cd0-be4a-33fa33264473', '2e77d8a6-0c53-4d7c-ac4d-94877a8d9759'),
53+
('9a58f19c-bf6a-4cd0-be4a-33fa33264473', '3e031ea1-47bb-4d1d-9e11-b4692f26c80e'),
54+
('9a58f19c-bf6a-4cd0-be4a-33fa33264473', 'c9700131-3740-4fb6-a9ab-d77abd4da907'),
55+
('f00b659b-95e6-47f6-af5f-ec25c4e38631', 'f3a82350-925b-4c84-803f-adf5ecb6de0d'),
56+
('58b46e28-ec87-4776-b46d-180046780132', '63c79713-2ba1-4006-9c37-6f54af18782d'),
57+
('54c8cf28-e071-47ed-97e5-2002512509ea', 'b7644e96-cc68-48a7-b6ce-67aa91b52bdf'),
58+
('aa8811c4-7f06-44f9-b934-a648d4d908c6', 'a32b8ccc-38d7-4f2f-9014-1d593f507dc2'),
59+
('aa8811c4-7f06-44f9-b934-a648d4d908c6', '43b7a06e-1b32-4e0a-b536-348d73d76cd5'),
60+
('f8a7f930-5b23-480b-bfea-7f77c5cb2db9', 'b7644e96-cc68-48a7-b6ce-67aa91b52bdf'),
61+
('f8a7f930-5b23-480b-bfea-7f77c5cb2db9', 'ed0cb2b8-b4a8-4ae7-882e-2e75d7791ef7'),
62+
('aa327ea7-58ee-42dc-8c49-83f68bc50da7', '08014407-cce6-417a-b3de-7252da4f3344');

docker-compose.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
version: '3.1'
2+
3+
services:
4+
5+
fasttablespace:
6+
image: busybox
7+
restart: "no"
8+
command: >
9+
sh -c "mkdir -p /ssd1/postgresql/data/fasttablespace
10+
&& chown -R 999:999 /ssd1/postgresql/data/fasttablespace"
11+
volumes:
12+
- fasttablespace:/ssd1/postgresql/data
13+
14+
postgres:
15+
image: postgres:13
16+
restart: always
17+
environment:
18+
POSTGRES_DB: postgres
19+
POSTGRES_USER: admin
20+
POSTGRES_PASSWORD: s3cr3t
21+
command:
22+
- "postgres"
23+
- "-c"
24+
- "shared_preload_libraries=pg_stat_statements"
25+
volumes:
26+
- pgdata:/var/lib/postgresql/data
27+
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
28+
- fasttablespace:/ssd1/postgresql/data
29+
depends_on:
30+
- fasttablespace
31+
32+
pgadmin:
33+
image: dpage/pgadmin4
34+
restart: always
35+
environment:
36+
PGADMIN_DEFAULT_EMAIL: [email protected]
37+
PGADMIN_DEFAULT_PASSWORD: s3cr3t
38+
volumes:
39+
- pgadmin-data:/var/lib/pgadmin
40+
- ./pgadmin4/servers.json:/pgadmin4/servers.json
41+
ports:
42+
- 8080:80
43+
depends_on:
44+
- postgres
45+
46+
volumes:
47+
pgdata:
48+
driver: local
49+
pgadmin-data:
50+
driver: local
51+
fasttablespace:
52+
driver: local

drop-all.sql

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DROP TABLE book_category,
2+
book_author,
3+
book,
4+
publisher,
5+
author,
6+
category;

img/tables.png

19.6 KB
Loading

img/tables.puml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
@startuml
2+
3+
hide circle
4+
5+
skinparam linetype ortho
6+
7+
entity "CATEGORY" as c {
8+
*CATEGORY_ID : UUID <<PK1>>
9+
--
10+
*NAME : VARCHAR(255) <<U1,NN>>
11+
}
12+
13+
entity "AUTHOR" as a {
14+
*AUTHOR_ID : UUID <<PK1>>
15+
--
16+
*FULL_NAME : VARCHAR(255) <<U1,NN>>
17+
}
18+
19+
entity "PUBLISHER" as p {
20+
*PUBLISHER_ID : UUID <<PK1>>
21+
--
22+
*NAME : VARCHAR(255) <<U1,NN>>
23+
}
24+
25+
entity "BOOK" as b {
26+
*BOOK_ID : UUID <<PK1>>
27+
--
28+
*ISBN : VARCHAR(14) <<U1>>
29+
*TITLE : VARCHAR(255) <<NN>>
30+
*PUBLICATION_DATE : DATE <<NN>>
31+
*PUBLISHER_ID : UUID <<FK>>
32+
*RATING : NUMERIC(4, 3)
33+
}
34+
35+
entity "BOOK_CATEGORY" as bc {
36+
*BOOK_ID : UUID <<PK1,FK>>
37+
*CATEGORY_ID : UUID <<PK1,FK>>
38+
--
39+
}
40+
41+
entity "BOOK_AUTHOR" as ba {
42+
*BOOK_ID : UUID <<PK1,FK>>
43+
*AUTHOR_ID : UUID <<PK1,FK>>
44+
--
45+
}
46+
47+
b ||..|{ bc
48+
b ||..|{ ba
49+
b ||..|{ p
50+
51+
c ||..|{ bc
52+
a ||..|{ ba
53+
54+
@enduml

pgadmin4/servers.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Servers": {
3+
"1": {
4+
"Name": "Local PostgreSQL",
5+
"Group": "Servers",
6+
"Host": "postgres",
7+
"Port": 5432,
8+
"SSLMode": "prefer",
9+
"MaintenanceDB": "postgres",
10+
"Username": "admin"
11+
}
12+
}
13+
}

schema.sql

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
2+
3+
CREATE TABLE category (
4+
category_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
5+
name VARCHAR(255) UNIQUE NOT NULL
6+
);
7+
8+
CREATE TABLE author (
9+
author_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
10+
full_name VARCHAR(255) UNIQUE NOT NULL
11+
);
12+
13+
CREATE TABLE publisher (
14+
publisher_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
15+
name VARCHAR(255) UNIQUE NOT NULL
16+
);
17+
18+
CREATE TABLE book (
19+
book_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
20+
isbn VARCHAR(14) UNIQUE,
21+
title VARCHAR(255) NOT NULL,
22+
publication_date DATE NOT NULL,
23+
publisher_id UUID REFERENCES publisher(publisher_id),
24+
rating NUMERIC(4, 3)
25+
);
26+
27+
CREATE TABLE book_category (
28+
book_id UUID REFERENCES book(book_id),
29+
category_id UUID REFERENCES category(category_id),
30+
PRIMARY KEY (book_id, category_id)
31+
);
32+
33+
CREATE TABLE book_author (
34+
book_id UUID REFERENCES book(book_id),
35+
author_id UUID REFERENCES author(author_id),
36+
PRIMARY KEY (book_id, author_id)
37+
);

toc.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// node toc.js README.md
2+
3+
const filename = process.argv[2];
4+
if (!filename) {
5+
throw 'Filename is not specified';
6+
}
7+
8+
const fs = require('fs');
9+
const os = require('os');
10+
const crypto = require('crypto');
11+
12+
function insert(str, start, delCount, newSubStr) {
13+
return str.slice(0, start) + newSubStr + str.slice(start + delCount);
14+
};
15+
16+
let data = fs.readFileSync(filename, 'utf8');
17+
const matches = data.matchAll(/(#{2,6})(\s+)(<a name=".+"><\/a>)?(.+)(\r?\n)?/g);
18+
19+
const toc = [];
20+
21+
for (const match of Array.from(matches).reverse()) {
22+
const level = match[1];
23+
const spaces = match[2];
24+
const anchor = match[3];
25+
const title = match[4];
26+
const hash = crypto.createHash('md5').update(title).digest('hex');
27+
data = insert(data, match.index + level.length + spaces.length, anchor ? anchor.length : 0, `<a name="${hash}"></a>`);
28+
toc.unshift(`${' '.repeat(level.length - 2)}* [${title}](#${hash})`);
29+
}
30+
31+
const lines = data.split(/\r?\n/);
32+
let matched = false;
33+
let tocStart = -1;
34+
let tocEnd = -1;
35+
for (let i = 0; i < lines.length; i++) {
36+
const line = lines[i];
37+
if (line.match(/^\s*\*\s+\[.+\]\(#\w+\)$/)) {
38+
if (!matched) {
39+
tocStart = i;
40+
}
41+
matched = true;
42+
} else {
43+
if (matched) {
44+
if (line.match(/^\s*$/)) {
45+
tocEnd = i + 1;
46+
} else {
47+
tocEnd = i;
48+
}
49+
break;
50+
}
51+
}
52+
i++;
53+
}
54+
55+
lines.splice(tocStart, tocEnd - tocStart);
56+
57+
data = '';
58+
for (let i = 0; i < lines.length; i++) {
59+
const line = lines[i];
60+
data += line + os.EOL;
61+
}
62+
63+
const match = /#\s+.+(\r?\n)/.exec(data);
64+
data = insert(data, match.index + match[0].length, 0, os.EOL + toc.join(os.EOL) + os.EOL);
65+
66+
fs.writeFileSync(filename, data, 'utf8');

truncate.sql

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TRUNCATE book_category,
2+
book_author,
3+
category,
4+
author,
5+
publisher,
6+
book;

0 commit comments

Comments
 (0)