]> git.xonotic.org Git - xonotic/xonstatdb.git/blob - scripts/gen_partitions.shl
The games table gets a gin index on players.
[xonotic/xonstatdb.git] / scripts / gen_partitions.shl
1 #!/bin/bash
2
3 table=$1
4 start_year=$2
5 end_year=$3
6
7 for year in `seq $start_year $end_year`
8 do
9     for qtr in "q1" "q2" "q3" "q4"
10     do
11         printf "CREATE TABLE IF NOT EXISTS xonstat.%s_%s%s ( \n" $table $year $qtr
12         if [[ $qtr = "q1" ]]
13         then
14             printf "\tCHECK ( create_dt >= DATE '%s-01-01' AND create_dt < DATE '%s-04-01' ) \n" $year $year
15         elif [[ $qtr = "q2" ]]
16         then
17             printf "\tCHECK ( create_dt >= DATE '%s-04-01' AND create_dt < DATE '%s-07-01' ) \n" $year $year
18         elif [[ $qtr = "q3" ]]
19         then
20             printf "\tCHECK ( create_dt >= DATE '%s-07-01' AND create_dt < DATE '%s-10-01' ) \n" $year $year
21         elif [[ $qtr = "q4" ]]
22         then
23             next_year=$[year + 1]
24             printf "\tCHECK ( create_dt >= DATE '%s-10-01' AND create_dt < DATE '%s-01-01' ) \n" $year $next_year
25         fi
26
27         printf ") INHERITS (%s);\n\n" $table
28
29         # indexes
30         printf "CREATE INDEX %s_%s%s_ix001 on %s_%s%s(create_dt);\n" $table $year $qtr $table $year $qtr
31
32         # conditional indexes that depend on the table
33         if [[ $table = "player_game_stats" || $table = "player_weapon_stats" ]]
34         then
35             printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
36             printf "CREATE INDEX %s_%s%s_ix003 on %s_%s%s(player_id);\n" $table $year $qtr $table $year $qtr
37         fi
38
39         if [[ $table = "team_game_stats" ]]
40         then
41             printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
42         fi
43
44         if [[ $table = "games" ]]
45         then
46             printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s using gin(players);\n" $table $year $qtr $table $year $qtr
47         fi
48
49         printf "\n"
50     done
51 done
52 printf "\n"
53
54 printf "CREATE OR REPLACE FUNCTION %s_ins()\n" $table
55 printf "RETURNS TRIGGER AS \$\$\n"
56 printf "BEGIN\n"
57
58 for i in `seq $start_year $end_year`
59 do
60
61     if [[ start_year -eq i ]]
62     then
63         printf "\tIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
64     else
65         printf "\tELSIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
66     fi
67     printf "\t\tINSERT INTO %s_%sQ1 VALUES (NEW.*);\n" $table $i
68
69     printf "\tELSIF (NEW.create_dt >= DATE '%s-04-01' AND NEW.create_dt < DATE '%s-07-01') THEN\n" $i $i
70     printf "\t\tINSERT INTO %s_%sQ2 VALUES (NEW.*);\n" $table $i
71
72     printf "\tELSIF (NEW.create_dt >= DATE '%s-07-01' AND NEW.create_dt < DATE '%s-10-01') THEN\n" $i $i
73     printf "\t\tINSERT INTO %s_%sQ3 VALUES (NEW.*);\n" $table $i
74
75     next_year=$[i + 1]
76     printf "\tELSIF (NEW.create_dt >= DATE '%s-10-01' AND NEW.create_dt < DATE '%s-01-01') THEN\n" $i $next_year
77     printf "\t\tINSERT INTO %s_%sQ4 VALUES (NEW.*);\n" $table $i
78
79 done
80
81 printf "\tELSE\n"
82 printf "\t\tRAISE EXCEPTION 'Date out of range. Fix the %s_ins() trigger!';\n" $table
83 printf "\tEND IF;\n"
84 printf "\tRETURN NULL;\n"
85
86 printf "END\n"
87 printf "\$\$\n"
88 printf "LANGUAGE plpgsql;\n\n"
89
90 printf "DROP TRIGGER IF EXISTS %s_ins_trg ON xonstat.%s;\n" $table $table
91 printf "CREATE TRIGGER %s_ins_trg\n" $table
92 printf "BEFORE INSERT on xonstat.%s\n" $table
93 printf "FOR EACH ROW EXECUTE PROCEDURE %s_ins();\n" $table