diff --git a/src/backend/access/transam/slru.c b/src/backend/access/transam/slru.c
index 73c481c46b7d6b4a9e5275d3ccc2d750c8c9addc..ba3054f14bbbbe52e94c2dd5c5f9541702552beb 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 91c951362157144d093087d6373dde2d180ac822..4180526301ac50cd73180e22b62777729afef95e 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 b5b3cfeefac0b67ac0e942023f744695cae67a15..66850d32d56229523972129e428d14b6b9b3d96f 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 7b6888918bd47a202ef3bdace0a022689f17441b..809f425bc656a72b71a67ee52e283a6efb598aea 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 b684f01c1199d1fd92f75cab154f480a65b839c3..606240c86eb20ae5c14e631315084c5e9067b3bd 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 ada86268e38dc3d85e493f32c28863cf2ae460f9..b10d4fd351af5f7ed241831bd281d454bf7a2c19 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 2a40b34261ec0922eb3c5a8ae0da0724fe884cf0..4f62112714bdb085cfa3c6fa0247d525f51b659b 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 56058fa05cc687026415a7feb4faa9558a96bc3f..3bddad4685a510e4e6cf6b870c6068c5b779f472 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;