From 2f0d43b251f5bfc257d45835965861202fcd263b Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 28 Jan 2004 21:02:40 +0000 Subject: [PATCH] Review uses of IsUnderPostmaster, change some tests to look at whereToSendOutput instead because they are really inquiring about the correct client communication protocol. Update some comments. This is pointing towards supporting regular FE/BE client protocol in a standalone backend, per discussion a month or so back. --- src/backend/access/transam/slru.c | 9 ++------- src/backend/catalog/index.c | 7 ++++--- src/backend/commands/copy.c | 8 ++++---- src/backend/commands/tablecmds.c | 7 ++++--- src/backend/postmaster/pgstat.c | 8 +++++++- src/backend/postmaster/postmaster.c | 14 +++++++++++++- src/backend/tcop/postgres.c | 8 ++++---- src/backend/utils/init/globals.c | 16 +++++++++++++--- 8 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/backend/access/transam/slru.c b/src/backend/access/transam/slru.c index 73c481c46b..ba3054f14b 100644 --- a/src/backend/access/transam/slru.c +++ b/src/backend/access/transam/slru.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.9 2004/01/26 22:35:31 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.10 2004/01/28 21:02:39 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -184,14 +184,12 @@ SimpleLruInit(SlruCtl ctl, const char *name, const char *subdir) shared = (SlruShared) ptr; #ifdef EXEC_BACKEND - /* * Locks are in shared memory */ locks = (SlruLock) (ptr + MAXALIGN(sizeof(SlruSharedData)) + BLCKSZ * NUM_CLOG_BUFFERS); #else - /* * Locks are in private memory */ @@ -200,10 +198,9 @@ SimpleLruInit(SlruCtl ctl, const char *name, const char *subdir) Assert(locks); #endif - if (!IsUnderPostmaster) - /* Initialize locks and shared memory area */ { + /* Initialize locks and shared memory area */ char *bufptr; int slotno; @@ -229,11 +226,9 @@ SimpleLruInit(SlruCtl ctl, const char *name, const char *subdir) else Assert(found); - ctl->locks = locks; ctl->shared = shared; - /* Init directory path */ snprintf(ctl->Dir, MAXPGPATH, "%s/%s", DataDir, subdir); } diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 91c9513621..4180526301 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.225 2004/01/07 18:56:25 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.226 2004/01/28 21:02:39 tgl Exp $ * * * INTERFACE ROUTINES @@ -508,8 +508,9 @@ index_create(Oid heapRelationId, * We cannot allow indexing a shared relation after initdb (because * there's no way to make the entry in other databases' pg_class). * Unfortunately we can't distinguish initdb from a manually started - * standalone backend. However, we can at least prevent this mistake - * under normal multi-user operation. + * standalone backend (toasting of shared rels happens after the bootstrap + * phase, so checking IsBootstrapProcessingMode() won't work). However, + * we can at least prevent this mistake under normal multi-user operation. */ if (shared_relation && IsUnderPostmaster) ereport(ERROR, diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index b5b3cfeefa..66850d32d5 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.216 2004/01/26 22:35:31 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.217 2004/01/28 21:02:39 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -829,7 +829,7 @@ DoCopy(const CopyStmt *stmt) } if (pipe) { - if (IsUnderPostmaster) + if (whereToSendOutput == Remote) ReceiveCopyBegin(binary, length(attnumlist)); else copy_file = stdin; @@ -879,7 +879,7 @@ DoCopy(const CopyStmt *stmt) } if (pipe) { - if (IsUnderPostmaster) + if (whereToSendOutput == Remote) SendCopyBegin(binary, length(attnumlist)); else copy_file = stdout; @@ -929,7 +929,7 @@ DoCopy(const CopyStmt *stmt) errmsg("could not write to file \"%s\": %m", filename))); } - else if (IsUnderPostmaster && !is_from) + else if (whereToSendOutput == Remote && !is_from) SendCopyEnd(binary); pfree(attribute_buf.data); pfree(line_buf.data); diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 7b6888918b..809f425bc6 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.96 2004/01/23 02:13:11 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.97 2004/01/28 21:02:39 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -4027,8 +4027,9 @@ AlterTableCreateToastTable(Oid relOid, bool silent) * We cannot allow toasting a shared relation after initdb (because * there's no way to mark it toasted in other databases' pg_class). * Unfortunately we can't distinguish initdb from a manually started - * standalone backend. However, we can at least prevent this mistake - * under normal multi-user operation. + * standalone backend (toasting happens after the bootstrap phase, + * so checking IsBootstrapProcessingMode() won't work). However, we can + * at least prevent this mistake under normal multi-user operation. */ shared_relation = rel->rd_rel->relisshared; if (shared_relation && IsUnderPostmaster) diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index b684f01c11..606240c86e 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -13,7 +13,7 @@ * * Copyright (c) 2001-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.56 2004/01/26 22:59:53 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.57 2004/01/28 21:02:40 tgl Exp $ * ---------- */ #include "postgres.h" @@ -1339,6 +1339,12 @@ pgstat_mainInit(void) { IsUnderPostmaster = true; /* we are a postmaster subprocess now */ +#ifdef EXEC_BACKEND + /* In EXEC case we will not have inherited these settings */ + IsPostmasterEnvironment = true; + whereToSendOutput = None; +#endif + MyProcPid = getpid(); /* reset MyProcPid */ /* Lose the postmaster's on-exit routines */ diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index ada86268e3..b10d4fd351 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -37,7 +37,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.363 2004/01/27 00:45:26 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.364 2004/01/28 21:02:40 tgl Exp $ * * NOTES * @@ -2697,6 +2697,10 @@ SubPostmasterMain(int argc, char* argv[]) /* Do this sooner rather than later... */ IsUnderPostmaster = true; /* we are a postmaster subprocess now */ + /* In EXEC case we will not have inherited these settings */ + IsPostmasterEnvironment = true; + whereToSendOutput = None; + /* Setup global context */ MemoryContextInit(); InitializeGUCOptions(); @@ -2994,6 +2998,14 @@ SSDataBaseInit(int xlop) IsUnderPostmaster = true; /* we are a postmaster subprocess * now */ +#ifdef EXEC_BACKEND + /* In EXEC case we will not have inherited these settings */ + IsPostmasterEnvironment = true; + whereToSendOutput = None; +#endif + + MyProcPid = getpid(); /* reset MyProcPid */ + /* Lose the postmaster's on-exit routines and port connections */ on_exit_reset(); diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 2a40b34261..4f62112714 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.386 2004/01/26 22:59:53 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.387 2004/01/28 21:02:40 tgl Exp $ * * NOTES * this is the "main" module of the postgres backend and @@ -392,7 +392,7 @@ ReadCommand(StringInfo inBuf) { int result; - if (IsUnderPostmaster) + if (whereToSendOutput == Remote) result = SocketBackend(inBuf); else result = InteractiveBackend(inBuf); @@ -2627,8 +2627,8 @@ PostgresMain(int argc, char *argv[], const char *username) /* Need not flush since ReadyForQuery will do it. */ } - /* Welcome banner for non-frontend case */ - if (!IsUnderPostmaster) + /* Welcome banner for standalone case */ + if (whereToSendOutput == Debug) printf("\nPostgreSQL stand-alone backend %s\n", PG_VERSION); /* diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c index 56058fa05c..3bddad4685 100644 --- a/src/backend/utils/init/globals.c +++ b/src/backend/utils/init/globals.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/init/globals.c,v 1.80 2004/01/26 22:59:53 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/init/globals.c,v 1.81 2004/01/28 21:02:40 tgl Exp $ * * NOTES * Globals used all over the place should be declared here and not @@ -53,9 +53,19 @@ BackendId MyBackendId; char *DatabasePath = NULL; Oid MyDatabaseId = InvalidOid; -pid_t PostmasterPid = 0; +pid_t PostmasterPid = 0; -/* these are initialized for the bootstrap/standalone case: */ +/* + * IsPostmasterEnvironment is true in a postmaster process and any postmaster + * child process; it is false in a standalone process (bootstrap or + * standalone backend). IsUnderPostmaster is true in postmaster child + * processes. Note that "child process" includes all children, not only + * regular backends. These should be set correctly as early as possible + * in the execution of a process, so that error handling will do the right + * things if an error should occur during process initialization. + * + * These are initialized for the bootstrap/standalone case. + */ bool IsPostmasterEnvironment = false; bool IsUnderPostmaster = false;