Add function to safely modify postgresql.auto.conf

This is required for PostgreSQL 12 and later.
This commit is contained in:
Ian Barwick
2019-08-14 16:57:42 +09:00
parent 4ebc43fd63
commit f5044465cb
7 changed files with 190 additions and 27 deletions

View File

@@ -23,6 +23,9 @@
#include "configfile.h"
#include "log.h"
#include <utils/elog.h>
#include <storage/fd.h> /* for durable_rename() */
const static char *_progname = NULL;
char config_file_path[MAXPGPATH] = "";
static bool config_file_provided = false;
@@ -462,7 +465,7 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *
exit(ERR_BAD_CONFIG);
}
(void) ProcessConfigFile(fp, config_file_path, options, error_list, warning_list);
(void) ProcessRepmgrConfigFile(fp, config_file_path, options, error_list, warning_list);
fclose(fp);
@@ -1800,6 +1803,7 @@ parse_bool(const char *s, const char *config_item, ItemList *error_list)
return false;
}
/*
* Split argument into old_dir and new_dir and append to tablespace mapping
* list.
@@ -1867,6 +1871,86 @@ tablespace_list_append(t_configuration_options *options, const char *arg)
}
bool
modify_auto_conf(const char *data_dir)
{
PQExpBufferData auto_conf;
PQExpBufferData auto_conf_tmp;
PQExpBufferData auto_conf_contents;
FILE *fp;
KeyValueList config = {NULL, NULL};
KeyValueListCell *cell = NULL;
initPQExpBuffer(&auto_conf);
appendPQExpBuffer(&auto_conf, "%s/%s",
data_dir, PG_AUTOCONF_FILENAME);
fp = fopen(auto_conf.data, "r");
if (fp == NULL)
{
fprintf(stderr, "unable to open \"%s\": %s\n",
auto_conf.data,
strerror(errno));
termPQExpBuffer(&auto_conf);
return false;
}
(void) ProcessPostgresConfigFile(fp, auto_conf.data, &config, NULL, NULL);
fclose(fp);
initPQExpBuffer(&auto_conf_tmp);
appendPQExpBuffer(&auto_conf_tmp, "%s.tmp",
auto_conf.data);
initPQExpBuffer(&auto_conf_contents);
/*
* Keep this in sync with src/backend/utils/misc/guc.c:write_auto_conf_file()
*/
appendPQExpBufferStr(&auto_conf_contents,
"# Do not edit this file manually!\n"
"# It will be overwritten by the ALTER SYSTEM command.\n");
for (cell = config.head; cell; cell = cell->next)
{
appendPQExpBuffer(&auto_conf_contents,
"%s = '%s'\n",
cell->key, cell->value);
}
fp = fopen(auto_conf_tmp.data, "w");
if (fp == NULL)
{
fprintf(stderr, "unable to open \"%s\": %s\n",
auto_conf_tmp.data,
strerror(errno));
}
else
{
if (fwrite(auto_conf_contents.data, strlen(auto_conf_contents.data) + 1, 1, fp) != 1)
{
fclose(fp);
}
else
{
fclose(fp);
(void) durable_rename(auto_conf_tmp.data, auto_conf.data, LOG);
}
}
termPQExpBuffer(&auto_conf);
termPQExpBuffer(&auto_conf_tmp);
termPQExpBuffer(&auto_conf_contents);
key_value_list_free(&config);
return true;
}
/*
* parse_event_notifications_list()
@@ -2043,9 +2127,6 @@ free_parsed_argv(char ***argv_array)
}
bool
parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_options *backup_options, int server_version_num, ItemList *error_list)
{