From 2a778f7fd265f3100435a0d528681d39978e47c6 Mon Sep 17 00:00:00 2001 From: Oliver Rice Date: Mon, 16 Dec 2024 12:40:45 -0600 Subject: [PATCH] pgmq migration safety (#1380) * improve pgmq migration safety * normalize whitespace --- .../20241215003910_backfill_pgmq_metadata.sql | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql b/migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql index 652be2a1d..59ff0bf3a 100644 --- a/migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql +++ b/migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql @@ -3,10 +3,24 @@ do $$ begin -- Check if the pgmq.meta table exists if exists ( - select 1 - from pg_catalog.pg_class c - join pg_catalog.pg_namespace n on c.relnamespace = n.oid - where n.nspname = 'pgmq' and c.relname = 'meta' + select + 1 + from + pg_catalog.pg_class c + join pg_catalog.pg_namespace n + on c.relnamespace = n.oid + where + n.nspname = 'pgmq' + and c.relname = 'meta' + and c.relkind = 'r' -- regular table + -- Make sure only expected columns exist and are correctly named + and ( + select array_agg(attname::text order by attname) + from pg_catalog.pg_attribute a + where + a.attnum > 0 + and a.attrelid = c.oid + ) = array['created_at', 'is_partitioned', 'is_unlogged', 'queue_name']::text[] ) then -- Insert data into pgmq.meta for all tables matching the naming pattern 'pgmq.q_' insert into pgmq.meta (queue_name, is_partitioned, is_unlogged, created_at) @@ -20,9 +34,10 @@ begin join pg_catalog.pg_namespace n on c.relnamespace = n.oid where - n.nspname = 'pgmq' - and c.relname like 'q_%' - and c.relkind in ('r', 'p', 'u'); + n.nspname = 'pgmq' + and c.relname like 'q_%' + and c.relkind in ('r', 'p', 'u') + on conflict (queue_name) do nothing; end if; end $$;