Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bd3b7a9

Browse files
committed
Support ALTER SYSTEM RESET command.
This patch allows us to execute ALTER SYSTEM RESET command to remove the configuration entry from postgresql.auto.conf. Vik Fearing, reviewed by Amit Kapila and me.
1 parent 01b6976 commit bd3b7a9

File tree

4 files changed

+115
-62
lines changed

4 files changed

+115
-62
lines changed

doc/src/sgml/ref/alter_system.sgml

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ PostgreSQL documentation
2222
<refsynopsisdiv>
2323
<synopsis>
2424
ALTER SYSTEM SET <replaceable class="PARAMETER">configuration_parameter</replaceable> { TO | = } { <replaceable class="PARAMETER">value</replaceable> | '<replaceable class="PARAMETER">value</replaceable>' | DEFAULT }
25+
26+
ALTER SYSTEM RESET <replaceable class="PARAMETER">configuration_parameter</replaceable>
27+
ALTER SYSTEM RESET ALL
2528
</synopsis>
2629
</refsynopsisdiv>
2730

@@ -30,10 +33,12 @@ ALTER SYSTEM SET <replaceable class="PARAMETER">configuration_parameter</replace
3033

3134
<para>
3235
<command>ALTER SYSTEM</command> writes the configuration parameter
33-
values to the <filename>postgresql.auto.conf</filename> file. With
34-
<literal>DEFAULT</literal>, it removes a configuration entry from
35-
<filename>postgresql.auto.conf</filename> file. The values will be
36-
effective after reload of server configuration (SIGHUP) or in next
36+
values to the <filename>postgresql.auto.conf</filename> file.
37+
Setting the parameter to <literal>DEFAULT</literal>, or using the
38+
<command>RESET</command> variant, removes the configuration entry from
39+
<filename>postgresql.auto.conf</filename> file. Use <literal>RESET
40+
ALL</literal> to clear all configuration entries. The values will
41+
be effective after reload of server configuration (SIGHUP) or in next
3742
server start based on the type of configuration parameter modified.
3843
</para>
3944

src/backend/parser/gram.y

+31-16
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
411411

412412
%type <istmt> insert_rest
413413

414-
%type <vsetstmt> generic_set set_rest set_rest_more SetResetClause FunctionSetResetClause
414+
%type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
415+
SetResetClause FunctionSetResetClause
415416

416417
%type <node> TableElement TypedTableElement ConstraintElem TableFuncElement
417418
%type <node> columnDef columnOptions
@@ -1579,39 +1580,47 @@ NonReservedWord_or_Sconst:
15791580
;
15801581

15811582
VariableResetStmt:
1582-
RESET var_name
1583+
RESET reset_rest { $$ = (Node *) $2; }
1584+
;
1585+
1586+
reset_rest:
1587+
generic_reset { $$ = $1; }
1588+
| TIME ZONE
15831589
{
15841590
VariableSetStmt *n = makeNode(VariableSetStmt);
15851591
n->kind = VAR_RESET;
1586-
n->name = $2;
1587-
$$ = (Node *) n;
1592+
n->name = "timezone";
1593+
$$ = n;
15881594
}
1589-
| RESET TIME ZONE
1595+
| TRANSACTION ISOLATION LEVEL
15901596
{
15911597
VariableSetStmt *n = makeNode(VariableSetStmt);
15921598
n->kind = VAR_RESET;
1593-
n->name = "timezone";
1594-
$$ = (Node *) n;
1599+
n->name = "transaction_isolation";
1600+
$$ = n;
15951601
}
1596-
| RESET TRANSACTION ISOLATION LEVEL
1602+
| SESSION AUTHORIZATION
15971603
{
15981604
VariableSetStmt *n = makeNode(VariableSetStmt);
15991605
n->kind = VAR_RESET;
1600-
n->name = "transaction_isolation";
1601-
$$ = (Node *) n;
1606+
n->name = "session_authorization";
1607+
$$ = n;
16021608
}
1603-
| RESET SESSION AUTHORIZATION
1609+
;
1610+
1611+
generic_reset:
1612+
var_name
16041613
{
16051614
VariableSetStmt *n = makeNode(VariableSetStmt);
16061615
n->kind = VAR_RESET;
1607-
n->name = "session_authorization";
1608-
$$ = (Node *) n;
1616+
n->name = $1;
1617+
$$ = n;
16091618
}
1610-
| RESET ALL
1619+
| ALL
16111620
{
16121621
VariableSetStmt *n = makeNode(VariableSetStmt);
16131622
n->kind = VAR_RESET_ALL;
1614-
$$ = (Node *) n;
1623+
$$ = n;
16151624
}
16161625
;
16171626

@@ -8494,7 +8503,7 @@ DropdbStmt: DROP DATABASE database_name
84948503

