Skip to content

Commit 7c83a3b

Browse files
committed
process startup: Split single user code out of PostgresMain().
It was harder than necessary to understand PostgresMain() because the code for a normal backend was interspersed with single-user mode specific code. Split most of the single-user mode code into its own function PostgresSingleUserMain(), that does all the necessary setup for single-user mode, and then hands off after that to PostgresMain(). There still is some single-user mode code in InitPostgres(), and it'd likely be worth moving at least some of it out. But that's for later. Reviewed-By: Kyotaro Horiguchi <horikyota.ntt@gmail.com> Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20210802164124.ufo5buo4apl6yuvs@alap3.anarazel.de
1 parent 499c9b1 commit 7c83a3b

File tree

4 files changed

+90
-80
lines changed

4 files changed

+90
-80
lines changed

src/backend/main/main.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,8 @@ main(int argc, char *argv[])
192192
else if (argc > 1 && strcmp(argv[1], "--describe-config") == 0)
193193
GucInfoMain();
194194
else if (argc > 1 && strcmp(argv[1], "--single") == 0)
195-
PostgresMain(argc, argv,
196-
NULL, /* no dbname */
197-
strdup(get_user_name_or_exit(progname)));
195+
PostgresSingleUserMain(argc, argv,
196+
strdup(get_user_name_or_exit(progname)));
198197
else
199198
PostmasterMain(argc, argv);
200199
/* the functions above should not return */

src/backend/postmaster/postmaster.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4551,19 +4551,13 @@ BackendInitialize(Port *port)
45514551
static void
45524552
BackendRun(Port *port)
45534553
{
4554-
char *av[2];
4555-
const int ac = 1;
4556-
4557-
av[0] = "postgres";
4558-
av[1] = NULL;
4559-
45604554
/*
45614555
* Make sure we aren't in PostmasterContext anymore. (We can't delete it
45624556
* just yet, though, because InitPostgres will need the HBA data.)
45634557
*/
45644558
MemoryContextSwitchTo(TopMemoryContext);
45654559

4566-
PostgresMain(ac, av, port->database_name, port->user_name);
4560+
PostgresMain(port->database_name, port->user_name);
45674561
}
45684562

45694563

src/backend/tcop/postgres.c

Lines changed: 84 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,7 +3654,7 @@ get_stats_option_name(const char *arg)
36543654

