Skip to content

Commit 31cfe34

Browse files
author
Commitfest Bot
committed
[CF 5447] v4 - log_min_messages per backend type
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5447 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/[email protected] Author(s): Euler Taveira
2 parents 2648eab + c859788 commit 31cfe34

File tree

15 files changed

+364
-43
lines changed

15 files changed

+364
-43
lines changed

doc/src/sgml/config.sgml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7053,7 +7053,7 @@ local0.* /var/log/postgresql
70537053
<variablelist>
70547054

70557055
<varlistentry id="guc-log-min-messages" xreflabel="log_min_messages">
7056-
<term><varname>log_min_messages</varname> (<type>enum</type>)
7056+
<term><varname>log_min_messages</varname> (<type>string</type>)
70577057
<indexterm>
70587058
<primary><varname>log_min_messages</varname> configuration parameter</primary>
70597059
</indexterm>
@@ -7062,14 +7062,27 @@ local0.* /var/log/postgresql
70627062
<para>
70637063
Controls which <link linkend="runtime-config-severity-levels">message
70647064
levels</link> are written to the server log.
7065-
Valid values are <literal>DEBUG5</literal>, <literal>DEBUG4</literal>,
7066-
<literal>DEBUG3</literal>, <literal>DEBUG2</literal>, <literal>DEBUG1</literal>,
7067-
<literal>INFO</literal>, <literal>NOTICE</literal>, <literal>WARNING</literal>,
7068-
<literal>ERROR</literal>, <literal>LOG</literal>, <literal>FATAL</literal>, and
7069-
<literal>PANIC</literal>. Each level includes all the levels that
7070-
follow it. The later the level, the fewer messages are sent
7071-
to the log. The default is <literal>WARNING</literal>. Note that
7072-
<literal>LOG</literal> has a different rank here than in
7065+
Valid values are a comma-separated list of <literal>backendtype:level</literal>
7066+
and a single <literal>level</literal>. The list allows it to use
7067+
different levels per backend type. Only the single <literal>level</literal>
7068+
is mandatory (order does not matter) and it is assigned to the backend
7069+
types that are not specified in the list.
7070+
Valid <literal>backendtype</literal> values are <literal>archiver</literal>,
7071+
<literal>autovacuum</literal>, <literal>backend</literal>,
7072+
<literal>bgworker</literal>, <literal>bgwriter</literal>,
7073+
<literal>checkpointer</literal>, <literal>ioworker</literal>,
7074+
<literal>syslogger</literal>, <literal>slotsyncworker</literal>,
7075+
<literal>startup</literal>, <literal>walreceiver</literal>,
7076+
<literal>walsender</literal>, <literal>walsummarizer</literal>, and
7077+
<literal>walwriter</literal>.
7078+
Valid <literal>level</literal> values are <literal>DEBUG5</literal>,
7079+
<literal>DEBUG4</literal>, <literal>DEBUG3</literal>, <literal>DEBUG2</literal>,
7080+
<literal>DEBUG1</literal>, <literal>INFO</literal>, <literal>NOTICE</literal>,
7081+
<literal>WARNING</literal>, <literal>ERROR</literal>, <literal>LOG</literal>,
7082+
<literal>FATAL</literal>, and <literal>PANIC</literal>. Each level includes
7083+
all the levels that follow it. The later the level, the fewer messages are sent
7084+
to the log. The default is <literal>WARNING</literal> for all backend types.
7085+
Note that <literal>LOG</literal> has a different rank here than in
70737086
<xref linkend="guc-client-min-messages"/>.
70747087
Only superusers and users with the appropriate <literal>SET</literal>
70757088
privilege can change this setting.

