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

Commit 790fbda

Browse files
committed
Enhance pg_log_backend_memory_contexts() for auxiliary processes.
Previously pg_log_backend_memory_contexts() could request to log the memory contexts of backends, but not of auxiliary processes such as checkpointer. This commit enhances the function so that it can also send the request to auxiliary processes. It's useful to look at the memory contexts of those processes for debugging purpose and better understanding of the memory usage pattern of them. Note that pg_log_backend_memory_contexts() cannot send the request to logger or statistics collector. Because this logging request mechanism is based on shared memory but those processes aren't connected to that. Author: Bharath Rupireddy Reviewed-by: Vignesh C, Kyotaro Horiguchi, Fujii Masao Discussion: https://postgr.es/m/CALj2ACU1nBzpacOK2q=a65S_4+Oaz_rLTsU1Ri0gf7YUmnmhfQ@mail.gmail.com
1 parent 85c61ba commit 790fbda

File tree

9 files changed

+65
-15
lines changed

9 files changed

+65
-15
lines changed

doc/src/sgml/func.sgml

+3-1
Original file line numberDiff line numberDiff line change
@@ -25421,7 +25421,9 @@ SELECT collation for ('foo' COLLATE "de_DE");
2542125421
</para>
2542225422
<para>
2542325423
Requests to log the memory contexts of the backend with the
25424-
specified process ID. These memory contexts will be logged at
25424+
specified process ID. This function can send the request to
25425+
backends and auxiliary processes except logger and statistics
25426+
collector. These memory contexts will be logged at
2542525427
<literal>LOG</literal> message level. They will appear in
2542625428
the server log based on the log configuration set
2542725429
(See <xref linkend="runtime-config-logging"/> for more information),

src/backend/postmaster/checkpointer.c

+4
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,10 @@ HandleCheckpointerInterrupts(void)
577577
/* Normal exit from the checkpointer is here */
578578
proc_exit(0); /* done */
579579
}
580+
581+
/* Perform logging of memory contexts of this process */
582+
if (LogMemoryContextPending)
583+
ProcessLogMemoryContextInterrupt();
580584
}
581585

582586
/*

src/backend/postmaster/interrupt.c

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "storage/latch.h"
2323
#include "storage/procsignal.h"
2424
#include "utils/guc.h"
25+
#include "utils/memutils.h"
2526

2627
volatile sig_atomic_t ConfigReloadPending = false;
2728
volatile sig_atomic_t ShutdownRequestPending = false;
@@ -43,6 +44,10 @@ HandleMainLoopInterrupts(void)
4344

4445
if (ShutdownRequestPending)
4546
proc_exit(0);
47+
48+
/* Perform logging of memory contexts of this process */
49+
if (LogMemoryContextPending)
50+
ProcessLogMemoryContextInterrupt();
4651
}
4752

4853
/*

src/backend/postmaster/pgarch.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "storage/shmem.h"
5151
#include "storage/spin.h"
5252
#include "utils/guc.h"
53+
#include "utils/memutils.h"
5354
#include "utils/ps_status.h"
5455

5556

@@ -861,8 +862,9 @@ pgarch_die(int code, Datum arg)
861862
* Interrupt handler for WAL archiver process.
862863
*
863864
* This is called in the loops pgarch_MainLoop and pgarch_ArchiverCopyLoop.
864-
* It checks for barrier events and config update, but not shutdown request
865-
* because how to handle shutdown request is different between those loops.
865+
* It checks for barrier events, config update and request for logging of
866+
* memory contexts, but not shutdown request because how to handle
867+
* shutdown request is different between those loops.
866868
*/
867869
static void
868870
HandlePgArchInterrupts(void)
@@ -875,4 +877,8 @@ HandlePgArchInterrupts(void)
875877
ConfigReloadPending = false;
876878
ProcessConfigFile(PGC_SIGHUP);
877879
}
880+
881+
/* Perform logging of memory contexts of this process */
882+
if (LogMemoryContextPending)
883+
ProcessLogMemoryContextInterrupt();
878884
}

src/backend/postmaster/startup.c

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "storage/procsignal.h"
3333
#include "storage/standby.h"
3434
#include "utils/guc.h"
35+
#include "utils/memutils.h"
3536
#include "utils/timeout.h"
3637

3738

@@ -200,6 +201,10 @@ HandleStartupProcInterrupts(void)
200201
/* Process barrier events */
201202
if (ProcSignalBarrierPending)
202203
ProcessProcSignalBarrier();
204+
205+
/* Perform logging of memory contexts of this process */
206+
if (LogMemoryContextPending)
207+
ProcessLogMemoryContextInterrupt();
203208
}
204209

205210

src/backend/postmaster/walwriter.c

+4
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,8 @@ HandleWalWriterInterrupts(void)
306306

