Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aXbWC-0008LL-Vv for pgsql-hackers@arkaria.postgresql.org; Sun, 21 Feb 2016 21:26:17 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aXbWC-0002fd-Bc for pgsql-hackers@arkaria.postgresql.org; Sun, 21 Feb 2016 21:26:16 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aXbWB-0002fQ-07 for pgsql-hackers@postgresql.org; Sun, 21 Feb 2016 21:26:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aXbW6-00054t-Vo for pgsql-hackers@postgresql.org; Sun, 21 Feb 2016 21:26:13 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u1LLQ6ce011104; Sun, 21 Feb 2016 16:26:06 -0500 From: Tom Lane To: Robert Haas cc: Andres Freund , "pgsql-hackers@postgresql.org" Subject: Re: Re: [COMMITTERS] pgsql: Add some isolation tests for deadlock detection and resolution. In-reply-to: <16182.1455721494@sss.pgh.pa.us> References: <21023.1455200996@sss.pgh.pa.us> <14581.1455205343@sss.pgh.pa.us> <20201.1455210279@sss.pgh.pa.us> <24444.1455251654@sss.pgh.pa.us> <1427.1455301014@sss.pgh.pa.us> <20160212232232.GA29473@awork2.anarazel.de> <1056.1455571766@sss.pgh.pa.us> <16182.1455721494@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 17 Feb 2016 10:04:54 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <11000.1456089805.0@sss.pgh.pa.us> Date: Sun, 21 Feb 2016 16:26:06 -0500 Message-ID: <11103.1456089966@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11000.1456089805.1@sss.pgh.pa.us> I wrote: > Robert Haas writes: >> As for the patch itself, I'm having trouble grokking what it's trying >> to do. I think it might be worth having a comment defining precisely >> what we mean by "A blocks B". I would define "A blocks B" in general >> as either A holds a lock which conflicts with one sought by B >> (hard-blocked) or A awaits a lock which conflicts with one sought by B >> and precedes it in the wait queue (soft-blocked). > Yes, that is exactly what I implemented ... and it's something you can't > find out from pg_locks. I'm not sure how that view could be made to > expose wait-queue ordering. Here's an updated version of this patch, now with user-facing docs. I decided that "pg_blocking_pids()" is a better function name than "pg_blocker_pids()". The code's otherwise the same, although I revisited some of the comments. I also changed quite a few references to "transaction" into "process" in the discussion of pg_locks. The previous choice to conflate processes with transactions was never terribly wise in my view, and it's certainly completely broken by parallel query. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="pg_blocking_pids_v2.patch"; charset="us-ascii" Content-ID: <11000.1456089805.2@sss.pgh.pa.us> Content-Description: pg_blocking_pids_v2.patch Content-Transfer-Encoding: quoted-printable diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index d77e999..d3270e4 100644 *** a/doc/src/sgml/catalogs.sgml --- b/doc/src/sgml/catalogs.sgml *************** *** 8015,8030 **** =20=20 The view pg_locks provides access to ! information about the locks held by open transactions within the database server. See for more discussion of locking. =20=20 pg_locks contains one row per active lockable ! object, requested lock mode, and relevant transaction. Thus, the same lockable object might ! appear many times, if multiple transactions are holding or waiting for locks on it. However, an object that currently has no locks on it will not appear at all. --- 8015,8030 ---- =20=20 The view pg_locks provides access to ! information about the locks held by active processes within the database server. See for more discussion of locking. =20=20 pg_locks contains one row per active lockable ! object, requested lock mode, and relevant process. Thus, the same lockable object might ! appear many times, if multiple processs are holding or waiting for locks on it. However, an object that currently has no locks on it will not appear at all. *************** *** 8200,8210 **** =20=20 granted is true in a row representing a lock ! held by the indicated transaction. False indicates that this transact= ion is ! currently waiting to acquire this lock, which implies that some other ! transaction is holding a conflicting lock mode on the same lockable ob= ject. ! The waiting transaction will sleep until the other lock is released (o= r a ! deadlock situation is detected). A single transaction can be waiting to acquire at most one lock at a time. =20=20 --- 8200,8210 ---- =20=20 granted is true in a row representing a lock ! held by the indicated process. False indicates that this process is ! currently waiting to acquire this lock, which implies that at least on= e other ! process is holding a conflicting lock mode on the same lockable object. ! The waiting process will sleep until the other lock is released (or a ! deadlock situation is detected). A single process can be waiting to acquire at most one lock at a time. =20=20 *************** *** 8224,8230 **** Although tuples are a lockable type of object, information about row-level locks is stored on disk, not in memory, and therefore row-level locks normally do not appear in this view. ! If a transaction is waiting for a row-level lock, it will usually appear in the view as waiting for the permanent transaction ID of the current holder of that row lock. --- 8224,8230 ---- Although tuples are a lockable type of object, information about row-level locks is stored on disk, not in memory, and therefore row-level locks normally do not appear in this view. ! If a process is waiting for a row-level lock, it will usually appear in the view as waiting for the permanent transaction ID of the current holder of that row lock. *************** SELECT * FROM pg_locks pl LEFT JOIN pg_p *** 8281,8286 **** --- 8281,8300 ---- =20=20 + While it is possible to obtain information about which processes block + which other processes by joining pg_locks aga= inst + itself, this is very difficult to get right in detail. Such a query w= ould + have to encode knowledge about which lock modes conflict with which + others. Worse, the pg_locks view does not ex= pose + information about which processes are ahead of which others in lock wa= it + queues, nor information about which processes are parallel workers run= ning + on behalf of which other client sessions. It is better to use + the pg_blocking_pids() function + (see ) to identify which + process(es) a waiting process is blocked behind. + +=20 + The pg_locks view displays data from both the regular lock manager and the predicate lock manager, which are separate systems; in addition, the regular lock manager subdivides its diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index b001ce5..2d9fe7f 100644 *** a/doc/src/sgml/func.sgml --- b/doc/src/sgml/func.sgml *************** SELECT * FROM pg_ls_dir('.') WITH ORDINA *** 14997,15002 **** --- 14997,15008 ---- =20=20 + pg_blocking_pids(int) + int[] + Process ID(s) that are blocking specified server process ID= + +=20 + pg_conf_load_time()= timestamp with time zone configuration load time *************** SET search_path TO schema =20=20 + pg_blocking_pids + +=20 + + pg_blocking_pids returns an array of the process= IDs + of the sessions that are blocking the server process with the specifi= ed + process ID, or an empty array if there is no such server process or i= t is + not blocked. One server process blocks another if it either holds a = lock + that conflicts with the blocked process's lock request (hard block), = or is + waiting for a lock that would conflict with the blocked process's lock + request and is ahead of it in the wait queue (soft block). When using + parallel queries the result always lists client-visible process IDs (= that + is, pg_backend_pid results) even if the actual lock is h= eld + or awaited by a child worker process. As a result of that, there may= be + duplicated PIDs in the result. Also note that when a prepared transa= ction + holds a conflicting lock, it will be represented by a zero process ID= in + the result of this function. + +=20 + pg_conf_load_time =20=20 diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/= procarray.c index 91218d0..97e8962 100644 *** a/src/backend/storage/ipc/procarray.c --- b/src/backend/storage/ipc/procarray.c *************** HaveVirtualXIDsDelayingChkpt(VirtualTran *** 2313,2318 **** --- 2313,2341 ---- PGPROC * BackendPidGetProc(int pid) { + PGPROC *result; +=20 + if (pid =3D=3D 0) /* never match dummy PGPROCs */ + return NULL; +=20 + LWLockAcquire(ProcArrayLock, LW_SHARED); +=20 + result =3D BackendPidGetProcWithLock(pid); +=20 + LWLockRelease(ProcArrayLock); +=20 + return result; + } +=20 + /* + * BackendPidGetProcWithLock -- get a backend's PGPROC given its PID + * + * Same as above, except caller must be holding ProcArrayLock. The found + * entry, if any, can be assumed to be valid as long as the lock remains = held. + */ + PGPROC * + BackendPidGetProcWithLock(int pid) + { PGPROC *result =3D NULL; ProcArrayStruct *arrayP =3D procArray; int index; *************** BackendPidGetProc(int pid) *** 2320,2327 **** if (pid =3D=3D 0) /* never match dummy PGPROCs */ return NULL; =20=20 - LWLockAcquire(ProcArrayLock, LW_SHARED); -=20 for (index =3D 0; index < arrayP->numProcs; index++) { PGPROC *proc =3D &allProcs[arrayP->pgprocnos[index]]; --- 2343,2348 ---- *************** BackendPidGetProc(int pid) *** 2333,2340 **** } } =20=20 - LWLockRelease(ProcArrayLock); -=20 return result; } =20=20 --- 2354,2359 ---- diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/loc= k.c index fef59a2..9eb4dfb 100644 *** a/src/backend/storage/lmgr/lock.c --- b/src/backend/storage/lmgr/lock.c *************** *** 21,27 **** * * Interface: * ! * InitLocks(), GetLocksMethodTable(), * LockAcquire(), LockRelease(), LockReleaseAll(), * LockCheckConflicts(), GrantLock() * --- 21,27 ---- * * Interface: * ! * InitLocks(), GetLocksMethodTable(), GetLockTagsMethodTable(), * LockAcquire(), LockRelease(), LockReleaseAll(), * LockCheckConflicts(), GrantLock() * *************** *** 41,46 **** --- 41,47 ---- #include "pg_trace.h" #include "pgstat.h" #include "storage/proc.h" + #include "storage/procarray.h" #include "storage/sinvaladt.h" #include "storage/spin.h" #include "storage/standby.h" *************** static void CleanUpLock(LOCK *lock, PROC *** 356,361 **** --- 357,364 ---- static void LockRefindAndRelease(LockMethod lockMethodTable, PGPROC *proc, LOCKTAG *locktag, LOCKMODE lockmode, bool decrement_strong_lock_count); + static void GetSingleProcBlockerStatusData(PGPROC *blocked_proc, + BlockedProcsData *data); =20=20 =20=20 /* *************** GetLocksMethodTable(const LOCK *lock) *** 462,467 **** --- 465,482 ---- return LockMethods[lockmethodid]; } =20=20 + /* + * Fetch the lock method table associated with a given locktag + */ + LockMethod + GetLockTagsMethodTable(const LOCKTAG *locktag) + { + LOCKMETHODID lockmethodid =3D (LOCKMETHODID) locktag->locktag_lockmethod= id; +=20 + Assert(0 < lockmethodid && lockmethodid < lengthof(LockMethods)); + return LockMethods[lockmethodid]; + } +=20 =20=20 /* * Compute the hash code associated with a LOCKTAG. *************** GetLockStatusData(void) *** 3406,3412 **** * impractical (in particular, note MAX_SIMUL_LWLOCKS). It shouldn't * matter too much, because none of these locks can be involved in lock * conflicts anyway - anything that might must be present in the main lo= ck ! * table. */ for (i =3D 0; i < ProcGlobal->allProcCount; ++i) { --- 3421,3430 ---- * impractical (in particular, note MAX_SIMUL_LWLOCKS). It shouldn't * matter too much, because none of these locks can be involved in lock * conflicts anyway - anything that might must be present in the main lo= ck ! * table. (For the same reason, we don't sweat about making leaderPid ! * completely valid. We cannot safely dereference another backend's ! * lockGroupLeader field without holding all lock partition locks, and ! * it's not worth that.) */ for (i =3D 0; i < ProcGlobal->allProcCount; ++i) { *************** GetLockStatusData(void) *** 3439,3444 **** --- 3457,3463 ---- instance->backend =3D proc->backendId; instance->lxid =3D proc->lxid; instance->pid =3D proc->pid; + instance->leaderPid =3D proc->pid; instance->fastpath =3D true; =20=20 el++; *************** GetLockStatusData(void) *** 3466,3471 **** --- 3485,3491 ---- instance->backend =3D proc->backendId; instance->lxid =3D proc->lxid; instance->pid =3D proc->pid; + instance->leaderPid =3D proc->pid; instance->fastpath =3D true; =20=20 el++; *************** GetLockStatusData(void) *** 3517,3522 **** --- 3537,3543 ---- instance->backend =3D proc->backendId; instance->lxid =3D proc->lxid; instance->pid =3D proc->pid; + instance->leaderPid =3D proclock->groupLeader->pid; instance->fastpath =3D false; =20=20 el++; *************** GetLockStatusData(void) *** 3538,3543 **** --- 3559,3755 ---- } =20=20 /* + * GetBlockerStatusData - Return a summary of the lock manager's state + * concerning locks that are blocking the specified PID or any member of + * the PID's lock group, for use in a user-level reporting function. + * + * For each PID within the lock group that is awaiting some heavyweight l= ock, + * the return data includes an array of LockInstanceData objects, which a= re + * the same data structure used by GetLockStatusData; but unlike that fun= ction, + * this one reports only the PROCLOCKs associated with the lock that that= pid + * is blocked on. (Hence, all the locktags should be the same for any one + * blocked PID.) In addition, we return an array of the PIDs of those ba= ckends + * that are ahead of the blocked PID in the lock's wait queue. These can= be + * compared with the pids in the LockInstanceData objects to determine wh= ich + * waiters are ahead of or behind the blocked PID in the queue. + * + * If blocked_pid isn't a valid backend PID or nothing in its lock group = is + * waiting on any heavyweight lock, return empty arrays. + * + * The design goal is to hold the LWLocks for as short a time as possible; + * thus, this function simply makes a copy of the necessary data and rele= ases + * the locks, allowing the caller to contemplate and format the data for = as + * long as it pleases. + */ + BlockedProcsData * + GetBlockerStatusData(int blocked_pid) + { + BlockedProcsData *data; + PGPROC *proc; + int i; +=20 + data =3D (BlockedProcsData *) palloc(sizeof(BlockedProcsData)); +=20 + /* + * Guess how much space we'll need, and preallocate. Most of the time + * this will avoid needing to do repalloc while holding the LWLocks. (We + * assume, but check with an Assert, that MaxBackends is enough entries + * for the procs[] array; the other two could need enlargement, though.) + */ + data->nprocs =3D data->nlocks =3D data->npids =3D 0; + data->maxprocs =3D data->maxlocks =3D data->maxpids =3D MaxBackends; + data->procs =3D (BlockedProcData *) palloc(sizeof(BlockedProcData) * dat= a->maxprocs); + data->locks =3D (LockInstanceData *) palloc(sizeof(LockInstanceData) * d= ata->maxlocks); + data->waiter_pids =3D (int *) palloc(sizeof(int) * data->maxpids); +=20 + /* + * In order to search the ProcArray for blocked_pid and assume that that + * entry won't immediately disappear under us, we must hold ProcArrayLoc= k. + * In addition, to examine the lock grouping fields of any other backend, + * we must hold all the hash partition locks. (Only one of those locks = is + * actually relevant for any one lock group, but we can't know which one + * ahead of time.) It's fairly annoying to hold all those locks + * throughout this, but it's no worse than GetLockStatusData(), and it + * does have the advantage that we're guaranteed to return a + * self-consistent instantaneous state. + */ + LWLockAcquire(ProcArrayLock, LW_SHARED); +=20 + proc =3D BackendPidGetProcWithLock(blocked_pid); +=20 + /* Nothing to do if it's gone */ + if (proc !=3D NULL) + { + /* + * Acquire lock on the entire shared lock data structure. See notes + * in GetLockStatusData(). + */ + for (i =3D 0; i < NUM_LOCK_PARTITIONS; i++) + LWLockAcquire(LockHashPartitionLockByIndex(i), LW_SHARED); +=20 + if (proc->lockGroupLeader =3D=3D NULL) + { + /* Easy case, proc is not a lock group member */ + GetSingleProcBlockerStatusData(proc, data); + } + else + { + /* Examine all procs in proc's lock group */ + dlist_iter iter; +=20 + dlist_foreach(iter, &proc->lockGroupLeader->lockGroupMembers) + { + PGPROC *memberProc; +=20 + memberProc =3D dlist_container(PGPROC, lockGroupLink, iter.cur); + GetSingleProcBlockerStatusData(memberProc, data); + } + } +=20 + /* + * And release locks. See notes in GetLockStatusData(). + */ + for (i =3D NUM_LOCK_PARTITIONS; --i >=3D 0;) + LWLockRelease(LockHashPartitionLockByIndex(i)); +=20 + Assert(data->nprocs <=3D data->maxprocs); + } +=20 + LWLockRelease(ProcArrayLock); +=20 + return data; + } +=20 + /* Accumulate data about one possibly-blocked proc for GetBlockerStatusDa= ta */ + static void + GetSingleProcBlockerStatusData(PGPROC *blocked_proc, BlockedProcsData *da= ta) + { + LOCK *theLock =3D blocked_proc->waitLock; + BlockedProcData *bproc; + SHM_QUEUE *procLocks; + PROCLOCK *proclock; + PROC_QUEUE *waitQueue; + PGPROC *proc; + int queue_size; + int i; +=20 + /* Nothing to do if this proc is not blocked */ + if (theLock =3D=3D NULL) + return; +=20 + /* Set up a procs[] element */ + bproc =3D &data->procs[data->nprocs++]; + bproc->pid =3D blocked_proc->pid; + bproc->first_lock =3D data->nlocks; + bproc->first_waiter =3D data->npids; +=20 + /* + * We may ignore the proc's fast-path arrays, since nothing in those cou= ld + * be related to a contended lock. + */ +=20 + /* Collect all PROCLOCKs associated with theLock */ + procLocks =3D &(theLock->procLocks); + proclock =3D (PROCLOCK *) SHMQueueNext(procLocks, procLocks, + offsetof(PROCLOCK, lockLink)); + while (proclock) + { + PGPROC *proc =3D proclock->tag.myProc; + LOCK *lock =3D proclock->tag.myLock; + LockInstanceData *instance; +=20 + if (data->nlocks >=3D data->maxlocks) + { + data->maxlocks +=3D MaxBackends; + data->locks =3D (LockInstanceData *) + repalloc(data->locks, sizeof(LockInstanceData) * data->maxlocks); + } +=20 + instance =3D &data->locks[data->nlocks]; + memcpy(&instance->locktag, &lock->tag, sizeof(LOCKTAG)); + instance->holdMask =3D proclock->holdMask; + if (proc->waitLock =3D=3D lock) + instance->waitLockMode =3D proc->waitLockMode; + else + instance->waitLockMode =3D NoLock; + instance->backend =3D proc->backendId; + instance->lxid =3D proc->lxid; + instance->pid =3D proc->pid; + instance->leaderPid =3D proclock->groupLeader->pid; + instance->fastpath =3D false; + data->nlocks++; +=20 + proclock =3D (PROCLOCK *) SHMQueueNext(procLocks, &proclock->lockLink, + offsetof(PROCLOCK, lockLink)); + } +=20 + /* Enlarge waiter_pids[] if it's too small to hold all wait queue PIDs */ + waitQueue =3D &(theLock->waitProcs); + queue_size =3D waitQueue->size; +=20 + if (queue_size > data->maxpids - data->npids) + { + data->maxpids =3D Max(data->maxpids + MaxBackends, + data->npids + queue_size); + data->waiter_pids =3D (int *) repalloc(data->waiter_pids, + sizeof(int) * data->maxpids); + } +=20 + /* Collect PIDs from the lock's wait queue, stopping at blocked_proc */ + proc =3D (PGPROC *) waitQueue->links.next; + for (i =3D 0; i < queue_size; i++) + { + if (proc =3D=3D blocked_proc) + break; + data->waiter_pids[data->npids++] =3D proc->pid; + proc =3D (PGPROC *) proc->links.next; + } +=20 + bproc->num_locks =3D data->nlocks - bproc->first_lock; + bproc->num_waiters =3D data->npids - bproc->first_waiter; + } +=20 + /* * Returns a list of currently held AccessExclusiveLocks, for use by * LogStandbySnapshot(). The result is a palloc'd array, * with the number of elements returned into *nlocks. diff --git a/src/backend/utils/adt/lockfuncs.c b/src/backend/utils/adt/lock= funcs.c index 73c78e9..6bcab81 100644 *** a/src/backend/utils/adt/lockfuncs.c --- b/src/backend/utils/adt/lockfuncs.c *************** *** 18,23 **** --- 18,24 ---- #include "funcapi.h" #include "miscadmin.h" #include "storage/predicate_internals.h" + #include "utils/array.h" #include "utils/builtins.h" =20=20 =20=20 *************** pg_lock_status(PG_FUNCTION_ARGS) *** 99,105 **** oldcontext =3D MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); =20=20 /* build tupdesc for result tuples */ ! /* this had better match pg_locks view in system_views.sql */ tupdesc =3D CreateTemplateTupleDesc(NUM_LOCK_STATUS_COLUMNS, false); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "locktype", TEXTOID, -1, 0); --- 100,106 ---- oldcontext =3D MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); =20=20 /* build tupdesc for result tuples */ ! /* this had better match function's declaration in pg_proc.h */ tupdesc =3D CreateTemplateTupleDesc(NUM_LOCK_STATUS_COLUMNS, false); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "locktype", TEXTOID, -1, 0); *************** pg_lock_status(PG_FUNCTION_ARGS) *** 395,400 **** --- 396,523 ---- =20=20 =20=20 /* + * pg_blocking_pids - produce an array of the PIDs blocking given PID + * + * The reported PIDs are those that hold a lock conflicting with blocked_= pid's + * current request (hard block), or are requesting such a lock and are ah= ead + * of blocked_pid in the lock's wait queue (soft block). + * + * In parallel-query cases, we report all PIDs blocking any member of the + * given PID's lock group, and the reported PIDs are those of the blocking + * PIDs' lock group leaders. This allows callers to compare the result to + * lists of clients' pg_backend_pid() results even during a parallel quer= y. + * + * Parallel query makes it possible for there to be duplicate PIDs in the + * result (either because multiple waiters are blocked by same PID, or + * because multiple blockers have same group leader PID). We do not both= er + * to eliminate such duplicates from the result. + * + * We need not consider predicate locks here, since those don't block any= thing. + */ + Datum + pg_blocking_pids(PG_FUNCTION_ARGS) + { + int blocked_pid =3D PG_GETARG_INT32(0); + Datum *arrayelems; + int narrayelems; + BlockedProcsData *lockData; /* state data from lmgr */ + int i, + j; +=20 + /* Collect a snapshot of lock manager state */ + lockData =3D GetBlockerStatusData(blocked_pid); +=20 + /* We can't need more output entries than there are reported PROCLOCKs */ + arrayelems =3D (Datum *) palloc(lockData->nlocks * sizeof(Datum)); + narrayelems =3D 0; +=20 + /* For each blocked proc in the lock group ... */ + for (i =3D 0; i < lockData->nprocs; i++) + { + BlockedProcData *bproc =3D &lockData->procs[i]; + LockInstanceData *instances =3D &lockData->locks[bproc->first_lock]; + int *preceding_waiters =3D &lockData->waiter_pids[bproc->first_wait= er]; + LockInstanceData *blocked_instance; + LockMethod lockMethodTable; + int conflictMask; +=20 + /* + * Locate the blocked proc's own entry in the LockInstanceData array. + * There should be exactly one matching entry. + */ + blocked_instance =3D NULL; + for (j =3D 0; j < bproc->num_locks; j++) + { + LockInstanceData *instance =3D &(instances[j]); +=20 + if (instance->pid =3D=3D bproc->pid) + { + Assert(blocked_instance =3D=3D NULL); + blocked_instance =3D instance; + } + } + Assert(blocked_instance !=3D NULL); +=20 + lockMethodTable =3D GetLockTagsMethodTable(&(blocked_instance->locktag)= ); + conflictMask =3D lockMethodTable->conflictTab[blocked_instance->waitLoc= kMode]; +=20 + /* Now scan the PROCLOCK data for conflicting procs */ + for (j =3D 0; j < bproc->num_locks; j++) + { + LockInstanceData *instance =3D &(instances[j]); +=20 + /* A proc never blocks itself, so ignore that entry */ + if (instance =3D=3D blocked_instance) + continue; + /* Members of same lock group never block each other, either */ + if (instance->leaderPid =3D=3D blocked_instance->leaderPid) + continue; +=20 + if (conflictMask & instance->holdMask) + { + /* hard block: blocked by lock already held by this entry */ + } + else if (instance->waitLockMode !=3D NoLock && + (conflictMask & LOCKBIT_ON(instance->waitLockMode))) + { + /* conflict in lock requests; who's in front in wait queue? */ + bool ahead =3D false; + int k; +=20 + for (k =3D 0; k < bproc->num_waiters; k++) + { + if (preceding_waiters[k] =3D=3D instance->pid) + { + /* soft block: this entry is ahead of blocked proc */ + ahead =3D true; + break; + } + } + if (!ahead) + continue; /* not blocked by this entry */ + } + else + { + /* not blocked by this entry */ + continue; + } +=20 + /* blocked by this entry, so emit a record */ + arrayelems[narrayelems++] =3D Int32GetDatum(instance->leaderPid); + } + } +=20 + /* Assert we didn't overrun arrayelems[] */ + Assert(narrayelems <=3D lockData->nlocks); +=20 + /* Construct array, using hardwired knowledge about int4 type */ + PG_RETURN_ARRAYTYPE_P(construct_array(arrayelems, narrayelems, + INT4OID, + sizeof(int32), true, 'i')); + } +=20 +=20 + /* * Functions for manipulating advisory locks * * We make use of the locktag fields as follows: diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index 59c50d9..62b9125 100644 *** a/src/include/catalog/pg_proc.h --- b/src/include/catalog/pg_proc.h *************** DATA(insert OID =3D 3329 ( pg_show_all_fi *** 3012,3017 **** --- 3012,3019 ---- DESCR("show config file settings"); DATA(insert OID =3D 1371 ( pg_lock_status PGNSP PGUID 12 1 1000 0 0 f = f f f t t v s 0 0 2249 "" "{25,26,26,23,21,25,28,26,26,21,25,23,25,16,16}" = "{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}" "{locktype,database,relation,page,tuple,v= irtualxid,transactionid,classid,objid,objsubid,virtualtransaction,pid,mode,= granted,fastpath}" _null_ _null_ pg_lock_status _null_ _null_ _null_ )); DESCR("view system lock information"); + DATA(insert OID =3D 2561 ( pg_blocking_pids PGNSP PGUID 12 1 0 0 0 f f f= f t f v s 1 0 1007 "23" _null_ _null_ _null_ _null_ _null_ pg_blocking_pid= s _null_ _null_ _null_ )); + DESCR("get array of PIDs of sessions blocking specified backend PID"); DATA(insert OID =3D 1065 ( pg_prepared_xact PGNSP PGUID 12 1 1000 0 0 f = f f f t t v s 0 0 2249 "" "{28,25,1184,26,26}" "{o,o,o,o,o}" "{transaction,= gid,prepared,ownerid,dbid}" _null_ _null_ pg_prepared_xact _null_ _null_ _n= ull_ )); DESCR("view two-phase transactions"); DATA(insert OID =3D 3819 ( pg_get_multixact_members PGNSP PGUID 12 1 100= 0 0 0 f f f f t t v s 1 0 2249 "28" "{28,28,25}" "{i,o,o}" "{multixid,xid,m= ode}" _null_ _null_ pg_get_multixact_members _null_ _null_ _null_ )); diff --git a/src/include/storage/lock.h b/src/include/storage/lock.h index 703eaf2..788d50a 100644 *** a/src/include/storage/lock.h --- b/src/include/storage/lock.h *************** typedef struct PROCLOCK *** 346,352 **** PROCLOCKTAG tag; /* unique identifier of proclock object */ =20=20 /* data */ ! PGPROC *groupLeader; /* group leader, or NULL if no lock group */ LOCKMASK holdMask; /* bitmask for lock types currently held */ LOCKMASK releaseMask; /* bitmask for lock types to be released */ SHM_QUEUE lockLink; /* list link in LOCK's list of proclocks */ --- 346,352 ---- PROCLOCKTAG tag; /* unique identifier of proclock object */ =20=20 /* data */ ! PGPROC *groupLeader; /* proc's lock group leader, or proc itself */ LOCKMASK holdMask; /* bitmask for lock types currently held */ LOCKMASK releaseMask; /* bitmask for lock types to be released */ SHM_QUEUE lockLink; /* list link in LOCK's list of proclocks */ *************** typedef struct LOCALLOCK *** 423,443 **** =20=20 typedef struct LockInstanceData { ! LOCKTAG locktag; /* locked object */ LOCKMASK holdMask; /* locks held by this PGPROC */ LOCKMODE waitLockMode; /* lock awaited by this PGPROC, if any */ BackendId backend; /* backend ID of this PGPROC */ LocalTransactionId lxid; /* local transaction ID of this PGPROC */ int pid; /* pid of this PGPROC */ bool fastpath; /* taken via fastpath? */ } LockInstanceData; =20=20 typedef struct LockData { int nelements; /* The length of the array */ ! LockInstanceData *locks; } LockData; =20=20 =20=20 /* Result codes for LockAcquire() */ typedef enum --- 423,470 ---- =20=20 typedef struct LockInstanceData { ! LOCKTAG locktag; /* tag for locked object */ LOCKMASK holdMask; /* locks held by this PGPROC */ LOCKMODE waitLockMode; /* lock awaited by this PGPROC, if any */ BackendId backend; /* backend ID of this PGPROC */ LocalTransactionId lxid; /* local transaction ID of this PGPROC */ int pid; /* pid of this PGPROC */ + int leaderPid; /* pid of group leader; =3D pid if no group */ bool fastpath; /* taken via fastpath? */ } LockInstanceData; =20=20 typedef struct LockData { int nelements; /* The length of the array */ ! LockInstanceData *locks; /* Array of per-PROCLOCK information */ } LockData; =20=20 + typedef struct BlockedProcData + { + int pid; /* pid of a blocked PGPROC */ + /* Per-PROCLOCK information about PROCLOCKs of the lock the pid awaits */ + /* (these fields refer to indexes in BlockedProcsData.locks[]) */ + int first_lock; /* index of first relevant LockInstanceData */ + int num_locks; /* number of relevant LockInstanceDatas */ + /* PIDs of PGPROCs that are ahead of "pid" in the lock's wait queue */ + /* (these fields refer to indexes in BlockedProcsData.waiter_pids[]) */ + int first_waiter; /* index of first preceding waiter */ + int num_waiters; /* number of preceding waiters */ + } BlockedProcData; +=20 + typedef struct BlockedProcsData + { + BlockedProcData *procs; /* Array of per-blocked-proc information */ + LockInstanceData *locks; /* Array of per-PROCLOCK information */ + int *waiter_pids; /* Array of PIDs of other blocked PGPROCs */ + int nprocs; /* # of valid entries in procs[] array */ + int maxprocs; /* Allocated length of procs[] array */ + int nlocks; /* # of valid entries in locks[] array */ + int maxlocks; /* Allocated length of locks[] array */ + int npids; /* # of valid entries in waiter_pids[] array */ + int maxpids; /* Allocated length of waiter_pids[] array */ + } BlockedProcsData; +=20 =20=20 /* Result codes for LockAcquire() */ typedef enum *************** typedef enum *** 489,494 **** --- 516,522 ---- */ extern void InitLocks(void); extern LockMethod GetLocksMethodTable(const LOCK *lock); + extern LockMethod GetLockTagsMethodTable(const LOCKTAG *locktag); extern uint32 LockTagHashCode(const LOCKTAG *locktag); extern bool DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2); extern LockAcquireResult LockAcquire(const LOCKTAG *locktag, *************** extern void GrantAwaitedLock(void); *** 521,526 **** --- 549,555 ---- extern void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode); extern Size LockShmemSize(void); extern LockData *GetLockStatusData(void); + extern BlockedProcsData *GetBlockerStatusData(int blocked_pid); =20=20 extern xl_standby_lock *GetRunningTransactionLocks(int *nlocks); extern const char *GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mo= de); diff --git a/src/include/storage/procarray.h b/src/include/storage/procarra= y.h index 1fbf4f3..dd37c0c 100644 *** a/src/include/storage/procarray.h --- b/src/include/storage/procarray.h *************** extern VirtualTransactionId *GetVirtualX *** 61,66 **** --- 61,67 ---- extern bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int= nvxids); =20=20 extern PGPROC *BackendPidGetProc(int pid); + extern PGPROC *BackendPidGetProcWithLock(int pid); extern int BackendXidGetPid(TransactionId xid); extern bool IsBackendPid(int pid); =20=20 diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index 94c1881..7ec93c9 100644 *** a/src/include/utils/builtins.h --- b/src/include/utils/builtins.h *************** extern Datum row_security_active_name(PG *** 1157,1162 **** --- 1157,1163 ---- =20=20 /* lockfuncs.c */ extern Datum pg_lock_status(PG_FUNCTION_ARGS); + extern Datum pg_blocking_pids(PG_FUNCTION_ARGS); extern Datum pg_advisory_lock_int8(PG_FUNCTION_ARGS); extern Datum pg_advisory_xact_lock_int8(PG_FUNCTION_ARGS); extern Datum pg_advisory_lock_shared_int8(PG_FUNCTION_ARGS); diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isol= ationtester.c index 0a9d25c..6461ae8 100644 *** a/src/test/isolation/isolationtester.c --- b/src/test/isolation/isolationtester.c *************** main(int argc, char **argv) *** 227,253 **** */ initPQExpBuffer(&wait_query); appendPQExpBufferStr(&wait_query, ! "SELECT 1 FROM pg_locks holder, pg_locks waiter " ! "WHERE NOT waiter.granted AND waiter.pid =3D $1 " ! "AND holder.granted " ! "AND holder.pid <> $1 AND holder.pid IN ("); /* The spec syntax requires at least one session; assume that here. */ appendPQExpBufferStr(&wait_query, backend_pids[1]); for (i =3D 2; i < nconns; i++) ! appendPQExpBuffer(&wait_query, ", %s", backend_pids[i]); ! appendPQExpBufferStr(&wait_query, ! ") " !=20 ! "AND holder.locktype IS NOT DISTINCT FROM waiter.locktype " ! "AND holder.database IS NOT DISTINCT FROM waiter.database " ! "AND holder.relation IS NOT DISTINCT FROM waiter.relation " ! "AND holder.page IS NOT DISTINCT FROM waiter.page " ! "AND holder.tuple IS NOT DISTINCT FROM waiter.tuple " ! "AND holder.virtualxid IS NOT DISTINCT FROM waiter.virtualxid " ! "AND holder.transactionid IS NOT DISTINCT FROM waiter.transactionid " ! "AND holder.classid IS NOT DISTINCT FROM waiter.classid " ! "AND holder.objid IS NOT DISTINCT FROM waiter.objid " ! "AND holder.objsubid IS NOT DISTINCT FROM waiter.objsubid "); =20=20 res =3D PQprepare(conns[0], PREP_WAITING, wait_query.data, 0, NULL); if (PQresultStatus(res) !=3D PGRES_COMMAND_OK) --- 227,238 ---- */ initPQExpBuffer(&wait_query); appendPQExpBufferStr(&wait_query, ! "SELECT pg_catalog.pg_blocking_pids($1) && '{"); /* The spec syntax requires at least one session; assume that here. */ appendPQExpBufferStr(&wait_query, backend_pids[1]); for (i =3D 2; i < nconns; i++) ! appendPQExpBuffer(&wait_query, ",%s", backend_pids[i]); ! appendPQExpBufferStr(&wait_query, "}'::integer[]"); =20=20 res =3D PQprepare(conns[0], PREP_WAITING, wait_query.data, 0, NULL); if (PQresultStatus(res) !=3D PGRES_COMMAND_OK) *************** try_complete_step(Step *step, int flags) *** 745,765 **** /* If it's OK for the step to block, check whether it has. */ if (flags & STEP_NONBLOCK) { ! int ntuples; =20=20 res =3D PQexecPrepared(conns[0], PREP_WAITING, 1, &backend_pids[step->session + 1], NULL, NULL, 0); ! if (PQresultStatus(res) !=3D PGRES_TUPLES_OK) { fprintf(stderr, "lock wait query failed: %s", PQerrorMessage(conn)); exit_nicely(); } ! ntuples =3D PQntuples(res); PQclear(res); =20=20 ! if (ntuples >=3D 1) /* waiting to acquire a lock */ { if (!(flags & STEP_RETRY)) printf("step %s: %s \n", --- 730,751 ---- /* If it's OK for the step to block, check whether it has. */ if (flags & STEP_NONBLOCK) { ! bool waiting; =20=20 res =3D PQexecPrepared(conns[0], PREP_WAITING, 1, &backend_pids[step->session + 1], NULL, NULL, 0); ! if (PQresultStatus(res) !=3D PGRES_TUPLES_OK || ! PQntuples(res) !=3D 1) { fprintf(stderr, "lock wait query failed: %s", PQerrorMessage(conn)); exit_nicely(); } ! waiting =3D ((PQgetvalue(res, 0, 0))[0] =3D=3D 't'); PQclear(res); =20=20 ! if (waiting) /* waiting to acquire a lock */ { if (!(flags & STEP_RETRY)) printf("step %s: %s \n", ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--