*** a/doc/src/sgml/config.sgml --- b/doc/src/sgml/config.sgml *************** *** 158,163 **** SET ENABLE_SEQSCAN TO OFF; --- 158,176 ---- require superuser permission to change via SET or ALTER. + + + Another way to change configuration parameters persistently is by + use of + command, for example: + + ALTER SYSTEM SET checkpoint_timeout TO 600; + + This command will allow users to change values persistently + through SQL command. The values will be effective after reload of server configuration + (SIGHUP) or server startup. The effect of this command is similar to when + user manually changes values in postgresql.conf. + *** a/doc/src/sgml/ref/allfiles.sgml --- b/doc/src/sgml/ref/allfiles.sgml *************** *** 30,35 **** Complete list of usable sgml source files in this directory. --- 30,36 ---- + *** /dev/null --- b/doc/src/sgml/ref/alter_system.sgml *************** *** 0 **** --- 1,114 ---- + + + + + ALTER SYSTEM + 7 + SQL - Language Statements + + + + ALTER SYSTEM + change a server configuration parameter + + + + ALTER SYSTEM + + + + + ALTER SYSTEM SET configuration_parameter { TO | = } { value | 'value' | DEFAULT } + + + + + Description + + + ALTER SYSTEM writes the configuration parameter + values to the postgresql.auto.conf file. With + DEFAULT, it removes a configuration entry from + postgresql.auto.conf file. The values will be + effective after reload of server configuration (SIGHUP) or in next + server start based on the type of configuration parameter modified. + + + + This command is not allowed inside transaction block or function. + + + + See for other ways to set the parameters and + how they become effective. + + + + + Parameters + + + + configuration_parameter + + + Name of a settable run-time parameter. Available parameters are + documented in . + + + + + + value + + + New value of parameter. Values can be specified as string + constants, identifiers, numbers, or comma-separated lists of + these, as appropriate for the particular parameter. + DEFAULT can be written to specify to remove the + parameter and its value from postgresql.auto.conf + + + + + + + + Examples + + + Set the wal_level: + + ALTER SYSTEM SET wal_level = hot_standby; + + + + + Set the authentication_timeout: + + ALTER SYSTEM SET authentication_timeout = 10; + + + + + Compatibility + + + The ALTER SYSTEM statement is a + PostgreSQL extension. + + + + + See Also + + + + + + + + *** a/doc/src/sgml/reference.sgml --- b/doc/src/sgml/reference.sgml *************** *** 58,63 **** --- 58,64 ---- &alterSchema; &alterSequence; &alterServer; + &alterSystem; &alterTable; &alterTableSpace; &alterTSConfig; *** a/doc/src/sgml/storage.sgml --- b/doc/src/sgml/storage.sgml *************** *** 34,40 **** these required items, the cluster configuration files postgresql.conf, pg_hba.conf, and pg_ident.conf are traditionally stored in PGDATA (although in PostgreSQL 8.0 and ! later, it is possible to place them elsewhere). --- 34,41 ---- postgresql.conf, pg_hba.conf, and pg_ident.conf are traditionally stored in PGDATA (although in PostgreSQL 8.0 and ! later, it is possible to place them elsewhere). By default directory config is stored ! in PGDATA, however it needs to be kept along with postgresql.conf.
*************** *** 57,62 **** Item --- 58,68 ---- + config + Subdirectory containing generated configuration files + + + base Subdirectory containing per-database subdirectories *************** *** 253,258 **** where PPP is the PID of the owning backend and --- 259,269 ---- NNN distinguishes different temporary files of that backend. + + Configuration variables changed by command ALTER SYSTEM will be stored in + PGDATA/config. + + *** a/src/backend/nodes/copyfuncs.c --- b/src/backend/nodes/copyfuncs.c *************** *** 3266,3271 **** _copyRefreshMatViewStmt(const RefreshMatViewStmt *from) --- 3266,3281 ---- return newnode; } + static AlterSystemStmt * + _copyAlterSystemStmt(const AlterSystemStmt * from) + { + AlterSystemStmt *newnode = makeNode(AlterSystemStmt); + + COPY_NODE_FIELD(setstmt); + + return newnode; + } + static CreateSeqStmt * _copyCreateSeqStmt(const CreateSeqStmt *from) { *************** *** 4339,4344 **** copyObject(const void *from) --- 4349,4357 ---- case T_RefreshMatViewStmt: retval = _copyRefreshMatViewStmt(from); break; + case T_AlterSystemStmt: + retval = _copyAlterSystemStmt(from); + break; case T_CreateSeqStmt: retval = _copyCreateSeqStmt(from); break; *** a/src/backend/nodes/equalfuncs.c --- b/src/backend/nodes/equalfuncs.c *************** *** 1533,1538 **** _equalRefreshMatViewStmt(const RefreshMatViewStmt *a, const RefreshMatViewStmt * --- 1533,1547 ---- } static bool + _equalAlterSystemStmt(const AlterSystemStmt * a, const AlterSystemStmt * b) + { + COMPARE_NODE_FIELD(setstmt); + + return true; + } + + + static bool _equalCreateSeqStmt(const CreateSeqStmt *a, const CreateSeqStmt *b) { COMPARE_NODE_FIELD(sequence); *************** *** 2806,2811 **** equal(const void *a, const void *b) --- 2815,2823 ---- case T_RefreshMatViewStmt: retval = _equalRefreshMatViewStmt(a, b); break; + case T_AlterSystemStmt: + retval = _equalAlterSystemStmt(a, b); + break; case T_CreateSeqStmt: retval = _equalCreateSeqStmt(a, b); break; *** a/src/backend/parser/gram.y --- b/src/backend/parser/gram.y *************** *** 216,222 **** static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); AlterEventTrigStmt AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt AlterFdwStmt AlterForeignServerStmt AlterGroupStmt ! AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt AlterExtensionStmt AlterExtensionContentsStmt AlterForeignTableStmt AlterCompositeTypeStmt AlterUserStmt AlterUserMappingStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt --- 216,222 ---- AlterEventTrigStmt AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt AlterFdwStmt AlterForeignServerStmt AlterGroupStmt ! AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterSystemStmt AlterTableStmt AlterExtensionStmt AlterExtensionContentsStmt AlterForeignTableStmt AlterCompositeTypeStmt AlterUserStmt AlterUserMappingStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt *************** *** 395,401 **** static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type insert_rest ! %type set_rest set_rest_more SetResetClause FunctionSetResetClause %type TableElement TypedTableElement ConstraintElem TableFuncElement %type columnDef columnOptions --- 395,401 ---- %type insert_rest ! %type generic_set set_rest set_rest_more SetResetClause FunctionSetResetClause %type TableElement TypedTableElement ConstraintElem TableFuncElement %type columnDef columnOptions *************** *** 720,725 **** stmt : --- 720,726 ---- | AlterObjectSchemaStmt | AlterOwnerStmt | AlterSeqStmt + | AlterSystemStmt | AlterTableStmt | AlterCompositeTypeStmt | AlterRoleSetStmt *************** *** 1329,1335 **** set_rest: | set_rest_more ; ! set_rest_more: /* Generic SET syntaxes: */ var_name TO var_list { VariableSetStmt *n = makeNode(VariableSetStmt); --- 1330,1336 ---- | set_rest_more ; ! generic_set: var_name TO var_list { VariableSetStmt *n = makeNode(VariableSetStmt); *************** *** 1360,1365 **** set_rest_more: /* Generic SET syntaxes: */ --- 1361,1369 ---- n->name = $1; $$ = n; } + + set_rest_more: /* Generic SET syntaxes: */ + generic_set {$$ = $1;} | var_name FROM CURRENT_P { VariableSetStmt *n = makeNode(VariableSetStmt); *************** *** 8243,8248 **** DropdbStmt: DROP DATABASE database_name --- 8247,8269 ---- /***************************************************************************** * + * ALTER SYSTEM SET + * + * Command to edit postgresql.conf + *****************************************************************************/ + + AlterSystemStmt: + ALTER SYSTEM_P SET generic_set + { + AlterSystemStmt *n = makeNode(AlterSystemStmt); + n->setstmt = $4; + $$ = (Node *)n; + } + ; + + + /***************************************************************************** + * * Manipulate a domain * *****************************************************************************/ *** a/src/backend/replication/basebackup.c --- b/src/backend/replication/basebackup.c *************** *** 803,808 **** sendDir(char *path, int basepathlen, bool sizeonly) --- 803,815 ---- strlen(PG_TEMP_FILE_PREFIX)) == 0) continue; + /* skip auto conf temporary file */ + if (strncmp(de->d_name, + PG_AUTOCONF_FILENAME ".temp", + sizeof(PG_AUTOCONF_FILENAME) + 4) == 0) + continue; + + /* * If there's a backup_label file, it belongs to a backup started by * the user with pg_start_backup(). It is *not* correct for this *** a/src/backend/tcop/utility.c --- b/src/backend/tcop/utility.c *************** *** 687,692 **** standard_ProcessUtility(Node *parsetree, --- 687,697 ---- ExplainQuery((ExplainStmt *) parsetree, queryString, params, dest); break; + case T_AlterSystemStmt: + PreventTransactionChain(isTopLevel, "ALTER SYSTEM"); + AlterSystemSetConfigFile((AlterSystemStmt *) parsetree); + break; + case T_VariableSetStmt: ExecSetVariableStmt((VariableSetStmt *) parsetree); break; *************** *** 2155,2160 **** CreateCommandTag(Node *parsetree) --- 2160,2169 ---- tag = "REFRESH MATERIALIZED VIEW"; break; + case T_AlterSystemStmt: + tag = "ALTER SYSTEM"; + break; + case T_VariableSetStmt: switch (((VariableSetStmt *) parsetree)->kind) { *************** *** 2721,2726 **** GetCommandLogLevel(Node *parsetree) --- 2730,2739 ---- lev = LOGSTMT_DDL; break; + case T_AlterSystemStmt: + lev = LOGSTMT_ALL; + break; + case T_VariableSetStmt: lev = LOGSTMT_ALL; break; *** a/src/backend/utils/misc/guc-file.l --- b/src/backend/utils/misc/guc-file.l *************** *** 50,55 **** int GUC_yylex(void); --- 50,57 ---- static int GUC_flex_fatal(const char *msg); static char *GUC_scanstr(const char *s); + static char * + AbsoluteConfigLocation(const char *location, const char *calling_file); %} *************** *** 120,125 **** ProcessConfigFile(GucContext context) --- 122,130 ---- *head, *tail; int i; + char *ConfigAutoFileName; + char Filename[MAXPGPATH]; + char *ErrorConfFile; /* * Config files are processed on startup (by the postmaster only) *************** *** 134,139 **** ProcessConfigFile(GucContext context) --- 139,146 ---- */ elevel = IsUnderPostmaster ? DEBUG2 : LOG; + ErrorConfFile = ConfigFileName; + /* Parse the file into a list of option names and values */ head = tail = NULL; *************** *** 145,150 **** ProcessConfigFile(GucContext context) --- 152,172 ---- } /* + * Parse postgresql.auto.conf file after postgresql.conf to replace + * parameters set by ALTER SYSTEM command + */ + snprintf(Filename,sizeof(Filename),"%s/%s", PG_AUTOCONF_DIR, PG_AUTOCONF_FILENAME); + ConfigAutoFileName = AbsoluteConfigLocation(Filename, ConfigFileName); + + if (!ParseConfigFile(ConfigAutoFileName, NULL, false, 0, elevel, &head, &tail)) + { + /* Syntax error(s) detected in the file, so bail out */ + error = true; + ErrorConfFile = ConfigAutoFileName; + goto cleanup_list; + } + + /* * Mark all extant GUC variables as not present in the config file. * We need this so that we can tell below which ones have been removed * from the file since we last processed it. *************** *** 348,364 **** ProcessConfigFile(GucContext context) ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("configuration file \"%s\" contains errors", ! ConfigFileName))); else if (apply) ereport(elevel, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("configuration file \"%s\" contains errors; unaffected changes were applied", ! ConfigFileName))); else ereport(elevel, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("configuration file \"%s\" contains errors; no changes were applied", ! ConfigFileName))); } } --- 370,386 ---- ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("configuration file \"%s\" contains errors", ! ErrorConfFile))); else if (apply) ereport(elevel, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("configuration file \"%s\" contains errors; unaffected changes were applied", ! ErrorConfFile))); else ereport(elevel, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("configuration file \"%s\" contains errors; no changes were applied", ! ErrorConfFile))); } } *** a/src/backend/utils/misc/guc.c --- b/src/backend/utils/misc/guc.c *************** *** 57,68 **** --- 57,70 ---- #include "postmaster/postmaster.h" #include "postmaster/syslogger.h" #include "postmaster/walwriter.h" + #include "port.h" #include "replication/syncrep.h" #include "replication/walreceiver.h" #include "replication/walsender.h" #include "storage/bufmgr.h" #include "storage/standby.h" #include "storage/fd.h" + #include "storage/lwlock.h" #include "storage/proc.h" #include "storage/predicate.h" #include "tcop/tcopprot.h" *************** *** 204,209 **** static char *config_enum_get_options(struct config_enum * record, --- 206,215 ---- const char *prefix, const char *suffix, const char *separator); + static int validate_conf_option(struct config_generic * record, + const char *name, const char *value, int elevel, + bool freemem, void *newval, void **newextra); + /* * Options for enum values defined in this module. *************** *** 3414,3419 **** static void ShowAllGUCConfig(DestReceiver *dest); --- 3420,3428 ---- static char *_ShowOption(struct config_generic * record, bool use_units); static bool validate_option_array_item(const char *name, const char *value, bool skipIfNoPermissions); + static void write_auto_conf_file(int fd, const char *filename, ConfigVariable **head_p); + static void replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p, + char *config_file, char *name, char *value); /* *************** *** 5176,5181 **** config_enum_get_options(struct config_enum * record, const char *prefix, --- 5185,5404 ---- return retstr.data; } + /* + * Validates configuration parameter and value, by calling check hook functions + * depending on record's vartype. It validates if the parameter + * value given is in range of expected predefined value for that parameter. + * + * freemem - true indicates memory for newval and newextra will be + * freed in this function, false indicates it will be freed + * by caller. + * Return value: + * 1: the value is valid + * 0: the name or value is invalid + */ + int + validate_conf_option(struct config_generic * record, + const char *name, const char *value, int elevel, + bool freemem, void *newval, void **newextra) + { + /* + * Validate the value for the passed record, to ensure it is in expected + * range. + */ + switch (record->vartype) + { + + case PGC_BOOL: + { + struct config_bool *conf = (struct config_bool *) record; + bool tmpnewval; + + if (newval == NULL) + newval = &tmpnewval; + + if (value != NULL) + { + if (!parse_bool(value, newval)) + { + ereport(elevel, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a Boolean value", + name))); + return 0; + } + + if (!call_bool_check_hook(conf, newval, newextra, + PGC_S_FILE, elevel)) + return 0; + + if (*newextra && freemem) + free(*newextra); + } + } + break; + case PGC_INT: + { + struct config_int *conf = (struct config_int *) record; + int tmpnewval; + + if (newval == NULL) + newval = &tmpnewval; + + if (value != NULL) + { + const char *hintmsg; + + if (!parse_int(value, newval, conf->gen.flags, &hintmsg)) + { + ereport(elevel, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": \"%s\"", + name, value), + hintmsg ? errhint("%s", _(hintmsg)) : 0)); + return 0; + } + + if (*((int *) newval) < conf->min || *((int *) newval) > conf->max) + { + ereport(elevel, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("%d is outside the valid range for parameter \"%s\" (%d .. %d)", + *((int *) newval), name, conf->min, conf->max))); + return 0; + } + + if (!call_int_check_hook(conf, newval, newextra, + PGC_S_FILE, elevel)) + return 0; + + if (*newextra && freemem) + free(*newextra); + } + } + break; + case PGC_REAL: + { + struct config_real *conf = (struct config_real *) record; + double tmpnewval; + + if (newval == NULL) + newval = &tmpnewval; + + if (value != NULL) + { + if (!parse_real(value, newval)) + { + ereport(elevel, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a numeric value", + name))); + return 0; + } + + if (*((double *) newval) < conf->min || *((double *) newval) > conf->max) + { + ereport(elevel, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("%g is outside the valid range for parameter \"%s\" (%g .. %g)", + *((double *) newval), name, conf->min, conf->max))); + return 0; + } + + if (!call_real_check_hook(conf, newval, newextra, + PGC_S_FILE, elevel)) + return 0; + + if (*newextra && freemem) + free(*newextra); + } + } + break; + case PGC_STRING: + { + struct config_string *conf = (struct config_string *) record; + char *tempPtr; + char **tmpnewval = newval; + + if (newval == NULL) + tmpnewval = &tempPtr; + + if (value != NULL) + { + /* + * The value passed by the caller could be transient, so + * we always strdup it. + */ + *tmpnewval = guc_strdup(elevel, value); + if (*tmpnewval == NULL) + return 0; + + /* + * The only built-in "parsing" check we have is to apply + * truncation if GUC_IS_NAME. + */ + if (conf->gen.flags & GUC_IS_NAME) + truncate_identifier(*tmpnewval, strlen(*tmpnewval), true); + + if (!call_string_check_hook(conf, tmpnewval, newextra, + PGC_S_FILE, elevel)) + { + free(*tmpnewval); + return 0; + } + + /* Free the malloc'd data if any */ + if (freemem) + { + if (*tmpnewval != NULL) + free(*tmpnewval); + if (*newextra != NULL) + free(*newextra); + } + } + } + break; + case PGC_ENUM: + { + struct config_enum *conf = (struct config_enum *) record; + int tmpnewval; + + if (newval == NULL) + newval = &tmpnewval; + + if (value != NULL) + { + if (!config_enum_lookup_by_name(conf, value, newval)) + { + char *hintmsg; + + hintmsg = config_enum_get_options(conf, + "Available values: ", + ".", ", "); + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": \"%s\"", + name, value), + hintmsg ? errhint("%s", _(hintmsg)) : 0)); + + if (hintmsg != NULL) + pfree(hintmsg); + return 0; + } + if (!call_enum_check_hook(conf, newval, newextra, + PGC_S_FILE, LOG)) + return 0; + + if (*newextra && freemem) + free(*newextra); + } + } + break; + } + return 1; + } + /* * Sets option `name' to given value. *************** *** 5424,5439 **** set_config_option(const char *name, const char *value, if (value) { ! if (!parse_bool(value, &newval)) ! { ! ereport(elevel, ! (errcode(ERRCODE_INVALID_PARAMETER_VALUE), ! errmsg("parameter \"%s\" requires a Boolean value", ! name))); ! return 0; ! } ! if (!call_bool_check_hook(conf, &newval, &newextra, ! source, elevel)) return 0; } else if (source == PGC_S_DEFAULT) --- 5647,5654 ---- if (value) { ! if (!validate_conf_option(record, name, value, elevel, ! false, &newval, &newextra)) return 0; } else if (source == PGC_S_DEFAULT) *************** *** 5517,5543 **** set_config_option(const char *name, const char *value, if (value) { ! const char *hintmsg; ! ! if (!parse_int(value, &newval, conf->gen.flags, &hintmsg)) ! { ! ereport(elevel, ! (errcode(ERRCODE_INVALID_PARAMETER_VALUE), ! errmsg("invalid value for parameter \"%s\": \"%s\"", ! name, value), ! hintmsg ? errhint("%s", _(hintmsg)) : 0)); ! return 0; ! } ! if (newval < conf->min || newval > conf->max) ! { ! ereport(elevel, ! (errcode(ERRCODE_INVALID_PARAMETER_VALUE), ! errmsg("%d is outside the valid range for parameter \"%s\" (%d .. %d)", ! newval, name, conf->min, conf->max))); ! return 0; ! } ! if (!call_int_check_hook(conf, &newval, &newextra, ! source, elevel)) return 0; } else if (source == PGC_S_DEFAULT) --- 5732,5739 ---- if (value) { ! if (!validate_conf_option(record, name, value, elevel, ! false, &newval, &newextra)) return 0; } else if (source == PGC_S_DEFAULT) *************** *** 5621,5644 **** set_config_option(const char *name, const char *value, if (value) { ! if (!parse_real(value, &newval)) ! { ! ereport(elevel, ! (errcode(ERRCODE_INVALID_PARAMETER_VALUE), ! errmsg("parameter \"%s\" requires a numeric value", ! name))); ! return 0; ! } ! if (newval < conf->min || newval > conf->max) ! { ! ereport(elevel, ! (errcode(ERRCODE_INVALID_PARAMETER_VALUE), ! errmsg("%g is outside the valid range for parameter \"%s\" (%g .. %g)", ! newval, name, conf->min, conf->max))); ! return 0; ! } ! if (!call_real_check_hook(conf, &newval, &newextra, ! source, elevel)) return 0; } else if (source == PGC_S_DEFAULT) --- 5817,5824 ---- if (value) { ! if (!validate_conf_option(record, name, value, elevel, ! false, &newval, &newextra)) return 0; } else if (source == PGC_S_DEFAULT) *************** *** 5722,5748 **** set_config_option(const char *name, const char *value, if (value) { ! /* ! * The value passed by the caller could be transient, so ! * we always strdup it. ! */ ! newval = guc_strdup(elevel, value); ! if (newval == NULL) return 0; - - /* - * The only built-in "parsing" check we have is to apply - * truncation if GUC_IS_NAME. - */ - if (conf->gen.flags & GUC_IS_NAME) - truncate_identifier(newval, strlen(newval), true); - - if (!call_string_check_hook(conf, &newval, &newextra, - source, elevel)) - { - free(newval); - return 0; - } } else if (source == PGC_S_DEFAULT) { --- 5902,5910 ---- if (value) { ! if (!validate_conf_option(record, name, value, elevel, ! false, &newval, &newextra)) return 0; } else if (source == PGC_S_DEFAULT) { *************** *** 5848,5873 **** set_config_option(const char *name, const char *value, if (value) { ! if (!config_enum_lookup_by_name(conf, value, &newval)) ! { ! char *hintmsg; ! ! hintmsg = config_enum_get_options(conf, ! "Available values: ", ! ".", ", "); ! ! ereport(elevel, ! (errcode(ERRCODE_INVALID_PARAMETER_VALUE), ! errmsg("invalid value for parameter \"%s\": \"%s\"", ! name, value), ! hintmsg ? errhint("%s", _(hintmsg)) : 0)); ! ! if (hintmsg) ! pfree(hintmsg); ! return 0; ! } ! if (!call_enum_check_hook(conf, &newval, &newextra, ! source, elevel)) return 0; } else if (source == PGC_S_DEFAULT) --- 6010,6017 ---- if (value) { ! if (!validate_conf_option(record, name, value, elevel, ! false, &newval, &newextra)) return 0; } else if (source == PGC_S_DEFAULT) *************** *** 6237,6242 **** flatten_set_variable_args(const char *name, List *args) --- 6381,6677 ---- return buf.data; } + /* + * Writes updated configuration parameter values into + * postgresql.auto.conf.temp file. It traverses the list of parameters + * and quote the string values before writing them to temporaray file. + */ + static void + write_auto_conf_file(int fd, const char *filename, ConfigVariable **head_p) + { + ConfigVariable *item; + StringInfoData buf; + + initStringInfo(&buf); + appendStringInfoString(&buf, "# Do not edit this file manually! \n"); + appendStringInfoString(&buf, "# It will be overwritten by ALTER SYSTEM command. \n"); + + /* + * traverse the list of parameters, quote the string parameter and write + * it file. Once all parameters are written fsync the file. + */ + + for (item = *head_p; item != NULL; item = item->next) + { + char *escaped; + + appendStringInfoString(&buf, item->name); + appendStringInfoString(&buf, " = "); + + appendStringInfoString(&buf, "\'"); + escaped = escape_single_quotes_ascii(item->value); + appendStringInfoString(&buf, escaped); + free(escaped); + appendStringInfoString(&buf, "\'"); + + appendStringInfoString(&buf, "\n"); + + if (write(fd, buf.data, buf.len) < 0) + ereport(ERROR, + (errmsg("failed to write to \"%s\" file", filename))); + resetStringInfo(&buf); + } + + if (pg_fsync(fd) != 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not fsync file \"%s\": %m", filename))); + + pfree(buf.data); + } + + + /* + * This function takes list of all configuration parameters in postgresql.auto.conf + * and parameter to be updated as input arguments and replace the updated + * configuration parameter value in a list. + * If the parameter to be updated is new then it is appended to the + * list of parameters. + */ + static void + replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p, + char *config_file, + char *name, char *value) + { + ConfigVariable *item, + *prev = NULL; + + if (*head_p != NULL) + { + for (item = *head_p; item != NULL; item = item->next) + { + if (strcmp(item->name, name) == 0) + { + pfree(item->value); + if (value != NULL) + /* update the parameter value */ + item->value = pstrdup(value); + 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; + + pfree(item->name); + pfree(item->filename); + pfree(item); + } + return; + } + prev = item; + } + } + + if (value == NULL) + return; + + item = palloc(sizeof *item); + item->name = pstrdup(name); + item->value = pstrdup(value); + item->filename = pstrdup(config_file); + item->next = NULL; + + if (*head_p == NULL) + { + item->sourceline = 1; + *head_p = item; + } + else + { + item->sourceline = (*tail_p)->sourceline + 1; + (*tail_p)->next = item; + } + + *tail_p = item; + + return; + } + + + /* + * Persist the configuration parameter value. + * + * This function takes all previous configuration parameters + * set by ALTER SYSTEM command and the currently set ones + * and write them all to the automatic configuration file. + * + * The configuration parameters are written to a temporary + * file then renamed to the final name. The template for the + * temporary file is postgresql.auto.conf.temp. + * + * An LWLock is used to serialize writing to the same file. + * + * In case of an error, we leave the original automatic + * configuration file (postgresql.auto.conf) intact. + * A stale temporary file may be left behind in case we crash. + * Such files are removed on the next server restart. + */ + void + AlterSystemSetConfigFile(AlterSystemStmt * altersysstmt) + { + char *name; + char *value; + int Tmpfd = -1; + FILE *infile; + struct config_generic *record; + ConfigVariable *head = NULL; + ConfigVariable *tail = NULL; + char ConfigFileDir[MAXPGPATH]; + char AutoConfFileName[MAXPGPATH]; + char AutoConfTmpFileName[MAXPGPATH]; + struct stat st; + void *newextra = NULL; + + if (!superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + (errmsg("must be superuser to execute ALTER SYSTEM command")))); + + /* + * Validate the name and arguments [value1, value2 ... ]. + */ + name = altersysstmt->setstmt->name; + + switch (altersysstmt->setstmt->kind) + { + case VAR_SET_VALUE: + value = ExtractSetVariableArgs(altersysstmt->setstmt); + break; + + case VAR_SET_DEFAULT: + value = NULL; + break; + default: + elog(ERROR, "unrecognized alter system stmt type: %d", + altersysstmt->setstmt->kind); + break; + } + + record = find_option(name, true, LOG); + if (record == NULL) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("unrecognized configuration parameter \"%s\"", name))); + + if ((record->context == PGC_INTERNAL) || + (record->flags & GUC_DISALLOW_IN_FILE)) + ereport(ERROR, + (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM), + errmsg("parameter \"%s\" cannot be changed", + name))); + + if (!validate_conf_option(record, name, value, ERROR, + true, NULL, &newextra)) + ereport(ERROR, + (errmsg("invalid value for parameter \"%s\": \"%s\"", name, value))); + + + /* + * The "config" directory should follow the postgresql.conf file directory + * not the data_directory. So while framing the name for + * postgresql.auto.conf and it's temp file we need to follow the + * postgresql.conf file directory. + */ + StrNCpy(ConfigFileDir, ConfigFileName, sizeof(ConfigFileDir)); + get_parent_directory(ConfigFileDir); + + /* Frame config dir, auto conf file and auto conf sample temp file names */ + snprintf(AutoConfFileName, sizeof(AutoConfFileName), "%s/%s/%s", + ConfigFileDir, + PG_AUTOCONF_DIR, + PG_AUTOCONF_FILENAME); + snprintf(AutoConfTmpFileName, sizeof(AutoConfTmpFileName), "%s/%s/%s.%s", + ConfigFileDir, + PG_AUTOCONF_DIR, + PG_AUTOCONF_FILENAME, + "temp"); + + /* + * one backend is allowed to operate on .auto.conf file, to ensure that we + * need to update the contents of the file with AutoFileLock. To ensure + * crash safety, first the contents are written to temporary file and then + * rename it to postgresql.auto.conf + */ + + LWLockAcquire(AutoFileLock, LW_EXCLUSIVE); + + Tmpfd = open(AutoConfTmpFileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); + if (Tmpfd < 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("failed to open auto conf temp file \"%s\": %m ", + AutoConfTmpFileName))); + + PG_TRY(); + { + if (stat(AutoConfFileName, &st) == 0) + { + /* open postgresql.auto.conf file */ + infile = AllocateFile(AutoConfFileName, "r"); + if (infile == NULL) + ereport(ERROR, + (errmsg("failed to open auto conf file \"%s\": %m ", + AutoConfFileName))); + + /* Parse the postgresql.auto.conf file */ + ParseConfigFp(infile, AutoConfFileName, 0, LOG, &head, &tail); + + FreeFile(infile); + } + + /* + * replace with new value if the configuration parameter already + * exists OR add it as a new cofiguration parameter in the file. + */ + replace_auto_config_value(&head, &tail, AutoConfFileName, name, value); + + /* Write and sync the New contents to postgresql.auto.conf.temp file */ + write_auto_conf_file(Tmpfd, AutoConfTmpFileName, &head); + + close(Tmpfd); + Tmpfd = -1; + + /* + * As the rename is atomic operation, if any problem occurs after this + * at max it can loose the parameters set by last ALTER SYSTEM + * command. + */ + if (rename(AutoConfTmpFileName, AutoConfFileName) < 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not rename file \"%s\" to \"%s\" : %m", + AutoConfTmpFileName, AutoConfFileName))); + } + PG_CATCH(); + { + if (Tmpfd >= 0) + close(Tmpfd); + + unlink(AutoConfTmpFileName); + FreeConfigVariables(head); + PG_RE_THROW(); + } + PG_END_TRY(); + + FreeConfigVariables(head); + LWLockRelease(AutoFileLock); + return; + } /* * SET command *** a/src/bin/initdb/initdb.c --- b/src/bin/initdb/initdb.c *************** *** 193,199 **** const char *subdirs[] = { "base/1", "pg_tblspc", "pg_stat", ! "pg_stat_tmp" }; --- 193,200 ---- "base/1", "pg_tblspc", "pg_stat", ! "pg_stat_tmp", ! PG_AUTOCONF_DIR }; *************** *** 1182,1187 **** setup_config(void) --- 1183,1190 ---- char repltok[MAXPGPATH]; char path[MAXPGPATH]; const char *default_timezone; + char *autoconflines[3]; + char tempautobuf[256]; fputs(_("creating configuration files ... "), stdout); fflush(stdout); *************** *** 1269,1274 **** setup_config(void) --- 1272,1295 ---- writefile(path, conflines); chmod(path, S_IRUSR | S_IWUSR); + /* + * create the automatic configuration file to store the configuration + * parameters set by ALTER SYSTEM command. The parameters present in this + * file will override the value of parameters that exists before parse of + * this file. + */ + + strcpy(tempautobuf, "# Do not edit this file manually! \n"); + autoconflines[0] = pg_strdup(tempautobuf); + strcpy(tempautobuf, "# It will be overwritten by the ALTER SYSTEM command. \n"); + autoconflines[1] = pg_strdup(tempautobuf); + autoconflines[2] = NULL; + + sprintf(path, "%s/%s/%s", pg_data, PG_AUTOCONF_DIR, PG_AUTOCONF_FILENAME); + + writefile(path, autoconflines); + chmod(path, S_IRUSR | S_IWUSR); + free(conflines); *** a/src/include/nodes/nodes.h --- b/src/include/nodes/nodes.h *************** *** 362,367 **** typedef enum NodeTag --- 362,368 ---- T_CreateEventTrigStmt, T_AlterEventTrigStmt, T_RefreshMatViewStmt, + T_AlterSystemStmt, /* * TAGS FOR PARSE TREE NODES (parsenodes.h) *** a/src/include/nodes/parsenodes.h --- b/src/include/nodes/parsenodes.h *************** *** 2417,2422 **** typedef struct DropdbStmt --- 2417,2432 ---- } DropdbStmt; /* ---------------------- + * Alter System Statement + * ---------------------- + */ + typedef struct AlterSystemStmt + { + NodeTag type; + VariableSetStmt *setstmt; /* SET subcommand */ + } AlterSystemStmt; + + /* ---------------------- * Cluster Statement (support pbrown's cluster index implementation) * ---------------------- */ *** a/src/include/pg_config_manual.h --- b/src/include/pg_config_manual.h *************** *** 281,283 **** --- 281,290 ---- /* #define HEAPDEBUGALL */ /* #define ACLDEBUG */ /* #define RTDEBUG */ + + /* + * Automatic configuration directory and file name + * for ALTER SYSTEM. + */ + #define PG_AUTOCONF_DIR "config" + #define PG_AUTOCONF_FILENAME "postgresql.auto.conf" *** a/src/include/storage/lwlock.h --- b/src/include/storage/lwlock.h *************** *** 80,85 **** typedef enum LWLockId --- 80,86 ---- OldSerXidLock, SyncRepLock, BackgroundWorkerLock, + AutoFileLock, /* Individual lock IDs end here */ FirstBufMappingLock, FirstLockMgrLock = FirstBufMappingLock + NUM_BUFFER_PARTITIONS, *** a/src/include/utils/guc.h --- b/src/include/utils/guc.h *************** *** 322,327 **** extern bool parse_real(const char *value, double *result); --- 322,328 ---- extern int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel); + extern void AlterSystemSetConfigFile(AlterSystemStmt * setstmt); extern char *GetConfigOptionByName(const char *name, const char **varname); extern void GetConfigOptionByNum(int varnum, const char **values, bool *noshow); extern int GetNumConfigOptions(void);