36553655
/* ----------------------------------------------------------------
36563656
* process_postgres_switches
3657-
* Parse command line arguments for PostgresMain
3657+
* Parse command line arguments for backends
36583658
*
36593659
* This is called twice, once for the "secure" options coming from the
36603660
* postmaster or command line, and once for the "insecure" options coming
@@ -3915,40 +3915,30 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx,
39153915
}
39163916

39173917

3918-
/* ----------------------------------------------------------------
3919-
* PostgresMain
3920-
* postgres main loop -- all backends, interactive or otherwise start here
3918+
/*
3919+
* PostgresSingleUserMain
3920+
* Entry point for single user mode. argc/argv are the command line
3921+
* arguments to be used.
39213922
*
3922-
* argc/argv are the command line arguments to be used. (When being forked
3923-
* by the postmaster, these are not the original argv array of the process.)
3924-
* dbname is the name of the database to connect to, or NULL if the database
3925-
* name should be extracted from the command line arguments or defaulted.
3926-
* username is the PostgreSQL user name to be used for the session.
3927-
* ----------------------------------------------------------------
3923+
* Performs single user specific setup then calls PostgresMain() to actually
3924+
* process queries. Single user mode specific setup should go here, rather
3925+
* than PostgresMain() or InitPostgres() when reasonably possible.
39283926
*/
39293927
void
3930-
PostgresMain(int argc, char *argv[],
3931-
const char *dbname,
3932-
const char *username)
3928+
PostgresSingleUserMain(int argc, char *argv[],
3929+
const char *username)
39333930
{
3934-
int firstchar;
3935-
StringInfoData input_message;
3936-
sigjmp_buf local_sigjmp_buf;
3937-
volatile bool send_ready_for_query = true;
3938-
bool idle_in_transaction_timeout_enabled = false;
3939-
bool idle_session_timeout_enabled = false;
3931+
const char *dbname = NULL;
39403932

3941-
/* Initialize startup process environment if necessary. */
3942-
if (!IsUnderPostmaster)
3943-
InitStandaloneProcess(argv[0]);
3933+
Assert(!IsUnderPostmaster);
39443934

3945-
SetProcessingMode(InitProcessing);
3935+
/* Initialize startup process environment. */
3936+
InitStandaloneProcess(argv[0]);
39463937

39473938
/*
39483939
* Set default values for command-line options.
39493940
*/
3950-
if (!IsUnderPostmaster)
3951-
InitializeGUCOptions();
3941+
InitializeGUCOptions();
39523942

39533943
/*
39543944
* Parse command-line options.
@@ -3966,12 +3956,75 @@ PostgresMain(int argc, char *argv[],
39663956
progname)));
39673957
}
39683958

3969-
/* Acquire configuration parameters, unless inherited from postmaster */
3970-
if (!IsUnderPostmaster)
3971-
{
3972-
if (!SelectConfigFiles(userDoption, progname))
3973-
proc_exit(1);
3974-
}
3959+
/* Acquire configuration parameters */
3960+
if (!SelectConfigFiles(userDoption, progname))
3961+
proc_exit(1);
3962+
3963+
/*
3964+
* Validate we have been given a reasonable-looking DataDir and change
3965+
* into it.
3966+
*/
3967+
checkDataDir();
3968+
ChangeToDataDir();
3969+
3970+
/*
3971+
* Create lockfile for data directory.
3972+
*/
3973+
CreateDataDirLockFile(false);
3974+
3975+
/* read control file (error checking and contains config ) */
3976+
LocalProcessControlFile(false);
3977+
3978+
/* Initialize MaxBackends */
3979+
InitializeMaxBackends();
3980+
3981+
CreateSharedMemoryAndSemaphores();
3982+
3983+
/*
3984+
* Remember stand-alone backend startup time,roughly at the same point
3985+
* during startup that postmaster does so.
3986+
*/
3987+
PgStartTime = GetCurrentTimestamp();
3988+
3989+
/*
3990+
* Create a per-backend PGPROC struct in shared memory. We must do this
3991+
* before we can use LWLocks.
3992+
*/
3993+
InitProcess();
3994+
3995+
/*
3996+
* Now that sufficient infrastructure has been initialized, PostgresMain()
3997+
* can do the rest.
3998+
*/
3999+
PostgresMain(dbname, username);
4000+
}
4001+
4002+
4003+
/* ----------------------------------------------------------------
4004+
* PostgresMain
4005+
* postgres main loop -- all backends, interactive or otherwise loop here
4006+
*
4007+
* dbname is the name of the database to connect to, username is the
4008+
* PostgreSQL user name to be used for the session.
4009+
*
4010+
* NB: Single user mode specific setup should go to PostgresSingleUserMain()
4011+
* if reasonably possible.
4012+
* ----------------------------------------------------------------
4013+
*/
4014+
void
4015+
PostgresMain(const char *dbname, const char *username)
4016+
{
4017+
int firstchar;
4018+
StringInfoData input_message;
4019+
sigjmp_buf local_sigjmp_buf;
4020+
volatile bool send_ready_for_query = true;
4021+
bool idle_in_transaction_timeout_enabled = false;
4022+
bool idle_session_timeout_enabled = false;
4023+
4024+
AssertArg(dbname != NULL);
4025+
AssertArg(username != NULL);
4026+
4027+
SetProcessingMode(InitProcessing);
39754028

39764029
/*
39774030
* Set up signal handlers. (InitPostmasterChild or InitStandaloneProcess
@@ -4029,43 +4082,6 @@ PostgresMain(int argc, char *argv[],
40294082
* platforms */
40304083
}
40314084

4032-
if (!IsUnderPostmaster)
4033-
{
4034-
/*
4035-
* Validate we have been given a reasonable-looking DataDir (if under
4036-
* postmaster, assume postmaster did this already).
4037-
*/
4038-
checkDataDir();
4039-
4040-
/* Change into DataDir (if under postmaster, was done already) */
4041-
ChangeToDataDir();
4042-
4043-
/*
4044-
* Create lockfile for data directory.
4045-
*/
4046-
CreateDataDirLockFile(false);
4047-
4048-
/* read control file (error checking and contains config ) */
4049-
LocalProcessControlFile(false);
4050-
4051-
/* Initialize MaxBackends (if under postmaster, was done already) */
4052-
InitializeMaxBackends();
4053-
4054-
CreateSharedMemoryAndSemaphores();
4055-
4056-
/*
4057-
* Remember stand-alone backend startup time, roughly at the same
4058-
* point during startup that postmaster does so.
4059-
*/
4060-
PgStartTime = GetCurrentTimestamp();
4061-
4062-
/*
4063-
* Create a per-backend PGPROC struct in shared memory. We must do
4064-
* this before we can use LWLocks.
4065-
*/
4066-
InitProcess();
4067-
}
4068-
40694085
/* Early initialization */
40704086
BaseInit();
40714087

src/include/tcop/tcopprot.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ extern void ProcessClientWriteInterrupt(bool blocked);
7575

7676
extern void process_postgres_switches(int argc, char *argv[],
7777
GucContext ctx, const char **dbname);
78-
extern void PostgresMain(int argc, char *argv[],
79-
const char *dbname,
78+
extern void PostgresSingleUserMain(int argc, char *argv[],
79+
const char *username) pg_attribute_noreturn();
80+
extern void PostgresMain(const char *dbname,
8081
const char *username) pg_attribute_noreturn();
8182
extern long get_stack_depth_rlimit(void);
8283
extern void ResetUsage(void);

0 commit comments

Comments
 (0)