From 77ddb7b2947697e586f8d228e32f683abc157d79 Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Fri, 7 Jan 2022 16:48:18 +0000 Subject: [PATCH v7] enhance pg_log_backend_memory_contexts to log memory contexts of aux procs Currently pg_log_backend_memory_contexts() doesn't log the memory contexts of auxiliary processes such as bgwriter, checkpointer, wal writer, archiver, startup process and wal receiver. It will be useful to look at the memory contexts of these processes too, for debugging purposes and better understanding of the memory usage pattern of these processes. Inside the code, we could use the AuxiliaryPidGetProc() to get the PGPROC of these processes. Note that, neither AuxiliaryPidGetProc() nor BackendPidGetProc() can return PGPROC(as they don't have PGPROC entries at all) entries for the syslogger, stats collector processes. --- doc/src/sgml/func.sgml | 12 ++++---- src/backend/postmaster/checkpointer.c | 4 +++ src/backend/postmaster/interrupt.c | 5 +++ src/backend/postmaster/pgarch.c | 5 +++ src/backend/postmaster/startup.c | 5 +++ src/backend/postmaster/walwriter.c | 4 +++ src/backend/utils/adt/mcxtfuncs.c | 32 +++++++++++++------- src/test/regress/expected/misc_functions.out | 6 ++++ src/test/regress/sql/misc_functions.sql | 2 ++ 9 files changed, 58 insertions(+), 17 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index e58efce586..2c17f1d54f 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -25420,13 +25420,13 @@ SELECT collation for ('foo' COLLATE "de_DE"); boolean - Requests to log the memory contexts of the backend with the - specified process ID. These memory contexts will be logged at - LOG message level. They will appear in - the server log based on the log configuration set + Requests to log the memory contexts of the backend with the specified + process ID. This function can send the request to all backends and + auxiliary processes except logger and stats collector. These memory + contexts will be logged at LOG message level. They + will appear in the server log based on the log configuration set (See for more information), - but will not be sent to the client regardless of - . + but will not be sent to the client regardless of . diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c index 25a18b7a14..648522bf83 100644 --- a/src/backend/postmaster/checkpointer.c +++ b/src/backend/postmaster/checkpointer.c @@ -577,6 +577,10 @@ HandleCheckpointerInterrupts(void) /* Normal exit from the checkpointer is here */ proc_exit(0); /* done */ } + + /* Perform logging of memory contexts of this process */ + if (LogMemoryContextPending) + ProcessLogMemoryContextInterrupt(); } /* diff --git a/src/backend/postmaster/interrupt.c b/src/backend/postmaster/interrupt.c index dd9136a942..7c1d35a715 100644 --- a/src/backend/postmaster/interrupt.c +++ b/src/backend/postmaster/interrupt.c @@ -22,6 +22,7 @@ #include "storage/latch.h" #include "storage/procsignal.h" #include "utils/guc.h" +#include "utils/memutils.h" volatile sig_atomic_t ConfigReloadPending = false; volatile sig_atomic_t ShutdownRequestPending = false; @@ -43,6 +44,10 @@ HandleMainLoopInterrupts(void) if (ShutdownRequestPending) proc_exit(0); + + /* Perform logging of memory contexts of this process */ + if (LogMemoryContextPending) + ProcessLogMemoryContextInterrupt(); } /* diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c index 5b6bf9f4e0..6610844998 100644 --- a/src/backend/postmaster/pgarch.c +++ b/src/backend/postmaster/pgarch.c @@ -50,6 +50,7 @@ #include "storage/shmem.h" #include "storage/spin.h" #include "utils/guc.h" +#include "utils/memutils.h" #include "utils/ps_status.h" @@ -875,4 +876,8 @@ HandlePgArchInterrupts(void) ConfigReloadPending = false; ProcessConfigFile(PGC_SIGHUP); } + + /* Perform logging of memory contexts of this process */ + if (LogMemoryContextPending) + ProcessLogMemoryContextInterrupt(); } diff --git a/src/backend/postmaster/startup.c b/src/backend/postmaster/startup.c index 47ec737888..2b35a82903 100644 --- a/src/backend/postmaster/startup.c +++ b/src/backend/postmaster/startup.c @@ -32,6 +32,7 @@ #include "storage/procsignal.h" #include "storage/standby.h" #include "utils/guc.h" +#include "utils/memutils.h" #include "utils/timeout.h" @@ -200,6 +201,10 @@ HandleStartupProcInterrupts(void) /* Process barrier events */ if (ProcSignalBarrierPending) ProcessProcSignalBarrier(); + + /* Perform logging of memory contexts of this process */ + if (LogMemoryContextPending) + ProcessLogMemoryContextInterrupt(); } diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c index 626fae8454..19c9118fd8 100644 --- a/src/backend/postmaster/walwriter.c +++ b/src/backend/postmaster/walwriter.c @@ -306,4 +306,8 @@ HandleWalWriterInterrupts(void) proc_exit(0); } + + /* Perform logging of memory contexts of this process */ + if (LogMemoryContextPending) + ProcessLogMemoryContextInterrupt(); } diff --git a/src/backend/utils/adt/mcxtfuncs.c b/src/backend/utils/adt/mcxtfuncs.c index 6ddbf70b30..8e6d21d87c 100644 --- a/src/backend/utils/adt/mcxtfuncs.c +++ b/src/backend/utils/adt/mcxtfuncs.c @@ -167,26 +167,36 @@ pg_get_backend_memory_contexts(PG_FUNCTION_ARGS) * rate would cause lots of log messages and which can lead to denial of * service. Additional roles can be permitted with GRANT. * - * On receipt of this signal, a backend sets the flag in the signal - * handler, which causes the next CHECK_FOR_INTERRUPTS() to log the - * memory contexts. + * On receipt of this signal, a backend sets the flag in the signal handler, + * which causes the next CHECK_FOR_INTERRUPTS() or process specific interrupt + * handlers to log the memory contexts. */ Datum pg_log_backend_memory_contexts(PG_FUNCTION_ARGS) { int pid = PG_GETARG_INT32(0); PGPROC *proc; + BackendId backendId = InvalidBackendId; proc = BackendPidGetProc(pid); /* - * BackendPidGetProc returns NULL if the pid isn't valid; but by the time - * we reach kill(), a process for which we get a valid proc here might - * have terminated on its own. There's no way to acquire a lock on an - * arbitrary process to prevent that. But since this mechanism is usually - * used to debug a backend running and consuming lots of memory, that it - * might end on its own first and its memory contexts are not logged is - * not a problem. + * See if the process with given pid is an auxiliary process. Only regular + * backends will have valid backend id, auxiliary processes don't. + */ + if (proc != NULL) + backendId = proc->backendId; + else + proc = AuxiliaryPidGetProc(pid); + + /* + * BackendPidGetProc or AuxiliaryPidGetProc returns NULL if the pid isn't + * valid; but by the time we reach kill(), a process for which we get a + * valid proc here might have terminated on its own. There's no way to + * acquire a lock on an arbitrary process to prevent that. But since this + * mechanism is usually used to debug a backend running and consuming lots + * of memory, that it might end on its own first and its memory contexts + * are not logged is not a problem. */ if (proc == NULL) { @@ -199,7 +209,7 @@ pg_log_backend_memory_contexts(PG_FUNCTION_ARGS) PG_RETURN_BOOL(false); } - if (SendProcSignal(pid, PROCSIG_LOG_MEMORY_CONTEXT, proc->backendId) < 0) + if (SendProcSignal(pid, PROCSIG_LOG_MEMORY_CONTEXT, backendId) < 0) { /* Again, just a warning to allow loops */ ereport(WARNING, diff --git a/src/test/regress/expected/misc_functions.out b/src/test/regress/expected/misc_functions.out index 1013d17f87..07c0ffbbe1 100644 --- a/src/test/regress/expected/misc_functions.out +++ b/src/test/regress/expected/misc_functions.out @@ -147,6 +147,12 @@ SELECT pg_log_backend_memory_contexts(pg_backend_pid()); t (1 row) +SELECT pg_log_backend_memory_contexts(pid) FROM pg_stat_activity WHERE backend_type = 'checkpointer'; + pg_log_backend_memory_contexts +-------------------------------- + t +(1 row) + CREATE ROLE regress_log_memory; SELECT has_function_privilege('regress_log_memory', 'pg_log_backend_memory_contexts(integer)', 'EXECUTE'); -- no diff --git a/src/test/regress/sql/misc_functions.sql b/src/test/regress/sql/misc_functions.sql index 7ab9b2a150..88449edab4 100644 --- a/src/test/regress/sql/misc_functions.sql +++ b/src/test/regress/sql/misc_functions.sql @@ -41,6 +41,8 @@ SELECT num_nulls(); SELECT pg_log_backend_memory_contexts(pg_backend_pid()); +SELECT pg_log_backend_memory_contexts(pid) FROM pg_stat_activity WHERE backend_type = 'checkpointer'; + CREATE ROLE regress_log_memory; SELECT has_function_privilege('regress_log_memory', -- 2.25.1