Commit 8a6fab41 authored by Tom Lane's avatar Tom Lane

Remove ShutdownBufferPoolAccess exit callback, and do the work in

ProcKill instead, where we still have a PGPROC with which to wait on
LWLocks.  This fixes 'can't wait without a PROC structure' failures
occasionally seen during backend shutdown (I'm surprised they weren't
more frequent, actually).  Add an Assert() to LWLockAcquire to help
catch any similar mistakes in future.  Fix failure to update MyProcPid
for standalone backends and pgstat processes.
parent 691aefcf
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.142 2002/09/22 19:42:50 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.143 2002/09/25 20:31:40 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -369,6 +369,9 @@ BootstrapMain(int argc, char *argv[]) ...@@ -369,6 +369,9 @@ BootstrapMain(int argc, char *argv[])
BaseInit(); BaseInit();
if (IsUnderPostmaster)
InitDummyProcess(); /* needed to get LWLocks */
/* /*
* XLOG operations * XLOG operations
*/ */
...@@ -386,8 +389,6 @@ BootstrapMain(int argc, char *argv[]) ...@@ -386,8 +389,6 @@ BootstrapMain(int argc, char *argv[])
break; break;
case BS_XLOG_CHECKPOINT: case BS_XLOG_CHECKPOINT:
if (IsUnderPostmaster)
InitDummyProcess(); /* needed to get LWLocks */
CreateDummyCaches(); CreateDummyCaches();
CreateCheckPoint(false); CreateCheckPoint(false);
SetSavedRedoRecPtr(); /* pass redo ptr back to SetSavedRedoRecPtr(); /* pass redo ptr back to
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* *
* Copyright (c) 2001, PostgreSQL Global Development Group * Copyright (c) 2001, PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.28 2002/09/05 18:26:18 petere Exp $ * $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.29 2002/09/25 20:31:40 tgl Exp $
* ---------- * ----------
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -314,6 +314,8 @@ pgstat_start(void) ...@@ -314,6 +314,8 @@ pgstat_start(void)
IsUnderPostmaster = true; /* we are a postmaster subprocess now */ IsUnderPostmaster = true; /* we are a postmaster subprocess now */
MyProcPid = getpid(); /* reset MyProcPid */
/* Lose the postmaster's on-exit routines */ /* Lose the postmaster's on-exit routines */
on_exit_reset(); on_exit_reset();
...@@ -1190,6 +1192,8 @@ pgstat_main(void) ...@@ -1190,6 +1192,8 @@ pgstat_main(void)
*/ */
pqsignal(SIGCHLD, SIG_DFL); pqsignal(SIGCHLD, SIG_DFL);
MyProcPid = getpid(); /* reset MyProcPid */
/* /*
* Identify myself via ps * Identify myself via ps
*/ */
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.52 2002/09/04 20:31:25 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.53 2002/09/25 20:31:40 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -35,8 +35,6 @@ ...@@ -35,8 +35,6 @@
#include "utils/memutils.h" #include "utils/memutils.h"
static void ShutdownBufferPoolAccess(void);
/* /*
* if BMTRACE is defined, we trace the last 200 buffer allocations and * if BMTRACE is defined, we trace the last 200 buffer allocations and
* deallocations in a circular buffer in shared memory. * deallocations in a circular buffer in shared memory.
...@@ -219,6 +217,10 @@ InitBufferPool(void) ...@@ -219,6 +217,10 @@ InitBufferPool(void)
* This is called during backend startup (whether standalone or under the * This is called during backend startup (whether standalone or under the
* postmaster). It sets up for this backend's access to the already-existing * postmaster). It sets up for this backend's access to the already-existing
* buffer pool. * buffer pool.
*
* NB: this is called before InitProcess(), so we do not have a PGPROC and
* cannot do LWLockAcquire; hence we can't actually access the bufmgr's
* shared memory yet. We are only initializing local data here.
*/ */
void void
InitBufferPoolAccess(void) InitBufferPoolAccess(void)
...@@ -238,27 +240,6 @@ InitBufferPoolAccess(void) ...@@ -238,27 +240,6 @@ InitBufferPoolAccess(void)
*/ */
for (i = 0; i < NBuffers; i++) for (i = 0; i < NBuffers; i++)
BufferBlockPointers[i] = (Block) MAKE_PTR(BufferDescriptors[i].data); BufferBlockPointers[i] = (Block) MAKE_PTR(BufferDescriptors[i].data);
/*
* Now that buffer access is initialized, set up a callback to shut it
* down again at backend exit.
*/
on_shmem_exit(ShutdownBufferPoolAccess, 0);
}
/*
* Shut down buffer manager at backend exit.
*
* This is needed mainly to ensure that we don't leave any buffer reference
* counts set during an error exit.
*/
static void
ShutdownBufferPoolAccess(void)
{
/* Release any buffer context locks we are holding */
UnlockBuffers();
/* Release any buffer reference counts we are holding */
AtEOXact_Buffers(false);
} }
/* ----------------------------------------------------- /* -----------------------------------------------------
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lwlock.c,v 1.13 2002/09/04 20:31:26 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lwlock.c,v 1.14 2002/09/25 20:31:40 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -203,6 +203,13 @@ LWLockAcquire(LWLockId lockid, LWLockMode mode) ...@@ -203,6 +203,13 @@ LWLockAcquire(LWLockId lockid, LWLockMode mode)
PRINT_LWDEBUG("LWLockAcquire", lockid, lock); PRINT_LWDEBUG("LWLockAcquire", lockid, lock);
/*
* We can't wait if we haven't got a PGPROC. This should only occur
* during bootstrap or shared memory initialization. Put an Assert
* here to catch unsafe coding practices.
*/
Assert(!(proc == NULL && IsUnderPostmaster));
/* /*
* Lock out cancel/die interrupts until we exit the code section * Lock out cancel/die interrupts until we exit the code section
* protected by the LWLock. This ensures that interrupts will not * protected by the LWLock. This ensures that interrupts will not
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.125 2002/09/04 20:31:26 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.126 2002/09/25 20:31:40 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -391,8 +391,15 @@ ProcKill(void) ...@@ -391,8 +391,15 @@ ProcKill(void)
/* Release any LW locks I am holding */ /* Release any LW locks I am holding */
LWLockReleaseAll(); LWLockReleaseAll();
/* Abort any buffer I/O in progress */ /*
* Make real sure we release any buffer locks and pins we might be
* holding, too. It is pretty ugly to do this here and not in a
* shutdown callback registered by the bufmgr ... but we must do this
* *after* LWLockReleaseAll and *before* zapping MyProc.
*/
AbortBufferIO(); AbortBufferIO();
UnlockBuffers();
AtEOXact_Buffers(false);
/* Get off any wait queue I might be on */ /* Get off any wait queue I might be on */
LockWaitCancel(); LockWaitCancel();
...@@ -430,8 +437,10 @@ DummyProcKill(void) ...@@ -430,8 +437,10 @@ DummyProcKill(void)
/* Release any LW locks I am holding */ /* Release any LW locks I am holding */
LWLockReleaseAll(); LWLockReleaseAll();
/* Abort any buffer I/O in progress */ /* Release buffer locks and pins, too */
AbortBufferIO(); AbortBufferIO();
UnlockBuffers();
AtEOXact_Buffers(false);
/* I can't be on regular lock queues, so needn't check */ /* I can't be on regular lock queues, so needn't check */
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.293 2002/09/20 03:45:08 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.294 2002/09/25 20:31:40 tgl Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
...@@ -1163,6 +1163,13 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -1163,6 +1163,13 @@ PostgresMain(int argc, char *argv[], const char *username)
} }
} }
/*
* initialize globals (already done if under postmaster, but not if
* standalone; cheap enough to do over)
*/
MyProcPid = getpid();
/* /*
* Fire up essential subsystems: error and memory management * Fire up essential subsystems: error and memory management
* *
...@@ -1691,7 +1698,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -1691,7 +1698,7 @@ PostgresMain(int argc, char *argv[], const char *username)
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ {
puts("\nPOSTGRES backend interactive interface "); puts("\nPOSTGRES backend interactive interface ");
puts("$Revision: 1.293 $ $Date: 2002/09/20 03:45:08 $\n"); puts("$Revision: 1.294 $ $Date: 2002/09/25 20:31:40 $\n");
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment