|
148 | 148 | # Patching pgmq ownership as it resets during upgrade
|
149 | 149 | HAS_PGMQ=$(run_sql -A -t -c "select count(*) > 0 from pg_extension where extname = 'pgmq';")
|
150 | 150 | if [ "$HAS_PGMQ" = "t" ]; then
|
151 |
| - PATCH_PGMQ_QUERY=$(cat <<EOF |
152 |
| - do \$\$ |
153 |
| - declare |
154 |
| - tbl record; |
155 |
| - seq_name text; |
156 |
| - new_seq_name text; |
157 |
| - archive_table_name text; |
158 |
| - begin |
159 |
| - -- Loop through each table in the pgmq schema starting with 'q_' |
160 |
| - -- Rebuild the pkey column's default to avoid pg_dumpall segfaults |
161 |
| - for tbl in |
162 |
| - select c.relname as table_name |
163 |
| - from pg_catalog.pg_attribute a |
164 |
| - join pg_catalog.pg_class c on c.oid = a.attrelid |
165 |
| - join pg_catalog.pg_namespace n on n.oid = c.relnamespace |
166 |
| - where n.nspname = 'pgmq' |
167 |
| - and c.relname like 'q_%' |
168 |
| - and a.attname = 'msg_id' |
169 |
| - and a.attidentity in ('a', 'd') -- 'a' for ALWAYS, 'd' for BY DEFAULT |
170 |
| - loop |
171 |
| - -- Check if msg_id is an IDENTITY column for idempotency |
172 |
| - -- Define sequence names |
173 |
| - seq_name := 'pgmq.' || format ('"%s_msg_id_seq"', tbl.table_name); |
174 |
| - new_seq_name := 'pgmq.' || format ('"%s_msg_id_seq2"', tbl.table_name); |
175 |
| - archive_table_name := regexp_replace(tbl.table_name, '^q_', 'a_'); |
176 |
| - -- Execute dynamic SQL to perform the required operations |
177 |
| - execute format(' |
178 |
| - create sequence %s; |
179 |
| - select setval(''%s'', nextval(''%s'')); |
180 |
| - alter table %s."%s" alter column msg_id drop identity; |
181 |
| - alter table %s."%s" alter column msg_id set default nextval(''%s''); |
182 |
| - alter sequence %s rename to "%s";', |
183 |
| - -- Parameters for format placeholders |
184 |
| - new_seq_name, |
185 |
| - new_seq_name, seq_name, |
186 |
| - 'pgmq', tbl.table_name, |
187 |
| - 'pgmq', tbl.table_name, |
188 |
| - new_seq_name, |
189 |
| - -- alter seq |
190 |
| - new_seq_name, |
191 |
| - tbl.table_name || '_msg_id_seq' |
192 |
| - ); |
193 |
| - end loop; |
194 |
| - -- No tables should be owned by the extension. |
195 |
| - -- We want them to be included in logical backups |
196 |
| - for tbl in |
197 |
| - select c.relname as table_name |
198 |
| - from pg_class c |
199 |
| - join pg_depend d |
200 |
| - on c.oid = d.objid |
201 |
| - join pg_extension e |
202 |
| - on d.refobjid = e.oid |
203 |
| - where |
204 |
| - c.relkind in ('r', 'p', 'u') |
205 |
| - and e.extname = 'pgmq' |
206 |
| - and (c.relname like 'q_%' or c.relname like 'a_%') |
207 |
| - loop |
208 |
| - execute format(' |
209 |
| - alter extension pgmq drop table pgmq."%s";', |
210 |
| - tbl.table_name |
211 |
| - ); |
212 |
| - end loop; |
213 |
| - end \$\$; |
214 |
| -EOF |
215 |
| - ) |
216 |
| - |
217 |
| - run_sql -c "$PATCH_PGMQ_QUERY" |
218 | 151 | run_sql -c "update pg_extension set extowner = 'postgres'::regrole where extname = 'pgmq';"
|
219 | 152 | fi
|
220 | 153 |
|
|
0 commit comments