Skip to content

Commit bf68b79

Browse files
committed
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(). Reviewed-by: Julien Rouhaud <[email protected]> Reviewed-by: Kuntal Ghosh <[email protected]> Reviewed-by: Alvaro Herrera <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent 899a04f commit bf68b79

File tree

19 files changed

+71
-71
lines changed

19 files changed

+71
-71
lines changed

src/backend/access/transam/xlog.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3648,7 +3648,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
36483648
/* Report recovery progress in PS display */
36493649
snprintf(activitymsg, sizeof(activitymsg), "waiting for %s",
36503650
xlogfname);
3651-
set_ps_display(activitymsg, false);
3651+
set_ps_display(activitymsg);
36523652

36533653
restoredFromArchive = RestoreArchivedFile(path, xlogfname,
36543654
"RECOVERYXLOG",
@@ -3691,7 +3691,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
36913691
/* Report recovery progress in PS display */
36923692
snprintf(activitymsg, sizeof(activitymsg), "recovering %s",
36933693
xlogfname);
3694-
set_ps_display(activitymsg, false);
3694+
set_ps_display(activitymsg);
36953695

36963696
/* Track source of data in assorted state variables */
36973697
readSource = source;

src/backend/bootstrap/bootstrap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ AuxiliaryProcessMain(int argc, char *argv[])
342342
statmsg = "??? process";
343343
break;
344344
}
345-
init_ps_display(statmsg, "", "", "");
345+
init_ps_display(statmsg);
346346
}
347347

348348
/* Acquire configuration parameters, unless inherited from postmaster */

src/backend/commands/async.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2225,7 +2225,7 @@ ProcessIncomingNotify(void)
22252225
if (Trace_notify)
22262226
elog(DEBUG1, "ProcessIncomingNotify");
22272227

2228-
set_ps_display("notify interrupt", false);
2228+
set_ps_display("notify interrupt");
22292229

22302230
/*
22312231
* We must run asyncQueueReadAllNotifications inside a transaction, else
@@ -2242,7 +2242,7 @@ ProcessIncomingNotify(void)
22422242
*/
22432243
pq_flush();
22442244

2245-
set_ps_display("idle", false);
2245+
set_ps_display("idle");
22462246

22472247
if (Trace_notify)
22482248
elog(DEBUG1, "ProcessIncomingNotify: done");

src/backend/postmaster/autovacuum.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ AutoVacLauncherMain(int argc, char *argv[])
434434
am_autovacuum_launcher = true;
435435

436436
/* Identify myself via ps */
437-
init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER), "", "", "");
437+
init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER));
438438

439439
ereport(DEBUG1,
440440
(errmsg("autovacuum launcher started")));
@@ -1507,7 +1507,7 @@ AutoVacWorkerMain(int argc, char *argv[])
15071507
am_autovacuum_worker = true;
15081508

15091509
/* Identify myself via ps */
1510-
init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER), "", "", "");
1510+
init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER));
15111511

15121512
SetProcessingMode(InitProcessing);
15131513

@@ -1680,7 +1680,7 @@ AutoVacWorkerMain(int argc, char *argv[])
16801680
*/
16811681
InitPostgres(NULL, dbid, NULL, InvalidOid, dbname, false);
16821682
SetProcessingMode(NormalProcessing);
1683-
set_ps_display(dbname, false);
1683+
set_ps_display(dbname);
16841684
ereport(DEBUG1,
16851685
(errmsg("autovacuum: processing database \"%s\"", dbname)));
16861686

src/backend/postmaster/bgworker.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ StartBackgroundWorker(void)
689689
IsBackgroundWorker = true;
690690

691691
/* Identify myself via ps */
692-
init_ps_display(worker->bgw_name, "", "", "");
692+
init_ps_display(worker->bgw_name);
693693

694694
/*
695695
* If we're not supposed to have shared memory access, then detach from

src/backend/postmaster/pgarch.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ PgArchiverMain(int argc, char *argv[])
241241
/*
242242
* Identify myself via ps
243243
*/
244-
init_ps_display("archiver", "", "", "");
244+
init_ps_display("archiver");
245245