307307
proc_exit(0);
308308
}
309+
310+
/* Perform logging of memory contexts of this process */
311+
if (LogMemoryContextPending)
312+
ProcessLogMemoryContextInterrupt();
309313
}

src/backend/utils/adt/mcxtfuncs.c

+26-12
Original file line numberDiff line numberDiff line change
@@ -160,33 +160,47 @@ pg_get_backend_memory_contexts(PG_FUNCTION_ARGS)
160160

161161
/*
162162
* pg_log_backend_memory_contexts
163-
* Signal a backend process to log its memory contexts.
163+
* Signal a backend or an auxiliary process to log its memory contexts.
164164
*
165165
* By default, only superusers are allowed to signal to log the memory
166166
* contexts because allowing any users to issue this request at an unbounded
167167
* rate would cause lots of log messages and which can lead to denial of
168168
* service. Additional roles can be permitted with GRANT.
169169
*
170-
* On receipt of this signal, a backend sets the flag in the signal
171-
* handler, which causes the next CHECK_FOR_INTERRUPTS() to log the
172-
* memory contexts.
170+
* On receipt of this signal, a backend or an auxiliary process sets the flag
171+
* in the signal handler, which causes the next CHECK_FOR_INTERRUPTS()
172+
* or process-specific interrupt handler to log the memory contexts.
173173
*/
174174
Datum
175175
pg_log_backend_memory_contexts(PG_FUNCTION_ARGS)
176176
{
177177
int pid = PG_GETARG_INT32(0);
178178
PGPROC *proc;
179+
BackendId backendId = InvalidBackendId;
179180

180181
proc = BackendPidGetProc(pid);
181182

182183
/*
183-
* BackendPidGetProc returns NULL if the pid isn't valid; but by the time
184-
* we reach kill(), a process for which we get a valid proc here might
185-
* have terminated on its own. There's no way to acquire a lock on an
186-
* arbitrary process to prevent that. But since this mechanism is usually
187-
* used to debug a backend running and consuming lots of memory, that it
188-
* might end on its own first and its memory contexts are not logged is
189-
* not a problem.
184+
* See if the process with given pid is a backend or an auxiliary process.
185+
*
186+
* If the given process is a backend, use its backend id in
187+
* SendProcSignal() later to speed up the operation. Otherwise, don't do
188+
* that because auxiliary processes (except the startup process) don't
189+
* have a valid backend id.
190+
*/
191+
if (proc != NULL)
192+
backendId = proc->backendId;
193+
else
194+
proc = AuxiliaryPidGetProc(pid);
195+
196+
/*
197+
* BackendPidGetProc() and AuxiliaryPidGetProc() return NULL if the pid
198+
* isn't valid; but by the time we reach kill(), a process for which we
199+
* get a valid proc here might have terminated on its own. There's no way
200+
* to acquire a lock on an arbitrary process to prevent that. But since
201+
* this mechanism is usually used to debug a backend or an auxiliary
202+
* process running and consuming lots of memory, that it might end on its
203+
* own first and its memory contexts are not logged is not a problem.
190204
*/
191205
if (proc == NULL)
192206
{
@@ -199,7 +213,7 @@ pg_log_backend_memory_contexts(PG_FUNCTION_ARGS)
199213
PG_RETURN_BOOL(false);
200214
}
201215

202-
if (SendProcSignal(pid, PROCSIG_LOG_MEMORY_CONTEXT, proc->backendId) < 0)
216+
if (SendProcSignal(pid, PROCSIG_LOG_MEMORY_CONTEXT, backendId) < 0)
203217
{
204218
/* Again, just a warning to allow loops */
205219
ereport(WARNING,

src/test/regress/expected/misc_functions.out

+7
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ SELECT pg_log_backend_memory_contexts(pg_backend_pid());
147147
t
148148
(1 row)
149149

150+
SELECT pg_log_backend_memory_contexts(pid) FROM pg_stat_activity
151+
WHERE backend_type = 'checkpointer';
152+
pg_log_backend_memory_contexts
153+
--------------------------------
154+
t
155+
(1 row)
156+
150157
CREATE ROLE regress_log_memory;
151158
SELECT has_function_privilege('regress_log_memory',
152159
'pg_log_backend_memory_contexts(integer)', 'EXECUTE'); -- no

src/test/regress/sql/misc_functions.sql

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ SELECT num_nulls();
4141

4242
SELECT pg_log_backend_memory_contexts(pg_backend_pid());
4343

44+
SELECT pg_log_backend_memory_contexts(pid) FROM pg_stat_activity
45+
WHERE backend_type = 'checkpointer';
46+
4447
CREATE ROLE regress_log_memory;
4548

4649
SELECT has_function_privilege('regress_log_memory',

0 commit comments

Comments
 (0)