From 9ebf93cc262ea4d969e4095707c7c090486497ba Mon Sep 17 00:00:00 2001 From: Mike Reeves Date: Mon, 10 Aug 2026 15:55:07 -0400 Subject: [PATCH] Empty a default and create its partition in one transaction Telegraf never stops writing. Clearing 50 defaults with separate TRUNCATEs left the earliest ones refilled by the time maintenance tried to attach today's child, which then failed on the default's constraint and aborted the whole run. Doing both under one transaction makes the concurrent inserts wait and land in the new partition. --- salt/postgres/tools/sbin/so-telegraf-repair | 32 +++++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/salt/postgres/tools/sbin/so-telegraf-repair b/salt/postgres/tools/sbin/so-telegraf-repair index ddfa5f7d7..75004402e 100644 --- a/salt/postgres/tools/sbin/so-telegraf-repair +++ b/salt/postgres/tools/sbin/so-telegraf-repair @@ -183,16 +183,30 @@ fi if [[ "$stranded" -gt 0 ]]; then echo "Clearing default partitions." + # One transaction: Telegraf is still writing, so a default emptied without + # its partition in place is refilled before maintenance can attach one. psql_tg -v ON_ERROR_STOP=1 <<'EOSQL' -SELECT format('TRUNCATE TABLE %I.%I', n.nspname, c.relname) -FROM partman.part_config pc -JOIN pg_class p ON p.oid = pc.parent_table::regclass -JOIN pg_inherits i ON i.inhparent = p.oid -JOIN pg_class c ON c.oid = i.inhrelid -JOIN pg_namespace n ON n.oid = c.relnamespace -WHERE pc.parent_table LIKE 'telegraf.%' - AND pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT' -\gexec +DO $$ +DECLARE + r record; +BEGIN + FOR r IN + SELECT pc.parent_table, + format('%I.%I', n.nspname, c.relname) AS default_table + FROM partman.part_config pc + JOIN pg_class p ON p.oid = pc.parent_table::regclass + JOIN pg_inherits i ON i.inhparent = p.oid + JOIN pg_class c ON c.oid = i.inhrelid + JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE pc.parent_table LIKE 'telegraf.%' + AND pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT' + LOOP + EXECUTE format('TRUNCATE TABLE %s', r.default_table); + PERFORM partman.create_partition_time( + r.parent_table, ARRAY[date_trunc('day', now())]::timestamptz[]); + END LOOP; +END +$$; EOSQL fi