]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Allow resetting unknown custom GUCs with reserved prefixes.
authorNathan Bossart <nathan@postgresql.org>
Fri, 1 Aug 2025 21:52:11 +0000 (16:52 -0500)
committerNathan Bossart <nathan@postgresql.org>
Fri, 1 Aug 2025 21:52:11 +0000 (16:52 -0500)
Currently, ALTER DATABASE/ROLE/SYSTEM RESET [ALL] with an unknown
custom GUC with a prefix reserved by MarkGUCPrefixReserved() errors
(unless a superuser runs a RESET ALL variant).  This is problematic
for cases such as an extension library upgrade that removes a GUC.
To fix, simply make sure the relevant code paths explicitly allow
it.  Note that we require superuser or privileges on the parameter
to reset it.  This is perhaps a bit more restrictive than is
necessary, but it's not clear whether further relaxing the
requirements is safe.

Oversight in commit 88103567cb.  The ALTER SYSTEM fix is dependent
on commit 2d870b4aef, which first appeared in v17.  Unfortunately,
back-patching that commit would introduce ABI breakage, and while
that breakage seems unlikely to bother anyone, it doesn't seem
worth the risk.  Hence, the ALTER SYSTEM part of this commit is
omitted on v15 and v16.

Reported-by: Mert Alev <mert@futo.org>
Reviewed-by: Laurenz Albe <laurenz.albe@cybertec.at>
Discussion: https://postgr.es/m/18964-ba09dea8c98fccd6%40postgresql.org
Backpatch-through: 15

contrib/auto_explain/Makefile
contrib/auto_explain/expected/alter_reset.out [new file with mode: 0644]
contrib/auto_explain/meson.build
contrib/auto_explain/sql/alter_reset.sql [new file with mode: 0644]
src/backend/utils/misc/guc.c

index efd127d3cae640904124590ebbd073aff5b729b0..94ab28e7c06b91f06ce295fb1dbc96a84ab592f8 100644 (file)
@@ -6,6 +6,8 @@ OBJS = \
        auto_explain.o
 PGFILEDESC = "auto_explain - logging facility for execution plans"
 
+REGRESS = alter_reset
+
 TAP_TESTS = 1
 
 ifdef USE_PGXS
diff --git a/contrib/auto_explain/expected/alter_reset.out b/contrib/auto_explain/expected/alter_reset.out
new file mode 100644 (file)
index 0000000..ec35518
--- /dev/null
@@ -0,0 +1,19 @@
+--
+-- This tests resetting unknown custom GUCs with reserved prefixes.  There's
+-- nothing specific to auto_explain; this is just a convenient place to put
+-- this test.
+--
+SELECT current_database() AS datname \gset
+CREATE ROLE regress_ae_role;
+ALTER DATABASE :"datname" SET auto_explain.bogus = 1;
+ALTER ROLE regress_ae_role SET auto_explain.bogus = 1;
+ALTER ROLE regress_ae_role IN DATABASE :"datname" SET auto_explain.bogus = 1;
+ALTER SYSTEM SET auto_explain.bogus = 1;
+LOAD 'auto_explain';
+WARNING:  invalid configuration parameter name "auto_explain.bogus", removing it
+DETAIL:  "auto_explain" is now a reserved prefix.
+ALTER DATABASE :"datname" RESET auto_explain.bogus;
+ALTER ROLE regress_ae_role RESET auto_explain.bogus;
+ALTER ROLE regress_ae_role IN DATABASE :"datname" RESET auto_explain.bogus;
+ALTER SYSTEM RESET auto_explain.bogus;
+DROP ROLE regress_ae_role;
index 92dc9df6f7cac76e33f50933e887095a46049c14..a9b45cc235f12423405f39cf712fcbe204b0483c 100644 (file)
@@ -20,6 +20,11 @@ tests += {
   'name': 'auto_explain',
   'sd': meson.current_source_dir(),
   'bd': meson.current_build_dir(),
+  'regress': {
+    'sql': [
+      'alter_reset',
+    ],
+  },
   'tap': {
     'tests': [
       't/001_auto_explain.pl',
diff --git a/contrib/auto_explain/sql/alter_reset.sql b/contrib/auto_explain/sql/alter_reset.sql
new file mode 100644 (file)
index 0000000..bf62145
--- /dev/null
@@ -0,0 +1,22 @@
+--
+-- This tests resetting unknown custom GUCs with reserved prefixes.  There's
+-- nothing specific to auto_explain; this is just a convenient place to put
+-- this test.
+--
+
+SELECT current_database() AS datname \gset
+CREATE ROLE regress_ae_role;
+
+ALTER DATABASE :"datname" SET auto_explain.bogus = 1;
+ALTER ROLE regress_ae_role SET auto_explain.bogus = 1;
+ALTER ROLE regress_ae_role IN DATABASE :"datname" SET auto_explain.bogus = 1;
+ALTER SYSTEM SET auto_explain.bogus = 1;
+
+LOAD 'auto_explain';
+
+ALTER DATABASE :"datname" RESET auto_explain.bogus;
+ALTER ROLE regress_ae_role RESET auto_explain.bogus;
+ALTER ROLE regress_ae_role IN DATABASE :"datname" RESET auto_explain.bogus;
+ALTER SYSTEM RESET auto_explain.bogus;
+
+DROP ROLE regress_ae_role;
index 667df448732f2e70dbee73ef0231d5de321de149..ce5449f287853305082c84deaf2975950fdd1c5b 100644 (file)
@@ -4722,8 +4722,13 @@ AlterSystemSetConfigFile(AlterSystemStmt *altersysstmt)
                         * the config file cannot cause postmaster start to fail, so we
                         * don't have to be too tense about possibly installing a bad
                         * value.)
+                        *
+                        * As an exception, we skip this check if this is a RESET command
+                        * for an unknown custom GUC, else there'd be no way for users to
+                        * remove such settings with reserved prefixes.
                         */
-                       (void) assignable_custom_variable_name(name, false, ERROR);
+                       if (value || !valid_custom_variable_name(name))
+                               (void) assignable_custom_variable_name(name, false, ERROR);
                }
 
                /*
@@ -6711,6 +6716,7 @@ validate_option_array_item(const char *name, const char *value,
 
 {
        struct config_generic *gconf;
+       bool            reset_custom;
 
        /*
         * There are three cases to consider:
@@ -6729,16 +6735,21 @@ validate_option_array_item(const char *name, const char *value,
         * it's assumed to be fully validated.)
         *
         * name is not known and can't be created as a placeholder.  Throw error,
-        * unless skipIfNoPermissions is true, in which case return false.
+        * unless skipIfNoPermissions or reset_custom is true.  If reset_custom is
+        * true, this is a RESET or RESET ALL operation for an unknown custom GUC
+        * with a reserved prefix, in which case we want to fall through to the
+        * placeholder case described in the preceding paragraph (else there'd be
+        * no way for users to remove them).  Otherwise, return false.
         */
-       gconf = find_option(name, true, skipIfNoPermissions, ERROR);
-       if (!gconf)
+       reset_custom = (!value && valid_custom_variable_name(name));
+       gconf = find_option(name, true, skipIfNoPermissions || reset_custom, ERROR);
+       if (!gconf && !reset_custom)
        {
                /* not known, failed to make a placeholder */
                return false;
        }
 
-       if (gconf->flags & GUC_CUSTOM_PLACEHOLDER)
+       if (!gconf || gconf->flags & GUC_CUSTOM_PLACEHOLDER)
        {
                /*
                 * We cannot do any meaningful check on the value, so only permissions