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

Commit f78308b

Browse files
Sergey DudoladovCommitfest Bot
Sergey Dudoladov
authored and
Commitfest Bot
committed
Introduce log_connection_messages
This patch removes 'log_connections' and 'log_disconnections' in favor of 'log_connection_messages', thereby introducing incompatibility Author: Sergey Dudoladov Reviewed-by: Justin Pryzby, Jacob Champion Discussion: https://www.postgresql.org/message-id/flat/CAA8Fd-qCB96uwfgMKrzfNs32mqqysi53yZFNVaRNJ6xDthZEgA%40mail.gmail.com
1 parent 54d2360 commit f78308b

File tree

31 files changed

+197
-87
lines changed

31 files changed

+197
-87
lines changed

doc/src/sgml/config.sgml

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
An example of what this file might look like is:
141141
<programlisting>
142142
# This is a comment
143-
log_connections = yes
143+
log_connection_messages = all
144144
log_destination = 'syslog'
145145
search_path = '"$user", public'
146146
shared_buffers = 128MB
@@ -337,7 +337,7 @@ UPDATE pg_settings SET setting = reset_val WHERE name = 'configuration_parameter
337337
<option>-c name=value</option> command-line parameter, or its equivalent
338338
<option>--name=value</option> variation. For example,
339339
<programlisting>
340-
postgres -c log_connections=yes --log-destination='syslog'
340+
postgres -c log_connection_messages=all --log-destination='syslog'
341341
</programlisting>
342342
Settings provided in this way override those set via
343343
<filename>postgresql.conf</filename> or <command>ALTER SYSTEM</command>,
@@ -7314,23 +7314,75 @@ local0.* /var/log/postgresql
73147314
</listitem>
73157315
</varlistentry>
73167316

7317-
<varlistentry id="guc-log-connections" xreflabel="log_connections">
7318-
<term><varname>log_connections</varname> (<type>boolean</type>)
7317+
<varlistentry id="guc-log-connection-messages" xreflabel="log_connection_messages">
7318+
<term><varname>log_connection_messages</varname> (<type>string</type>)
73197319
<indexterm>
7320-
<primary><varname>log_connections</varname> configuration parameter</primary>
7320+
<primary><varname>log_connection_messages</varname> configuration parameter</primary>
73217321
</indexterm>
73227322
</term>
73237323
<listitem>
73247324
<para>
7325-
Causes each attempted connection to the server to be logged,
7326-
as well as successful completion of both client authentication (if
7327-
necessary) and authorization.
7325+
Causes the specified stages of each connection attempt to be logged.
7326+
Example: <literal>authorized,disconnected</literal>.
7327+
The default is the empty string, which means that nothing is logged.
73287328
Only superusers and users with the appropriate <literal>SET</literal>
73297329
privilege can change this parameter at session start,
73307330
and it cannot be changed at all within a session.
7331-
The default is <literal>off</literal>.
73327331
</para>
73337332

7333+
<table id="connection-messages">
7334+
<title>Connection messages</title>
7335+
<tgroup cols="2">
7336+
<colspec colname="col1" colwidth="1*"/>
7337+
<colspec colname="col2" colwidth="2*"/>
7338+
<thead>
7339+
<row>
7340+
<entry>Name</entry>
7341+
<entry>Description</entry>
7342+
</row>
7343+
</thead>
7344+
<tbody>
7345+
<row>
7346+
<entry><literal>received</literal></entry>
7347+
<entry>Logs receipt of a connection. At this point a connection has been
7348+
received, but no further work has been done: PostgreSQL is about to start
7349+
interacting with a client.</entry>
7350+
</row>
7351+
7352+
<row>
7353+
<entry><literal>authenticated</literal></entry>
7354+
<entry>Logs the original identity used by an authentication method
7355+
to identify a user. In most cases, the identity string matches the
7356+
PostgreSQL username, but some third-party authentication methods may
7357+
alter the original user identifier before the server stores it. Failed
7358+
authentication is always logged regardless of the value of this setting.
7359+
</entry>
7360+
</row>
7361+
7362+
<row>
7363+
<entry><literal>authorized</literal></entry>
7364+
<entry>Logs successful completion of authorization. At this point the
7365+
connection has been fully established.
7366+
</entry>
7367+
</row>
7368+
7369+
<row>
7370+
<entry><literal>disconnected</literal></entry>
7371+
<entry>Logs session termination. The log output provides information
7372+
similar to <literal>authorized</literal>, plus the duration of the session.
7373+
</entry>
7374+
</row>
7375+
7376+
<row>
7377+
<entry><literal>all</literal></entry>
7378+
<entry>A convenience alias. If present, it must be the only value in the
7379+
list.</entry>
7380+
</row>
7381+
7382+
</tbody>
7383+
</tgroup>
7384+
</table>
7385+
73347386
<note>
73357387
<para>
73367388
Some client programs, like <application>psql</application>, attempt
@@ -7342,26 +7394,6 @@ local0.* /var/log/postgresql
73427394
</listitem>
73437395
</varlistentry>
73447396

