Skip to content

Commit 234e6f4

Browse files
jakubwartakEDBhackorum
authored andcommitted
Add log_postmaster_overloads and log_postmaster_excess_connections
log_postmaster_overloads - Add possibility to log basic resource consumption, activity metrics and errors by the postmaster process in cases where it would be overloaded (defined as 75% or getting late on CPU). log_postmaster_excess_connections - Allow logging the same postmaster diagnostic stats just based just on the rate of new connections accepted per second, independent of the postmaster's own CPU usage. This GUC allows detecting connection storms even before the postmaster is not fully saturated. Author: Jakub Wartak <jakub.wartak@enterprisedb.com> Reviewed-by: Robert Haas <robertmhaas@gmail.com> Reviewed-by: Quan Zongliang <quanzongliang@yeah.net> Discussion: https://postgr.es/m/CAKZiRmxeTNAe_VZ9m%3Drt%3D5155mvL%3DCFsQ4ENJWhuUdgMe%3D_Udg%40mail.gmail.com
1 parent 31878cc commit 234e6f4

9 files changed

Lines changed: 434 additions & 2 deletions

File tree

‎doc/src/sgml/config.sgml‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8536,6 +8536,105 @@ log_line_prefix = '%m [%p] %q%u@%d/%a '
85368536
</listitem>
85378537
</varlistentry>
85388538

8539+
<varlistentry id="guc-log-postmaster-excess-connections" xreflabel="log_postmaster_excess_connections">
8540+
<term><varname>log_postmaster_excess_connections</varname> (<type>integer</type>)
8541+
<indexterm>
8542+
<primary><varname>log_postmaster_excess_connections</varname></primary>
8543+
<secondary>configuration parameter</secondary>
8544+
</indexterm>
8545+
</term>
8546+
<listitem>
8547+
<para>
8548+
If set to a positive value, the postmaster checks once per second
8549+
how many new connections it accepted during that second, and if
8550+
that rate exceeds this many connections per second, emits the same
8551+
<literal>LOG</literal>-level message as
8552+
<xref linkend="guc-log-postmaster-overloads"/> does. This can help
8553+
detect connection storms even in cases where the postmaster is not
8554+
overloaded.
8555+
</para>
8556+
<para>
8557+
The default is <literal>0</literal>, which disables this check.
8558+
This parameter can only be set in the
8559+
<filename>postgresql.conf</filename> file or on the server
8560+
command line.
8561+
</para>
8562+
</listitem>
8563+
</varlistentry>
8564+
8565+
<varlistentry id="guc-log-postmaster-overloads" xreflabel="log_postmaster_overloads">
8566+
<term><varname>log_postmaster_overloads</varname> (<type>boolean</type>)
8567+
<indexterm>
8568+
<primary><varname>log_postmaster_overloads</varname></primary>
8569+
<secondary>configuration parameter</secondary>
8570+
</indexterm>
8571+
</term>
8572+
<listitem>
8573+
<para>
8574+
If enabled, the postmaster checks its own resource usage every
8575+
second, and if it has used more than 75% of one CPU core during
8576+
that second, or could not serve at least one incoming connection,
8577+
emits a message at <literal>LOG</literal> level
8578+
summarizing its recent activity. Since by design the postmaster is a
8579+
single process handling all new connections and parallel worker
8580+
launches, constant CPU usage above this threshold indicates that it
8581+
may be struggling to keep up, typically because of a very high rate of
8582+
incoming connection attempts. A sample message looks like:
8583+
<screen>
8584+
postmaster potentially overloaded, stats: avg 2905.00 conns/sec; 2904.00 disconns/sec; 0.00 conn errors/sec; 0.00 parallel workers started/sec; CPU: user: 0.01 s, system: 0.97 s, elapsed: 1.00 s
8585+
</screen>
8586+
</para>
8587+
<para>
8588+
<literal>conn errors</literal> counts connections the postmaster
8589+
could not serve:
8590+
</para>
8591+
<itemizedlist>
8592+
<listitem>
8593+
<para>
8594+
a failed <function>accept()</function> system call, for example
8595+
because the postmaster ran out of file descriptors
8596+
</para>
8597+
</listitem>
8598+
<listitem>
8599+
<para>
8600+
a failure to start a backend process for the connection, for
8601+
example because <function>fork()</function> failed due to lack of
8602+
memory or process limits
8603+
</para>
8604+
</listitem>
8605+
<listitem>
8606+
<para>
8607+
a connection that was only given an error because the server was
8608+
starting up, shutting down, in recovery, or had no free child
8609+
process slot
8610+
</para>
8611+
</listitem>
8612+
</itemizedlist>
8613+
<para>
8614+
Ordinary <quote>too many clients</quote> rejections caused by
8615+
<xref linkend="guc-max-connections"/> are issued by a regular backend
8616+
process and are not counted. Note that a single parallel query may
8617+
register multiple workers. The final <literal>CPU</literal> portion
8618+
reports the postmaster's own resource usage over the same one second
8619+
window. See also
8620+
<xref linkend="guc-log-postmaster-excess-connections"/>, which emits
8621+
the same message based just on the incoming connection rate.
8622+
</para>
8623+
<para>
8624+
The default is <literal>off</literal>. This parameter can only be
8625+
set in the <filename>postgresql.conf</filename> file or on the
8626+
server command line.
8627+
</para>
8628+
<tip>
8629+
<para>
8630+
If you are receiving warnings about potentially overloaded postmaster,
8631+
be sure to investigate the application, their connection pools and usage
8632+
of independent connection poolers. Be also sure to double-check if the
8633+
huge pages are used and if the OS is not being overloaded.
8634+
</para>
8635+
</tip>
8636+
</listitem>
8637+
</varlistentry>
85398638

85408639
<varlistentry id="guc-log-recovery-conflict-waits" xreflabel="log_recovery_conflict_waits">
85418640
<term><varname>log_recovery_conflict_waits</varname> (<type>boolean</type>)

‎src/backend/postmaster/bgworker.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ BackgroundWorkerShmemInit(void *arg)
239239
}
240240
}
241241

242+
/*
243+
* Return the total count of parallel-worker registrations seen since
244+
* startup by doing unlocked read (for stats it should be good enough).
245+
*/
246+
uint32
247+
GetParallelWorkerRegisterCount(void)
248+
{
249+
return BackgroundWorkerData->parallel_register_count;
250+
}
251+
242252
/*
243253
* Search the postmaster's backend-private list of RegisteredBgWorker objects
244254
* for the one that maps to the given slot number.

0 commit comments

Comments
 (0)