246246
pgarch_MainLoop();
247247

@@ -584,7 +584,7 @@ pgarch_archiveXlog(char *xlog)
584584

585585
/* Report archive activity in PS display */
586586
snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
587-
set_ps_display(activitymsg, false);
587+
set_ps_display(activitymsg);
588588

589589
rc = system(xlogarchcmd);
590590
if (rc != 0)
@@ -634,14 +634,14 @@ pgarch_archiveXlog(char *xlog)
634634
}
635635

636636
snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
637-
set_ps_display(activitymsg, false);
637+
set_ps_display(activitymsg);
638638

639639
return false;
640640
}
641641
elog(DEBUG1, "archived write-ahead log file \"%s\"", xlog);
642642

643643
snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
644-
set_ps_display(activitymsg, false);
644+
set_ps_display(activitymsg);
645645

646646
return true;
647647
}

src/backend/postmaster/pgstat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4450,7 +4450,7 @@ PgstatCollectorMain(int argc, char *argv[])
44504450
/*
44514451
* Identify myself via ps
44524452
*/
4453-
init_ps_display("stats collector", "", "", "");
4453+
init_ps_display("stats collector");
44544454

44554455
/*
44564456
* Read in existing stats files or initialize the stats to zero.

src/backend/postmaster/postmaster.c

+14-18
Original file line numberDiff line numberDiff line change
@@ -4282,7 +4282,7 @@ BackendInitialize(Port *port)
42824282
int ret;
42834283
char remote_host[NI_MAXHOST];
42844284
char remote_port[NI_MAXSERV];
4285-
char remote_ps_data[NI_MAXHOST];
4285+
StringInfoData ps_data;
42864286

42874287
/* Save port etc. for ps status */
42884288
MyProcPort = port;
@@ -4346,10 +4346,6 @@ BackendInitialize(Port *port)
43464346
ereport(WARNING,
43474347
(errmsg_internal("pg_getnameinfo_all() failed: %s",
43484348
gai_strerror(ret))));
4349-
if (remote_port[0] == '\0')
4350-
snprintf(remote_ps_data, sizeof(remote_ps_data), "%s", remote_host);
4351-
else
4352-
snprintf(remote_ps_data, sizeof(remote_ps_data), "%s(%s)", remote_host, remote_port);
43534349

43544350
/*
43554351
* Save remote_host and remote_port in port structure (after this, they
@@ -4423,21 +4419,21 @@ BackendInitialize(Port *port)
44234419
/*
44244420
* Now that we have the user and database name, we can set the process
44254421
* title for ps. It's good to do this as early as possible in startup.
4426-
*
4427-
* For a walsender, the ps display is set in the following form:
4428-
*
4429-
* postgres: walsender <user> <host> <activity>
4430-
*
4431-
* To achieve that, we pass "walsender" as username and username as dbname
4432-
* to init_ps_display(). XXX: should add a new variant of
4433-
* init_ps_display() to avoid abusing the parameters like this.
44344422
*/
4423+
initStringInfo(&ps_data);
44354424
if (am_walsender)
4436-
init_ps_display(pgstat_get_backend_desc(B_WAL_SENDER), port->user_name, remote_ps_data,
4437-
update_process_title ? "authentication" : "");
4438-
else
4439-
init_ps_display(port->user_name, port->database_name, remote_ps_data,
4440-
update_process_title ? "authentication" : "");
4425+
appendStringInfo(&ps_data, "%s ", pgstat_get_backend_desc(B_WAL_SENDER));
4426+
appendStringInfo(&ps_data, "%s ", port->user_name);
4427+
if (!am_walsender)
4428+
appendStringInfo(&ps_data, "%s ", port->database_name);
4429+
appendStringInfo(&ps_data, "%s", port->remote_host);
4430+
if (port->remote_port[0] != '\0')
4431+
appendStringInfo(&ps_data, "(%s)", port->remote_port);
4432+
4433+
init_ps_display(ps_data.data);
4434+
pfree(ps_data.data);
4435+
4436+
set_ps_display("initializing");
44414437

