From bf28870a342871400b6fbf07bbb5eaa71e23d3bd Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 20 Feb 2020 18:07:37 +0100 Subject: [PATCH v3 1/4] Refactor ps_status.c API The init_ps_display() arguments were mostly lies by now, so to match typical usage, just use one argument and let the caller assemble it from multiple sources if necessary. The only user of the additional arguments is BackendInitialize(), which was already doing string assembly on the caller side anyway. Remove the second argument of set_ps_display() ("force") and just handle that in init_ps_display() internally. BackendInitialize() also used to set the initial status as "authentication", but that was very far from where authentication actually happened. So now it's set to "initializing" and then "authentication" just before the actual call to ClientAuthentication(). --- src/backend/access/transam/xlog.c | 4 ++-- src/backend/bootstrap/bootstrap.c | 2 +- src/backend/commands/async.c | 4 ++-- src/backend/postmaster/autovacuum.c | 6 ++--- src/backend/postmaster/bgworker.c | 2 +- src/backend/postmaster/pgarch.c | 8 +++---- src/backend/postmaster/pgstat.c | 2 +- src/backend/postmaster/postmaster.c | 32 ++++++++++++--------------- src/backend/postmaster/syslogger.c | 2 +- src/backend/replication/basebackup.c | 2 +- src/backend/replication/syncrep.c | 4 ++-- src/backend/replication/walreceiver.c | 7 +++--- src/backend/replication/walsender.c | 2 +- src/backend/storage/ipc/standby.c | 4 ++-- src/backend/storage/lmgr/lock.c | 6 ++--- src/backend/tcop/postgres.c | 16 +++++++------- src/backend/utils/init/postinit.c | 3 ++- src/backend/utils/misc/ps_status.c | 31 +++++++++++++++----------- src/include/utils/ps_status.h | 5 ++--- 19 files changed, 71 insertions(+), 71 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 4361568882..182e65b2df 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -3648,7 +3648,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli, /* Report recovery progress in PS display */ snprintf(activitymsg, sizeof(activitymsg), "waiting for %s", xlogfname); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); restoredFromArchive = RestoreArchivedFile(path, xlogfname, "RECOVERYXLOG", @@ -3691,7 +3691,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli, /* Report recovery progress in PS display */ snprintf(activitymsg, sizeof(activitymsg), "recovering %s", xlogfname); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); /* Track source of data in assorted state variables */ readSource = source; diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c index 657b18ecc8..7923d1ec9b 100644 --- a/src/backend/bootstrap/bootstrap.c +++ b/src/backend/bootstrap/bootstrap.c @@ -342,7 +342,7 @@ AuxiliaryProcessMain(int argc, char *argv[]) statmsg = "??? process"; break; } - init_ps_display(statmsg, "", "", ""); + init_ps_display(statmsg); } /* Acquire configuration parameters, unless inherited from postmaster */ diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c index dae939a4ab..0c9d20ebfc 100644 --- a/src/backend/commands/async.c +++ b/src/backend/commands/async.c @@ -2225,7 +2225,7 @@ ProcessIncomingNotify(void) if (Trace_notify) elog(DEBUG1, "ProcessIncomingNotify"); - set_ps_display("notify interrupt", false); + set_ps_display("notify interrupt"); /* * We must run asyncQueueReadAllNotifications inside a transaction, else @@ -2242,7 +2242,7 @@ ProcessIncomingNotify(void) */ pq_flush(); - set_ps_display("idle", false); + set_ps_display("idle"); if (Trace_notify) elog(DEBUG1, "ProcessIncomingNotify: done"); diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index e3a43d3296..a6499fc3da 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -434,7 +434,7 @@ AutoVacLauncherMain(int argc, char *argv[]) am_autovacuum_launcher = true; /* Identify myself via ps */ - init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER), "", "", ""); + init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER)); ereport(DEBUG1, (errmsg("autovacuum launcher started"))); @@ -1507,7 +1507,7 @@ AutoVacWorkerMain(int argc, char *argv[]) am_autovacuum_worker = true; /* Identify myself via ps */ - init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER), "", "", ""); + init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER)); SetProcessingMode(InitProcessing); @@ -1680,7 +1680,7 @@ AutoVacWorkerMain(int argc, char *argv[]) */ InitPostgres(NULL, dbid, NULL, InvalidOid, dbname, false); SetProcessingMode(NormalProcessing); - set_ps_display(dbname, false); + set_ps_display(dbname); ereport(DEBUG1, (errmsg("autovacuum: processing database \"%s\"", dbname))); diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c index 75fc0d5d33..684250984d 100644 --- a/src/backend/postmaster/bgworker.c +++ b/src/backend/postmaster/bgworker.c @@ -689,7 +689,7 @@ StartBackgroundWorker(void) IsBackgroundWorker = true; /* Identify myself via ps */ - init_ps_display(worker->bgw_name, "", "", ""); + init_ps_display(worker->bgw_name); /* * If we're not supposed to have shared memory access, then detach from diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c index 3ca30badb2..58f54544f6 100644 --- a/src/backend/postmaster/pgarch.c +++ b/src/backend/postmaster/pgarch.c @@ -241,7 +241,7 @@ PgArchiverMain(int argc, char *argv[]) /* * Identify myself via ps */ - init_ps_display("archiver", "", "", ""); + init_ps_display("archiver"); pgarch_MainLoop(); @@ -584,7 +584,7 @@ pgarch_archiveXlog(char *xlog) /* Report archive activity in PS display */ snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); rc = system(xlogarchcmd); if (rc != 0) @@ -634,14 +634,14 @@ pgarch_archiveXlog(char *xlog) } snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); return false; } elog(DEBUG1, "archived write-ahead log file \"%s\"", xlog); snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); return true; } diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 462b4d7e06..107c965336 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -4450,7 +4450,7 @@ PgstatCollectorMain(int argc, char *argv[]) /* * Identify myself via ps */ - init_ps_display("stats collector", "", "", ""); + init_ps_display("stats collector"); /* * Read in existing stats files or initialize the stats to zero. diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 55187eb910..46be78aadb 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -4282,7 +4282,7 @@ BackendInitialize(Port *port) int ret; char remote_host[NI_MAXHOST]; char remote_port[NI_MAXSERV]; - char remote_ps_data[NI_MAXHOST]; + StringInfoData ps_data; /* Save port etc. for ps status */ MyProcPort = port; @@ -4346,10 +4346,6 @@ BackendInitialize(Port *port) ereport(WARNING, (errmsg_internal("pg_getnameinfo_all() failed: %s", gai_strerror(ret)))); - if (remote_port[0] == '\0') - snprintf(remote_ps_data, sizeof(remote_ps_data), "%s", remote_host); - else - snprintf(remote_ps_data, sizeof(remote_ps_data), "%s(%s)", remote_host, remote_port); /* * Save remote_host and remote_port in port structure (after this, they @@ -4423,21 +4419,21 @@ BackendInitialize(Port *port) /* * Now that we have the user and database name, we can set the process * title for ps. It's good to do this as early as possible in startup. - * - * For a walsender, the ps display is set in the following form: - * - * postgres: walsender - * - * To achieve that, we pass "walsender" as username and username as dbname - * to init_ps_display(). XXX: should add a new variant of - * init_ps_display() to avoid abusing the parameters like this. */ + initStringInfo(&ps_data); if (am_walsender) - init_ps_display(pgstat_get_backend_desc(B_WAL_SENDER), port->user_name, remote_ps_data, - update_process_title ? "authentication" : ""); - else - init_ps_display(port->user_name, port->database_name, remote_ps_data, - update_process_title ? "authentication" : ""); + appendStringInfo(&ps_data, "%s ", pgstat_get_backend_desc(B_WAL_SENDER)); + appendStringInfo(&ps_data, "%s ", port->user_name); + if (!am_walsender) + appendStringInfo(&ps_data, "%s ", port->database_name); + appendStringInfo(&ps_data, "%s", port->remote_host); + if (port->remote_port[0] != '\0') + appendStringInfo(&ps_data, "(%s)", port->remote_port); + + init_ps_display(ps_data.data); + pfree(ps_data.data); + + set_ps_display("initializing"); /* * Disable the timeout, and prevent SIGTERM/SIGQUIT again. diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c index cf7b535e4e..b394599236 100644 --- a/src/backend/postmaster/syslogger.c +++ b/src/backend/postmaster/syslogger.c @@ -179,7 +179,7 @@ SysLoggerMain(int argc, char *argv[]) am_syslogger = true; - init_ps_display("logger", "", "", ""); + init_ps_display("logger"); /* * If we restarted, our stderr is already redirected into our own input diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c index f66cbc2428..806d013108 100644 --- a/src/backend/replication/basebackup.c +++ b/src/backend/replication/basebackup.c @@ -828,7 +828,7 @@ SendBaseBackup(BaseBackupCmd *cmd) snprintf(activitymsg, sizeof(activitymsg), "sending backup \"%s\"", opt.label); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); } perform_base_backup(&opt); diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c index c284103b54..ffd5b31eb2 100644 --- a/src/backend/replication/syncrep.c +++ b/src/backend/replication/syncrep.c @@ -209,7 +209,7 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit) memcpy(new_status, old_status, len); sprintf(new_status + len, " waiting for %X/%X", (uint32) (lsn >> 32), (uint32) lsn); - set_ps_display(new_status, false); + set_ps_display(new_status); new_status[len] = '\0'; /* truncate off " waiting ..." */ } @@ -311,7 +311,7 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit) if (new_status) { /* Reset ps display */ - set_ps_display(new_status, false); + set_ps_display(new_status); pfree(new_status); } } diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 2ab15c3cbb..930cc03e0f 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -665,8 +665,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI) walrcv->receiveStartTLI = 0; SpinLockRelease(&walrcv->mutex); - if (update_process_title) - set_ps_display("idle", false); + set_ps_display("idle"); /* * nudge startup process to notice that we've stopped streaming and are @@ -714,7 +713,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI) snprintf(activitymsg, sizeof(activitymsg), "restarting at %X/%X", (uint32) (*startpoint >> 32), (uint32) *startpoint); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); } } @@ -1027,7 +1026,7 @@ XLogWalRcvFlush(bool dying) snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X", (uint32) (LogstreamResult.Write >> 32), (uint32) LogstreamResult.Write); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); } /* Also let the master know that we made some progress */ diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index ae4a9cbe11..4a598b906b 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -2768,7 +2768,7 @@ XLogSendPhysical(void) snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X", (uint32) (sentPtr >> 32), (uint32) sentPtr); - set_ps_display(activitymsg, false); + set_ps_display(activitymsg); } } diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c index 3090e57fa4..0a53230714 100644 --- a/src/backend/storage/ipc/standby.c +++ b/src/backend/storage/ipc/standby.c @@ -254,7 +254,7 @@ ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist, new_status = (char *) palloc(len + 8 + 1); memcpy(new_status, old_status, len); strcpy(new_status + len, " waiting"); - set_ps_display(new_status, false); + set_ps_display(new_status); new_status[len] = '\0'; /* truncate off " waiting" */ } @@ -285,7 +285,7 @@ ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist, /* Reset ps display if we changed it */ if (new_status) { - set_ps_display(new_status, false); + set_ps_display(new_status); pfree(new_status); } } diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c index 56dba09299..1df7b8e2ab 100644 --- a/src/backend/storage/lmgr/lock.c +++ b/src/backend/storage/lmgr/lock.c @@ -1737,7 +1737,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner) new_status = (char *) palloc(len + 8 + 1); memcpy(new_status, old_status, len); strcpy(new_status + len, " waiting"); - set_ps_display(new_status, false); + set_ps_display(new_status); new_status[len] = '\0'; /* truncate off " waiting" */ } @@ -1789,7 +1789,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner) /* Report change to non-waiting status */ if (update_process_title) { - set_ps_display(new_status, false); + set_ps_display(new_status); pfree(new_status); } @@ -1803,7 +1803,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner) /* Report change to non-waiting status */ if (update_process_title) { - set_ps_display(new_status, false); + set_ps_display(new_status); pfree(new_status); } diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 9dba3b0566..00c77b66c7 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -1081,7 +1081,7 @@ exec_simple_query(const char *query_string) */ commandTag = CreateCommandTag(parsetree->stmt); - set_ps_display(GetCommandTagName(commandTag), false); + set_ps_display(GetCommandTagName(commandTag)); BeginCommand(commandTag, dest); @@ -1365,7 +1365,7 @@ exec_parse_message(const char *query_string, /* string to execute */ pgstat_report_activity(STATE_RUNNING, query_string); - set_ps_display("PARSE", false); + set_ps_display("PARSE"); if (save_log_statement_stats) ResetUsage(); @@ -1656,7 +1656,7 @@ exec_bind_message(StringInfo input_message) pgstat_report_activity(STATE_RUNNING, psrc->query_string); - set_ps_display("BIND", false); + set_ps_display("BIND"); if (save_log_statement_stats) ResetUsage(); @@ -2099,7 +2099,7 @@ exec_execute_message(const char *portal_name, long max_rows) pgstat_report_activity(STATE_RUNNING, sourceText); - set_ps_display(GetCommandTagName(portal->commandTag), false); + set_ps_display(GetCommandTagName(portal->commandTag)); if (save_log_statement_stats) ResetUsage(); @@ -4175,7 +4175,7 @@ PostgresMain(int argc, char *argv[], { if (IsAbortedTransactionBlockState()) { - set_ps_display("idle in transaction (aborted)", false); + set_ps_display("idle in transaction (aborted)"); pgstat_report_activity(STATE_IDLEINTRANSACTION_ABORTED, NULL); /* Start the idle-in-transaction timer */ @@ -4188,7 +4188,7 @@ PostgresMain(int argc, char *argv[], } else if (IsTransactionOrTransactionBlock()) { - set_ps_display("idle in transaction", false); + set_ps_display("idle in transaction"); pgstat_report_activity(STATE_IDLEINTRANSACTION, NULL); /* Start the idle-in-transaction timer */ @@ -4215,7 +4215,7 @@ PostgresMain(int argc, char *argv[], pgstat_report_stat(false); - set_ps_display("idle", false); + set_ps_display("idle"); pgstat_report_activity(STATE_IDLE, NULL); } @@ -4365,7 +4365,7 @@ PostgresMain(int argc, char *argv[], /* Report query to various monitoring facilities. */ pgstat_report_activity(STATE_FASTPATH, NULL); - set_ps_display("", false); + set_ps_display(""); /* start an xact for this function invocation */ start_xact_command(); diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 8a47dcdcb1..f4247ea70d 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -236,6 +236,7 @@ PerformAuthentication(Port *port) /* * Now perform authentication exchange. */ + set_ps_display("authentication"); ClientAuthentication(port); /* might not return, if failure */ /* @@ -303,7 +304,7 @@ PerformAuthentication(Port *port) } } - set_ps_display("startup", false); + set_ps_display("startup"); ClientAuthInProgress = false; /* client_min_messages is active now */ } diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c index ed23c840e9..8b160c0b40 100644 --- a/src/backend/utils/misc/ps_status.c +++ b/src/backend/utils/misc/ps_status.c @@ -250,12 +250,11 @@ save_ps_display_args(int argc, char **argv) * values. At this point, the original argv[] array may be overwritten. */ void -init_ps_display(const char *username, const char *dbname, - const char *host_info, const char *initial_str) +init_ps_display(const char *fixed_part) { - Assert(username); - Assert(dbname); - Assert(host_info); + bool save_update_process_title; + + Assert(fixed_part); #ifndef PS_USE_NONE /* no ps display for stand-alone backend */ @@ -309,19 +308,25 @@ init_ps_display(const char *username, const char *dbname, if (*cluster_name == '\0') { snprintf(ps_buffer, ps_buffer_size, - PROGRAM_NAME_PREFIX "%s %s %s ", - username, dbname, host_info); + PROGRAM_NAME_PREFIX "%s ", + fixed_part); } else { snprintf(ps_buffer, ps_buffer_size, - PROGRAM_NAME_PREFIX "%s: %s %s %s ", - cluster_name, username, dbname, host_info); + PROGRAM_NAME_PREFIX "%s: %s ", + cluster_name, fixed_part); } ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer); - set_ps_display(initial_str, true); + /* + * On the first run, force the update. + */ + save_update_process_title = update_process_title; + update_process_title = true; + set_ps_display(""); + update_process_title = save_update_process_title; #endif /* not PS_USE_NONE */ } @@ -332,11 +337,11 @@ init_ps_display(const char *username, const char *dbname, * indication of what you're currently doing passed in the argument. */ void -set_ps_display(const char *activity, bool force) +set_ps_display(const char *activity) { #ifndef PS_USE_NONE - /* update_process_title=off disables updates, unless force = true */ - if (!force && !update_process_title) + /* update_process_title=off disables updates */ + if (!update_process_title) return; /* no ps display for stand-alone backend */ diff --git a/src/include/utils/ps_status.h b/src/include/utils/ps_status.h index 23f1e59ffc..9f43e1fdf0 100644 --- a/src/include/utils/ps_status.h +++ b/src/include/utils/ps_status.h @@ -16,10 +16,9 @@ extern bool update_process_title; extern char **save_ps_display_args(int argc, char **argv); -extern void init_ps_display(const char *username, const char *dbname, - const char *host_info, const char *initial_str); +extern void init_ps_display(const char *fixed_part); -extern void set_ps_display(const char *activity, bool force); +extern void set_ps_display(const char *activity); extern const char *get_ps_display(int *displen); base-commit: 7e39b968f118c6444bd3a3bd59c3e9d73e652e0c -- 2.25.0