The CREATE OR the REPLACE the FUNCTION auto_insert_into_tbl_partition () the RETURNS Trigger the AS $ $ the BODY the DECLARE time_column_name text ; - a time field, the date is determined by the time this sub-table, part of a required curMMDD VARCHAR ( 16 ); - 'YYYYMMDD' format string , used sub-partition table suffix ISEXIST Boolean; - Analyzing the additional sub-table already exists strSQL text ; the BEGIN - obtaining from the call parameter time_column_name time_column_name: = TG_ARGV [ 0 ] ; - Analyzing the corresponding partition table already exists the EXECUTE ' the SELECT. 1 $. ' || time_column_name the INTO strSQL the USING NEW; curMMDD: = TO_CHAR (strSQL :: timestamp , ' YYYYMMDD ' ); SELECT COUNT ( . 1 ) the INTO ISEXIST from the pg_class entry that WHERE relname = ( ' t_audit_ ' || curMMDD); - if there create sub-partitions insert the iF (ISEXIST = to false) tHEN -Create a child inherits the parent partition table table strSQL: = ' the CREATE TABLE IF the NOT EXISTS t_audit_ ' || curMMDD || ' () INHERITS (t_audit); ' ; EXECUTE strSQL; - creating an index strSQL: = ' the CREATE INDEX t_audit_ ' || curMMDD || ' _INDEX_ ' || time_column_name || ' ON t_audit_ ' || curMMDD || ' ( ' || time_column_name || '); ' ; The EXECUTE strSQL; the END the IF ; - inserting data into the child partition! StrSQL: = ' the INSERT the INTO t_audit_ ' || curMMDD || ' the SELECT * $ 1. ' ; the EXECUTE strSQL the USING NEW; the RETURN NULL ; the END $ $ the BODY plpgsql LANGUAGE; - create a trigger, trigger the operation INSERT the cREATE tRIGGER insert_tbl_partition_trigger the BEFORE INSERT ON t_audit the FOR EACH ROW EXECUTE PROCEDURE auto_insert_into_tbl_partition('time');
The whole is this code, you can complete a simple operation according to the schedule.