From b189c953958cdb195269f28ce57fd2662b1c5bba Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mon, 25 Jan 2021 15:13:10 +0100 Subject: [PATCH] Add first work on the migration SQL file to partition the DG DB Signed-off-by: Pierre-Yves Chibon --- scripts/migration.sql | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 scripts/migration.sql diff --git a/scripts/migration.sql b/scripts/migration.sql new file mode 100644 index 0000000..382379c --- /dev/null +++ b/scripts/migration.sql @@ -0,0 +1,65 @@ +-- Update the current messages table so it stores the year that we'll partition +-- on. + +ALTER TABLE messages ADD COLUMN year integer; +INSERT INTO messages(year) select extract(year from timestamp) from messages; + + + +-- Create the messages2 table that will be partitioned and will have the messages +-- moved to + +CREATE TABLE messages2 ( + id integer NOT NULL, + i integer NOT NULL, + "timestamp" timestamp without time zone NOT NULL, + year integer NOT NULL, + certificate text, + signature text, + topic text, + _msg text NOT NULL, + category text, + source_name text, + source_version text, + msg_id text, + _headers text, + username text, + crypto text +) PARTITION BY LIST(year); + +CREATE SEQUENCE public.messages2_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.messages2_id_seq OWNER TO datanommer; +ALTER SEQUENCE public.messages2_id_seq OWNED BY public.messages2.id; +ALTER TABLE ONLY public.messages2 ALTER COLUMN id SET DEFAULT nextval('public.messages2_id_seq'::regclass); +ALTER TABLE ONLY public.messages2 + ADD CONSTRAINT messages2_msg_id_key UNIQUE (msg_id); +ALTER TABLE ONLY public.messages2 + ADD CONSTRAINT messages2_pkey PRIMARY KEY (id); +CREATE INDEX index_msg2_category ON public.messages2 USING btree (category); +CREATE INDEX index_msg2_timestamp ON public.messages2 USING btree ("timestamp"); +CREATE INDEX index_msg2_topic ON public.messages2 USING btree (topic); +CREATE INDEX messages2_datanommer_timestamp_category_idx ON public.messages2 USING btree ("timestamp" DESC, category); +CREATE INDEX messages2_datanommer_timestamp_topic_idx ON public.messages2 USING btree ("timestamp" DESC, topic); +GRANT SELECT ON TABLE public.messages2 TO datagrepper; +GRANT SELECT ON SEQUENCE public.messages2_id_seq TO datagrepper; + +-- Create the partitions + +CREATE TABLE messages_2021 PARTITION OF messages FOR VALUES IN (2021); +CREATE TABLE messages_2020 PARTITION OF messages FOR VALUES IN (2020); +CREATE TABLE messages_2019 PARTITION OF messages FOR VALUES IN (2019); +CREATE TABLE messages_2018 PARTITION OF messages FOR VALUES IN (2018); +CREATE TABLE messages_2017 PARTITION OF messages FOR VALUES IN (2017); +CREATE TABLE messages_2016 PARTITION OF messages FOR VALUES IN (2016); +CREATE TABLE messages_2015 PARTITION OF messages FOR VALUES IN (2015); +CREATE TABLE messages_2014 PARTITION OF messages FOR VALUES IN (2014); +CREATE TABLE messages_2013 PARTITION OF messages FOR VALUES IN (2013); +CREATE TABLE messages_2012 PARTITION OF messages FOR VALUES IN (2012); +