84958504
/*****************************************************************************
84968505
*
8497-
* ALTER SYSTEM SET
8506+
* ALTER SYSTEM
84988507
*
84998508
* This is used to change configuration parameters persistently.
85008509
*****************************************************************************/
@@ -8506,6 +8515,12 @@ AlterSystemStmt:
85068515
n->setstmt = $4;
85078516
$$ = (Node *)n;
85088517
}
8518+
| ALTER SYSTEM_P RESET generic_reset
8519+
{
8520+
AlterSystemStmt *n = makeNode(AlterSystemStmt);
8521+
n->setstmt = $4;
8522+
$$ = (Node *)n;
8523+
}
85098524
;
85108525

85118526

src/backend/utils/misc/guc.c

+60-38
Original file line numberDiff line numberDiff line change
@@ -6696,6 +6696,8 @@ replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
66966696
* This function takes all previous configuration parameters
66976697
* set by ALTER SYSTEM command and the currently set ones
66986698
* and write them all to the automatic configuration file.
6699+
* It just writes an empty file incase user wants to reset
6700+
* all the parameters.
66996701
*
67006702
* The configuration parameters are written to a temporary
67016703
* file then renamed to the final name.
@@ -6710,6 +6712,7 @@ AlterSystemSetConfigFile(AlterSystemStmt *altersysstmt)
67106712
{
67116713
char *name;
67126714
char *value;
6715+
bool resetall = false;
67136716
int Tmpfd = -1;
67146717
FILE *infile;
67156718
struct config_generic *record;
@@ -6737,37 +6740,48 @@ AlterSystemSetConfigFile(AlterSystemStmt *altersysstmt)
67376740
break;
67386741

67396742
case VAR_SET_DEFAULT:
6743+
case VAR_RESET:
6744+
value = NULL;
6745+
break;
6746+
6747+
case VAR_RESET_ALL:
67406748
value = NULL;
6749+
resetall = true;
67416750
break;
6751+
67426752
default:
67436753
elog(ERROR, "unrecognized alter system stmt type: %d",
67446754
altersysstmt->setstmt->kind);
67456755
break;
67466756
}
67476757

6748-
record = find_option(name, false, LOG);
6749-
if (record == NULL)
6750-
ereport(ERROR,
6751-
(errcode(ERRCODE_UNDEFINED_OBJECT),
6752-
errmsg("unrecognized configuration parameter \"%s\"", name)));
6758+
/* If we're resetting everything, there's no need to validate anything */
6759+
if (!resetall)
6760+
{
6761+
record = find_option(name, false, LOG);
6762+
if (record == NULL)
6763+
ereport(ERROR,
6764+
(errcode(ERRCODE_UNDEFINED_OBJECT),
6765+
errmsg("unrecognized configuration parameter \"%s\"", name)));
67536766

6754-
/*
6755-
* Don't allow the parameters which can't be set in configuration
6756-
* files to be set in PG_AUTOCONF_FILENAME file.
6757-
*/
6758-
if ((record->context == PGC_INTERNAL) ||
6759-
(record->flags & GUC_DISALLOW_IN_FILE) ||
6760-
(record->flags & GUC_DISALLOW_IN_AUTO_FILE))
6761-
ereport(ERROR,
6762-
(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
6763-
errmsg("parameter \"%s\" cannot be changed",
6764-
name)));
6765-
6766-
if (!validate_conf_option(record, name, value, PGC_S_FILE,
6767-
ERROR, true, NULL,
6768-
&newextra))
6769-
ereport(ERROR,
6770-
(errmsg("invalid value for parameter \"%s\": \"%s\"", name, value)));
6767+
/*
6768+
* Don't allow the parameters which can't be set in configuration
6769+
* files to be set in PG_AUTOCONF_FILENAME file.
6770+
*/
6771+
if ((record->context == PGC_INTERNAL) ||
6772+
(record->flags & GUC_DISALLOW_IN_FILE) ||
6773+
(record->flags & GUC_DISALLOW_IN_AUTO_FILE))
6774+
ereport(ERROR,
6775+
(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
6776+
errmsg("parameter \"%s\" cannot be changed",
6777+
name)));
6778+
6779+
if (!validate_conf_option(record, name, value, PGC_S_FILE,
6780+
ERROR, true, NULL,
6781+
&newextra))
6782+
ereport(ERROR,
6783+
(errmsg("invalid value for parameter \"%s\": \"%s\"", name, value)));
6784+
}
67716785

67726786