7345-
<varlistentry id="guc-log-disconnections" xreflabel="log_disconnections">
7346-
<term><varname>log_disconnections</varname> (<type>boolean</type>)
7347-
<indexterm>
7348-
<primary><varname>log_disconnections</varname> configuration parameter</primary>
7349-
</indexterm>
7350-
</term>
7351-
<listitem>
7352-
<para>
7353-
Causes session terminations to be logged. The log output
7354-
provides information similar to <varname>log_connections</varname>,
7355-
plus the duration of the session.
7356-
Only superusers and users with the appropriate <literal>SET</literal>
7357-
privilege can change this parameter at session start,
7358-
and it cannot be changed at all within a session.
7359-
The default is <literal>off</literal>.
7360-
</para>
7361-
</listitem>
7362-
</varlistentry>
7363-
7364-
73657397
<varlistentry id="guc-log-duration" xreflabel="log_duration">
73667398
<term><varname>log_duration</varname> (<type>boolean</type>)
73677399
<indexterm>

src/backend/commands/variable.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,78 @@ show_role(void)
10481048
}
10491049

10501050

1051+
/* check_hook: validate new log_connection_messages value
1052+
*
1053+
* The implementation follows the check_log_destination hook.
1054+
*/
1055+
bool
1056+
check_log_connection_messages(char **newval, void **extra, GucSource source)
1057+
{
1058+
char *rawname;
1059+
List *namelist;
1060+
ListCell *l;
1061+
int newlogconnect = 0;
1062+
int *myextra;
1063+
1064+
if (pg_strcasecmp(*newval, "all") == 0)
1065+
{
1066+
newlogconnect |= LOG_CONNECTION_ALL;
1067+
myextra = (int *) guc_malloc(LOG, sizeof(int));
1068+
if (!myextra)
1069+
return false;
1070+
*myextra = newlogconnect;
1071+
*extra = (void *) myextra;
1072+
return true;
1073+
}
1074+
1075+
/* Need a modifiable copy of string */
1076+
rawname = pstrdup(*newval);
1077+
1078+
/* Parse string into list of identifiers */
1079+
/* We rely on SplitIdentifierString to downcase and strip whitespace */
1080+
if (!SplitIdentifierString(rawname, ',', &namelist))
1081+
{
1082+
/* syntax error in name list */
1083+
GUC_check_errdetail("List syntax is invalid.");
1084+
pfree(rawname);
1085+
list_free(namelist);
1086+
return false;
1087+
}
1088+
1089+
foreach(l, namelist)
1090+
{
1091+
char *stage = (char *) lfirst(l);
1092+
if (pg_strcasecmp(stage, "received") == 0)
1093+
newlogconnect |= LOG_CONNECTION_RECEIVED;
1094+
else if (pg_strcasecmp(stage, "authenticated") == 0)
1095+
newlogconnect |= LOG_CONNECTION_AUTHENTICATED;
1096+
else if (pg_strcasecmp(stage, "authorized") == 0)
1097+
newlogconnect |= LOG_CONNECTION_AUTHORIZED;
1098+
else if (pg_strcasecmp(stage, "disconnected") == 0)
1099+
newlogconnect |= LOG_CONNECTION_DISCONNECTED;
1100+
else {
1101+
GUC_check_errcode(ERRCODE_INVALID_PARAMETER_VALUE);
1102+
GUC_check_errmsg("invalid value for parameter '%s'", stage);
1103+
GUC_check_errdetail("Valid values to use in the list are 'all', 'received', 'authenticated', 'authorized', and 'disconnected'."
1104+
" If 'all' is present, it must be the only value in the list.");
1105+
pfree(rawname);
1106+
list_free(namelist);
1107+
return false;
1108+
}
1109+
}
1110+
1111+
pfree(rawname);
1112+
list_free(namelist);
1113+
1114+
myextra = (int *) guc_malloc(LOG, sizeof(int));
1115+
if (!myextra)
1116+
return false;
1117+
*myextra = newlogconnect;
1118+
*extra = (void *) myextra;
1119+
1120+
return true;
1121+
}
1122+
10511123
/*
10521124
* PATH VARIABLES
10531125
*
@@ -1070,6 +1142,15 @@ check_canonical_path(char **newval, void **extra, GucSource source)
10701142

10711143

10721144
/*
1145+
* assign_log_connection_messages: GUC assign_hook for log_connection_messages
1146+
*/
1147+
void
1148+
assign_log_connection_messages(const char *newval, void *extra)
1149+
{
1150+
Log_connection_messages = *((int *) extra);
1151+
}
1152+
1153+
/*
10731154
* MISCELLANEOUS
10741155
*/
10751156

