In the Code tab, paste the PL/SQL script from
$ZCHATOPS_HOME/samples/sql/pgAgent/maintainPartition.sql
that you create to automatically create table partitions for zchatops
database.DO $q$
DECLARE
table_num int;
start_year varchar;
start_month varchar;
end_year varchar;
end_month varchar;
start_date varchar;
end_date varchar;
BEGIN
RAISE NOTICE 'Start to maintain the partition tables for zchatops database ...';
-- Get start and end date
SELECT to_char(CURRENT_DATE + interval '1 month', 'YYYY') INTO start_year;
SELECT to_char(CURRENT_DATE + interval '1 month', 'MM') INTO start_month;
SELECT to_char(CURRENT_DATE + interval '2 months', 'YYYY') INTO end_year;
SELECT to_char(CURRENT_DATE + interval '2 months', 'MM') INTO end_month;
start_date = start_year || '-' || start_month || '-01';
end_date = end_year || '-' || end_month || '-01';
RAISE NOTICE 'Partition table start date: %', start_date;
RAISE NOTICE 'Partition table end date: %', end_date;
-- Check whether the message table for next month exists or not
SELECT count(1) INTO table_num FROM pg_tables WHERE schemaname='bnz_webchat' AND tablename=format('message_y%sm%s', start_year, start_month);
-- Create message partition table for next month
IF table_num >= 1 THEN
RAISE NOTICE 'The partition table bnz_webchat.message_y%m% already exists!', start_year, start_month;
ELSE
-- Create partition table
RAISE NOTICE 'Creating the partition table bnz_webchat.message_y%m% ...', start_year, start_month;
EXECUTE format('CREATE TABLE bnz_webchat.message_y%sm%s (LIKE bnz_webchat.message INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES)', start_year, start_month);
EXECUTE format('ALTER TABLE bnz_webchat.message_y%sm%s ADD CONSTRAINT y%sm%s CHECK ( create_time >= DATE %L AND create_time < DATE %L )', start_year, start_month, start_year, start_month, start_date, end_date);
EXECUTE format('ALTER TABLE bnz_webchat.message ATTACH PARTITION bnz_webchat.message_y%sm%s FOR VALUES FROM ( %L ) TO ( %L )', start_year, start_month, start_date, end_date);
RAISE NOTICE 'The partition table bnz_webchat.message_y%m% has already been created!', start_year, start_month;
END IF;
-- Check whether the incident table for next month exists or not
SELECT count(1) INTO table_num FROM pg_tables WHERE schemaname='bnz_webchat' AND tablename=format('incident_y%sm%s', start_year, start_month);
-- Create incident partition table for next month
IF table_num >= 1 THEN
RAISE NOTICE 'The partition table bnz_webchat.incident_y%m% already exists!', start_year, start_month;
ELSE
-- Create partition table
RAISE NOTICE 'Creating the partition table bnz_webchat.incident_y%m% ...', start_year, start_month;
EXECUTE format('CREATE TABLE bnz_webchat.incident_y%sm%s (LIKE bnz_webchat.incident INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES)', start_year, start_month);
EXECUTE format('ALTER TABLE bnz_webchat.incident_y%sm%s ADD CONSTRAINT y%sm%s CHECK ( create_time >= DATE %L AND create_time < DATE %L )', start_year, start_month, start_year, start_month, start_date, end_date);
EXECUTE format('ALTER TABLE bnz_webchat.incident ATTACH PARTITION bnz_webchat.incident_y%sm%s FOR VALUES FROM ( %L ) TO ( %L )', start_year, start_month, start_date, end_date);
RAISE NOTICE 'The partition table bnz_webchat.incident_y%m% has already been created!', start_year, start_month;
END IF;
RAISE NOTICE 'Finish to maintain the partition tables for zchatops database!';
RETURN;
END;
$q$ LANGUAGE plpgsql;