src/backend/commands/extension.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
11431143
(void) set_config_option("client_min_messages", "warning",
11441144
PGC_USERSET, PGC_S_SESSION,
11451145
GUC_ACTION_SAVE, true, 0, false);
1146-
if (log_min_messages < WARNING)
1146+
if (log_min_messages[MyBackendType] < WARNING)
11471147
(void) set_config_option_ext("log_min_messages", "warning",
11481148
PGC_SUSET, PGC_S_SESSION,
11491149
BOOTSTRAP_SUPERUSERID,

src/backend/commands/variable.c

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "utils/datetime.h"
3636
#include "utils/fmgrprotos.h"
3737
#include "utils/guc_hooks.h"
38+
#include "utils/guc_tables.h"
3839
#include "utils/snapmgr.h"
3940
#include "utils/syscache.h"
4041
#include "utils/timestamp.h"
@@ -1257,3 +1258,207 @@ check_ssl(bool *newval, void **extra, GucSource source)
12571258
#endif
12581259
return true;
12591260
}
1261+
1262+
/*
1263+
* GUC check_hook for log_min_messages
1264+
*
1265+
* The parsing consists of a comma-separated list of BACKENDTYPENAME:LEVEL
1266+
* elements. BACKENDTYPENAME is log_min_messages_backend_types. LEVEL is
1267+
* server_message_level_options. A single LEVEL element should be part of this
1268+
* list and it is applied as a final step to the backend types that are not
1269+
* specified. For backward compatibility, the old syntax is still accepted and
1270+
* it means to apply the LEVEL for all backend types.
1271+
*/
1272+
bool
1273+
check_log_min_messages(char **newval, void **extra, GucSource source)
1274+
{
1275+
char *rawstring;
1276+
List *elemlist;
1277+
ListCell *l;
1278+
int newlevel[BACKEND_NUM_TYPES];
1279+
bool assigned[BACKEND_NUM_TYPES];
1280+
int genericlevel = -1; /* -1 means not assigned */
1281+
1282+
/* Initialize the array. */
1283+
memset(newlevel, WARNING, BACKEND_NUM_TYPES * sizeof(int));
1284+
memset(assigned, false, BACKEND_NUM_TYPES * sizeof(bool));
1285+
1286+
/* Need a modifiable copy of string. */
1287+
rawstring = guc_strdup(LOG, *newval);
1288+
1289+
/* Parse string into list of identifiers. */
1290+
if (!SplitGUCList(rawstring, ',', &elemlist))
1291+
{
1292+
/* syntax error in list */
1293+
GUC_check_errdetail("List syntax is invalid.");
1294+
guc_free(rawstring);
1295+
list_free(elemlist);
1296+
return false;
1297+
}
1298+
1299+
/* Validate and assign log level and backend type. */
1300+
foreach(l, elemlist)
1301+
{
1302+
char *tok = (char *) lfirst(l);
1303+
char *sep;
1304+
const struct config_enum_entry *entry;
1305+
1306+
/*
1307+
* Check whether there is a backend type following the log level. If
1308+
* there is no separator, it means this is the generic log level. The
1309+
* generic log level will be assigned to the backend types that were
1310+
* not informed.
1311+
*/
1312+
sep = strchr(tok, ':');
1313+
if (sep == NULL)
1314+
{
1315+
bool found = false;
1316+
1317+
/* Reject duplicates for generic log level. */
1318+
if (genericlevel != -1)
1319+
{
1320+
GUC_check_errdetail("Generic log level was already assigned.");
1321+
guc_free(rawstring);
1322+
list_free(elemlist);
1323+
return false;
1324+
}
1325+
1326+
/* Is the log level valid? */
1327+
for (entry = server_message_level_options; entry && entry->name; entry++)
1328+
{
1329+
if (pg_strcasecmp(entry->name, tok) == 0)
1330+
{
1331+
genericlevel = entry->val;
1332+
found = true;
1333+
break;
1334+
}
1335+
}
1336+
1337+
if (!found)
1338+
{
1339+
GUC_check_errdetail("Unrecognized log level: \"%s\".", tok);
1340+
guc_free(rawstring);
1341+
list_free(elemlist);
1342+
return false;
1343+
}
1344+
}
1345+
else
1346+
{
1347+
char *loglevel;
1348+
char *btype;
1349+
bool found = false;
1350+
1351+
btype = guc_malloc(LOG, (sep - tok) + 1);
1352+
if (!btype)
1353+
{
1354+
guc_free(rawstring);
1355+
list_free(elemlist);
1356+
return false;
1357+
}
1358+
memcpy(btype, tok, sep - tok);
1359+
btype[sep - tok] = '\0';
1360+
loglevel = sep + 1;
1361+
1362+
/* Is the log level valid? */
1363+
for (entry = server_message_level_options; entry && entry->name; entry++)
1364+
{
1365+
if (pg_strcasecmp(entry->name, loglevel) == 0)
1366+
{
1367+
found = true;
1368+
break;
1369+
}
1370+
}
1371+
1372+
if (!found)
1373+
{
1374+
GUC_check_errdetail("Unrecognized log level: \"%s\".", loglevel);
1375+
guc_free(btype);
1376+
guc_free(rawstring);
1377+
list_free(elemlist);
1378+
return false;
1379+
}
1380+
1381+
/*
1382+
* Is the backend type name valid? There might be multiple entries
1383+
* per backend type, don't bail out because it can assign the
1384+
* value for multiple entries.
1385+
*/
1386+
found = false;
1387+
for (int i = 0; i < BACKEND_NUM_TYPES; i++)
1388+
{
1389+
if (pg_strcasecmp(log_min_messages_backend_types[i], btype) == 0)
1390+
{
1391+
/* Reject duplicates for a backend type. */
1392+
if (assigned[i])
1393+
{
1394+
GUC_check_errdetail("Backend type \"%s\" was already assigned.", btype);
1395+
guc_free(btype);
1396+
guc_free(rawstring);
1397+
list_free(elemlist);
1398+
return false;
1399+
}
1400+
1401+
newlevel[i] = entry->val;
1402+
assigned[i] = true;
1403+
found = true;
1404+
}
1405+
}
1406+
1407+
if (!found)
1408+
{
1409+
GUC_check_errdetail("Unrecognized backend type: \"%s\".", btype);
1410+
guc_free(btype);
1411+
guc_free(rawstring);
1412+
list_free(elemlist);
1413+
return false;
1414+
}
1415+
1416+
guc_free(btype);
1417+
}
1418+
}
1419+
1420+
/*
1421+
* The generic log level must be specified. It is the fallback value.
1422+
*/
1423+
if (genericlevel == -1)
1424+
{
1425+
GUC_check_errdetail("Generic log level was not defined.");
1426+
guc_free(rawstring);
1427+
list_free(elemlist);
1428+
return false;
1429+
}
1430+
1431+
/*
1432+
* Apply the generic log level after all of the specific backend type have
1433+
* been assigned. Hence, it doesn't matter the order you specify the
1434+
* generic log level, the final result will be the same.
1435+
*/
1436+
for (int i = 0; i < BACKEND_NUM_TYPES; i++)
1437+
{
1438+
if (!assigned[i])
1439+
newlevel[i] = genericlevel;
1440+
}
1441+
1442+
guc_free(rawstring);
1443+
list_free(elemlist);
1444+
1445+
/*
1446+
* Pass back data for assign_log_min_messages to use.
1447+
*/
1448+
*extra = guc_malloc(LOG, BACKEND_NUM_TYPES * sizeof(int));
1449+
if (!*extra)
1450+
return false;
1451+
memcpy(*extra, newlevel, BACKEND_NUM_TYPES * sizeof(int));
1452+
1453+
return true;
1454+
}
1455+
1456+
/*
1457+
* GUC assign_hook for log_min_messages
1458+
*/
1459+
void
1460+
assign_log_min_messages(const char *newval, void *extra)
1461+
{
1462+
for (int i = 0; i < BACKEND_NUM_TYPES; i++)
1463+
log_min_messages[i] = ((int *) extra)[i];
1464+
}

