-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#66 Add parent table structure for monthly centreline table
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
data_aggregation/sql/function_create_yearly_monthly_centreline_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
CREATE OR REPLACE FUNCTION congestion.create_yearly_monthly_centreline_table(yyyy text) | ||
RETURNS void | ||
LANGUAGE 'plpgsql' | ||
COST 100 | ||
VOLATILE STRICT SECURITY DEFINER PARALLEL UNSAFE | ||
AS $BODY$ | ||
|
||
DECLARE | ||
startdate DATE; | ||
enddate DATE; | ||
basetablename TEXT := 'centreline_monthly_'; | ||
tablename TEXT; | ||
BEGIN | ||
|
||
startdate:= to_date(yyyy||'-01-01', 'YYYY-MM-DD'); | ||
enddate:= startdate + INTERVAL '1 year'; | ||
tablename:= basetablename||yyyy; | ||
EXECUTE format($$CREATE TABLE congestion.%I | ||
PARTITION OF congestion.centreline_monthly | ||
FOR VALUES FROM (%L) TO (%L); | ||
CREATE INDEX ON congestion.%I (uid); | ||
CREATE INDEX ON congestion.%I (hr); | ||
CREATE INDEX ON congestion.%I (mth); | ||
ALTER TABLE congestion.%I ADD UNIQUE(uid, hr, mth, day_type); | ||
ALTER TABLE congestion.%I OWNER TO congestion_admins; | ||
$$ | ||
, tablename, startdate, enddate, tablename, tablename, tablename, tablename, tablename); | ||
END; | ||
$BODY$; | ||
|
||
ALTER FUNCTION congestion.create_yearly_monthly_centreline_table(text) | ||
OWNER TO congestion_admins; | ||
|
||
COMMENT ON FUNCTION congestion.create_yearly_monthly_centreline_table(text) | ||
IS 'Function to create yearly partitioned table for centreline_monthly. Scheduled to execute at the end of the year with EOY maintanence airflow DAG.'; |