44424438
/*
44434439
* Disable the timeout, and prevent SIGTERM/SIGQUIT again.

src/backend/postmaster/syslogger.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ SysLoggerMain(int argc, char *argv[])
179179

180180
am_syslogger = true;
181181

182-
init_ps_display("logger", "", "", "");
182+
init_ps_display("logger");
183183

184184
/*
185185
* If we restarted, our stderr is already redirected into our own input

src/backend/replication/basebackup.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ SendBaseBackup(BaseBackupCmd *cmd)
828828

829829
snprintf(activitymsg, sizeof(activitymsg), "sending backup \"%s\"",
830830
opt.label);
831-
set_ps_display(activitymsg, false);
831+
set_ps_display(activitymsg);
832832
}
833833

834834
perform_base_backup(&opt);

src/backend/replication/syncrep.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
209209
memcpy(new_status, old_status, len);
210210
sprintf(new_status + len, " waiting for %X/%X",
211211
(uint32) (lsn >> 32), (uint32) lsn);
212-
set_ps_display(new_status, false);
212+
set_ps_display(new_status);
213213
new_status[len] = '\0'; /* truncate off " waiting ..." */
214214
}
215215

@@ -311,7 +311,7 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
311311
if (new_status)
312312
{
313313
/* Reset ps display */
314-
set_ps_display(new_status, false);
314+
set_ps_display(new_status);
315315
pfree(new_status);
316316
}
317317
}

src/backend/replication/walreceiver.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
666666
walrcv->receiveStartTLI = 0;
667667
SpinLockRelease(&walrcv->mutex);
668668

669-
if (update_process_title)
670-
set_ps_display("idle", false);
669+
set_ps_display("idle");
671670

672671
/*
673672
* nudge startup process to notice that we've stopped streaming and are
@@ -715,7 +714,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
715714
snprintf(activitymsg, sizeof(activitymsg), "restarting at %X/%X",
716715
(uint32) (*startpoint >> 32),
717716
(uint32) *startpoint);
718-
set_ps_display(activitymsg, false);
717+
set_ps_display(activitymsg);
719718
}
720719
}
721720

@@ -1028,7 +1027,7 @@ XLogWalRcvFlush(bool dying)
10281027
snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
10291028
(uint32) (LogstreamResult.Write >> 32),
10301029
(uint32) LogstreamResult.Write);
1031-
set_ps_display(activitymsg, false);
1030+
set_ps_display(activitymsg);
10321031
}
10331032

10341033
/* Also let the master know that we made some progress */

src/backend/replication/walsender.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2769,7 +2769,7 @@ XLogSendPhysical(void)
27692769

27702770
snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
27712771
(uint32) (sentPtr >> 32), (uint32) sentPtr);
2772-
set_ps_display(activitymsg, false);
2772+
set_ps_display(activitymsg);
27732773
}
27742774
}
27752775

src/backend/storage/ipc/standby.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
259259
new_status = (char *) palloc(len + 8 + 1);
260260
memcpy(new_status, old_status, len);
261261
strcpy(new_status + len, " waiting");
262-
set_ps_display(new_status, false);
262+
set_ps_display(new_status);
263263
new_status[len] = '\0'; /* truncate off " waiting" */
264264
}
265265

@@ -290,7 +290,7 @@ ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
290290
/* Reset ps display if we changed it */
291291
if (new_status)
292292
{
293-
set_ps_display(new_status, false);
293+
set_ps_display(new_status);
294294
pfree(new_status);
295295
}
296296
}

src/backend/storage/lmgr/lock.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
17371737
new_status = (char *) palloc(len + 8 + 1);
17381738
memcpy(new_status, old_status, len);
17391739
strcpy(new_status + len, " waiting");
1740-
set_ps_display(new_status, false);
1740+
set_ps_display(new_status);
17411741
new_status[len] = '\0'; /* truncate off " waiting" */
17421742
}
17431743

@@ -1789,7 +1789,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
17891789
/* Report change to non-waiting status */
17901790
if (update_process_title)
17911791
{
1792-
set_ps_display(new_status, false);
1792+
set_ps_display(new_status);
17931793
pfree(new_status);
17941794
}
17951795