src/backend/postmaster/launch_backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ typedef struct
179179
} child_process_kind;
180180

181181
static child_process_kind child_process_kinds[] = {
182-
#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
182+
#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
183183
[bktype] = {description, main_func, shmem_attach},
184184
#include "postmaster/proctypelist.h"
185185
#undef PG_PROCTYPE

src/backend/utils/error/elog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ is_log_level_output(int elevel, int log_min_level)
235235
static inline bool
236236
should_output_to_server(int elevel)
237237
{
238-
return is_log_level_output(elevel, log_min_messages);
238+
return is_log_level_output(elevel, log_min_messages[MyBackendType]);
239239
}
240240

241241
/*

src/backend/utils/init/miscinit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ GetBackendTypeDesc(BackendType backendType)
266266

267267
switch (backendType)
268268
{
269-
#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
269+
#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
270270
case bktype: backendDesc = description; break;
271271
#include "postmaster/proctypelist.h"
272272
#undef PG_PROCTYPE

src/backend/utils/misc/guc_parameters.dat

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,6 +2709,16 @@
27092709
boot_val => '"%m [%p] "',
27102710
},
27112711

2712+
{ name => 'log_min_messages', type => 'string', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
2713+
short_desc => 'Sets the message levels that are logged.',
2714+
long_desc => 'Each level includes all the levels that follow it. The later the level, the fewer messages are sent.',
2715+
flags => 'GUC_LIST_INPUT',
2716+
variable => 'log_min_messages_string',
2717+
boot_val => '"WARNING"',
2718+
check_hook => 'check_log_min_messages',
2719+
assign_hook => 'assign_log_min_messages',
2720+
},
2721+
27122722
{ name => 'log_timezone', type => 'string', context => 'PGC_SIGHUP', group => 'LOGGING_WHAT',
27132723
short_desc => 'Sets the time zone to use in log messages.',
27142724
variable => 'log_timezone_string',
@@ -3278,14 +3288,6 @@
32783288
options => 'log_error_verbosity_options',
32793289
},
32803290

3281-
{ name => 'log_min_messages', type => 'enum', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
3282-
short_desc => 'Sets the message levels that are logged.',
3283-
long_desc => 'Each level includes all the levels that follow it. The later the level, the fewer messages are sent.',
3284-
variable => 'log_min_messages',
3285-
boot_val => 'WARNING',
3286-
options => 'server_message_level_options',
3287-
},
3288-
32893291
{ name => 'log_min_error_statement', type => 'enum', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
32903292
short_desc => 'Causes all statements generating error at or above this level to be logged.',
32913293
long_desc => 'Each level includes all the levels that follow it. The later the level, the fewer messages are sent.',

src/backend/utils/misc/guc_tables.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static const struct config_enum_entry client_message_level_options[] = {
146146
{NULL, 0, false}
147147
};
148148

149-
static const struct config_enum_entry server_message_level_options[] = {
149+
const struct config_enum_entry server_message_level_options[] = {
150150
{"debug5", DEBUG5, false},
151151
{"debug4", DEBUG4, false},
152152
{"debug3", DEBUG3, false},
@@ -537,7 +537,6 @@ static bool default_with_oids = false;
537537
bool current_role_is_superuser;
538538

539539
int log_min_error_statement = ERROR;
540-
int log_min_messages = WARNING;
541540
int client_min_messages = NOTICE;
542541
int log_min_duration_sample = -1;
543542
int log_min_duration_statement = -1;
@@ -595,6 +594,7 @@ static char *server_version_string;
595594
static int server_version_num;
596595
static char *debug_io_direct_string;
597596
static char *restrict_nonsystem_relation_kind_string;
597+
static char *log_min_messages_string;
598598

599599
#ifdef HAVE_SYSLOG
600600
#define DEFAULT_SYSLOG_FACILITY LOG_LOCAL0
@@ -639,6 +639,27 @@ char *role_string;
639639
/* should be static, but guc.c needs to get at this */
640640
bool in_hot_standby_guc;
641641

642+
/*
643+
* It should be static, but commands/variable.c needs to get at this.
644+
*/
645+
int log_min_messages[] = {
646+
#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
647+
[bktype] = log_min_messages,
648+
#include "postmaster/proctypelist.h"
649+
#undef PG_PROCTYPE
650+
};
651+
652+
/*
653+
* It might be in commands/variable.c but for convenience it is near
654+
* log_min_messages.
655+
*/
656+
const char *const log_min_messages_backend_types[] = {
657+
#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
658+
[bktype] = bkcategory,
659+
#include "postmaster/proctypelist.h"
660+
#undef PG_PROCTYPE
661+
};
662+
642663

643664
/*
644665
* Displayable names for context types (enum GucContext)

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@
541541
# log
542542
# fatal
543543
# panic
544+
# and an optional comma-separated list
545+
# of backendtype:level
544546

545547
#log_min_error_statement = error # values in order of decreasing detail:
546548
# debug5

0 commit comments

Comments
 (0)