Commit 62d16c7f authored by Tom Lane's avatar Tom Lane

Improve design and implementation of pg_file_settings view.

As first committed, this view reported on the file contents as they were
at the last SIGHUP event.  That's not as useful as reporting on the current
contents, and what's more, it didn't work right on Windows unless the
current session had serviced at least one SIGHUP.  Therefore, arrange to
re-read the files when pg_show_all_settings() is called.  This requires
only minor refactoring so that we can pass changeVal = false to
set_config_option() so that it won't actually apply any changes locally.

In addition, add error reporting so that errors that would prevent the
configuration files from being loaded, or would prevent individual settings
from being applied, are visible directly in the view.  This makes the view
usable for pre-testing whether edits made in the config files will have the
desired effect, before one actually issues a SIGHUP.

I also added an "applied" column so that it's easy to identify entries that
are superseded by later entries; this was the main use-case for the original
design, but it seemed unnecessarily hard to use for that.

Also fix a 9.4.1 regression that allowed multiple entries for a
PGC_POSTMASTER variable to cause bogus complaints in the postmaster log.
(The issue here was that commit bf007a27 unintentionally reverted
3e3f6597, which suppressed any duplicate entries within
ParseConfigFp.  However, since the original coding of the pg_file_settings
view depended on such suppression *not* happening, we couldn't have fixed
this issue now without first doing something with pg_file_settings.
Now we suppress duplicates by marking them "ignored" within
ProcessConfigFileInternal, which doesn't hide them in the view.)

Lesser changes include:

Drive the view directly off the ConfigVariable list, instead of making a
basically-equivalent second copy of the data.  There's no longer any need
to hang onto the data permanently, anyway.

Convert show_all_file_settings() to do its work in one call and return a
tuplestore; this avoids risks associated with assuming that the GUC state
will hold still over the course of query execution.  (I think there were
probably latent bugs here, though you might need something like a cursor
on the view to expose them.)

Arrange to run SIGHUP processing in a short-lived memory context, to
forestall process-lifespan memory leaks.  (There is one known leak in this
code, in ProcessConfigDirectory; it seems minor enough to not be worth
back-patching a specific fix for.)

Remove mistaken assignment to ConfigFileLineno that caused line counting
after an include_dir directive to be completely wrong.

Add missed failure check in AlterSystemSetConfigFile().  We don't really
expect ParseConfigFp() to fail, but that's not an excuse for not checking.
parent d661532e
......@@ -7626,7 +7626,7 @@
<row>
<entry><link linkend="view-pg-file-settings"><structname>pg_file_settings</structname></link></entry>
<entry>file location of parameter settings</entry>
<entry>summary of configuration file contents</entry>
</row>
<row>
......@@ -8007,13 +8007,26 @@
</indexterm>
<para>
The view <structname>pg_file_settings</structname> provides the file
name, line number and value of all parameters which are set through
configuration files.
In contrast to <structname>pg_settings</structname>, a row is provided for
each occurrence of the parameter across all configuration files. This is helpful
for discovering why one value may have been used in preference to another
when the parameters were loaded.
The view <structname>pg_file_settings</structname> provides a summary of
the contents of the server's configuration file(s). A row appears in
this view for each <quote>name = value</> entry appearing in the files,
with annotations indicating whether the value could be applied
successfully. Additional row(s) may appear for problems not linked to
a <quote>name = value</> entry, such as syntax errors in the files.
</para>
<para>
This view is helpful for checking whether planned changes in the
configuration files will work, or for diagnosing a previous failure.
Note that this view reports on the <emphasis>current</> contents of the
files, not on what was last applied by the server. (The
<link linkend="view-pg-settings"><structname>pg_settings</structname></link>
view is usually sufficient to determine that.)
</para>
<para>
The <structname>pg_file_settings</structname> view can be read only by
superusers.
</para>
<table>
......@@ -8031,43 +8044,64 @@
<row>
<entry><structfield>sourcefile</structfield></entry>
<entry><structfield>text</structfield></entry>
<entry>Path to and name of the configration file</entry>
<entry>Full pathname of the configuration file</entry>
</row>
<row>
<entry><structfield>sourceline</structfield></entry>
<entry><structfield>integer</structfield></entry>
<entry>
Line number within the configuration file where the value was set
Line number within the configuration file where the entry appears
</entry>
</row>
<row>
<entry><structfield>seqno</structfield></entry>
<entry><structfield>integer</structfield></entry>
<entry>Order in which the setting was loaded</entry>
<entry>Order in which the entries are processed (1..<replaceable>n</>)</entry>
</row>
<row>
<entry><structfield>name</structfield></entry>
<entry><structfield>text</structfield></entry>
<entry>Run-time configuration parameter name</entry>
<entry>Configuration parameter name</entry>
</row>
<row>
<entry><structfield>setting</structfield></entry>
<entry><structfield>text</structfield></entry>
<entry>value of the parameter</entry>
<entry>Value to be assigned to the parameter</entry>
</row>
<row>
<entry><structfield>applied</structfield></entry>
<entry><structfield>boolean</structfield></entry>
<entry>True if the value can be applied successfully</entry>
</row>
<row>
<entry><structfield>error</structfield></entry>
<entry><structfield>text</structfield></entry>
<entry>If not null, an error message indicating why this entry could
not be applied</entry>
</row>
</tbody>
</tgroup>
</table>
</table>
<para>
See <xref linkend="config-setting"> for more information about the various
ways to change these parameters.
If the configuration file contains syntax errors or invalid parameter
names, the server will not attempt to apply any settings from it, and
therefore all the <structfield>applied</> fields will read as false.
In such a case there will be one or more rows with
non-null <structfield>error</structfield> fields indicating the
problem(s). Otherwise, individual settings will be applied if possible.
If an individual setting cannot be applied (e.g., invalid value, or the
setting cannot be changed after server start) it will have an appropriate
message in the <structfield>error</structfield> field. Another way that
an entry might have <structfield>applied</> = false is that it is
overridden by a later entry for the same parameter name; this case is not
considered an error so nothing appears in
the <structfield>error</structfield> field.
</para>
<para>
The <structname>pg_file_settings</structname> view cannot be modified
directly as it represents information, as read in at server start or
reload time, about all parameter settings across all configuration files.
See <xref linkend="config-setting"> for more information about the various
ways to change run-time parameters.
</para>
</sect1>
......
......@@ -175,6 +175,14 @@ shared_buffers = 128MB
effect in the same way. Settings in <filename>postgresql.auto.conf</>
override those in <filename>postgresql.conf</>.
</para>
<para>
The system view
<link linkend="view-pg-file-settings"><structname>pg_file_settings</structname></link>
can be helpful for pre-testing changes to the configuration file, or for
diagnosing problems if a <systemitem>SIGHUP</> signal did not have the
desired effects.
</para>
</sect2>
<sect2 id="config-setting-sql-command-interaction">
......
......@@ -6,8 +6,8 @@
<note>
<title>Release Date</title>
<para>AS OF 2015-06-01</para>
<simpara>2015-XX-XX</simpara>
<simpara>2015-??-??</simpara>
<simpara>Current as of 2015-06-01</simpara>
</note>
<sect2>
......@@ -23,6 +23,7 @@
<listitem>
<para>
... to be filled in ...
</para>
</listitem>
......@@ -480,10 +481,10 @@
<listitem>
<para>
Add function and view <link
linkend="view-pg-file-settings"><function>pg_file_settings</></>
to show the source of <acronym>GUC</> values set in configuration
files (Sawada Masahiko)
Add system view <link
linkend="view-pg-file-settings"><structname>pg_file_settings</></>
to show the contents of the server's configuration files
(Sawada Masahiko)
</para>
</listitem>
......@@ -503,7 +504,7 @@
</para>
<para>
This removes the setting from <filename>postgresql.auto.conf</>.
This command removes the setting from <filename>postgresql.auto.conf</>.
</para>
</listitem>
......
This diff is collapsed.
......@@ -182,6 +182,10 @@ static bool check_cluster_name(char **newval, void **extra, GucSource source);
static const char *show_unix_socket_permissions(void);
static const char *show_log_file_mode(void);
/* Private functions in guc-file.l that need to be called from guc.c */
static ConfigVariable *ProcessConfigFileInternal(GucContext context,
bool applySettings, int elevel);
/*
* Options for enum values defined in this module.
......@@ -3678,22 +3682,6 @@ static struct config_generic **guc_variables;
/* Current number of variables contained in the vector */
static int num_guc_variables;
/*
* Lookup of variables for pg_file_settings view.
* guc_file_variables is an array of length num_guc_file_variables.
*/
typedef struct ConfigFileVariable
{
char *name;
char *value;
char *filename;
int sourceline;
} ConfigFileVariable;
static struct ConfigFileVariable *guc_file_variables;
/* Number of file variables */
static int num_guc_file_variables;
/* Vector capacity */
static int size_guc_variables;
......@@ -6781,8 +6769,11 @@ replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
item = palloc(sizeof *item);
item->name = pstrdup(name);
item->value = pstrdup(value);
item->errmsg = NULL;
item->filename = pstrdup(""); /* new item has no location */
item->sourceline = 0;
item->ignore = false;
item->applied = false;
item->next = NULL;
if (*head_p == NULL)
......@@ -6931,7 +6922,10 @@ AlterSystemSetConfigFile(AlterSystemStmt *altersysstmt)
AutoConfFileName)));
/* parse it */
ParseConfigFp(infile, AutoConfFileName, 0, LOG, &head, &tail);
if (!ParseConfigFp(infile, AutoConfFileName, 0, LOG, &head, &tail))
ereport(ERROR,
(errmsg("could not parse contents of file \"%s\"",
AutoConfFileName)));
FreeFile(infile);
}
......@@ -8181,10 +8175,12 @@ show_all_settings(PG_FUNCTION_ARGS)
/*
* show_all_file_settings
*
* returns a table of all parameter settings in all configuration files
* which includes the config file path/name, the line number, a sequence number
* indicating when we loaded it, the parameter name, and the value it is
* set to.
* Returns a table of all parameter settings in all configuration files
* which includes the config file pathname, the line number, a sequence number
* indicating the order in which the settings were encountered, the parameter
* name and value, a bool showing if the value could be applied, and possibly
* an associated error message. (For problems such as syntax errors, the
* parameter name/value might be NULL.)
*
* Note: no filtering is done here, instead we depend on the GRANT system
* to prevent unprivileged users from accessing this function or the view
......@@ -8193,92 +8189,111 @@ show_all_settings(PG_FUNCTION_ARGS)
Datum
show_all_file_settings(PG_FUNCTION_ARGS)
{
#define NUM_PG_FILE_SETTINGS_ATTS 5
FuncCallContext *funcctx;
#define NUM_PG_FILE_SETTINGS_ATTS 7
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
TupleDesc tupdesc;
int call_cntr;
int max_calls;
AttInMetadata *attinmeta;
Tuplestorestate *tupstore;
ConfigVariable *conf;
int seqno;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
if (SRF_IS_FIRSTCALL())
{
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* Check to see if caller supports us returning a tuplestore */
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set")));
if (!(rsinfo->allowedModes & SFRM_Materialize))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("materialize mode required, but it is not " \
"allowed in this context")));
/*
* need a tuple descriptor representing NUM_PG_FILE_SETTINGS_ATTS
* columns of the appropriate types
*/
/* Scan the config files using current context as workspace */
conf = ProcessConfigFileInternal(PGC_SIGHUP, false, DEBUG3);
tupdesc = CreateTemplateTupleDesc(NUM_PG_FILE_SETTINGS_ATTS, false);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "sourcefile",
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "sourceline",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "seqno",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "name",
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "setting",
TEXTOID, -1, 0);
/* Switch into long-lived context to construct returned data structures */
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
funcctx->max_calls = num_guc_file_variables;
MemoryContextSwitchTo(oldcontext);
}
/* Build a tuple descriptor for our result type */
tupdesc = CreateTemplateTupleDesc(NUM_PG_FILE_SETTINGS_ATTS, false);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "sourcefile",
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "sourceline",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "seqno",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "name",
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "setting",
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "applied",
BOOLOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 7, "error",
TEXTOID, -1, 0);
funcctx = SRF_PERCALL_SETUP();
/* Build a tuplestore to return our results in */
tupstore = tuplestore_begin_heap(true, false, work_mem);
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
call_cntr = funcctx->call_cntr;
max_calls = funcctx->max_calls;
attinmeta = funcctx->attinmeta;
/* The rest can be done in short-lived context */
MemoryContextSwitchTo(oldcontext);
if (call_cntr < max_calls)
/* Process the results and create a tuplestore */
for (seqno = 1; conf != NULL; conf = conf->next, seqno++)
{
char *values[NUM_PG_FILE_SETTINGS_ATTS];
HeapTuple tuple;
Datum result;
ConfigFileVariable conf;
char buffer[12]; /* must be at least 12, per pg_ltoa */
Datum values[NUM_PG_FILE_SETTINGS_ATTS];
bool nulls[NUM_PG_FILE_SETTINGS_ATTS];
/* Check to avoid going past end of array */
if (call_cntr > num_guc_file_variables)
SRF_RETURN_DONE(funcctx);
conf = guc_file_variables[call_cntr];
memset(values, 0, sizeof(values));
memset(nulls, 0, sizeof(nulls));
/* sourcefile */
values[0] = conf.filename;
if (conf->filename)
values[0] = PointerGetDatum(cstring_to_text(conf->filename));
else
nulls[0] = true;
/* sourceline */
pg_ltoa(conf.sourceline, buffer);
values[1] = pstrdup(buffer);
/* sourceline (not meaningful if no sourcefile) */
if (conf->filename)
values[1] = Int32GetDatum(conf->sourceline);
else
nulls[1] = true;
/* seqno */
pg_ltoa(call_cntr + 1, buffer);
values[2] = pstrdup(buffer);
values[2] = Int32GetDatum(seqno);
/* name */
values[3] = conf.name;
if (conf->name)
values[3] = PointerGetDatum(cstring_to_text(conf->name));
else
nulls[3] = true;
/* setting */
values[4] = conf.value;
if (conf->value)
values[4] = PointerGetDatum(cstring_to_text(conf->value));
else
nulls[4] = true;
/* build a tuple */
tuple = BuildTupleFromCStrings(attinmeta, values);
/* applied */
values[5] = BoolGetDatum(conf->applied);
/* make the tuple into a datum */
result = HeapTupleGetDatum(tuple);
/* error */
if (conf->errmsg)
values[6] = PointerGetDatum(cstring_to_text(conf->errmsg));
else
nulls[6] = true;
SRF_RETURN_NEXT(funcctx, result);
}
else
{
SRF_RETURN_DONE(funcctx);
/* shove row into tuplestore */
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
tuplestore_donestoring(tupstore);
return (Datum) 0;
}
static char *
......
......@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 201506281
#define CATALOG_VERSION_NO 201506282
#endif
......@@ -3071,7 +3071,7 @@ DATA(insert OID = 2078 ( set_config PGNSP PGUID 12 1 0 0 0 f f f f f f v 3 0 2
DESCR("SET X as a function");
DATA(insert OID = 2084 ( pg_show_all_settings PGNSP PGUID 12 1 1000 0 0 f f f f t t s 0 0 2249 "" "{25,25,25,25,25,25,25,25,25,25,25,1009,25,25,25,23,16}" "{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}" "{name,setting,unit,category,short_desc,extra_desc,context,vartype,source,min_val,max_val,enumvals,boot_val,reset_val,sourcefile,sourceline,pending_restart}" _null_ _null_ show_all_settings _null_ _null_ _null_ ));
DESCR("SHOW ALL as a function");
DATA(insert OID = 3329 ( pg_show_all_file_settings PGNSP PGUID 12 1 1000 0 0 f f f f t t s 0 0 2249 "" "{25,23,23,25,25}" "{o,o,o,o,o}" "{sourcefile,sourceline,seqno,name,setting}" _null_ _null_ show_all_file_settings _null_ _null_ _null_ ));
DATA(insert OID = 3329 ( pg_show_all_file_settings PGNSP PGUID 12 1 1000 0 0 f f f f t t v 0 0 2249 "" "{25,23,23,25,25,16,25}" "{o,o,o,o,o,o,o}" "{sourcefile,sourceline,seqno,name,setting,applied,error}" _null_ _null_ show_all_file_settings _null_ _null_ _null_ ));
DESCR("show config file settings");
DATA(insert OID = 1371 ( pg_lock_status PGNSP PGUID 12 1 1000 0 0 f f f f t t v 0 0 2249 "" "{25,26,26,23,21,25,28,26,26,21,25,23,25,16,16}" "{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}" "{locktype,database,relation,page,tuple,virtualxid,transactionid,classid,objid,objsubid,virtualtransaction,pid,mode,granted,fastpath}" _null_ _null_ pg_lock_status _null_ _null_ _null_ ));
DESCR("view system lock information");
......
......@@ -121,26 +121,36 @@ typedef enum
} GucSource;
/*
* Parsing the configuration file will return a list of name-value pairs
* with source location info.
* Parsing the configuration file(s) will return a list of name-value pairs
* with source location info. We also abuse this data structure to carry
* error reports about the config files. An entry reporting an error will
* have errmsg != NULL, and might have NULLs for name, value, and/or filename.
*
* If "ignore" is true, don't attempt to apply the item (it might be an error
* report, or an item we determined to be duplicate). "applied" is set true
* if we successfully applied, or could have applied, the setting.
*/
typedef struct ConfigVariable
{
char *name;
char *value;
char *errmsg;
char *filename;
int sourceline;
bool ignore;
bool applied;
struct ConfigVariable *next;
} ConfigVariable;
extern bool ParseConfigFile(const char *config_file, const char *calling_file,
bool strict, int depth, int elevel,
extern bool ParseConfigFile(const char *config_file, bool strict,
const char *calling_file, int calling_lineno,
int depth, int elevel,
ConfigVariable **head_p, ConfigVariable **tail_p);
extern bool ParseConfigFp(FILE *fp, const char *config_file,
int depth, int elevel,
ConfigVariable **head_p, ConfigVariable **tail_p);
extern bool ParseConfigDirectory(const char *includedir,
const char *calling_file,
const char *calling_file, int calling_lineno,
int depth, int elevel,
ConfigVariable **head_p,
ConfigVariable **tail_p);
......
......@@ -1316,8 +1316,10 @@ pg_file_settings| SELECT a.sourcefile,
a.sourceline,
a.seqno,
a.name,
a.setting
FROM pg_show_all_file_settings() a(sourcefile, sourceline, seqno, name, setting);
a.setting,
a.applied,
a.error
FROM pg_show_all_file_settings() a(sourcefile, sourceline, seqno, name, setting, applied, error);
pg_group| SELECT pg_authid.rolname AS groname,
pg_authid.oid AS grosysid,
ARRAY( SELECT pg_auth_members.member
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment