From 09c5c274c2c7b098c49dfbea4c8e2ffa63a01c8f Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Wed, 22 May 2013 21:24:36 -0400 Subject: [PATCH] Partition games through 2014. --- tables/games.tab | 40 ++++++++++++++++++++++++++++++++++++++ triggers/games_ins_trg.sql | 34 ++++++++++++++++---------------- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/tables/games.tab b/tables/games.tab index 3f5c3e2..3ff3a4d 100755 --- a/tables/games.tab +++ b/tables/games.tab @@ -28,6 +28,7 @@ WITH ( CREATE INDEX games_ix001 on games(create_dt); ALTER TABLE xonstat.games OWNER TO xonstat; +-- 2011 CREATE TABLE xonstat.games_2011Q2 ( CHECK ( create_dt >= DATE '2011-04-01' AND create_dt < DATE '2011-07-01' ) ) INHERITS (games); @@ -46,6 +47,7 @@ CREATE TABLE xonstat.games_2011Q4 ( CREATE INDEX games_2011Q4_ix001 on games_2011Q4(create_dt); ALTER TABLE xonstat.games_2011Q4 OWNER TO xonstat; +-- 2012 CREATE TABLE xonstat.games_2012Q1 ( CHECK ( create_dt >= DATE '2012-01-01' AND create_dt < DATE '2012-04-01' ) ) INHERITS (games); @@ -70,6 +72,7 @@ CREATE TABLE xonstat.games_2012Q4 ( CREATE INDEX games_2012Q4_ix001 on games_2012Q4(create_dt); ALTER TABLE xonstat.games_2012Q4 OWNER TO xonstat; +-- 2013 CREATE TABLE xonstat.games_2013Q1 ( CHECK ( create_dt >= DATE '2013-01-01' AND create_dt < DATE '2013-04-01' ) ) INHERITS (games); @@ -81,3 +84,40 @@ CREATE TABLE xonstat.games_2013Q2 ( ) INHERITS (games); CREATE INDEX games_2013Q2_ix001 on games_2013Q2(create_dt); ALTER TABLE xonstat.games_2013Q2 OWNER TO xonstat; + +CREATE TABLE xonstat.games_2013Q3 ( + CHECK ( create_dt >= DATE '2013-07-01' AND create_dt < DATE '2013-10-01' ) +) INHERITS (games); +CREATE INDEX games_2013Q3_ix001 on games_2013Q3(create_dt); +ALTER TABLE xonstat.games_2013Q3 OWNER TO xonstat; + +CREATE TABLE xonstat.games_2013Q4 ( + CHECK ( create_dt >= DATE '2013-10-01' AND create_dt < DATE '2014-01-01' ) +) INHERITS (games); +CREATE INDEX games_2013Q4_ix001 on games_2013Q4(create_dt); +ALTER TABLE xonstat.games_2013Q4 OWNER TO xonstat; + +-- 2014 +CREATE TABLE xonstat.games_2014Q1 ( + CHECK ( create_dt >= DATE '2014-01-01' AND create_dt < DATE '2014-04-01' ) +) INHERITS (games); +CREATE INDEX games_2014Q1_ix001 on games_2014Q1(create_dt); +ALTER TABLE xonstat.games_2014Q1 OWNER TO xonstat; + +CREATE TABLE xonstat.games_2014Q2 ( + CHECK ( create_dt >= DATE '2014-04-01' AND create_dt < DATE '2014-07-01' ) +) INHERITS (games); +CREATE INDEX games_2014Q2_ix001 on games_2014Q2(create_dt); +ALTER TABLE xonstat.games_2014Q2 OWNER TO xonstat; + +CREATE TABLE xonstat.games_2014Q3 ( + CHECK ( create_dt >= DATE '2014-07-01' AND create_dt < DATE '2014-10-01' ) +) INHERITS (games); +CREATE INDEX games_2014Q3_ix001 on games_2014Q3(create_dt); +ALTER TABLE xonstat.games_2014Q3 OWNER TO xonstat; + +CREATE TABLE xonstat.games_2014Q4 ( + CHECK ( create_dt >= DATE '2014-10-01' AND create_dt < DATE '2014-01-01' ) +) INHERITS (games); +CREATE INDEX games_2014Q4_ix001 on games_2014Q4(create_dt); +ALTER TABLE xonstat.games_2014Q4 OWNER TO xonstat; diff --git a/triggers/games_ins_trg.sql b/triggers/games_ins_trg.sql index 5eada1b..0490d18 100644 --- a/triggers/games_ins_trg.sql +++ b/triggers/games_ins_trg.sql @@ -1,24 +1,24 @@ CREATE OR REPLACE FUNCTION games_ins() RETURNS TRIGGER AS $$ BEGIN - IF (NEW.create_dt >= DATE '2011-04-01' AND NEW.create_dt < DATE '2011-07-01') THEN - INSERT INTO games_2011Q2 VALUES (NEW.*); - ELSIF (NEW.create_dt >= DATE '2011-07-01' AND NEW.create_dt < DATE '2011-10-01') THEN - INSERT INTO games_2011Q3 VALUES (NEW.*); - ELSIF (NEW.create_dt >= DATE '2011-10-01' AND NEW.create_dt < DATE '2012-01-01') THEN - INSERT INTO games_2011Q4 VALUES (NEW.*); - ELSIF (NEW.create_dt >= DATE '2012-01-01' AND NEW.create_dt < DATE '2012-04-01') THEN - INSERT INTO games_2012Q1 VALUES (NEW.*); - ELSIF (NEW.create_dt >= DATE '2012-04-01' AND NEW.create_dt < DATE '2012-07-01') THEN - INSERT INTO games_2012Q2 VALUES (NEW.*); - ELSIF (NEW.create_dt >= DATE '2012-07-01' AND NEW.create_dt < DATE '2012-10-01') THEN - INSERT INTO games_2012Q3 VALUES (NEW.*); - ELSIF (NEW.create_dt >= DATE '2012-10-01' AND NEW.create_dt < DATE '2013-01-01') THEN - INSERT INTO games_2012Q4 VALUES (NEW.*); - ELSIF (NEW.create_dt >= DATE '2013-01-01' AND NEW.create_dt < DATE '2013-04-01') THEN - INSERT INTO games_2013Q1 VALUES (NEW.*); - ELSIF (NEW.create_dt >= DATE '2013-04-01' AND NEW.create_dt < DATE '2013-07-01') THEN + -- 2013 + IF (NEW.create_dt >= DATE '2013-04-01' AND NEW.create_dt < DATE '2013-07-01') THEN INSERT INTO games_2013Q2 VALUES (NEW.*); + ELSIF (NEW.create_dt >= DATE '2013-07-01' AND NEW.create_dt < DATE '2013-10-01') THEN + INSERT INTO games_2013Q3 VALUES (NEW.*); + ELSIF (NEW.create_dt >= DATE '2013-10-01' AND NEW.create_dt < DATE '2014-01-01') THEN + INSERT INTO games_2013Q4 VALUES (NEW.*); + + -- 2014 + ELSIF (NEW.create_dt >= DATE '2014-01-01' AND NEW.create_dt < DATE '2014-04-01') THEN + INSERT INTO games_2014Q1 VALUES (NEW.*); + ELSIF (NEW.create_dt >= DATE '2014-04-01' AND NEW.create_dt < DATE '2014-07-01') THEN + INSERT INTO games_2014Q2 VALUES (NEW.*); + ELSIF (NEW.create_dt >= DATE '2014-07-01' AND NEW.create_dt < DATE '2014-10-01') THEN + INSERT INTO games_2014Q3 VALUES (NEW.*); + ELSIF (NEW.create_dt >= DATE '2014-10-01' AND NEW.create_dt < DATE '2015-01-01') THEN + INSERT INTO games_2014Q4 VALUES (NEW.*); + ELSE RAISE EXCEPTION 'Date out of range. Fix the games_ins() trigger!'; END IF; -- 2.39.2