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

Commit d438f52

Browse files
author
Commitfest Bot
committed
[CF 5442] v14 - Log connection establishment timings
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5442 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/CAAKRu_Zgk-_xLxpddscwYSzGXS_jbLS_fzK1TvjUBYFDEXuPMg@mail.gmail.com Author(s): Melanie Plageman
2 parents 043745c + acac639 commit d438f52

File tree

16 files changed

+508
-25
lines changed

16 files changed

+508
-25
lines changed

doc/src/sgml/config.sgml

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7315,20 +7315,100 @@ local0.* /var/log/postgresql
73157315
</varlistentry>
73167316

73177317
<varlistentry id="guc-log-connections" xreflabel="log_connections">
7318-
<term><varname>log_connections</varname> (<type>boolean</type>)
7318+
<term><varname>log_connections</varname> (<type>string</type>)
73197319
<indexterm>
73207320
<primary><varname>log_connections</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 aspects of each connection attempt to the server to be
7326+
logged. The default is the empty string, <literal>''</literal>,
7327+
which disables all connection logging. The following options may
7328+
be specified alone or in a comma-separated list:
7329+
</para>
7330+
7331+
<table id="log-connections-options">
7332+
<title>Log Connection Options</title>
7333+
<tgroup cols="2">
7334+
<colspec colname="col1" colwidth="1*"/>
7335+
<colspec colname="col2" colwidth="2*"/>
7336+
<thead>
7337+
<row>
7338+
<entry>Name</entry>
7339+
<entry>Description</entry>
7340+
</row>
7341+
</thead>
7342+
<tbody>
7343+
<row>
7344+
<entry><literal>receipt</literal></entry>
7345+
<entry>Logs receipt of a connection.</entry>
7346+
</row>
7347+
7348+
<row>
7349+
<entry><literal>authentication</literal></entry>
7350+
<entry>
7351+
Logs the original identity used by an authentication method
7352+
to identify a user. In most cases, the identity string
7353+
matches the PostgreSQL username, but some third-party
7354+
authentication methods may alter the original user
7355+
identifier before the server stores it. Failed
7356+
authentication is always logged regardless of the value of
7357+
this setting.
7358+
</entry>
7359+
</row>
7360+
7361+
<row>
7362+
<entry><literal>authorization</literal></entry>
7363+
<entry>
7364+
Logs successful completion of authorization. At this point
7365+
the connection has been established but the backend is not
7366+
yet fully set up. The log message includes the authorized
7367+
username as well as the database name and application name,
7368+
if applicable.
7369+
</entry>
7370+
</row>
7371+
7372+
<row>
7373+
<entry><literal>setup_durations</literal></entry>
7374+
<entry>
7375+
Logs the time spent establishing the connection and setting up the
7376+
backend at the time the connection is ready to execute its first
7377+
query. The log message includes the total setup duration, starting
7378+
from the postmaster accepting the incoming connection and ending
7379+
when the connection is ready for query. It also includes the time
7380+
it took to fork the new backend and the time it took to
7381+
authenticate the user.
7382+
</entry>
7383+
</row>
7384+
7385+
<row>
7386+
<entry><literal>all</literal></entry>
7387+
<entry>
7388+
A convenience alias equivalent to specifying all options. If
7389+
<literal>all</literal> is specified in a list of other
7390+
options, all connection aspects will be logged.
7391+
</entry>
7392+
</row>
7393+
7394+
</tbody>
7395+
</tgroup>
7396+
</table>
7397+
7398+
<para>
7399+
For the purposes of backwards compatibility, <literal>on</literal>,
7400+
<literal>off</literal>, <literal>true</literal>,
7401+
<literal>false</literal>, <literal>yes</literal>,
7402+
<literal>no</literal>, <literal>1</literal>, and <literal>0</literal>
7403+
are still supported. The positive values are equivalent to specifying
7404+
the <literal>receipt</literal>, <literal>authentication</literal>, and
7405+
<literal>authorization</literal> options.
7406+
</para>
7407+
7408+
<para>
73287409
Only superusers and users with the appropriate <literal>SET</literal>
73297410
privilege can change this parameter at session start,
73307411
and it cannot be changed at all within a session.
7331-
The default is <literal>off</literal>.
73327412
</para>
73337413

73347414
<note>

src/backend/libpq/auth.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "postmaster/postmaster.h"
3939
#include "replication/walsender.h"
4040
#include "storage/ipc.h"
41+
#include "tcop/backend_startup.h"
4142
#include "utils/memutils.h"
4243

