I am currently running my Zabbix DB on PostgreSQL 9.4 but plan on upgrading to PostgreSQL 10 soon.
PostgreSQL 10 comes with native partitioning. This is better than the inherit partitioning which will help avoid triggers & functions which caused issues in our database in the past.
I am looking for the best way to automate the range partitioning on the clock column for the history and trend tables.
We plan on partitioning the database beforehand and then dumping the data in there where all the data will reside in the partitions. I tested this manually with the SQL queries below.
Would it be wiser to write a SQL function to perform the following below or to use a shell or python (python-psycopg2) script?
Zabbix.org provides some partitioning for PostgreSQL but they are all trigger based; something we want to avoid.
Ideally the program would find the oldest epoch/date value of the clock column and then create weekly (or monthly) partitions since that date. The script would run weekly and delete partitions that are older than 31 days for history and 365 days for trends.
Any suggestions on how to get this done using the native range partitioning that comes packaged with PostgreSQL 10?
PostgreSQL 10 comes with native partitioning. This is better than the inherit partitioning which will help avoid triggers & functions which caused issues in our database in the past.
I am looking for the best way to automate the range partitioning on the clock column for the history and trend tables.
We plan on partitioning the database beforehand and then dumping the data in there where all the data will reside in the partitions. I tested this manually with the SQL queries below.
Would it be wiser to write a SQL function to perform the following below or to use a shell or python (python-psycopg2) script?
Zabbix.org provides some partitioning for PostgreSQL but they are all trigger based; something we want to avoid.
Ideally the program would find the oldest epoch/date value of the clock column and then create weekly (or monthly) partitions since that date. The script would run weekly and delete partitions that are older than 31 days for history and 365 days for trends.
Any suggestions on how to get this done using the native range partitioning that comes packaged with PostgreSQL 10?
Code:
-- Drop the Zabbix public history table because we need to create a new one with partitions
DROP TABLE if exists public.history;
-- Create a ranged partitioned table
CREATE TABLE public.history
(
itemid bigint NOT NULL,
clock integer NOT NULL DEFAULT 0,
value numeric(16,4) NOT NULL DEFAULT 0.0000,
ns integer NOT NULL DEFAULT 0
) PARTITION BY RANGE (clock);
GRANT ALL ON TABLE public.history TO postgres;
-- Create the partitioned table. Each table needs the correct permissions.
-- You need to constrain each leaf partition individually since primary keys are not supported in partitioning.
CREATE TABLE public.history_y2017 PARTITION OF public.history
FOR VALUES FROM (MINVALUE) TO (1514764800);
GRANT ALL ON TABLE public.history_y2017 TO postgres;
CREATE TABLE public.history_y2018m01 PARTITION OF public.history
FOR VALUES FROM (1514764800) TO (1517443200);
GRANT ALL ON TABLE public.history_y2018m01 TO postgres;
CREATE TABLE public.history_y2018m02 PARTITION OF public.history
FOR VALUES FROM (1517443200) TO (1519862400);
GRANT ALL ON TABLE public.history_y2018m02 TO postgres;
CREATE TABLE public.history_y2018m03 PARTITION OF public.history
FOR VALUES FROM (1519862400) TO (1522540800);
GRANT ALL ON TABLE public.history_y2018m02 TO postgres;
CREATE TABLE public.history_y2018m04 PARTITION OF public.history
FOR VALUES FROM (1522540800) TO (1525132800);
GRANT ALL ON TABLE public.history_y2018m03 TO postgres;
CREATE TABLE public.history_y2018m05 PARTITION OF public.history
FOR VALUES FROM (1525132800) TO (1527811200);
GRANT ALL ON TABLE public.history_y2018m04 TO postgres;
-- Create an Index on each partition created
CREATE INDEX ON public.history_y2017 USING btree (itemid, clock);
CREATE INDEX ON public.history_y2018m01 USING btree (itemid, clock);
CREATE INDEX ON public.history_y2018m02 USING btree (itemid, clock);
CREATE INDEX ON public.history_y2018m03 USING btree (itemid, clock);
CREATE INDEX ON public.history_y2018m04 USING btree (itemid, clock);
CREATE INDEX ON public.history_y2018m05 USING btree (itemid, clock);
-- What about using date instead of epoch value such as 2017-01-01 00:00:00; would that be easier?
Comment