Skip to content

Commit 15830fa

Browse files
committed
fix(pgmq): override drop_queue to conditionally drop objects
We introduced a patch to override pgmq.drop_queue(TEXT) to conditionally drop objects only if they are part of the extension in `ansible/ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql` . This script has been installed together with existing 1.4.4 extensions. When the user tries to upgrade pgmq from 1.4.4 to 1.5.1, the upgrade process will fail because the upgrade script doesn't expect `pgmq.drop_queue(TEXT)` to be exist. This change introduce the same patch present in `æfter-creat.Sql` in the upgrade from 1.4.5 to 1.5.0 to make sure the upgrade process will keep our custom behavior.
1 parent 1b91692 commit 15830fa

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
From bffc70636d34584158cd40c969048cacfcc1661d Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Roche?= <[email protected]>
3+
Date: Thu, 9 Oct 2025 00:33:22 +0200
4+
Subject: [PATCH] feat: override pgmq.drop_queue(TEXT) to conditionally drop
5+
objects only if they are part of the extension
6+
7+
Override the pgmq.drop_queue to check if relevant tables are owned
8+
by the pgmq extension before attempting to run
9+
`alter extension pgmq drop table ...`
10+
this is necessary because, to enable nightly logical backups to include user queues
11+
we automatically detach them from pgmq.
12+
13+
this update is backwards compatible with version 1.4.4 but should be removed once we're on
14+
physical backups everywhere
15+
---
16+
pgmq-extension/sql/pgmq--1.4.5--1.5.0.sql | 55 ++++++++++++++++++++++-
17+
1 file changed, 54 insertions(+), 1 deletion(-)
18+
19+
diff --git a/pgmq-extension/sql/pgmq--1.4.5--1.5.0.sql b/pgmq-extension/sql/pgmq--1.4.5--1.5.0.sql
20+
index 5754eed..3053a3d 100644
21+
--- a/pgmq-extension/sql/pgmq--1.4.5--1.5.0.sql
22+
+++ b/pgmq-extension/sql/pgmq--1.4.5--1.5.0.sql
23+
@@ -122,7 +122,7 @@ BEGIN
24+
END;
25+
$$ LANGUAGE plpgsql;
26+
27+
-CREATE FUNCTION pgmq.drop_queue(queue_name TEXT)
28+
+CREATE OR REPLACE FUNCTION pgmq.drop_queue(queue_name TEXT)
29+
RETURNS BOOLEAN AS $$
30+
DECLARE
31+
qtable TEXT := pgmq.format_table_name(queue_name, 'q');
32+
@@ -139,6 +139,59 @@ BEGIN
33+
queue_name
34+
) INTO partitioned;
35+
36+
+ -- NEW CONDITIONAL CHECK
37+
+ if exists (
38+
+ select 1
39+
+ from pg_class c
40+
+ join pg_depend d on c.oid = d.objid
41+
+ join pg_extension e on d.refobjid = e.oid
42+
+ where c.relname = qtable and e.extname = 'pgmq'
43+
+ ) then
44+
+
45+
+ EXECUTE FORMAT(
46+
+ $QUERY$
47+
+ ALTER EXTENSION pgmq DROP TABLE pgmq.%I
48+
+ $QUERY$,
49+
+ qtable
50+
+ );
51+
+
52+
+ end if;
53+
+
54+
+ -- NEW CONDITIONAL CHECK
55+
+ if exists (
56+
+ select 1
57+
+ from pg_class c
58+
+ join pg_depend d on c.oid = d.objid
59+
+ join pg_extension e on d.refobjid = e.oid
60+
+ where c.relname = qtable_seq and e.extname = 'pgmq'
61+
+ ) then
62+
+ EXECUTE FORMAT(
63+
+ $QUERY$
64+
+ ALTER EXTENSION pgmq DROP SEQUENCE pgmq.%I
65+
+ $QUERY$,
66+
+ qtable_seq
67+
+ );
68+
+
69+
+ end if;
70+
+
71+
+ -- NEW CONDITIONAL CHECK
72+
+ if exists (
73+
+ select 1
74+
+ from pg_class c
75+
+ join pg_depend d on c.oid = d.objid
76+
+ join pg_extension e on d.refobjid = e.oid
77+
+ where c.relname = atable and e.extname = 'pgmq'
78+
+ ) then
79+
+
80+
+ EXECUTE FORMAT(
81+
+ $QUERY$
82+
+ ALTER EXTENSION pgmq DROP TABLE pgmq.%I
83+
+ $QUERY$,
84+
+ atable
85+
+ );
86+
+
87+
+ end if;
88+
+
89+
EXECUTE FORMAT(
90+
$QUERY$
91+
ALTER EXTENSION pgmq DROP TABLE pgmq.%I
92+
--
93+
2.51.0
94+

nix/ext/pgmq.nix renamed to nix/ext/pgmq/default.nix

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let
99
pname = "pgmq";
1010

1111
# Load version configuration from external file
12-
allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).${pname};
12+
allVersions = (builtins.fromJSON (builtins.readFile ../versions.json)).${pname};
1313

1414
# Filter versions compatible with current PostgreSQL version
1515
supportedVersions = lib.filterAttrs (
@@ -37,6 +37,10 @@ let
3737
inherit hash;
3838
};
3939

40+
patches = lib.optionals (version == "1.5.1") [
41+
./0001-feat-override-pgmq.drop_queue-TEXT-to-conditionally-.patch
42+
];
43+
4044
buildPhase = ''
4145
cd pgmq-extension
4246
'';

nix/packages/postgres.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
../ext/pgroonga.nix
2323
../ext/index_advisor.nix
2424
../ext/wal2json.nix
25-
../ext/pgmq.nix
25+
../ext/pgmq
2626
../ext/pg_repack.nix
2727
../ext/pg-safeupdate.nix
2828
../ext/plpgsql-check.nix

0 commit comments

Comments
 (0)