Comment 0 for bug 2007794

Revision history for this message
wycitox incox (wycitox) wrote : pg_conftool does not "recognize" lines for logging_collector

While configuring Postgres for our Docker containers running Ubuntu/Postgres15 (new deployment) we have found a peculiar problem.

When setting the "logging_collector = on/off" we have found pg_conftool uncomments the incorrect line, please note this does not happen to any other line, but very well may as we do not change all default settings.

Here is an excerpt for /etc/postgresql/15/main/postgresql.conf referring to logging_collector:

Default install:
------------------------------------
446 # eventlog, depending on platform.
447 # csvlog and jsonlog require
448 # logging_collector to be on.
449
450 # This is used when logging to stderr:
451 #logging_collector = off # Enable capturing of stderr, jsonlog,
452 # and csvlog into log files. Required
453 # to be on for csvlogs and jsonlogs.
-------------------------------------

Please note this ONLY happens on PG15 which happens to have comments on line 447/448 in a separate line placing "logging_collector" in the start of the sentence.

When running:

-------------------------------------
# pg_conftool /etc/postgresql/15/main/postgresql.conf set logging_collector on
-------------------------------------

It will modify the postgresql.conf but not how you would think it would; after setting logging_collector to on:

-------------------------------------
447 # csvlog and jsonlog require
448 logging_collector on
449
450 # This is used when logging to stderr:
451 #logging_collector = off # Enable capturing of stderr, jsonlog,
452 # and csvlog into log files. Required
453 # to be on for csvlogs and jsonlogs.
-------------------------------------

The incorrect line was uncommented (448), it should have uncommented line 451.

Looking into the source code for pg_conftool we found the regex used for parsing postgresql.conf is the problem, it recognizes line 448 as a valid setting line when it is indeed a comment:

-------------------------------------
448 # logging_collector to be on
-------------------------------------

Here is the snip for file ".\postgresql-common\PgCommon.pm" which handles parsing:

-------------------------------------
my $found = 0;
    # first, search for an uncommented setting
    for (my $i=0; $i <= $#lines; ++$i) {
 if ($lines[$i] =~ /^\s*($key)(\s*(?:=|\s)\s*)\w+\b((?:\s*#.*)?)/i or
     $lines[$i] =~ /^\s*($key)(\s*(?:=|\s)\s*)'[^']*'((?:\s*#.*)?)/i) {
     $lines[$i] = "$1$2$value$3\n";
     $found = 1;
     last;
 }
    }

    # now check if the setting exists as a comment; if so, change that instead
    # of appending
    if (!$found) {
 for (my $i=0; $i <= $#lines; ++$i) {
     if ($lines[$i] =~ /^\s*#\s*($key)(\s*(?:=|\s)\s*)\w+\b((?:\s*#.*)?)/i or
  $lines[$i] =~ /^\s*#\s*($key)(\s*(?:=|\s)\s*)'[^']*'((?:\s*#.*)?)/i) {
  $lines[$i] = "$1$2$value$3\n";
  $found = 1;
  last;
     }
 }
-------------------------------------