Header And Logo

PostgreSQL
| The world's most advanced open source database.

proc.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * proc.c
00004  *    routines to manage per-process shared memory data structure
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/backend/storage/lmgr/proc.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 /*
00016  * Interface (a):
00017  *      ProcSleep(), ProcWakeup(),
00018  *      ProcQueueAlloc() -- create a shm queue for sleeping processes
00019  *      ProcQueueInit() -- create a queue without allocing memory
00020  *
00021  * Waiting for a lock causes the backend to be put to sleep.  Whoever releases
00022  * the lock wakes the process up again (and gives it an error code so it knows
00023  * whether it was awoken on an error condition).
00024  *
00025  * Interface (b):
00026  *
00027  * ProcReleaseLocks -- frees the locks associated with current transaction
00028  *
00029  * ProcKill -- destroys the shared memory state (and locks)
00030  * associated with the process.
00031  */
00032 #include "postgres.h"
00033 
00034 #include <signal.h>
00035 #include <unistd.h>
00036 #include <sys/time.h>
00037 
00038 #include "access/transam.h"
00039 #include "access/twophase.h"
00040 #include "access/xact.h"
00041 #include "miscadmin.h"
00042 #include "postmaster/autovacuum.h"
00043 #include "replication/syncrep.h"
00044 #include "storage/ipc.h"
00045 #include "storage/lmgr.h"
00046 #include "storage/pmsignal.h"
00047 #include "storage/proc.h"
00048 #include "storage/procarray.h"
00049 #include "storage/procsignal.h"
00050 #include "storage/spin.h"
00051 #include "utils/timeout.h"
00052 #include "utils/timestamp.h"
00053 
00054 
00055 /* GUC variables */
00056 int         DeadlockTimeout = 1000;
00057 int         StatementTimeout = 0;
00058 int         LockTimeout = 0;
00059 bool        log_lock_waits = false;
00060 
00061 /* Pointer to this process's PGPROC and PGXACT structs, if any */
00062 PGPROC     *MyProc = NULL;
00063 PGXACT     *MyPgXact = NULL;
00064 
00065 /*
00066  * This spinlock protects the freelist of recycled PGPROC structures.
00067  * We cannot use an LWLock because the LWLock manager depends on already
00068  * having a PGPROC and a wait semaphore!  But these structures are touched
00069  * relatively infrequently (only at backend startup or shutdown) and not for
00070  * very long, so a spinlock is okay.
00071  */
00072 NON_EXEC_STATIC slock_t *ProcStructLock = NULL;
00073 
00074 /* Pointers to shared-memory structures */
00075 PROC_HDR   *ProcGlobal = NULL;
00076 NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL;
00077 PGPROC     *PreparedXactProcs = NULL;
00078 
00079 /* If we are waiting for a lock, this points to the associated LOCALLOCK */
00080 static LOCALLOCK *lockAwaited = NULL;
00081 
00082 /* Mark this volatile because it can be changed by signal handler */
00083 static volatile DeadLockState deadlock_state = DS_NOT_YET_CHECKED;
00084 
00085 
00086 static void RemoveProcFromArray(int code, Datum arg);
00087 static void ProcKill(int code, Datum arg);
00088 static void AuxiliaryProcKill(int code, Datum arg);
00089 
00090 
00091 /*
00092  * Report shared-memory space needed by InitProcGlobal.
00093  */
00094 Size
00095 ProcGlobalShmemSize(void)
00096 {
00097     Size        size = 0;
00098 
00099     /* ProcGlobal */
00100     size = add_size(size, sizeof(PROC_HDR));
00101     /* MyProcs, including autovacuum workers and launcher */
00102     size = add_size(size, mul_size(MaxBackends, sizeof(PGPROC)));
00103     /* AuxiliaryProcs */
00104     size = add_size(size, mul_size(NUM_AUXILIARY_PROCS, sizeof(PGPROC)));
00105     /* Prepared xacts */
00106     size = add_size(size, mul_size(max_prepared_xacts, sizeof(PGPROC)));
00107     /* ProcStructLock */
00108     size = add_size(size, sizeof(slock_t));
00109 
00110     size = add_size(size, mul_size(MaxBackends, sizeof(PGXACT)));
00111     size = add_size(size, mul_size(NUM_AUXILIARY_PROCS, sizeof(PGXACT)));
00112     size = add_size(size, mul_size(max_prepared_xacts, sizeof(PGXACT)));
00113 
00114     return size;
00115 }
00116 
00117 /*
00118  * Report number of semaphores needed by InitProcGlobal.
00119  */
00120 int
00121 ProcGlobalSemas(void)
00122 {
00123     /*
00124      * We need a sema per backend (including autovacuum), plus one for each
00125      * auxiliary process.
00126      */
00127     return MaxBackends + NUM_AUXILIARY_PROCS;
00128 }
00129 
00130 /*
00131  * InitProcGlobal -
00132  *    Initialize the global process table during postmaster or standalone
00133  *    backend startup.
00134  *
00135  *    We also create all the per-process semaphores we will need to support
00136  *    the requested number of backends.  We used to allocate semaphores
00137  *    only when backends were actually started up, but that is bad because
00138  *    it lets Postgres fail under load --- a lot of Unix systems are
00139  *    (mis)configured with small limits on the number of semaphores, and
00140  *    running out when trying to start another backend is a common failure.
00141  *    So, now we grab enough semaphores to support the desired max number
00142  *    of backends immediately at initialization --- if the sysadmin has set
00143  *    MaxConnections or autovacuum_max_workers higher than his kernel will
00144  *    support, he'll find out sooner rather than later.  (The number of
00145  *    background worker processes registered by loadable modules is also taken
00146  *    into consideration.)
00147  *
00148  *    Another reason for creating semaphores here is that the semaphore
00149  *    implementation typically requires us to create semaphores in the
00150  *    postmaster, not in backends.
00151  *
00152  * Note: this is NOT called by individual backends under a postmaster,
00153  * not even in the EXEC_BACKEND case.  The ProcGlobal and AuxiliaryProcs
00154  * pointers must be propagated specially for EXEC_BACKEND operation.
00155  */
00156 void
00157 InitProcGlobal(void)
00158 {
00159     PGPROC     *procs;
00160     PGXACT     *pgxacts;
00161     int         i,
00162                 j;
00163     bool        found;
00164     uint32      TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS + max_prepared_xacts;
00165 
00166     /* Create the ProcGlobal shared structure */
00167     ProcGlobal = (PROC_HDR *)
00168         ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
00169     Assert(!found);
00170 
00171     /*
00172      * Initialize the data structures.
00173      */
00174     ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;
00175     ProcGlobal->freeProcs = NULL;
00176     ProcGlobal->autovacFreeProcs = NULL;
00177     ProcGlobal->bgworkerFreeProcs = NULL;
00178     ProcGlobal->startupProc = NULL;
00179     ProcGlobal->startupProcPid = 0;
00180     ProcGlobal->startupBufferPinWaitBufId = -1;
00181     ProcGlobal->walwriterLatch = NULL;
00182     ProcGlobal->checkpointerLatch = NULL;
00183 
00184     /*
00185      * Create and initialize all the PGPROC structures we'll need.  There are
00186      * five separate consumers: (1) normal backends, (2) autovacuum workers
00187      * and the autovacuum launcher, (3) background workers, (4) auxiliary
00188      * processes, and (5) prepared transactions.  Each PGPROC structure is
00189      * dedicated to exactly one of these purposes, and they do not move between
00190      * groups.
00191      */
00192     procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC));
00193     ProcGlobal->allProcs = procs;
00194     ProcGlobal->allProcCount = TotalProcs;
00195     if (!procs)
00196         ereport(FATAL,
00197                 (errcode(ERRCODE_OUT_OF_MEMORY),
00198                  errmsg("out of shared memory")));
00199     MemSet(procs, 0, TotalProcs * sizeof(PGPROC));
00200 
00201     /*
00202      * Also allocate a separate array of PGXACT structures.  This is separate
00203      * from the main PGPROC array so that the most heavily accessed data is
00204      * stored contiguously in memory in as few cache lines as possible. This
00205      * provides significant performance benefits, especially on a
00206      * multiprocessor system.  There is one PGXACT structure for every PGPROC
00207      * structure.
00208      */
00209     pgxacts = (PGXACT *) ShmemAlloc(TotalProcs * sizeof(PGXACT));
00210     MemSet(pgxacts, 0, TotalProcs * sizeof(PGXACT));
00211     ProcGlobal->allPgXact = pgxacts;
00212 
00213     for (i = 0; i < TotalProcs; i++)
00214     {
00215         /* Common initialization for all PGPROCs, regardless of type. */
00216 
00217         /*
00218          * Set up per-PGPROC semaphore, latch, and backendLock. Prepared xact
00219          * dummy PGPROCs don't need these though - they're never associated
00220          * with a real process
00221          */
00222         if (i < MaxBackends + NUM_AUXILIARY_PROCS)
00223         {
00224             PGSemaphoreCreate(&(procs[i].sem));
00225             InitSharedLatch(&(procs[i].procLatch));
00226             procs[i].backendLock = LWLockAssign();
00227         }
00228         procs[i].pgprocno = i;
00229 
00230         /*
00231          * Newly created PGPROCs for normal backends, autovacuum and bgworkers
00232          * must be queued up on the appropriate free list.  Because there can
00233          * only ever be a small, fixed number of auxiliary processes, no free
00234          * list is used in that case; InitAuxiliaryProcess() instead uses a
00235          * linear search.   PGPROCs for prepared transactions are added to a
00236          * free list by TwoPhaseShmemInit().
00237          */
00238         if (i < MaxConnections)
00239         {
00240             /* PGPROC for normal backend, add to freeProcs list */
00241             procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs;
00242             ProcGlobal->freeProcs = &procs[i];
00243         }
00244         else if (i < MaxConnections + autovacuum_max_workers + 1)
00245         {
00246             /* PGPROC for AV launcher/worker, add to autovacFreeProcs list */
00247             procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs;
00248             ProcGlobal->autovacFreeProcs = &procs[i];
00249         }
00250         else if (i < MaxBackends)
00251         {
00252             /* PGPROC for bgworker, add to bgworkerFreeProcs list */
00253             procs[i].links.next = (SHM_QUEUE *) ProcGlobal->bgworkerFreeProcs;
00254             ProcGlobal->bgworkerFreeProcs = &procs[i];
00255         }
00256 
00257         /* Initialize myProcLocks[] shared memory queues. */
00258         for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
00259             SHMQueueInit(&(procs[i].myProcLocks[j]));
00260     }
00261 
00262     /*
00263      * Save pointers to the blocks of PGPROC structures reserved for auxiliary
00264      * processes and prepared transactions.
00265      */
00266     AuxiliaryProcs = &procs[MaxBackends];
00267     PreparedXactProcs = &procs[MaxBackends + NUM_AUXILIARY_PROCS];
00268 
00269     /* Create ProcStructLock spinlock, too */
00270     ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));
00271     SpinLockInit(ProcStructLock);
00272 }
00273 
00274 /*
00275  * InitProcess -- initialize a per-process data structure for this backend
00276  */
00277 void
00278 InitProcess(void)
00279 {
00280     /* use volatile pointer to prevent code rearrangement */
00281     volatile PROC_HDR *procglobal = ProcGlobal;
00282 
00283     /*
00284      * ProcGlobal should be set up already (if we are a backend, we inherit
00285      * this by fork() or EXEC_BACKEND mechanism from the postmaster).
00286      */
00287     if (procglobal == NULL)
00288         elog(PANIC, "proc header uninitialized");
00289 
00290     if (MyProc != NULL)
00291         elog(ERROR, "you already exist");
00292 
00293     /*
00294      * Initialize process-local latch support.  This could fail if the kernel
00295      * is low on resources, and if so we want to exit cleanly before acquiring
00296      * any shared-memory resources.
00297      */
00298     InitializeLatchSupport();
00299 
00300     /*
00301      * Try to get a proc struct from the free list.  If this fails, we must be
00302      * out of PGPROC structures (not to mention semaphores).
00303      *
00304      * While we are holding the ProcStructLock, also copy the current shared
00305      * estimate of spins_per_delay to local storage.
00306      */
00307     SpinLockAcquire(ProcStructLock);
00308 
00309     set_spins_per_delay(procglobal->spins_per_delay);
00310 
00311     if (IsAnyAutoVacuumProcess())
00312         MyProc = procglobal->autovacFreeProcs;
00313     else if (IsBackgroundWorker)
00314         MyProc = procglobal->bgworkerFreeProcs;
00315     else
00316         MyProc = procglobal->freeProcs;
00317 
00318     if (MyProc != NULL)
00319     {
00320         if (IsAnyAutoVacuumProcess())
00321             procglobal->autovacFreeProcs = (PGPROC *) MyProc->links.next;
00322         else if (IsBackgroundWorker)
00323             procglobal->bgworkerFreeProcs = (PGPROC *) MyProc->links.next;
00324         else
00325             procglobal->freeProcs = (PGPROC *) MyProc->links.next;
00326         SpinLockRelease(ProcStructLock);
00327     }
00328     else
00329     {
00330         /*
00331          * If we reach here, all the PGPROCs are in use.  This is one of the
00332          * possible places to detect "too many backends", so give the standard
00333          * error message.  XXX do we need to give a different failure message
00334          * in the autovacuum case?
00335          */
00336         SpinLockRelease(ProcStructLock);
00337         ereport(FATAL,
00338                 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
00339                  errmsg("sorry, too many clients already")));
00340     }
00341     MyPgXact = &ProcGlobal->allPgXact[MyProc->pgprocno];
00342 
00343     /*
00344      * Now that we have a PGPROC, mark ourselves as an active postmaster
00345      * child; this is so that the postmaster can detect it if we exit without
00346      * cleaning up.  (XXX autovac launcher currently doesn't participate in
00347      * this; it probably should.)
00348      */
00349     if (IsUnderPostmaster && !IsAutoVacuumLauncherProcess())
00350         MarkPostmasterChildActive();
00351 
00352     /*
00353      * Initialize all fields of MyProc, except for those previously
00354      * initialized by InitProcGlobal.
00355      */
00356     SHMQueueElemInit(&(MyProc->links));
00357     MyProc->waitStatus = STATUS_OK;
00358     MyProc->lxid = InvalidLocalTransactionId;
00359     MyProc->fpVXIDLock = false;
00360     MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
00361     MyPgXact->xid = InvalidTransactionId;
00362     MyPgXact->xmin = InvalidTransactionId;
00363     MyProc->pid = MyProcPid;
00364     /* backendId, databaseId and roleId will be filled in later */
00365     MyProc->backendId = InvalidBackendId;
00366     MyProc->databaseId = InvalidOid;
00367     MyProc->roleId = InvalidOid;
00368     MyPgXact->delayChkpt = false;
00369     MyPgXact->vacuumFlags = 0;
00370     /* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
00371     if (IsAutoVacuumWorkerProcess())
00372         MyPgXact->vacuumFlags |= PROC_IS_AUTOVACUUM;
00373     MyProc->lwWaiting = false;
00374     MyProc->lwWaitMode = 0;
00375     MyProc->lwWaitLink = NULL;
00376     MyProc->waitLock = NULL;
00377     MyProc->waitProcLock = NULL;
00378 #ifdef USE_ASSERT_CHECKING
00379     if (assert_enabled)
00380     {
00381         int         i;
00382 
00383         /* Last process should have released all locks. */
00384         for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
00385             Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
00386     }
00387 #endif
00388     MyProc->recoveryConflictPending = false;
00389 
00390     /* Initialize fields for sync rep */
00391     MyProc->waitLSN = 0;
00392     MyProc->syncRepState = SYNC_REP_NOT_WAITING;
00393     SHMQueueElemInit(&(MyProc->syncRepLinks));
00394 
00395     /*
00396      * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
00397      * Note that there's no particular need to do ResetLatch here.
00398      */
00399     OwnLatch(&MyProc->procLatch);
00400 
00401     /*
00402      * We might be reusing a semaphore that belonged to a failed process. So
00403      * be careful and reinitialize its value here.  (This is not strictly
00404      * necessary anymore, but seems like a good idea for cleanliness.)
00405      */
00406     PGSemaphoreReset(&MyProc->sem);
00407 
00408     /*
00409      * Arrange to clean up at backend exit.
00410      */
00411     on_shmem_exit(ProcKill, 0);
00412 
00413     /*
00414      * Now that we have a PGPROC, we could try to acquire locks, so initialize
00415      * the deadlock checker.
00416      */
00417     InitDeadLockChecking();
00418 }
00419 
00420 /*
00421  * InitProcessPhase2 -- make MyProc visible in the shared ProcArray.
00422  *
00423  * This is separate from InitProcess because we can't acquire LWLocks until
00424  * we've created a PGPROC, but in the EXEC_BACKEND case ProcArrayAdd won't
00425  * work until after we've done CreateSharedMemoryAndSemaphores.
00426  */
00427 void
00428 InitProcessPhase2(void)
00429 {
00430     Assert(MyProc != NULL);
00431 
00432     /*
00433      * Add our PGPROC to the PGPROC array in shared memory.
00434      */
00435     ProcArrayAdd(MyProc);
00436 
00437     /*
00438      * Arrange to clean that up at backend exit.
00439      */
00440     on_shmem_exit(RemoveProcFromArray, 0);
00441 }
00442 
00443 /*
00444  * InitAuxiliaryProcess -- create a per-auxiliary-process data structure
00445  *
00446  * This is called by bgwriter and similar processes so that they will have a
00447  * MyProc value that's real enough to let them wait for LWLocks.  The PGPROC
00448  * and sema that are assigned are one of the extra ones created during
00449  * InitProcGlobal.
00450  *
00451  * Auxiliary processes are presently not expected to wait for real (lockmgr)
00452  * locks, so we need not set up the deadlock checker.  They are never added
00453  * to the ProcArray or the sinval messaging mechanism, either.  They also
00454  * don't get a VXID assigned, since this is only useful when we actually
00455  * hold lockmgr locks.
00456  *
00457  * Startup process however uses locks but never waits for them in the
00458  * normal backend sense. Startup process also takes part in sinval messaging
00459  * as a sendOnly process, so never reads messages from sinval queue. So
00460  * Startup process does have a VXID and does show up in pg_locks.
00461  */
00462 void
00463 InitAuxiliaryProcess(void)
00464 {
00465     PGPROC     *auxproc;
00466     int         proctype;
00467 
00468     /*
00469      * ProcGlobal should be set up already (if we are a backend, we inherit
00470      * this by fork() or EXEC_BACKEND mechanism from the postmaster).
00471      */
00472     if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
00473         elog(PANIC, "proc header uninitialized");
00474 
00475     if (MyProc != NULL)
00476         elog(ERROR, "you already exist");
00477 
00478     /*
00479      * Initialize process-local latch support.  This could fail if the kernel
00480      * is low on resources, and if so we want to exit cleanly before acquiring
00481      * any shared-memory resources.
00482      */
00483     InitializeLatchSupport();
00484 
00485     /*
00486      * We use the ProcStructLock to protect assignment and releasing of
00487      * AuxiliaryProcs entries.
00488      *
00489      * While we are holding the ProcStructLock, also copy the current shared
00490      * estimate of spins_per_delay to local storage.
00491      */
00492     SpinLockAcquire(ProcStructLock);
00493 
00494     set_spins_per_delay(ProcGlobal->spins_per_delay);
00495 
00496     /*
00497      * Find a free auxproc ... *big* trouble if there isn't one ...
00498      */
00499     for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)
00500     {
00501         auxproc = &AuxiliaryProcs[proctype];
00502         if (auxproc->pid == 0)
00503             break;
00504     }
00505     if (proctype >= NUM_AUXILIARY_PROCS)
00506     {
00507         SpinLockRelease(ProcStructLock);
00508         elog(FATAL, "all AuxiliaryProcs are in use");
00509     }
00510 
00511     /* Mark auxiliary proc as in use by me */
00512     /* use volatile pointer to prevent code rearrangement */
00513     ((volatile PGPROC *) auxproc)->pid = MyProcPid;
00514 
00515     MyProc = auxproc;
00516     MyPgXact = &ProcGlobal->allPgXact[auxproc->pgprocno];
00517 
00518     SpinLockRelease(ProcStructLock);
00519 
00520     /*
00521      * Initialize all fields of MyProc, except for those previously
00522      * initialized by InitProcGlobal.
00523      */
00524     SHMQueueElemInit(&(MyProc->links));
00525     MyProc->waitStatus = STATUS_OK;
00526     MyProc->lxid = InvalidLocalTransactionId;
00527     MyProc->fpVXIDLock = false;
00528     MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
00529     MyPgXact->xid = InvalidTransactionId;
00530     MyPgXact->xmin = InvalidTransactionId;
00531     MyProc->backendId = InvalidBackendId;
00532     MyProc->databaseId = InvalidOid;
00533     MyProc->roleId = InvalidOid;
00534     MyPgXact->delayChkpt = false;
00535     MyPgXact->vacuumFlags = 0;
00536     MyProc->lwWaiting = false;
00537     MyProc->lwWaitMode = 0;
00538     MyProc->lwWaitLink = NULL;
00539     MyProc->waitLock = NULL;
00540     MyProc->waitProcLock = NULL;
00541 #ifdef USE_ASSERT_CHECKING
00542     if (assert_enabled)
00543     {
00544         int         i;
00545 
00546         /* Last process should have released all locks. */
00547         for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
00548             Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
00549     }
00550 #endif
00551 
00552     /*
00553      * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
00554      * Note that there's no particular need to do ResetLatch here.
00555      */
00556     OwnLatch(&MyProc->procLatch);
00557 
00558     /*
00559      * We might be reusing a semaphore that belonged to a failed process. So
00560      * be careful and reinitialize its value here.  (This is not strictly
00561      * necessary anymore, but seems like a good idea for cleanliness.)
00562      */
00563     PGSemaphoreReset(&MyProc->sem);
00564 
00565     /*
00566      * Arrange to clean up at process exit.
00567      */
00568     on_shmem_exit(AuxiliaryProcKill, Int32GetDatum(proctype));
00569 }
00570 
00571 /*
00572  * Record the PID and PGPROC structures for the Startup process, for use in
00573  * ProcSendSignal().  See comments there for further explanation.
00574  */
00575 void
00576 PublishStartupProcessInformation(void)
00577 {
00578     /* use volatile pointer to prevent code rearrangement */
00579     volatile PROC_HDR *procglobal = ProcGlobal;
00580 
00581     SpinLockAcquire(ProcStructLock);
00582 
00583     procglobal->startupProc = MyProc;
00584     procglobal->startupProcPid = MyProcPid;
00585 
00586     SpinLockRelease(ProcStructLock);
00587 }
00588 
00589 /*
00590  * Used from bufgr to share the value of the buffer that Startup waits on,
00591  * or to reset the value to "not waiting" (-1). This allows processing
00592  * of recovery conflicts for buffer pins. Set is made before backends look
00593  * at this value, so locking not required, especially since the set is
00594  * an atomic integer set operation.
00595  */
00596 void
00597 SetStartupBufferPinWaitBufId(int bufid)
00598 {
00599     /* use volatile pointer to prevent code rearrangement */
00600     volatile PROC_HDR *procglobal = ProcGlobal;
00601 
00602     procglobal->startupBufferPinWaitBufId = bufid;
00603 }
00604 
00605 /*
00606  * Used by backends when they receive a request to check for buffer pin waits.
00607  */
00608 int
00609 GetStartupBufferPinWaitBufId(void)
00610 {
00611     /* use volatile pointer to prevent code rearrangement */
00612     volatile PROC_HDR *procglobal = ProcGlobal;
00613 
00614     return procglobal->startupBufferPinWaitBufId;
00615 }
00616 
00617 /*
00618  * Check whether there are at least N free PGPROC objects.
00619  *
00620  * Note: this is designed on the assumption that N will generally be small.
00621  */
00622 bool
00623 HaveNFreeProcs(int n)
00624 {
00625     PGPROC     *proc;
00626 
00627     /* use volatile pointer to prevent code rearrangement */
00628     volatile PROC_HDR *procglobal = ProcGlobal;
00629 
00630     SpinLockAcquire(ProcStructLock);
00631 
00632     proc = procglobal->freeProcs;
00633 
00634     while (n > 0 && proc != NULL)
00635     {
00636         proc = (PGPROC *) proc->links.next;
00637         n--;
00638     }
00639 
00640     SpinLockRelease(ProcStructLock);
00641 
00642     return (n <= 0);
00643 }
00644 
00645 /*
00646  * Check if the current process is awaiting a lock.
00647  */
00648 bool
00649 IsWaitingForLock(void)
00650 {
00651     if (lockAwaited == NULL)
00652         return false;
00653 
00654     return true;
00655 }
00656 
00657 /*
00658  * Cancel any pending wait for lock, when aborting a transaction, and revert
00659  * any strong lock count acquisition for a lock being acquired.
00660  *
00661  * (Normally, this would only happen if we accept a cancel/die
00662  * interrupt while waiting; but an ereport(ERROR) before or during the lock
00663  * wait is within the realm of possibility, too.)
00664  */
00665 void
00666 LockErrorCleanup(void)
00667 {
00668     LWLockId    partitionLock;
00669     DisableTimeoutParams timeouts[2];
00670 
00671     AbortStrongLockAcquire();
00672 
00673     /* Nothing to do if we weren't waiting for a lock */
00674     if (lockAwaited == NULL)
00675         return;
00676 
00677     /*
00678      * Turn off the deadlock and lock timeout timers, if they are still
00679      * running (see ProcSleep).  Note we must preserve the LOCK_TIMEOUT
00680      * indicator flag, since this function is executed before
00681      * ProcessInterrupts when responding to SIGINT; else we'd lose the
00682      * knowledge that the SIGINT came from a lock timeout and not an external
00683      * source.
00684      */
00685     timeouts[0].id = DEADLOCK_TIMEOUT;
00686     timeouts[0].keep_indicator = false;
00687     timeouts[1].id = LOCK_TIMEOUT;
00688     timeouts[1].keep_indicator = true;
00689     disable_timeouts(timeouts, 2);
00690 
00691     /* Unlink myself from the wait queue, if on it (might not be anymore!) */
00692     partitionLock = LockHashPartitionLock(lockAwaited->hashcode);
00693     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
00694 
00695     if (MyProc->links.next != NULL)
00696     {
00697         /* We could not have been granted the lock yet */
00698         RemoveFromWaitQueue(MyProc, lockAwaited->hashcode);
00699     }
00700     else
00701     {
00702         /*
00703          * Somebody kicked us off the lock queue already.  Perhaps they
00704          * granted us the lock, or perhaps they detected a deadlock. If they
00705          * did grant us the lock, we'd better remember it in our local lock
00706          * table.
00707          */
00708         if (MyProc->waitStatus == STATUS_OK)
00709             GrantAwaitedLock();
00710     }
00711 
00712     lockAwaited = NULL;
00713 
00714     LWLockRelease(partitionLock);
00715 
00716     /*
00717      * We used to do PGSemaphoreReset() here to ensure that our proc's wait
00718      * semaphore is reset to zero.  This prevented a leftover wakeup signal
00719      * from remaining in the semaphore if someone else had granted us the lock
00720      * we wanted before we were able to remove ourselves from the wait-list.
00721      * However, now that ProcSleep loops until waitStatus changes, a leftover
00722      * wakeup signal isn't harmful, and it seems not worth expending cycles to
00723      * get rid of a signal that most likely isn't there.
00724      */
00725 }
00726 
00727 
00728 /*
00729  * ProcReleaseLocks() -- release locks associated with current transaction
00730  *          at main transaction commit or abort
00731  *
00732  * At main transaction commit, we release standard locks except session locks.
00733  * At main transaction abort, we release all locks including session locks.
00734  *
00735  * Advisory locks are released only if they are transaction-level;
00736  * session-level holds remain, whether this is a commit or not.
00737  *
00738  * At subtransaction commit, we don't release any locks (so this func is not
00739  * needed at all); we will defer the releasing to the parent transaction.
00740  * At subtransaction abort, we release all locks held by the subtransaction;
00741  * this is implemented by retail releasing of the locks under control of
00742  * the ResourceOwner mechanism.
00743  */
00744 void
00745 ProcReleaseLocks(bool isCommit)
00746 {
00747     if (!MyProc)
00748         return;
00749     /* If waiting, get off wait queue (should only be needed after error) */
00750     LockErrorCleanup();
00751     /* Release standard locks, including session-level if aborting */
00752     LockReleaseAll(DEFAULT_LOCKMETHOD, !isCommit);
00753     /* Release transaction-level advisory locks */
00754     LockReleaseAll(USER_LOCKMETHOD, false);
00755 }
00756 
00757 
00758 /*
00759  * RemoveProcFromArray() -- Remove this process from the shared ProcArray.
00760  */
00761 static void
00762 RemoveProcFromArray(int code, Datum arg)
00763 {
00764     Assert(MyProc != NULL);
00765     ProcArrayRemove(MyProc, InvalidTransactionId);
00766 }
00767 
00768 /*
00769  * ProcKill() -- Destroy the per-proc data structure for
00770  *      this process. Release any of its held LW locks.
00771  */
00772 static void
00773 ProcKill(int code, Datum arg)
00774 {
00775     /* use volatile pointer to prevent code rearrangement */
00776     volatile PROC_HDR *procglobal = ProcGlobal;
00777 
00778     Assert(MyProc != NULL);
00779 
00780     /* Make sure we're out of the sync rep lists */
00781     SyncRepCleanupAtProcExit();
00782 
00783 #ifdef USE_ASSERT_CHECKING
00784     if (assert_enabled)
00785     {
00786         int         i;
00787 
00788         /* Last process should have released all locks. */
00789         for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
00790             Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
00791     }
00792 #endif
00793 
00794     /*
00795      * Release any LW locks I am holding.  There really shouldn't be any, but
00796      * it's cheap to check again before we cut the knees off the LWLock
00797      * facility by releasing our PGPROC ...
00798      */
00799     LWLockReleaseAll();
00800 
00801     /* Release ownership of the process's latch, too */
00802     DisownLatch(&MyProc->procLatch);
00803 
00804     SpinLockAcquire(ProcStructLock);
00805 
00806     /* Return PGPROC structure (and semaphore) to appropriate freelist */
00807     if (IsAnyAutoVacuumProcess())
00808     {
00809         MyProc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
00810         procglobal->autovacFreeProcs = MyProc;
00811     }
00812     else if (IsBackgroundWorker)
00813     {
00814         MyProc->links.next = (SHM_QUEUE *) procglobal->bgworkerFreeProcs;
00815         procglobal->bgworkerFreeProcs = MyProc;
00816     }
00817     else
00818     {
00819         MyProc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
00820         procglobal->freeProcs = MyProc;
00821     }
00822 
00823     /* PGPROC struct isn't mine anymore */
00824     MyProc = NULL;
00825 
00826     /* Update shared estimate of spins_per_delay */
00827     procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay);
00828 
00829     SpinLockRelease(ProcStructLock);
00830 
00831     /*
00832      * This process is no longer present in shared memory in any meaningful
00833      * way, so tell the postmaster we've cleaned up acceptably well. (XXX
00834      * autovac launcher should be included here someday)
00835      */
00836     if (IsUnderPostmaster && !IsAutoVacuumLauncherProcess())
00837         MarkPostmasterChildInactive();
00838 
00839     /* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
00840     if (AutovacuumLauncherPid != 0)
00841         kill(AutovacuumLauncherPid, SIGUSR2);
00842 }
00843 
00844 /*
00845  * AuxiliaryProcKill() -- Cut-down version of ProcKill for auxiliary
00846  *      processes (bgwriter, etc).  The PGPROC and sema are not released, only
00847  *      marked as not-in-use.
00848  */
00849 static void
00850 AuxiliaryProcKill(int code, Datum arg)
00851 {
00852     int         proctype = DatumGetInt32(arg);
00853     PGPROC     *auxproc PG_USED_FOR_ASSERTS_ONLY;
00854 
00855     Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
00856 
00857     auxproc = &AuxiliaryProcs[proctype];
00858 
00859     Assert(MyProc == auxproc);
00860 
00861     /* Release any LW locks I am holding (see notes above) */
00862     LWLockReleaseAll();
00863 
00864     /* Release ownership of the process's latch, too */
00865     DisownLatch(&MyProc->procLatch);
00866 
00867     SpinLockAcquire(ProcStructLock);
00868 
00869     /* Mark auxiliary proc no longer in use */
00870     MyProc->pid = 0;
00871 
00872     /* PGPROC struct isn't mine anymore */
00873     MyProc = NULL;
00874 
00875     /* Update shared estimate of spins_per_delay */
00876     ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
00877 
00878     SpinLockRelease(ProcStructLock);
00879 }
00880 
00881 
00882 /*
00883  * ProcQueue package: routines for putting processes to sleep
00884  *      and  waking them up
00885  */
00886 
00887 /*
00888  * ProcQueueAlloc -- alloc/attach to a shared memory process queue
00889  *
00890  * Returns: a pointer to the queue
00891  * Side Effects: Initializes the queue if it wasn't there before
00892  */
00893 #ifdef NOT_USED
00894 PROC_QUEUE *
00895 ProcQueueAlloc(const char *name)
00896 {
00897     PROC_QUEUE *queue;
00898     bool        found;
00899 
00900     queue = (PROC_QUEUE *)
00901         ShmemInitStruct(name, sizeof(PROC_QUEUE), &found);
00902 
00903     if (!found)
00904         ProcQueueInit(queue);
00905 
00906     return queue;
00907 }
00908 #endif
00909 
00910 /*
00911  * ProcQueueInit -- initialize a shared memory process queue
00912  */
00913 void
00914 ProcQueueInit(PROC_QUEUE *queue)
00915 {
00916     SHMQueueInit(&(queue->links));
00917     queue->size = 0;
00918 }
00919 
00920 
00921 /*
00922  * ProcSleep -- put a process to sleep on the specified lock
00923  *
00924  * Caller must have set MyProc->heldLocks to reflect locks already held
00925  * on the lockable object by this process (under all XIDs).
00926  *
00927  * The lock table's partition lock must be held at entry, and will be held
00928  * at exit.
00929  *
00930  * Result: STATUS_OK if we acquired the lock, STATUS_ERROR if not (deadlock).
00931  *
00932  * ASSUME: that no one will fiddle with the queue until after
00933  *      we release the partition lock.
00934  *
00935  * NOTES: The process queue is now a priority queue for locking.
00936  *
00937  * P() on the semaphore should put us to sleep.  The process
00938  * semaphore is normally zero, so when we try to acquire it, we sleep.
00939  */
00940 int
00941 ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
00942 {
00943     LOCKMODE    lockmode = locallock->tag.mode;
00944     LOCK       *lock = locallock->lock;
00945     PROCLOCK   *proclock = locallock->proclock;
00946     uint32      hashcode = locallock->hashcode;
00947     LWLockId    partitionLock = LockHashPartitionLock(hashcode);
00948     PROC_QUEUE *waitQueue = &(lock->waitProcs);
00949     LOCKMASK    myHeldLocks = MyProc->heldLocks;
00950     bool        early_deadlock = false;
00951     bool        allow_autovacuum_cancel = true;
00952     int         myWaitStatus;
00953     PGPROC     *proc;
00954     int         i;
00955 
00956     /*
00957      * Determine where to add myself in the wait queue.
00958      *
00959      * Normally I should go at the end of the queue.  However, if I already
00960      * hold locks that conflict with the request of any previous waiter, put
00961      * myself in the queue just in front of the first such waiter. This is not
00962      * a necessary step, since deadlock detection would move me to before that
00963      * waiter anyway; but it's relatively cheap to detect such a conflict
00964      * immediately, and avoid delaying till deadlock timeout.
00965      *
00966      * Special case: if I find I should go in front of some waiter, check to
00967      * see if I conflict with already-held locks or the requests before that
00968      * waiter.  If not, then just grant myself the requested lock immediately.
00969      * This is the same as the test for immediate grant in LockAcquire, except
00970      * we are only considering the part of the wait queue before my insertion
00971      * point.
00972      */
00973     if (myHeldLocks != 0)
00974     {
00975         LOCKMASK    aheadRequests = 0;
00976 
00977         proc = (PGPROC *) waitQueue->links.next;
00978         for (i = 0; i < waitQueue->size; i++)
00979         {
00980             /* Must he wait for me? */
00981             if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
00982             {
00983                 /* Must I wait for him ? */
00984                 if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
00985                 {
00986                     /*
00987                      * Yes, so we have a deadlock.  Easiest way to clean up
00988                      * correctly is to call RemoveFromWaitQueue(), but we
00989                      * can't do that until we are *on* the wait queue. So, set
00990                      * a flag to check below, and break out of loop.  Also,
00991                      * record deadlock info for later message.
00992                      */
00993                     RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
00994                     early_deadlock = true;
00995                     break;
00996                 }
00997                 /* I must go before this waiter.  Check special case. */
00998                 if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
00999                     LockCheckConflicts(lockMethodTable,
01000                                        lockmode,
01001                                        lock,
01002                                        proclock,
01003                                        MyProc) == STATUS_OK)
01004                 {
01005                     /* Skip the wait and just grant myself the lock. */
01006                     GrantLock(lock, proclock, lockmode);
01007                     GrantAwaitedLock();
01008                     return STATUS_OK;
01009                 }
01010                 /* Break out of loop to put myself before him */
01011                 break;
01012             }
01013             /* Nope, so advance to next waiter */
01014             aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
01015             proc = (PGPROC *) proc->links.next;
01016         }
01017 
01018         /*
01019          * If we fall out of loop normally, proc points to waitQueue head, so
01020          * we will insert at tail of queue as desired.
01021          */
01022     }
01023     else
01024     {
01025         /* I hold no locks, so I can't push in front of anyone. */
01026         proc = (PGPROC *) &(waitQueue->links);
01027     }
01028 
01029     /*
01030      * Insert self into queue, ahead of the given proc (or at tail of queue).
01031      */
01032     SHMQueueInsertBefore(&(proc->links), &(MyProc->links));
01033     waitQueue->size++;
01034 
01035     lock->waitMask |= LOCKBIT_ON(lockmode);
01036 
01037     /* Set up wait information in PGPROC object, too */
01038     MyProc->waitLock = lock;
01039     MyProc->waitProcLock = proclock;
01040     MyProc->waitLockMode = lockmode;
01041 
01042     MyProc->waitStatus = STATUS_WAITING;
01043 
01044     /*
01045      * If we detected deadlock, give up without waiting.  This must agree with
01046      * CheckDeadLock's recovery code, except that we shouldn't release the
01047      * semaphore since we haven't tried to lock it yet.
01048      */
01049     if (early_deadlock)
01050     {
01051         RemoveFromWaitQueue(MyProc, hashcode);
01052         return STATUS_ERROR;
01053     }
01054 
01055     /* mark that we are waiting for a lock */
01056     lockAwaited = locallock;
01057 
01058     /*
01059      * Release the lock table's partition lock.
01060      *
01061      * NOTE: this may also cause us to exit critical-section state, possibly
01062      * allowing a cancel/die interrupt to be accepted. This is OK because we
01063      * have recorded the fact that we are waiting for a lock, and so
01064      * LockErrorCleanup will clean up if cancel/die happens.
01065      */
01066     LWLockRelease(partitionLock);
01067 
01068     /*
01069      * Also, now that we will successfully clean up after an ereport, it's
01070      * safe to check to see if there's a buffer pin deadlock against the
01071      * Startup process.  Of course, that's only necessary if we're doing Hot
01072      * Standby and are not the Startup process ourselves.
01073      */
01074     if (RecoveryInProgress() && !InRecovery)
01075         CheckRecoveryConflictDeadlock();
01076 
01077     /* Reset deadlock_state before enabling the timeout handler */
01078     deadlock_state = DS_NOT_YET_CHECKED;
01079 
01080     /*
01081      * Set timer so we can wake up after awhile and check for a deadlock. If a
01082      * deadlock is detected, the handler releases the process's semaphore and
01083      * sets MyProc->waitStatus = STATUS_ERROR, allowing us to know that we
01084      * must report failure rather than success.
01085      *
01086      * By delaying the check until we've waited for a bit, we can avoid
01087      * running the rather expensive deadlock-check code in most cases.
01088      *
01089      * If LockTimeout is set, also enable the timeout for that.  We can save a
01090      * few cycles by enabling both timeout sources in one call.
01091      */
01092     if (LockTimeout > 0)
01093     {
01094         EnableTimeoutParams timeouts[2];
01095 
01096         timeouts[0].id = DEADLOCK_TIMEOUT;
01097         timeouts[0].type = TMPARAM_AFTER;
01098         timeouts[0].delay_ms = DeadlockTimeout;
01099         timeouts[1].id = LOCK_TIMEOUT;
01100         timeouts[1].type = TMPARAM_AFTER;
01101         timeouts[1].delay_ms = LockTimeout;
01102         enable_timeouts(timeouts, 2);
01103     }
01104     else
01105         enable_timeout_after(DEADLOCK_TIMEOUT, DeadlockTimeout);
01106 
01107     /*
01108      * If someone wakes us between LWLockRelease and PGSemaphoreLock,
01109      * PGSemaphoreLock will not block.  The wakeup is "saved" by the semaphore
01110      * implementation.  While this is normally good, there are cases where a
01111      * saved wakeup might be leftover from a previous operation (for example,
01112      * we aborted ProcWaitForSignal just before someone did ProcSendSignal).
01113      * So, loop to wait again if the waitStatus shows we haven't been granted
01114      * nor denied the lock yet.
01115      *
01116      * We pass interruptOK = true, which eliminates a window in which
01117      * cancel/die interrupts would be held off undesirably.  This is a promise
01118      * that we don't mind losing control to a cancel/die interrupt here.  We
01119      * don't, because we have no shared-state-change work to do after being
01120      * granted the lock (the grantor did it all).  We do have to worry about
01121      * canceling the deadlock timeout and updating the locallock table, but if
01122      * we lose control to an error, LockErrorCleanup will fix that up.
01123      */
01124     do
01125     {
01126         PGSemaphoreLock(&MyProc->sem, true);
01127 
01128         /*
01129          * waitStatus could change from STATUS_WAITING to something else
01130          * asynchronously.  Read it just once per loop to prevent surprising
01131          * behavior (such as missing log messages).
01132          */
01133         myWaitStatus = MyProc->waitStatus;
01134 
01135         /*
01136          * If we are not deadlocked, but are waiting on an autovacuum-induced
01137          * task, send a signal to interrupt it.
01138          */
01139         if (deadlock_state == DS_BLOCKED_BY_AUTOVACUUM && allow_autovacuum_cancel)
01140         {
01141             PGPROC     *autovac = GetBlockingAutoVacuumPgproc();
01142             PGXACT     *autovac_pgxact = &ProcGlobal->allPgXact[autovac->pgprocno];
01143 
01144             LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
01145 
01146             /*
01147              * Only do it if the worker is not working to protect against Xid
01148              * wraparound.
01149              */
01150             if ((autovac != NULL) &&
01151                 (autovac_pgxact->vacuumFlags & PROC_IS_AUTOVACUUM) &&
01152                 !(autovac_pgxact->vacuumFlags & PROC_VACUUM_FOR_WRAPAROUND))
01153             {
01154                 int         pid = autovac->pid;
01155                 StringInfoData locktagbuf;
01156                 StringInfoData logbuf;      /* errdetail for server log */
01157 
01158                 initStringInfo(&locktagbuf);
01159                 initStringInfo(&logbuf);
01160                 DescribeLockTag(&locktagbuf, &lock->tag);
01161                 appendStringInfo(&logbuf,
01162                       _("Process %d waits for %s on %s."),
01163                          MyProcPid,
01164                          GetLockmodeName(lock->tag.locktag_lockmethodid,
01165                                          lockmode),
01166                          locktagbuf.data);
01167 
01168                 /* release lock as quickly as possible */
01169                 LWLockRelease(ProcArrayLock);
01170 
01171                 ereport(LOG,
01172                         (errmsg("sending cancel to blocking autovacuum PID %d",
01173                             pid),
01174                          errdetail_log("%s", logbuf.data)));
01175 
01176                 pfree(logbuf.data);
01177                 pfree(locktagbuf.data);
01178 
01179                 /* send the autovacuum worker Back to Old Kent Road */
01180                 if (kill(pid, SIGINT) < 0)
01181                 {
01182                     /* Just a warning to allow multiple callers */
01183                     ereport(WARNING,
01184                             (errmsg("could not send signal to process %d: %m",
01185                                     pid)));
01186                 }
01187             }
01188             else
01189                 LWLockRelease(ProcArrayLock);
01190 
01191             /* prevent signal from being resent more than once */
01192             allow_autovacuum_cancel = false;
01193         }
01194 
01195         /*
01196          * If awoken after the deadlock check interrupt has run, and
01197          * log_lock_waits is on, then report about the wait.
01198          */
01199         if (log_lock_waits && deadlock_state != DS_NOT_YET_CHECKED)
01200         {
01201             StringInfoData buf;
01202             const char *modename;
01203             long        secs;
01204             int         usecs;
01205             long        msecs;
01206 
01207             initStringInfo(&buf);
01208             DescribeLockTag(&buf, &locallock->tag.lock);
01209             modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
01210                                        lockmode);
01211             TimestampDifference(get_timeout_start_time(DEADLOCK_TIMEOUT),
01212                                 GetCurrentTimestamp(),
01213                                 &secs, &usecs);
01214             msecs = secs * 1000 + usecs / 1000;
01215             usecs = usecs % 1000;
01216 
01217             if (deadlock_state == DS_SOFT_DEADLOCK)
01218                 ereport(LOG,
01219                         (errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
01220                               MyProcPid, modename, buf.data, msecs, usecs)));
01221             else if (deadlock_state == DS_HARD_DEADLOCK)
01222             {
01223                 /*
01224                  * This message is a bit redundant with the error that will be
01225                  * reported subsequently, but in some cases the error report
01226                  * might not make it to the log (eg, if it's caught by an
01227                  * exception handler), and we want to ensure all long-wait
01228                  * events get logged.
01229                  */
01230                 ereport(LOG,
01231                         (errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
01232                               MyProcPid, modename, buf.data, msecs, usecs)));
01233             }
01234 
01235             if (myWaitStatus == STATUS_WAITING)
01236                 ereport(LOG,
01237                         (errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
01238                               MyProcPid, modename, buf.data, msecs, usecs)));
01239             else if (myWaitStatus == STATUS_OK)
01240                 ereport(LOG,
01241                     (errmsg("process %d acquired %s on %s after %ld.%03d ms",
01242                             MyProcPid, modename, buf.data, msecs, usecs)));
01243             else
01244             {
01245                 Assert(myWaitStatus == STATUS_ERROR);
01246 
01247                 /*
01248                  * Currently, the deadlock checker always kicks its own
01249                  * process, which means that we'll only see STATUS_ERROR when
01250                  * deadlock_state == DS_HARD_DEADLOCK, and there's no need to
01251                  * print redundant messages.  But for completeness and
01252                  * future-proofing, print a message if it looks like someone
01253                  * else kicked us off the lock.
01254                  */
01255                 if (deadlock_state != DS_HARD_DEADLOCK)
01256                     ereport(LOG,
01257                             (errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
01258                               MyProcPid, modename, buf.data, msecs, usecs)));
01259             }
01260 
01261             /*
01262              * At this point we might still need to wait for the lock. Reset
01263              * state so we don't print the above messages again.
01264              */
01265             deadlock_state = DS_NO_DEADLOCK;
01266 
01267             pfree(buf.data);
01268         }
01269     } while (myWaitStatus == STATUS_WAITING);
01270 
01271     /*
01272      * Disable the timers, if they are still running
01273      */
01274     if (LockTimeout > 0)
01275     {
01276         DisableTimeoutParams timeouts[2];
01277 
01278         timeouts[0].id = DEADLOCK_TIMEOUT;
01279         timeouts[0].keep_indicator = false;
01280         timeouts[1].id = LOCK_TIMEOUT;
01281         timeouts[1].keep_indicator = false;
01282         disable_timeouts(timeouts, 2);
01283     }
01284     else
01285         disable_timeout(DEADLOCK_TIMEOUT, false);
01286 
01287     /*
01288      * Re-acquire the lock table's partition lock.  We have to do this to hold
01289      * off cancel/die interrupts before we can mess with lockAwaited (else we
01290      * might have a missed or duplicated locallock update).
01291      */
01292     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
01293 
01294     /*
01295      * We no longer want LockErrorCleanup to do anything.
01296      */
01297     lockAwaited = NULL;
01298 
01299     /*
01300      * If we got the lock, be sure to remember it in the locallock table.
01301      */
01302     if (MyProc->waitStatus == STATUS_OK)
01303         GrantAwaitedLock();
01304 
01305     /*
01306      * We don't have to do anything else, because the awaker did all the
01307      * necessary update of the lock table and MyProc.
01308      */
01309     return MyProc->waitStatus;
01310 }
01311 
01312 
01313 /*
01314  * ProcWakeup -- wake up a process by releasing its private semaphore.
01315  *
01316  *   Also remove the process from the wait queue and set its links invalid.
01317  *   RETURN: the next process in the wait queue.
01318  *
01319  * The appropriate lock partition lock must be held by caller.
01320  *
01321  * XXX: presently, this code is only used for the "success" case, and only
01322  * works correctly for that case.  To clean up in failure case, would need
01323  * to twiddle the lock's request counts too --- see RemoveFromWaitQueue.
01324  * Hence, in practice the waitStatus parameter must be STATUS_OK.
01325  */
01326 PGPROC *
01327 ProcWakeup(PGPROC *proc, int waitStatus)
01328 {
01329     PGPROC     *retProc;
01330 
01331     /* Proc should be sleeping ... */
01332     if (proc->links.prev == NULL ||
01333         proc->links.next == NULL)
01334         return NULL;
01335     Assert(proc->waitStatus == STATUS_WAITING);
01336 
01337     /* Save next process before we zap the list link */
01338     retProc = (PGPROC *) proc->links.next;
01339 
01340     /* Remove process from wait queue */
01341     SHMQueueDelete(&(proc->links));
01342     (proc->waitLock->waitProcs.size)--;
01343 
01344     /* Clean up process' state and pass it the ok/fail signal */
01345     proc->waitLock = NULL;
01346     proc->waitProcLock = NULL;
01347     proc->waitStatus = waitStatus;
01348 
01349     /* And awaken it */
01350     PGSemaphoreUnlock(&proc->sem);
01351 
01352     return retProc;
01353 }
01354 
01355 /*
01356  * ProcLockWakeup -- routine for waking up processes when a lock is
01357  *      released (or a prior waiter is aborted).  Scan all waiters
01358  *      for lock, waken any that are no longer blocked.
01359  *
01360  * The appropriate lock partition lock must be held by caller.
01361  */
01362 void
01363 ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
01364 {
01365     PROC_QUEUE *waitQueue = &(lock->waitProcs);
01366     int         queue_size = waitQueue->size;
01367     PGPROC     *proc;
01368     LOCKMASK    aheadRequests = 0;
01369 
01370     Assert(queue_size >= 0);
01371 
01372     if (queue_size == 0)
01373         return;
01374 
01375     proc = (PGPROC *) waitQueue->links.next;
01376 
01377     while (queue_size-- > 0)
01378     {
01379         LOCKMODE    lockmode = proc->waitLockMode;
01380 
01381         /*
01382          * Waken if (a) doesn't conflict with requests of earlier waiters, and
01383          * (b) doesn't conflict with already-held locks.
01384          */
01385         if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
01386             LockCheckConflicts(lockMethodTable,
01387                                lockmode,
01388                                lock,
01389                                proc->waitProcLock,
01390                                proc) == STATUS_OK)
01391         {
01392             /* OK to waken */
01393             GrantLock(lock, proc->waitProcLock, lockmode);
01394             proc = ProcWakeup(proc, STATUS_OK);
01395 
01396             /*
01397              * ProcWakeup removes proc from the lock's waiting process queue
01398              * and returns the next proc in chain; don't use proc's next-link,
01399              * because it's been cleared.
01400              */
01401         }
01402         else
01403         {
01404             /*
01405              * Cannot wake this guy. Remember his request for later checks.
01406              */
01407             aheadRequests |= LOCKBIT_ON(lockmode);
01408             proc = (PGPROC *) proc->links.next;
01409         }
01410     }
01411 
01412     Assert(waitQueue->size >= 0);
01413 }
01414 
01415 /*
01416  * CheckDeadLock
01417  *
01418  * We only get to this routine if the DEADLOCK_TIMEOUT fired
01419  * while waiting for a lock to be released by some other process.  Look
01420  * to see if there's a deadlock; if not, just return and continue waiting.
01421  * (But signal ProcSleep to log a message, if log_lock_waits is true.)
01422  * If we have a real deadlock, remove ourselves from the lock's wait queue
01423  * and signal an error to ProcSleep.
01424  *
01425  * NB: this is run inside a signal handler, so be very wary about what is done
01426  * here or in called routines.
01427  */
01428 void
01429 CheckDeadLock(void)
01430 {
01431     int         i;
01432 
01433     /*
01434      * Acquire exclusive lock on the entire shared lock data structures. Must
01435      * grab LWLocks in partition-number order to avoid LWLock deadlock.
01436      *
01437      * Note that the deadlock check interrupt had better not be enabled
01438      * anywhere that this process itself holds lock partition locks, else this
01439      * will wait forever.  Also note that LWLockAcquire creates a critical
01440      * section, so that this routine cannot be interrupted by cancel/die
01441      * interrupts.
01442      */
01443     for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
01444         LWLockAcquire(FirstLockMgrLock + i, LW_EXCLUSIVE);
01445 
01446     /*
01447      * Check to see if we've been awoken by anyone in the interim.
01448      *
01449      * If we have, we can return and resume our transaction -- happy day.
01450      * Before we are awoken the process releasing the lock grants it to us so
01451      * we know that we don't have to wait anymore.
01452      *
01453      * We check by looking to see if we've been unlinked from the wait queue.
01454      * This is quicker than checking our semaphore's state, since no kernel
01455      * call is needed, and it is safe because we hold the lock partition lock.
01456      */
01457     if (MyProc->links.prev == NULL ||
01458         MyProc->links.next == NULL)
01459         goto check_done;
01460 
01461 #ifdef LOCK_DEBUG
01462     if (Debug_deadlocks)
01463         DumpAllLocks();
01464 #endif
01465 
01466     /* Run the deadlock check, and set deadlock_state for use by ProcSleep */
01467     deadlock_state = DeadLockCheck(MyProc);
01468 
01469     if (deadlock_state == DS_HARD_DEADLOCK)
01470     {
01471         /*
01472          * Oops.  We have a deadlock.
01473          *
01474          * Get this process out of wait state. (Note: we could do this more
01475          * efficiently by relying on lockAwaited, but use this coding to
01476          * preserve the flexibility to kill some other transaction than the
01477          * one detecting the deadlock.)
01478          *
01479          * RemoveFromWaitQueue sets MyProc->waitStatus to STATUS_ERROR, so
01480          * ProcSleep will report an error after we return from the signal
01481          * handler.
01482          */
01483         Assert(MyProc->waitLock != NULL);
01484         RemoveFromWaitQueue(MyProc, LockTagHashCode(&(MyProc->waitLock->tag)));
01485 
01486         /*
01487          * Unlock my semaphore so that the interrupted ProcSleep() call can
01488          * finish.
01489          */
01490         PGSemaphoreUnlock(&MyProc->sem);
01491 
01492         /*
01493          * We're done here.  Transaction abort caused by the error that
01494          * ProcSleep will raise will cause any other locks we hold to be
01495          * released, thus allowing other processes to wake up; we don't need
01496          * to do that here.  NOTE: an exception is that releasing locks we
01497          * hold doesn't consider the possibility of waiters that were blocked
01498          * behind us on the lock we just failed to get, and might now be
01499          * wakable because we're not in front of them anymore.  However,
01500          * RemoveFromWaitQueue took care of waking up any such processes.
01501          */
01502     }
01503     else if (log_lock_waits || deadlock_state == DS_BLOCKED_BY_AUTOVACUUM)
01504     {
01505         /*
01506          * Unlock my semaphore so that the interrupted ProcSleep() call can
01507          * print the log message (we daren't do it here because we are inside
01508          * a signal handler).  It will then sleep again until someone releases
01509          * the lock.
01510          *
01511          * If blocked by autovacuum, this wakeup will enable ProcSleep to send
01512          * the canceling signal to the autovacuum worker.
01513          */
01514         PGSemaphoreUnlock(&MyProc->sem);
01515     }
01516 
01517     /*
01518      * And release locks.  We do this in reverse order for two reasons: (1)
01519      * Anyone else who needs more than one of the locks will be trying to lock
01520      * them in increasing order; we don't want to release the other process
01521      * until it can get all the locks it needs. (2) This avoids O(N^2)
01522      * behavior inside LWLockRelease.
01523      */
01524 check_done:
01525     for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
01526         LWLockRelease(FirstLockMgrLock + i);
01527 }
01528 
01529 
01530 /*
01531  * ProcWaitForSignal - wait for a signal from another backend.
01532  *
01533  * This can share the semaphore normally used for waiting for locks,
01534  * since a backend could never be waiting for a lock and a signal at
01535  * the same time.  As with locks, it's OK if the signal arrives just
01536  * before we actually reach the waiting state.  Also as with locks,
01537  * it's necessary that the caller be robust against bogus wakeups:
01538  * always check that the desired state has occurred, and wait again
01539  * if not.  This copes with possible "leftover" wakeups.
01540  */
01541 void
01542 ProcWaitForSignal(void)
01543 {
01544     PGSemaphoreLock(&MyProc->sem, true);
01545 }
01546 
01547 /*
01548  * ProcSendSignal - send a signal to a backend identified by PID
01549  */
01550 void
01551 ProcSendSignal(int pid)
01552 {
01553     PGPROC     *proc = NULL;
01554 
01555     if (RecoveryInProgress())
01556     {
01557         /* use volatile pointer to prevent code rearrangement */
01558         volatile PROC_HDR *procglobal = ProcGlobal;
01559 
01560         SpinLockAcquire(ProcStructLock);
01561 
01562         /*
01563          * Check to see whether it is the Startup process we wish to signal.
01564          * This call is made by the buffer manager when it wishes to wake up a
01565          * process that has been waiting for a pin in so it can obtain a
01566          * cleanup lock using LockBufferForCleanup(). Startup is not a normal
01567          * backend, so BackendPidGetProc() will not return any pid at all. So
01568          * we remember the information for this special case.
01569          */
01570         if (pid == procglobal->startupProcPid)
01571             proc = procglobal->startupProc;
01572 
01573         SpinLockRelease(ProcStructLock);
01574     }
01575 
01576     if (proc == NULL)
01577         proc = BackendPidGetProc(pid);
01578 
01579     if (proc != NULL)
01580         PGSemaphoreUnlock(&proc->sem);
01581 }