Index: src/backend/storage/ipc/ipc.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/storage/ipc/ipc.c,v
retrieving revision 1.95
diff -c -r1.95 ipc.c
*** src/backend/storage/ipc/ipc.c	5 Jan 2007 22:19:37 -0000	1.95
--- src/backend/storage/ipc/ipc.c	31 Jan 2007 15:48:55 -0000
***************
*** 110,115 ****
--- 110,145 ----
  								  on_proc_exit_list[on_proc_exit_index].arg);
  
  	elog(DEBUG3, "exit(%d)", code);
+ 
+ #ifdef LINUX_PROFILE
+     {
+         /*
+          * If we are profiling ourself then gprof's mcleanup() is about
+          * to write out a profile to ./gmon.out.  Since mcleanup() always 
+          * uses a fixed file name, each backend will overwrite earlier
+          * profiles. To fix that, we create a separate subdirectory for
+          * each backend (./gprof/pid) and 'cd' to that subdirectory before
+          * we exit() - that forces mcleanup() to write each profile into
+          * its own directory.  We end up with something like:
+          *  $PGDATA/gprof/8829/gmon.out
+          *  $PGDATA/gprof/8845/gmon.out
+          *      ...
+          *
+          * Note that we do this here instead of in an on_proc_exit() 
+          * callback because we want to ensure that this code executes
+          * last - we don't want to interfere with any other on_proc_exit()
+          * callback.
+          */
+         char gprofDirName[MAXPGPATH];
+ 
+         snprintf(gprofDirName, MAXPGPATH, "./gprof/%d", getpid());
+         
+         mkdir("./gprof", 0777);
+         mkdir(gprofDirName, 0777);
+         chdir(gprofDirName);
+     }
+ #endif
+ 
  	exit(code);
  }
  
