Attached patch introduces a new function pg_shmem_init_time(),
which returns the time shared memory was last (re)initialized.
It is created for use by monitoring tools to track backend crashes.
Currently, if the 'restart_after_crash' option is on, postgres will just
restart.
And the only way to know that it happened is to regularly parse logfile
or monitor it, catching restart messages. This approach is really
inconvenient for
users, who have gigabytes of logs.
This new function can be periodiacally called by a monitoring agent, and,
if /shmem_init_time/ doesn't match /pg_postmaster_start_time,/
we know that server crashed-restarted, and also know the exact time, when.
Also, working on this patch, I noticed a bit of dead code
and some discordant comments in postmaster.c.
I see no reason to leave it as is.
So there is a small remove_dead_shmem_reinit_code_v0.patch.
--
Anastasia Lubennikova
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
commit d396ef1ccf12be031c1cf90cc5a357da4933a3b8
Author: Anastasia <a.lubennik...@postgrespro.ru>
Date: Wed Feb 28 13:50:18 2018 +0300
Remove some dead code related to shmem reinit. Fix outdated comments.
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index f3ddf82..a397260 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -223,11 +223,10 @@ static char ExtraOptions[MAXPGPATH];
/*
* These globals control the behavior of the postmaster in case some
* backend dumps core. Normally, it kills all peers of the dead backend
- * and reinitializes shared memory. By specifying -s or -n, we can have
+ * and reinitializes shared memory. By specifying -T, we can have
* the postmaster stop (rather than kill) peers and not reinitialize
- * shared data structures. (Reinit is currently dead code, though.)
+ * shared data structures. It lets us to collect core dumps of all processes.
*/
-static bool Reinit = true;
static int SendStop = false;
/* still more option variables */
@@ -738,11 +737,6 @@ PostmasterMain(int argc, char *argv[])
SetConfigOption("max_connections", optarg, PGC_POSTMASTER, PGC_S_ARGV);
break;
- case 'n':
- /* Don't reinit shared mem after abnormal exit */
- Reinit = false;
- break;
-
case 'O':
SetConfigOption("allow_system_table_mods", "true", PGC_POSTMASTER, PGC_S_ARGV);
break;
@@ -3427,7 +3421,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
*
* SIGQUIT is the special signal that says exit without proc_exit
* and let the user know what's going on. But if SendStop is set
- * (-s on command line), then we send SIGSTOP instead, so that we
+ * (-T on command line), then we send SIGSTOP instead, so that we
* can get core dumps from all backends by hand.
*
* We could exclude dead_end children here, but at least in the
commit 13817de092ee53f2af2d73270ddc3e02556d7c0c
Author: Anastasia <a.lubennik...@postgrespro.ru>
Date: Wed Feb 28 14:04:00 2018 +0300
Add function pg_shmem_init_time() which returns the time shared memory was last initialized
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 2f59af2..bebdddf 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -15924,6 +15924,12 @@ SELECT * FROM pg_ls_dir('.') WITH ORDINALITY AS t(ls,n);
</row>
<row>
+ <entry><literal><function>pg_shmem_init_time()</function></literal></entry>
+ <entry><type>timestamp with time zone</type></entry>
+ <entry>shared memory initialization time</entry>
+ </row>
+
+ <row>
<entry><literal><function>pg_current_logfile(<optional><type>text</type></optional>)</function></literal></entry>
<entry><type>text</type></entry>
<entry>Primary log file name, or log in the requested format,
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index a397260..7cc9780 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -512,6 +512,7 @@ typedef struct
pid_t PostmasterPid;
TimestampTz PgStartTime;
TimestampTz PgReloadTime;
+ TimestampTz PgShmemInitTime;
pg_time_t first_syslogger_file_time;
bool redirection_done;
bool IsBinaryUpgrade;
@@ -2555,6 +2556,8 @@ reset_shared(int port)
* objects if the postmaster crashes and is restarted.
*/
CreateSharedMemoryAndSemaphores(false, port);
+
+ PgShmemInitTime = GetCurrentTimestamp();
}
@@ -6058,6 +6061,7 @@ save_backend_variables(BackendParameters *param, Port *port,
param->PostmasterPid = PostmasterPid;
param->PgStartTime = PgStartTime;
param->PgReloadTime = PgReloadTime;
+ param->PgShmemInitTime = PgShmemInitTime;
param->first_syslogger_file_time = first_syslogger_file_time;
param->redirection_done = redirection_done;
@@ -6290,6 +6294,7 @@ restore_backend_variables(BackendParameters *param, Port *port)
PostmasterPid = param->PostmasterPid;
PgStartTime = param->PgStartTime;
PgReloadTime = param->PgReloadTime;
+ PgShmemInitTime = param->PgShmemInitTime;
first_syslogger_file_time = param->first_syslogger_file_time;
redirection_done = param->redirection_done;
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 103f91a..e3132eb 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -51,6 +51,9 @@ TimestampTz PgStartTime;
/* Set at configuration reload */
TimestampTz PgReloadTime;
+/* Set at shared memory (re)initialization */
+TimestampTz PgShmemInitTime;
+
typedef struct
{
Timestamp current;
@@ -1560,6 +1563,12 @@ pg_conf_load_time(PG_FUNCTION_ARGS)
PG_RETURN_TIMESTAMPTZ(PgReloadTime);
}
+Datum
+pg_shmem_init_time(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_TIMESTAMPTZ(PgShmemInitTime);
+}
+
/*
* GetCurrentTimestamp -- get the current operating system time
*
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index c00d055..3f50b50 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -4334,6 +4334,9 @@ DESCR("postmaster start time");
/* config reload time function */
DATA(insert OID = 2034 ( pg_conf_load_time PGNSP PGUID 12 1 0 0 0 f f f f t f s r 0 0 1184 "" _null_ _null_ _null_ _null_ _null_ pg_conf_load_time _null_ _null_ _null_ ));
DESCR("configuration load time");
+/* shared memory (re)init time function */
+DATA(insert OID = 2579 ( pg_shmem_init_time PGNSP PGUID 12 1 0 0 0 f f f f t f s r 0 0 1184 "" _null_ _null_ _null_ _null_ _null_ pg_shmem_init_time _null_ _null_ _null_ ));
+DESCR("shared memory (re)init time");
/* new functions for Y-direction rtree opclasses */
DATA(insert OID = 2562 ( box_below PGNSP PGUID 12 1 0 0 0 f f f f t f i s 2 0 16 "603 603" _null_ _null_ _null_ _null_ _null_ box_below _null_ _null_ _null_ ));
diff --git a/src/include/utils/timestamp.h b/src/include/utils/timestamp.h
index 2b3b357..3c34b48 100644
--- a/src/include/utils/timestamp.h
+++ b/src/include/utils/timestamp.h
@@ -62,6 +62,8 @@ extern TimestampTz PgStartTime;
/* Set at configuration reload */
extern TimestampTz PgReloadTime;
+/* Set at shared memory (re)initialization */
+extern TimestampTz PgShmemInitTime;
/* Internal routines (not fmgr-callable) */