src/backend/libpq/auth.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ auth_failed(Port *port, int status, const char *logdetail)
317317
/*
318318
* Sets the authenticated identity for the current user. The provided string
319319
* will be stored into MyClientConnectionInfo, alongside the current HBA
320-
* method in use. The ID will be logged if log_connections is enabled.
320+
* method in use. The ID will be logged if
321+
* log_connection_messages contains the 'authenticated' value.
321322
*
322323
* Auth methods should call this routine exactly once, as soon as the user is
323324
* successfully authenticated, even if they have reasons to know that
@@ -349,7 +350,7 @@ set_authn_id(Port *port, const char *id)
349350
MyClientConnectionInfo.authn_id = MemoryContextStrdup(TopMemoryContext, id);
350351
MyClientConnectionInfo.auth_method = port->hba->auth_method;
351352

352-
if (Log_connections)
353+
if (Log_connection_messages & LOG_CONNECTION_AUTHENTICATED)
353354
{
354355
ereport(LOG,
355356
errmsg("connection authenticated: identity=\"%s\" method=%s "
@@ -633,11 +634,11 @@ ClientAuthentication(Port *port)
633634
#endif
634635
}
635636

636-
if (Log_connections && status == STATUS_OK &&
637+
if (Log_connection_messages && LOG_CONNECTION_AUTHENTICATED && status == STATUS_OK &&
637638
!MyClientConnectionInfo.authn_id)
638639
{
639640
/*
640-
* Normally, if log_connections is set, the call to set_authn_id()
641+
* Normally, if log_connection_messages is set, the call to set_authn_id()
641642
* will log the connection. However, if that function is never
642643
* called, perhaps because the trust method is in use, then we handle
643644
* the logging here instead.

src/backend/postmaster/postmaster.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ int PreAuthDelay = 0;
237237
int AuthenticationTimeout = 60;
238238

239239
bool log_hostname; /* for ps display and logging */
240-
bool Log_connections = false;
241240

242241
bool enable_bonjour = false;
243242
char *bonjour_name;

src/backend/tcop/backend_startup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ BackendInitialize(ClientSocket *client_sock, CAC_state cac)
201201
port->remote_host = MemoryContextStrdup(TopMemoryContext, remote_host);
202202
port->remote_port = MemoryContextStrdup(TopMemoryContext, remote_port);
203203

