Skip to content

Commit

Permalink
#66 Add parent table structure for monthly centreline table
Browse files Browse the repository at this point in the history
  • Loading branch information
chmnata committed Oct 11, 2022
1 parent d339b94 commit aff4ea5
Showing 1 changed file with 35 additions and 0 deletions.
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.';

0 comments on commit aff4ea5

Please sign in to comment.