4344
/*----------------------------------------------------------------
@@ -317,7 +318,8 @@ auth_failed(Port *port, int status, const char *logdetail)
317318
/*
318319
* Sets the authenticated identity for the current user. The provided string
319320
* will be stored into MyClientConnectionInfo, alongside the current HBA
320-
* method in use. The ID will be logged if log_connections is enabled.
321+
* method in use. The ID will be logged if log_connections has the
322+
* 'authentication' option specified.
321323
*
322324
* Auth methods should call this routine exactly once, as soon as the user is
323325
* successfully authenticated, even if they have reasons to know that
@@ -349,7 +351,7 @@ set_authn_id(Port *port, const char *id)
349351
MyClientConnectionInfo.authn_id = MemoryContextStrdup(TopMemoryContext, id);
350352
MyClientConnectionInfo.auth_method = port->hba->auth_method;
351353

352-
if (Log_connections)
354+
if (log_connections & LOG_CONNECTION_AUTHENTICATION)
353355
{
354356
ereport(LOG,
355357
errmsg("connection authenticated: identity=\"%s\" method=%s "
@@ -633,7 +635,8 @@ ClientAuthentication(Port *port)
633635
#endif
634636
}
635637

636-
if (Log_connections && status == STATUS_OK &&
638+
if ((log_connections & LOG_CONNECTION_AUTHENTICATION) &&
639+
status == STATUS_OK &&
637640
!MyClientConnectionInfo.authn_id)
638641
{
639642
/*

src/backend/postmaster/launch_backend.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ postmaster_child_launch(BackendType child_type, int child_slot,
232232

233233
Assert(IsPostmasterEnvironment && !IsUnderPostmaster);
234234

235+
/* Capture time Postmaster initiates process creation for logging */
236+
if (IsConnectionBackend(child_type))
237+
((BackendStartupData *) startup_data)->fork_started = GetCurrentTimestamp();
238+
235239
#ifdef EXEC_BACKEND
236240
pid = internal_forkexec(child_process_kinds[child_type].name, child_slot,
237241
startup_data, startup_data_len, client_sock);
@@ -240,6 +244,16 @@ postmaster_child_launch(BackendType child_type, int child_slot,
240244
pid = fork_process();
241245
if (pid == 0) /* child */
242246
{
247+
/* Capture and transfer timings that may be needed for logging */
248+
if (IsConnectionBackend(child_type))
249+
{
250+
conn_timing.socket_create =
251+
((BackendStartupData *) startup_data)->socket_created;
252+
conn_timing.fork_start =
253+
((BackendStartupData *) startup_data)->fork_started;
254+
conn_timing.fork_end = GetCurrentTimestamp();
255+
}
256+
243257
/* Close the postmaster's sockets */
244258
ClosePostmasterPorts(child_type == B_LOGGER);
245259

@@ -586,11 +600,18 @@ SubPostmasterMain(int argc, char *argv[])
586600
char *child_kind;
587601
BackendType child_type;
588602
bool found = false;
603+
TimestampTz fork_end;
589604

590605
/* In EXEC_BACKEND case we will not have inherited these settings */
591606
IsPostmasterEnvironment = true;
592607
whereToSendOutput = DestNone;
593608

609+
/*
610+
* Capture the end of process creation for logging. We don't include the
611+
* time spent copying data from shared memory and setting up the backend.
612+
*/
613+
fork_end = GetCurrentTimestamp();
614+
594615
/* Setup essential subsystems (to ensure elog() behaves sanely) */
595616
InitializeGUCOptions();
596617

@@ -648,6 +669,16 @@ SubPostmasterMain(int argc, char *argv[])
648669
/* Read in remaining GUC variables */
649670
read_nondefault_variables();
650671

672+
/* Capture and transfer timings that may be needed for log_connections */
673+
if (IsConnectionBackend(child_type))
674+
{
675+
conn_timing.socket_create =
676+
((BackendStartupData *) startup_data)->socket_created;
677+
conn_timing.fork_start =
678+
((BackendStartupData *) startup_data)->fork_started;
679+
conn_timing.fork_end = fork_end;
680+
}
681+
651682
/*
652683
* Check that the data directory looks valid, which will also check the
653684
* privileges on the data directory and update our umask and file/group

src/backend/postmaster/postmaster.c

Lines changed: 6 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;
@@ -3478,6 +3477,12 @@ BackendStartup(ClientSocket *client_sock)
34783477
BackendStartupData startup_data;
34793478
CAC_state cac;
34803479

3480+
/*
3481+
* Capture time that Postmaster got a socket from accept (for logging
3482+
* connection establishment and setup total duration).
3483+
*/
3484+
startup_data.socket_created = GetCurrentTimestamp();
3485+
34813486
/*
34823487
* Allocate and assign the child slot. Note we must do this before
34833488
* forking, so that we can handle failures (out of memory or child-process

0 commit comments

Comments
 (0)