Skip to content

Commit bd5ddbe

Browse files
committed
Fix ALTER EXTENSION SET SCHEMA with objects outside an extension's schema
As coded, the code would use as a base comparison the namespace OID from the first object scanned in pg_depend when switching its namespace dependency entry to the new one, and use it as a base of comparison for any follow-up checks. It would also be used as the old namespace OID to switch *from* for the extension's pg_depend entry. Hence, if the first object scanned has a namespace different than the one stored in the extension, we would finish by: - Not checking that the extension objects map with the extension's schema. - Not switching the extension -> namespace dependency entry to the new namespace provided by the user, making ALTER EXTENSION ineffective. This issue exists since this command has been introduced in d9572c4 for relocatable extension, so backpatch all the way down to 11. The test case has been provided by Heikki, that I have tweaked a bit to show the effects on pg_depend for the extension. Reported-by: Heikki Linnakangas Author: Michael Paquier, Heikki Linnakangas Discussion: https://postgr.es/m/20eea594-a05b-4c31-491b-007b6fceef28@iki.fi Backpatch-through: 11
1 parent 3c963d3 commit bd5ddbe

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

src/backend/commands/extension.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,7 +2750,7 @@ AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *o
27502750
{
27512751
Oid extensionOid;
27522752
Oid nspOid;
2753-
Oid oldNspOid = InvalidOid;
2753+
Oid oldNspOid;
27542754
AclResult aclresult;
27552755
Relation extRel;
27562756
ScanKeyData key[2];
@@ -2833,6 +2833,9 @@ AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *o
28332833

28342834
objsMoved = new_object_addresses();
28352835

2836+
/* store the OID of the namespace to-be-changed */
2837+
oldNspOid = extForm->extnamespace;
2838+
28362839
/*
28372840
* Scan pg_depend to find objects that depend directly on the extension,
28382841
* and alter each one's schema.
@@ -2912,12 +2915,6 @@ AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *o
29122915
nspOid,
29132916
objsMoved);
29142917

2915-
/*
2916-
* Remember previous namespace of first object that has one
2917-
*/
2918-
if (oldNspOid == InvalidOid && dep_oldNspOid != InvalidOid)
2919-
oldNspOid = dep_oldNspOid;
2920-
29212918
/*
29222919
* If not all the objects had the same old namespace (ignoring any
29232920
* that are not in namespaces), complain.

src/test/modules/test_extensions/expected/test_extensions.out

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,49 @@ Objects in extension "test_ext_cine"
312312
table ext_cine_tab3
313313
(9 rows)
314314

315+
--
316+
-- Test extension with objects outside the extension's schema.
317+
--
318+
CREATE SCHEMA test_func_dep1;
319+
CREATE SCHEMA test_func_dep2;
320+
CREATE SCHEMA test_func_dep3;
321+
CREATE EXTENSION test_ext_req_schema1 SCHEMA test_func_dep1;
322+
ALTER FUNCTION test_func_dep1.dep_req1() SET SCHEMA test_func_dep2;
323+
SELECT pg_describe_object(classid, objid, objsubid) as obj,
324+
pg_describe_object(refclassid, refobjid, refobjsubid) as objref,
325+
deptype
326+
FROM pg_depend
327+
WHERE classid = 'pg_extension'::regclass AND
328+
objid = (SELECT oid FROM pg_extension WHERE extname = 'test_ext_req_schema1')
329+
ORDER BY 1, 2;
330+
obj | objref | deptype
331+
--------------------------------+-----------------------+---------
332+
extension test_ext_req_schema1 | schema test_func_dep1 | n
333+
(1 row)
334+
335+
-- fails, as function dep_req1 is not in the same schema as the extension.
336+
ALTER EXTENSION test_ext_req_schema1 SET SCHEMA test_func_dep3;
337+
ERROR: extension "test_ext_req_schema1" does not support SET SCHEMA
338+
DETAIL: function test_func_dep2.dep_req1() is not in the extension's schema "test_func_dep1"
339+
-- Move back the function, and the extension can be moved.
340+
ALTER FUNCTION test_func_dep2.dep_req1() SET SCHEMA test_func_dep1;
341+
ALTER EXTENSION test_ext_req_schema1 SET SCHEMA test_func_dep3;
342+
SELECT pg_describe_object(classid, objid, objsubid) as obj,
343+
pg_describe_object(refclassid, refobjid, refobjsubid) as objref,
344+
deptype
345+
FROM pg_depend
346+
WHERE classid = 'pg_extension'::regclass AND
347+
objid = (SELECT oid FROM pg_extension WHERE extname = 'test_ext_req_schema1')
348+
ORDER BY 1, 2;
349+
obj | objref | deptype
350+
--------------------------------+-----------------------+---------
351+
extension test_ext_req_schema1 | schema test_func_dep3 | n
352+
(1 row)
353+
354+
DROP EXTENSION test_ext_req_schema1 CASCADE;
355+
DROP SCHEMA test_func_dep1;
356+
DROP SCHEMA test_func_dep2;
357+
DROP SCHEMA test_func_dep3;
315358
--
316359
-- Test @extschema:extname@ syntax and no_relocate option
317360
--

src/test/modules/test_extensions/sql/test_extensions.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,38 @@ ALTER EXTENSION test_ext_cine UPDATE TO '1.1';
210210

211211
\dx+ test_ext_cine
212212

213+
--
214+
-- Test extension with objects outside the extension's schema.
215+
--
216+
CREATE SCHEMA test_func_dep1;
217+
CREATE SCHEMA test_func_dep2;
218+
CREATE SCHEMA test_func_dep3;
219+
CREATE EXTENSION test_ext_req_schema1 SCHEMA test_func_dep1;
220+
ALTER FUNCTION test_func_dep1.dep_req1() SET SCHEMA test_func_dep2;
221+
SELECT pg_describe_object(classid, objid, objsubid) as obj,
222+
pg_describe_object(refclassid, refobjid, refobjsubid) as objref,
223+
deptype
224+
FROM pg_depend
225+
WHERE classid = 'pg_extension'::regclass AND
226+
objid = (SELECT oid FROM pg_extension WHERE extname = 'test_ext_req_schema1')
227+
ORDER BY 1, 2;
228+
-- fails, as function dep_req1 is not in the same schema as the extension.
229+
ALTER EXTENSION test_ext_req_schema1 SET SCHEMA test_func_dep3;
230+
-- Move back the function, and the extension can be moved.
231+
ALTER FUNCTION test_func_dep2.dep_req1() SET SCHEMA test_func_dep1;
232+
ALTER EXTENSION test_ext_req_schema1 SET SCHEMA test_func_dep3;
233+
SELECT pg_describe_object(classid, objid, objsubid) as obj,
234+
pg_describe_object(refclassid, refobjid, refobjsubid) as objref,
235+
deptype
236+
FROM pg_depend
237+
WHERE classid = 'pg_extension'::regclass AND
238+
objid = (SELECT oid FROM pg_extension WHERE extname = 'test_ext_req_schema1')
239+
ORDER BY 1, 2;
240+
DROP EXTENSION test_ext_req_schema1 CASCADE;
241+
DROP SCHEMA test_func_dep1;
242+
DROP SCHEMA test_func_dep2;
243+
DROP SCHEMA test_func_dep3;
244+
213245
--
214246
-- Test @extschema:extname@ syntax and no_relocate option
215247
--

0 commit comments

Comments
 (0)