test: update generated init_scripts

This commit is contained in:
Maksym Buz
2026-04-30 10:50:36 +00:00
parent c401efea51
commit 345495eaf5
4 changed files with 53 additions and 24 deletions

View File

@@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS partitions.version (
description text description text
); );
INSERT INTO partitions.version (version, description) VALUES ('7-1', 'Zabbix 7.4 and 7.0 compatible version') INSERT INTO partitions.version (version, description) VALUES ('7-2', 'Added housekeeper task interceptor trigger to drop tasks for partitioned tables')
ON CONFLICT (version) DO NOTHING; ON CONFLICT (version) DO NOTHING;
-- Default configuration for Zabbix tables (adjust as needed) -- Default configuration for Zabbix tables (adjust as needed)
@@ -32,9 +32,16 @@ INSERT INTO partitions.config (table_name, period, keep_history) VALUES
('history_uint', 'day', '30 days'), ('history_uint', 'day', '30 days'),
('history_str', 'day', '30 days'), ('history_str', 'day', '30 days'),
('history_log', 'day', '30 days'), ('history_log', 'day', '30 days'),
('history_text', 'day', '30 days') ('history_text', 'day', '30 days'),
('history_bin', 'day', '30 days')
ON CONFLICT (table_name) DO NOTHING; ON CONFLICT (table_name) DO NOTHING;
-- Zabbix 8.0+ only: Uncomment the following lines if running Zabbix 8.0 or later
-- INSERT INTO partitions.config (table_name, period, keep_history) VALUES
-- ('history_json', 'day', '30 days')
-- ON CONFLICT (table_name) DO NOTHING;
-- Trends tables: Monthly partitions, keep 12 months -- Trends tables: Monthly partitions, keep 12 months
INSERT INTO partitions.config (table_name, period, keep_history) VALUES INSERT INTO partitions.config (table_name, period, keep_history) VALUES
('trends', 'month', '12 months'), ('trends', 'month', '12 months'),

View File

@@ -2,14 +2,15 @@
-- Core functions for Zabbix partitioning (Create, Drop, Maintain). -- Core functions for Zabbix partitioning (Create, Drop, Maintain).
-- ============================================================================ -- ============================================================================
-- Function to check if a partition exists -- Function to check if a partition exists in a specific schema
CREATE OR REPLACE FUNCTION partitions.partition_exists(p_partition_name text) CREATE OR REPLACE FUNCTION partitions.partition_exists(p_partition_name text, p_schema text)
RETURNS boolean AS $$ RETURNS boolean AS $$
BEGIN BEGIN
RETURN EXISTS ( RETURN EXISTS (
SELECT 1 FROM pg_class c SELECT 1 FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = p_partition_name WHERE c.relname = p_partition_name
AND n.nspname = p_schema
); );
END; END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
@@ -32,7 +33,7 @@ BEGIN
SELECT n.nspname INTO v_parent_schema SELECT n.nspname INTO v_parent_schema
FROM pg_class c FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = p_parent_table; WHERE c.relname = p_parent_table AND pg_table_is_visible(c.oid);
IF NOT FOUND THEN IF NOT FOUND THEN
RAISE EXCEPTION 'Parent table % not found', p_parent_table; RAISE EXCEPTION 'Parent table % not found', p_parent_table;
@@ -51,15 +52,19 @@ BEGIN
v_partition_name := p_parent_table || '_p' || v_suffix; v_partition_name := p_parent_table || '_p' || v_suffix;
IF NOT partitions.partition_exists(v_partition_name) THEN IF NOT partitions.partition_exists(v_partition_name, v_parent_schema) THEN
BEGIN BEGIN
EXECUTE format( EXECUTE format(
'CREATE TABLE %I.%I PARTITION OF %I.%I FOR VALUES FROM (%s) TO (%s)', 'CREATE TABLE %I.%I PARTITION OF %I.%I FOR VALUES FROM (%s) TO (%s)',
v_parent_schema, v_partition_name, v_parent_schema, p_parent_table, v_start_ts, v_end_ts v_parent_schema, v_partition_name, v_parent_schema, p_parent_table, v_start_ts, v_end_ts
); );
EXCEPTION WHEN invalid_object_definition THEN EXCEPTION
-- Ignore overlap errors (e.g., when transitioning from daily to hourly partitioning) WHEN invalid_object_definition THEN
RAISE NOTICE 'Partition % overlaps with an existing partition. Skipping.', v_partition_name; -- Ignore overlap errors (e.g., when transitioning from daily to hourly partitioning)
RAISE NOTICE 'Partition % overlaps with an existing partition. Skipping.', v_partition_name;
WHEN duplicate_table THEN
-- Ignore race condition: another process created the partition concurrently
RAISE NOTICE 'Partition % already exists (concurrent creation). Skipping.', v_partition_name;
END; END;
END IF; END IF;
END; END;
@@ -89,7 +94,7 @@ BEGIN
JOIN pg_class parent ON pg_inherits.inhparent = parent.oid JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
JOIN pg_class child ON pg_inherits.inhrelid = child.oid JOIN pg_class child ON pg_inherits.inhrelid = child.oid
JOIN pg_namespace n ON child.relnamespace = n.oid JOIN pg_namespace n ON child.relnamespace = n.oid
WHERE parent.relname = p_parent_table WHERE parent.relname = p_parent_table AND pg_table_is_visible(parent.oid)
LOOP LOOP
-- Parse partition suffix to determine age -- Parse partition suffix to determine age
-- Format: parent_pYYYYMM or parent_pYYYYMMDD -- Format: parent_pYYYYMM or parent_pYYYYMMDD