@@ -1803,7 +1803,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
18031803
/* Report change to non-waiting status */
18041804
if (update_process_title)
18051805
{
1806-
set_ps_display(new_status, false);
1806+
set_ps_display(new_status);
18071807
pfree(new_status);
18081808
}
18091809

src/backend/tcop/postgres.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ exec_simple_query(const char *query_string)
10811081
*/
10821082
commandTag = CreateCommandTag(parsetree->stmt);
10831083

1084-
set_ps_display(GetCommandTagName(commandTag), false);
1084+
set_ps_display(GetCommandTagName(commandTag));
10851085

10861086
BeginCommand(commandTag, dest);
10871087

@@ -1365,7 +1365,7 @@ exec_parse_message(const char *query_string, /* string to execute */
13651365

13661366
pgstat_report_activity(STATE_RUNNING, query_string);
13671367

1368-
set_ps_display("PARSE", false);
1368+
set_ps_display("PARSE");
13691369

13701370
if (save_log_statement_stats)
13711371
ResetUsage();
@@ -1656,7 +1656,7 @@ exec_bind_message(StringInfo input_message)
16561656

16571657
pgstat_report_activity(STATE_RUNNING, psrc->query_string);
16581658

1659-
set_ps_display("BIND", false);
1659+
set_ps_display("BIND");
16601660

16611661
if (save_log_statement_stats)
16621662
ResetUsage();
@@ -2099,7 +2099,7 @@ exec_execute_message(const char *portal_name, long max_rows)
20992099

21002100
pgstat_report_activity(STATE_RUNNING, sourceText);
21012101

2102-
set_ps_display(GetCommandTagName(portal->commandTag), false);
2102+
set_ps_display(GetCommandTagName(portal->commandTag));
21032103

21042104
if (save_log_statement_stats)
21052105
ResetUsage();
@@ -4175,7 +4175,7 @@ PostgresMain(int argc, char *argv[],
41754175
{
41764176
if (IsAbortedTransactionBlockState())
41774177
{
4178-
set_ps_display("idle in transaction (aborted)", false);
4178+
set_ps_display("idle in transaction (aborted)");
41794179
pgstat_report_activity(STATE_IDLEINTRANSACTION_ABORTED, NULL);
41804180

41814181
/* Start the idle-in-transaction timer */
@@ -4188,7 +4188,7 @@ PostgresMain(int argc, char *argv[],
41884188
}
41894189
else if (IsTransactionOrTransactionBlock())
41904190
{
4191-
set_ps_display("idle in transaction", false);
4191+
set_ps_display("idle in transaction");
41924192
pgstat_report_activity(STATE_IDLEINTRANSACTION, NULL);
41934193

41944194
/* Start the idle-in-transaction timer */
@@ -4215,7 +4215,7 @@ PostgresMain(int argc, char *argv[],
42154215

42164216
pgstat_report_stat(false);
42174217

4218-
set_ps_display("idle", false);
4218+
set_ps_display("idle");
42194219
pgstat_report_activity(STATE_IDLE, NULL);
42204220
}
42214221

@@ -4365,7 +4365,7 @@ PostgresMain(int argc, char *argv[],
43654365

43664366
/* Report query to various monitoring facilities. */
43674367
pgstat_report_activity(STATE_FASTPATH, NULL);
4368-
set_ps_display("<FASTPATH>", false);
4368+
set_ps_display("<FASTPATH>");
43694369

43704370
/* start an xact for this function invocation */
43714371
start_xact_command();

src/backend/utils/init/postinit.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ PerformAuthentication(Port *port)
236236
/*
237237
* Now perform authentication exchange.
238238
*/
239+
set_ps_display("authentication");
239240
ClientAuthentication(port); /* might not return, if failure */
240241

241242
/*
@@ -303,7 +304,7 @@ PerformAuthentication(Port *port)
303304
}
304305
}
305306

306-
set_ps_display("startup", false);
307+
set_ps_display("startup");
307308

308309
ClientAuthInProgress = false; /* client_min_messages is active now */
309310
}

0 commit comments

Comments
 (0)