Skip to content

Commit f8c9a08

Browse files
committed
Fix ALTER SYSTEM to cope with duplicate entries in postgresql.auto.conf.
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
1 parent f2bdfeb commit f8c9a08

File tree

1 file changed

+22
-25
lines changed
  • src/backend/utils/misc

1 file changed

+22
-25
lines changed

src/backend/utils/misc/guc.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7046,40 +7046,37 @@ replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
70467046
const char *name, const char *value)
70477047
{
70487048
ConfigVariable *item,
7049+
*next,
70497050
*prev = NULL;
70507051

7051-
/* Search the list for an existing match (we assume there's only one) */
7052-
for (item = *head_p; item != NULL; item = item->next)
7052+
/*
7053+
* Remove any existing match(es) for "name". Normally there'd be at most
7054+
* one, but if external tools have modified the config file, there could
7055+
* be more.
7056+
*/
7057+
for (item = *head_p; item != NULL; item = next)
70537058
{
7054-
if (strcmp(item->name, name) == 0)
7059+
next = item->next;
7060+
if (guc_name_compare(item->name, name) == 0)
70557061
{
7056-
/* found a match, replace it */
7057-
pfree(item->value);
7058-
if (value != NULL)
7059-
{
7060-
/* update the parameter value */
7061-
item->value = pstrdup(value);
7062-
}
7062+
/* found a match, delete it */
7063+
if (prev)
7064+
prev->next = next;
70637065
else
7064-
{
7065-
/* delete the configuration parameter from list */
7066-
if (*head_p == item)
7067-
*head_p = item->next;
7068-
else
7069-
prev->next = item->next;
7070-
if (*tail_p == item)
7071-
*tail_p = prev;
7066+
*head_p = next;
7067+
if (next == NULL)
7068+
*tail_p = prev;
70727069

7073-
pfree(item->name);
7074-
pfree(item->filename);
7075-
pfree(item);
7076-
}
7077-
return;
7070+
pfree(item->name);
7071+
pfree(item->value);
7072+
pfree(item->filename);
7073+
pfree(item);
70787074
}
7079-
prev = item;
7075+
else
7076+
prev = item;
70807077
}
70817078

7082-
/* Not there; no work if we're trying to delete it */
7079+
/* Done if we're trying to delete it */
70837080
if (value == NULL)
70847081
return;
70857082

0 commit comments

Comments
 (0)