feat: introduce configurable future partition buffer and add monitoring for future partitions.
This commit is contained in:
@@ -11,6 +11,7 @@ CREATE TABLE IF NOT EXISTS partitions.config (
|
||||
table_name text NOT NULL,
|
||||
period text NOT NULL CHECK (period IN ('day', 'week', 'month', 'year')),
|
||||
keep_history interval NOT NULL,
|
||||
future_partitions integer NOT NULL DEFAULT 5,
|
||||
last_updated timestamp WITH TIME ZONE DEFAULT now(),
|
||||
PRIMARY KEY (table_name)
|
||||
);
|
||||
|
||||
@@ -105,7 +105,8 @@ $$;
|
||||
CREATE OR REPLACE PROCEDURE partitions.maintain_table(
|
||||
p_table_name text,
|
||||
p_period text,
|
||||
p_keep_history interval
|
||||
p_keep_history interval,
|
||||
p_future_partitions integer DEFAULT 5
|
||||
) LANGUAGE plpgsql AS $$
|
||||
DECLARE
|
||||
v_start_time timestamp with time zone;
|
||||
@@ -139,8 +140,8 @@ BEGIN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
-- 1. Create Future Partitions (Current + 3 ahead)
|
||||
FOR i IN 0..3 LOOP
|
||||
-- 1. Create Future Partitions (Current + Buffer)
|
||||
FOR i IN 0..p_future_partitions LOOP
|
||||
CALL partitions.create_partition(
|
||||
p_table_name,
|
||||
v_start_time + (i * v_period_interval),
|
||||
@@ -176,7 +177,7 @@ DECLARE
|
||||
v_row record;
|
||||
BEGIN
|
||||
FOR v_row IN SELECT * FROM partitions.config LOOP
|
||||
CALL partitions.maintain_table(v_row.table_name, v_row.period, v_row.keep_history);
|
||||
CALL partitions.maintain_table(v_row.table_name, v_row.period, v_row.keep_history, v_row.future_partitions);
|
||||
END LOOP;
|
||||
END;
|
||||
$$;
|
||||
|
||||
@@ -27,9 +27,9 @@ BEGIN
|
||||
|
||||
-- 3. Create initial partitions
|
||||
RAISE NOTICE 'Creating initial partitions for %...', v_table;
|
||||
CALL partitions.maintain_table(v_table, v_row.period, v_row.keep_history);
|
||||
CALL partitions.maintain_table(v_table, v_row.period, v_row.keep_history, v_row.future_partitions);
|
||||
|
||||
-- 4. (Optional) Copy data?
|
||||
-- Optional: Migrate existing data
|
||||
-- EXECUTE format('INSERT INTO public.%I SELECT * FROM public.%I', v_table, v_old_table);
|
||||
|
||||
ELSIF EXISTS (SELECT 1 FROM pg_class WHERE relname = v_table AND relkind = 'p') THEN
|
||||
|
||||
@@ -10,6 +10,12 @@ SELECT
|
||||
c.period,
|
||||
c.keep_history,
|
||||
count(child.relname) AS partition_count,
|
||||
count(child.relname) FILTER (
|
||||
WHERE
|
||||
(c.period = 'day' AND child.relname > (parent.relname || '_p' || to_char(now(), 'YYYYMMDD')))
|
||||
OR
|
||||
(c.period = 'month' AND child.relname > (parent.relname || '_p' || to_char(now(), 'YYYYMM')))
|
||||
) AS future_partitions,
|
||||
pg_size_pretty(sum(pg_total_relation_size(child.oid))) AS total_size,
|
||||
min(child.relname) AS oldest_partition,
|
||||
max(child.relname) AS newest_partition,
|
||||
|
||||
Reference in New Issue
Block a user