|
82 | 82 | # Patching pgmq ownership as it resets during upgrade
|
83 | 83 | HAS_PGMQ=$(run_sql -A -t -c "select count(*) > 0 from pg_extension where extname = 'pgmq';")
|
84 | 84 | if [ "$HAS_PGMQ" = "t" ]; then
|
85 |
| - PATCH_PGMQ_QUERY=$(cat <<EOF |
86 |
| - do \$\$ |
87 |
| - declare |
88 |
| - tbl record; |
89 |
| - seq_name text; |
90 |
| - new_seq_name text; |
91 |
| - archive_table_name text; |
92 |
| - begin |
93 |
| - -- Loop through each table in the pgmq schema starting with 'q_' |
94 |
| - -- Rebuild the pkey column's default to avoid pg_dumpall segfaults |
95 |
| - for tbl in |
96 |
| - select c.relname as table_name |
97 |
| - from pg_catalog.pg_attribute a |
98 |
| - join pg_catalog.pg_class c on c.oid = a.attrelid |
99 |
| - join pg_catalog.pg_namespace n on n.oid = c.relnamespace |
100 |
| - where n.nspname = 'pgmq' |
101 |
| - and c.relname like 'q_%' |
102 |
| - and a.attname = 'msg_id' |
103 |
| - and a.attidentity in ('a', 'd') -- 'a' for ALWAYS, 'd' for BY DEFAULT |
104 |
| - loop |
105 |
| - -- Check if msg_id is an IDENTITY column for idempotency |
106 |
| - -- Define sequence names |
107 |
| - seq_name := 'pgmq.' || format ('"%s_msg_id_seq"', tbl.table_name); |
108 |
| - new_seq_name := 'pgmq.' || format ('"%s_msg_id_seq2"', tbl.table_name); |
109 |
| - archive_table_name := regexp_replace(tbl.table_name, '^q_', 'a_'); |
110 |
| - -- Execute dynamic SQL to perform the required operations |
111 |
| - execute format(' |
112 |
| - create sequence %s; |
113 |
| - select setval(''%s'', nextval(''%s'')); |
114 |
| - alter table %s."%s" alter column msg_id drop identity; |
115 |
| - alter table %s."%s" alter column msg_id set default nextval(''%s''); |
116 |
| - alter sequence %s rename to "%s";', |
117 |
| - -- Parameters for format placeholders |
118 |
| - new_seq_name, |
119 |
| - new_seq_name, seq_name, |
120 |
| - 'pgmq', tbl.table_name, |
121 |
| - 'pgmq', tbl.table_name, |
122 |
| - new_seq_name, |
123 |
| - -- alter seq |
124 |
| - new_seq_name, |
125 |
| - tbl.table_name || '_msg_id_seq' |
126 |
| - ); |
127 |
| - end loop; |
128 |
| - -- No tables should be owned by the extension. |
129 |
| - -- We want them to be included in logical backups |
130 |
| - for tbl in |
131 |
| - select c.relname as table_name |
132 |
| - from pg_class c |
133 |
| - join pg_depend d |
134 |
| - on c.oid = d.objid |
135 |
| - join pg_extension e |
136 |
| - on d.refobjid = e.oid |
137 |
| - where |
138 |
| - c.relkind in ('r', 'p', 'u') |
139 |
| - and e.extname = 'pgmq' |
140 |
| - and (c.relname like 'q_%' or c.relname like 'a_%') |
141 |
| - loop |
142 |
| - execute format(' |
143 |
| - alter extension pgmq drop table pgmq."%s";', |
144 |
| - tbl.table_name |
145 |
| - ); |
146 |
| - end loop; |
147 |
| - end \$\$; |
148 |
| -EOF |
149 |
| - ) |
150 |
| - |
151 |
| - run_sql -c "$PATCH_PGMQ_QUERY" |
152 | 85 | run_sql -c "update pg_extension set extowner = 'postgres'::regrole where extname = 'pgmq';"
|
153 | 86 | fi
|
154 | 87 |
|
|
0 commit comments