51 lines
2.1 KiB
SQL
51 lines
2.1 KiB
SQL
-- ============================================================================
|
|
-- Converts Zabbix tables to Partitioned tables.
|
|
-- WARNING: This renames existing tables to *_old.
|
|
-- ============================================================================
|
|
|
|
DO $$
|
|
DECLARE
|
|
v_row record;
|
|
v_table text;
|
|
v_old_table text;
|
|
v_pk_sql text;
|
|
v_schema text;
|
|
BEGIN
|
|
FOR v_row IN SELECT * FROM partitions.config LOOP
|
|
v_table := v_row.table_name;
|
|
v_old_table := v_table || '_old';
|
|
|
|
-- Determine schema
|
|
SELECT n.nspname INTO v_schema
|
|
FROM pg_class c
|
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
WHERE c.relname = v_table;
|
|
|
|
|
|
-- Check if table exists and is NOT already partitioned
|
|
IF EXISTS (SELECT 1 FROM pg_class WHERE relname = v_table AND relkind = 'r') THEN
|
|
RAISE NOTICE 'Converting table % to partitioned table...', v_table;
|
|
|
|
-- 1. Rename existing table
|
|
EXECUTE format('ALTER TABLE %I.%I RENAME TO %I', v_schema, v_table, v_old_table);
|
|
|
|
-- 2. Create new partitioned table (copying structure)
|
|
EXECUTE format('CREATE TABLE %I.%I (LIKE %I.%I INCLUDING ALL) PARTITION BY RANGE (clock)', v_schema, v_table, v_schema, v_old_table);
|
|
|
|
-- 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, v_row.future_partitions);
|
|
|
|
-- Optional: Migrate existing data
|
|
-- EXECUTE format('INSERT INTO %I.%I SELECT * FROM %I.%I', v_schema, v_table, v_schema, v_old_table);
|
|
|
|
ELSIF EXISTS (SELECT 1 FROM pg_class WHERE relname = v_table AND relkind = 'p') THEN
|
|
RAISE NOTICE 'Table % is already partitioned. Skipping conversion.', v_table;
|
|
-- Just run maintenance to ensure partitions exist
|
|
CALL partitions.run_maintenance();
|
|
ELSE
|
|
RAISE WARNING 'Table % not found!', v_table;
|
|
END IF;
|
|
END LOOP;
|
|
END $$;
|