204-
/* And now we can issue the Log_connections message, if wanted */
205-
if (Log_connections)
204+
/* And now we can issue the "connection received" message, if wanted */
205+
if (Log_connection_messages & LOG_CONNECTION_RECEIVED)
206206
{
207207
if (remote_port[0])
208208
ereport(LOG,

src/backend/tcop/postgres.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ const char *debug_query_string; /* client-supplied query string */
8989
/* Note: whereToSendOutput is initialized for the bootstrap/standalone case */
9090
CommandDest whereToSendOutput = DestDebug;
9191

92-
/* flag for logging end of session */
93-
bool Log_disconnections = false;
92+
/* flags for logging information about session state */
93+
int Log_connection_messages = 0;
9494

9595
int log_statement = LOGSTMT_NONE;
9696

@@ -3654,8 +3654,7 @@ set_debug_options(int debug_flag, GucContext context, GucSource source)
36543654

36553655
if (debug_flag >= 1 && context == PGC_POSTMASTER)
36563656
{
3657-
SetConfigOption("log_connections", "true", context, source);
3658-
SetConfigOption("log_disconnections", "true", context, source);
3657+
SetConfigOption("log_connection_messages", "all", context, source);
36593658
}
36603659
if (debug_flag >= 2)
36613660
SetConfigOption("log_statement", "all", context, source);
@@ -4271,9 +4270,9 @@ PostgresMain(const char *dbname, const char *username)
42714270

42724271
/*
42734272
* Also set up handler to log session end; we have to wait till now to be
4274-
* sure Log_disconnections has its final value.
4273+
* sure Log_connection_messages has its final value.
42754274
*/
4276-
if (IsUnderPostmaster && Log_disconnections)
4275+
if (IsUnderPostmaster && (Log_connection_messages & LOG_CONNECTION_DISCONNECTED))
42774276
on_proc_exit(log_disconnections, 0);
42784277

42794278
pgstat_report_connect(MyDatabaseId);

src/backend/utils/init/postinit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ PerformAuthentication(Port *port)
252252
*/
253253
disable_timeout(STATEMENT_TIMEOUT, false);
254254

255-
if (Log_connections)
255+
if (Log_connection_messages & LOG_CONNECTION_AUTHORIZED)
256256
{
257257
StringInfoData logmsg;
258258

src/backend/utils/misc/guc_tables.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ static char *recovery_target_string;
616616
static char *recovery_target_xid_string;
617617
static char *recovery_target_name_string;
618618
static char *recovery_target_lsn_string;
619+
static char *log_connection_messages_string;
619620

620621
/* should be static, but commands/variable.c needs to get at this */
621622
char *role_string;
@@ -1219,15 +1220,6 @@ struct config_bool ConfigureNamesBool[] =
12191220
true,
12201221
NULL, NULL, NULL
12211222
},
1222-
{
1223-
{"log_connections", PGC_SU_BACKEND, LOGGING_WHAT,
1224-
gettext_noop("Logs each successful connection."),
1225-
NULL
1226-
},
1227-
&Log_connections,
1228-
false,
1229-
NULL, NULL, NULL
1230-
},
12311223
{
12321224
{"trace_connection_negotiation", PGC_POSTMASTER, DEVELOPER_OPTIONS,
12331225
gettext_noop("Logs details of pre-authentication connection handshake."),
@@ -1238,15 +1230,6 @@ struct config_bool ConfigureNamesBool[] =
12381230
false,
12391231
NULL, NULL, NULL
12401232
},
1241-
{
1242-
{"log_disconnections", PGC_SU_BACKEND, LOGGING_WHAT,
1243-
gettext_noop("Logs end of a session, including duration."),
1244-
NULL
1245-
},
1246-
&Log_disconnections,
1247-
false,
1248-
NULL, NULL, NULL
1249-
},
12501233
{
12511234
{"log_replication_commands", PGC_SUSET, LOGGING_WHAT,
12521235
gettext_noop("Logs each replication command."),
@@ -4467,6 +4450,16 @@ struct config_string ConfigureNamesString[] =
44674450
check_session_authorization, assign_session_authorization, NULL
44684451
},
44694452

4453+
{
4454+
{"log_connection_messages", PGC_SU_BACKEND, LOGGING_WHAT,
4455+
gettext_noop("Lists connection stages to log."),
4456+
NULL,
4457+
GUC_LIST_INPUT
4458+
},
4459+
&log_connection_messages_string,
4460+
"",
4461+
check_log_connection_messages, assign_log_connection_messages, NULL
4462+
},
44704463
{
44714464
{"log_destination", PGC_SIGHUP, LOGGING_WHERE,
44724465
gettext_noop("Sets the destination for server log output."),

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# require a server shutdown and restart to take effect.
2222
#
2323
# Any parameter can also be given as a command-line option to the server, e.g.,
24-
# "postgres -c log_connections=on". Some parameters can be changed at run time
24+
# "postgres -c log_connection_messages=all". Some parameters can be changed at run time
2525
# with the "SET" SQL command.
2626
#
2727
# Memory units: B = bytes Time units: us = microseconds
@@ -578,8 +578,7 @@
578578
# actions running at least this number
579579
# of milliseconds.
580580
#log_checkpoints = on
581-
#log_connections = off
582-
#log_disconnections = off
581+
#log_connection_messages = ''
583582
#log_duration = off
584583
#log_error_verbosity = default # terse, default, or verbose messages
585584
#log_hostname = off

0 commit comments

Comments
 (0)