Fix ALTER SYSTEM to cope with duplicate entries in postgresql.auto.conf.
authorTom Lane <[email protected]>
Wed, 14 Aug 2019 19:09:20 +0000 (15:09 -0400)
committerTom Lane <[email protected]>
Wed, 14 Aug 2019 19:09:20 +0000 (15:09 -0400)
ALTER SYSTEM itself normally won't make duplicate entries (although
up till this patch, it was possible to confuse it by writing case
variants of a GUC's name).  However, if some external tool has appended
entries to the file, that could result in duplicate entries for a single
GUC name.  In such a situation, ALTER SYSTEM did exactly the wrong thing,
because it replaced or removed only the first matching entry, leaving
the later one(s) still there and hence still determining the active value.

This patch fixes that by making ALTER SYSTEM sweep through the file and
remove all matching entries, then (if not ALTER SYSTEM RESET) append the
new setting to the end.  This means entries will be in order of last
setting rather than first setting, but that shouldn't hurt anything.

Also, make the comparisons case-insensitive so that the right things
happen if you do, say, ALTER SYSTEM SET "TimeZone" = 'whatever'.

This has been broken since ALTER SYSTEM was invented, so back-patch
to all supported branches.

Ian Barwick, with minor mods by me

Discussion: https://postgr.es/m/aed6cc9f-98f3-2693-ac81-52bb0052307e@2ndquadrant.com

src/backend/utils/misc/guc.c

index 8265f53f41cf903643d1a24437f0ba8145f5f187..4ee9c9c029b408c7e52d72a29e60c19ad0050ffd 100644 (file)
@@ -6614,40 +6614,37 @@ replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
                          const char *name, const char *value)
 {
    ConfigVariable *item,
+              *next,
               *prev = NULL;
 
-   /* Search the list for an existing match (we assume there's only one) */
-   for (item = *head_p; item != NULL; item = item->next)
+   /*
+    * Remove any existing match(es) for "name".  Normally there'd be at most
+    * one, but if external tools have modified the config file, there could
+    * be more.
+    */
+   for (item = *head_p; item != NULL; item = next)
    {
-       if (strcmp(item->name, name) == 0)
+       next = item->next;
+       if (guc_name_compare(item->name, name) == 0)
        {
-           /* found a match, replace it */
-           pfree(item->value);
-           if (value != NULL)
-           {
-               /* update the parameter value */
-               item->value = pstrdup(value);
-           }
+           /* found a match, delete it */
+           if (prev)
+               prev->next = next;
            else
-           {
-               /* delete the configuration parameter from list */
-               if (*head_p == item)
-                   *head_p = item->next;
-               else
-                   prev->next = item->next;
-               if (*tail_p == item)
-                   *tail_p = prev;
+               *head_p = next;
+           if (next == NULL)
+               *tail_p = prev;
 
-               pfree(item->name);
-               pfree(item->filename);
-               pfree(item);
-           }
-           return;
+           pfree(item->name);
+           pfree(item->value);
+           pfree(item->filename);
+           pfree(item);
        }
-       prev = item;
+       else
+           prev = item;
    }
 
-   /* Not there; no work if we're trying to delete it */
+   /* Done if we're trying to delete it */
    if (value == NULL)
        return;