67736787
/*
@@ -6799,26 +6813,34 @@ AlterSystemSetConfigFile(AlterSystemStmt *altersysstmt)
67996813

68006814
PG_TRY();
68016815
{
6802-
if (stat(AutoConfFileName, &st) == 0)
6816+
/*
6817+
* If we're going to reset everything, then don't open the file, don't
6818+
* parse it, and don't do anything with the configuration list. Just
6819+
* write out an empty file.
6820+
*/
6821+
if (!resetall)
68036822
{
6804-
/* open file PG_AUTOCONF_FILENAME */
6805-
infile = AllocateFile(AutoConfFileName, "r");
6806-
if (infile == NULL)
6807-
ereport(ERROR,
6808-
(errmsg("failed to open auto conf file \"%s\": %m ",
6809-
AutoConfFileName)));
6823+
if (stat(AutoConfFileName, &st) == 0)
6824+
{
6825+
/* open file PG_AUTOCONF_FILENAME */
6826+
infile = AllocateFile(AutoConfFileName, "r");
6827+
if (infile == NULL)
6828+
ereport(ERROR,
6829+
(errmsg("failed to open auto conf file \"%s\": %m ",
6830+
AutoConfFileName)));
68106831

6811-
/* parse it */
6812-
ParseConfigFp(infile, AutoConfFileName, 0, LOG, &head, &tail);
6832+
/* parse it */
6833+
ParseConfigFp(infile, AutoConfFileName, 0, LOG, &head, &tail);
68136834

6814-
FreeFile(infile);
6815-
}
6835+
FreeFile(infile);
6836+
}
68166837

6817-
/*
6818-
* replace with new value if the configuration parameter already
6819-
* exists OR add it as a new cofiguration parameter in the file.
6820-
*/
6821-
replace_auto_config_value(&head, &tail, AutoConfFileName, name, value);
6838+
/*
6839+
* replace with new value if the configuration parameter already
6840+
* exists OR add it as a new cofiguration parameter in the file.
6841+
*/
6842+
replace_auto_config_value(&head, &tail, AutoConfFileName, name, value);
6843+
}
68226844

68236845
/* Write and sync the new contents to the temporary file */
68246846
write_auto_conf_file(Tmpfd, AutoConfTmpFileName, &head);

src/bin/psql/tab-complete.c

+15-4
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ static const SchemaQuery Query_for_list_of_matviews = {
545545
"SELECT name FROM "\
546546
" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\
547547
" WHERE context != 'internal') ss "\
548-
" WHERE substring(name,1,%d)='%s'"
548+
" WHERE substring(name,1,%d)='%s'"\
549+
" UNION ALL SELECT 'all' ss"
549550

550551
#define Query_for_list_of_set_vars \
551552
"SELECT name FROM "\
@@ -963,7 +964,7 @@ psql_completion(const char *text, int start, int end)
963964
{"AGGREGATE", "COLLATION", "CONVERSION", "DATABASE", "DEFAULT PRIVILEGES", "DOMAIN",
964965
"EVENT TRIGGER", "EXTENSION", "FOREIGN DATA WRAPPER", "FOREIGN TABLE", "FUNCTION",
965966
"GROUP", "INDEX", "LANGUAGE", "LARGE OBJECT", "MATERIALIZED VIEW", "OPERATOR",
966-
"ROLE", "RULE", "SCHEMA", "SERVER", "SEQUENCE", "SYSTEM SET", "TABLE",
967+
"ROLE", "RULE", "SCHEMA", "SERVER", "SEQUENCE", "SYSTEM", "TABLE",
967968
"TABLESPACE", "TEXT SEARCH", "TRIGGER", "TYPE",
968969
"USER", "USER MAPPING FOR", "VIEW", NULL};
969970

@@ -1328,10 +1329,20 @@ psql_completion(const char *text, int start, int end)
13281329

13291330
COMPLETE_WITH_LIST(list_ALTER_SERVER);
13301331
}
1331-
/* ALTER SYSTEM SET <name> */
1332+
/* ALTER SYSTEM SET, RESET, RESET ALL */
1333+
else if (pg_strcasecmp(prev2_wd, "ALTER") == 0 &&
1334+
pg_strcasecmp(prev_wd, "SYSTEM") == 0)
1335+
{
1336+
static const char *const list_ALTERSYSTEM[] =
1337+
{"SET", "RESET", NULL};
1338+
1339+
COMPLETE_WITH_LIST(list_ALTERSYSTEM);
1340+
}
1341+
/* ALTER SYSTEM SET|RESET <name> */
13321342
else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
13331343
pg_strcasecmp(prev2_wd, "SYSTEM") == 0 &&
1334-
pg_strcasecmp(prev_wd, "SET") == 0)
1344+
(pg_strcasecmp(prev_wd, "SET") == 0 ||
1345+
pg_strcasecmp(prev_wd, "RESET") == 0))
13351346
COMPLETE_WITH_QUERY(Query_for_list_of_alter_system_set_vars);
13361347
/* ALTER VIEW <name> */
13371348
else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&

0 commit comments

Comments
 (0)