/
range_partitioning_example.sql
103 lines (85 loc) · 3.14 KB
/
range_partitioning_example.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
-- PARTITIONING example
-- Drop Tables in case such tables already exist.
DROP TABLE IF EXISTS vehicle_before_2000;
DROP TABLE IF EXISTS vehicle_2000_2005;
DROP TABLE IF EXISTS vehicle_2006_2016;
DROP TABLE IF EXISTS vehicle_master;
-- Create master table which will be inherited by partitioned tables
CREATE TABLE vehicle_master (
vehicle_id SERIAL PRIMARY KEY NOT NULL,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
year INTEGER NOT NULL,
price NUMERIC(5) NOT NULL
);
-- Tables stores vehicles which were manufactured before the year 2000
CREATE TABLE vehicle_before_2000
(
PRIMARY KEY (vehicle_id),
CHECK (year < 2000)
) INHERITS (vehicle_master);
-- Tables stores vehicles which were manufactured strting from the year 2000 and the year 2005 (2000 and 2005 inclluded)
CREATE TABLE vehicle_2000_2005
(
PRIMARY KEY (vehicle_id),
CHECK (year >= 2000 AND year <= 2005 )
) INHERITS (vehicle_master);
-- Tables stores vehicles which were manufactured strting from the year 2006 and the year 2016 (2006 and 2016 inclluded)
CREATE TABLE vehicle_2006_2016
(
PRIMARY KEY (vehicle_id),
CHECK (year >= 2006 AND year <= 2016 )
) INHERITS (vehicle_master);
-- Tables stores vehicles which were manufactured strting from the year 2006 and the year 2016 (2006 and 2016 inclluded)
CREATE TABLE vehicle_after_2017
(
PRIMARY KEY (vehicle_id),
CHECK (year >= 2017)
) INHERITS (vehicle_master);
-- TRIGGER Function which inserts vehicle into an appropriate partitioned table according to the year in which it was manufactured
DROP FUNCTION IF EXISTS vehicle_insert();
CREATE OR REPLACE FUNCTION vehicle_insert()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.year < 2000
THEN
INSERT INTO vehicle_before_2000 VALUES (NEW.*);
ELSEIF NEW.year >= 2000 AND NEW.year <= 2005
THEN
INSERT INTO vehicle_2000_2005 VALUES (NEW.*);
ELSEIF NEW.year >= 2006 AND NEW.year <= 2016
THEN
INSERT INTO vehicle_2006_2016 VALUES (NEW.*);
ELSEIF NEW.year >= 2017
THEN
INSERT INTO vehicle_after_2017 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'YEAR is out of range. Something is wrong with
vehicle_insert_trigger() function';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
-- Create TRIGGER which will execute vehicle_insert when new record is being inserted, updated or deleted into vehicle_master table
DROP TRIGGER IF EXISTS vehicle_year_trigger ON vehicle_master;
CREATE TRIGGER vehicle_year_trigger
BEFORE INSERT ON vehicle_master
FOR EACH ROW
EXECUTE PROCEDURE vehicle_insert();
-- Function for filling in vehicle_master table by randomly generated values
DROP FUNCTION IF EXISTS fillinvehicle_distributed(rowsToInsert INTEGER );
-- Creates a function for generating and inserting dynamic data into the 'vehicle' table.
-- rowsToInsert - is amount of row to be inserted
CREATE OR REPLACE FUNCTION fillinvehicle_distributed(rowsToInsert INTEGER)
RETURNS VOID AS $$
BEGIN
FOR i IN 1..rowsToInsert LOOP
INSERT INTO vehicle_master (make, model, price, year)
VALUES ('make' || i,
'model' || i,
CAST((random() * 100000) AS NUMERIC(5)),
extract(YEAR FROM DATE(NOW() - '1 year' :: INTERVAL * ROUND(RANDOM() * 100))));
END LOOP;
END;
$$ LANGUAGE plpgsql;