View File

@@ -19,10 +19,10 @@ BEGIN
SELECT n.nspname INTO v_schema SELECT n.nspname INTO v_schema
FROM pg_class c FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = v_table; WHERE c.relname = v_table AND pg_table_is_visible(c.oid);
IF EXISTS (SELECT 1 FROM pg_class WHERE relname = v_table AND relkind = 'r') THEN IF EXISTS (SELECT 1 FROM pg_class WHERE relname = v_table AND relkind = 'r' AND pg_table_is_visible(oid)) THEN
RAISE NOTICE 'Converting table % to partitioned table...', v_table; RAISE NOTICE 'Converting table % to partitioned table...', v_table;
-- 1. Rename existing table -- 1. Rename existing table
@@ -48,19 +48,37 @@ BEGIN
-- Optional: Migrate existing data -- Optional: Migrate existing data
-- EXECUTE format('INSERT INTO %I.%I SELECT * FROM %I.%I', v_schema, v_table, v_schema, v_old_table); -- 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 ELSIF EXISTS (SELECT 1 FROM pg_class WHERE relname = v_table AND relkind = 'p' AND pg_table_is_visible(oid)) THEN
RAISE NOTICE 'Table % is already partitioned. Skipping conversion.', v_table; RAISE NOTICE 'Table % is already partitioned. Skipping conversion.', v_table;
-- Just run maintenance to ensure partitions exist -- Just run maintenance for this specific table to ensure partitions exist
CALL partitions.run_maintenance(); CALL partitions.maintain_table(v_table, v_row.period, v_row.keep_history, v_row.future_partitions);
ELSE ELSE
RAISE WARNING 'Table % not found!', v_table; RAISE WARNING 'Table % not found!', v_table;
END IF; END IF;
END LOOP; END LOOP;
-- Attach trigger to housekeeper table to silently discard tasks for partitioned tables.
-- Dynamically determine the schema of the housekeeper table to support custom schemas.
SELECT n.nspname INTO v_schema
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = 'housekeeper' AND pg_table_is_visible(c.oid);
IF v_schema IS NOT NULL THEN
EXECUTE format('DROP TRIGGER IF EXISTS housekeeper_filter ON %I.housekeeper', v_schema);
EXECUTE format('CREATE TRIGGER housekeeper_filter BEFORE INSERT ON %I.housekeeper FOR EACH ROW EXECUTE FUNCTION partitions.housekeeper_insert_trigger()', v_schema);
RAISE NOTICE 'Housekeeper intercept trigger installed on %.housekeeper', v_schema;
ELSE
RAISE WARNING 'housekeeper table not found — trigger NOT installed!';
END IF;
END $$; END $$;
-- Attach trigger to housekeeper table to silently discard tasks for partitioned tables -- ==========================================================================
DROP TRIGGER IF EXISTS housekeeper_filter ON housekeeper; -- IMPORTANT: If the Zabbix Server connects with a non-superuser (e.g., 'zabbix'),
CREATE TRIGGER housekeeper_filter -- that user MUST have access to the partitions schema for the housekeeper trigger
BEFORE INSERT ON housekeeper -- to work. Without these GRANTs, every INSERT into housekeeper will FAIL.
FOR EACH ROW -- Uncomment and adjust the username below:
EXECUTE FUNCTION partitions.housekeeper_insert_trigger(); -- ==========================================================================
-- GRANT USAGE ON SCHEMA partitions TO zabbix;
-- GRANT SELECT ON partitions.config TO zabbix;

View File

@@ -2,8 +2,7 @@
-- Creates a view to monitor partition status and sizes. -- Creates a view to monitor partition status and sizes.
-- ============================================================================ -- ============================================================================
DROP VIEW IF EXISTS partitions.monitoring; CREATE OR REPLACE VIEW partitions.monitoring AS
CREATE VIEW partitions.monitoring AS
SELECT SELECT
parent.relname AS parent_table, parent.relname AS parent_table,
c.table_name, c.table_name,
@@ -27,7 +26,7 @@ SELECT
max(child.relname) AS newest_partition, max(child.relname) AS newest_partition,
c.last_updated c.last_updated
FROM partitions.config c FROM partitions.config c
JOIN pg_class parent ON parent.relname = c.table_name JOIN pg_class parent ON parent.relname = c.table_name AND pg_table_is_visible(parent.oid)
LEFT JOIN pg_inherits ON pg_inherits.inhparent = parent.oid LEFT JOIN pg_inherits ON pg_inherits.inhparent = parent.oid
LEFT JOIN pg_class child ON pg_inherits.inhrelid = child.oid LEFT JOIN pg_class child ON pg_inherits.inhrelid = child.oid
WHERE parent.relkind = 'p' -- Only partitioned tables WHERE parent.relkind = 'p' -- Only partitioned tables