Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aVm1r-0002Cr-Ut for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Feb 2016 20:15:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aVm1r-0003Sq-Fu for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Feb 2016 20:15:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aVm1q-0003Sk-RQ for pgsql-hackers@postgresql.org; Tue, 16 Feb 2016 20:15:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aVm1n-0006Ex-EZ for pgsql-hackers@postgresql.org; Tue, 16 Feb 2016 20:15:22 +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 u1GKFEJE012073; Tue, 16 Feb 2016 15:15:15 -0500 From: Tom Lane To: Robert Haas cc: "pgsql-hackers@postgresql.org" Subject: Re: Re: [COMMITTERS] pgsql: Introduce group locking to prevent parallel processes from deadl In-reply-to: References: <27041.1455388321@sss.pgh.pa.us> <22842.1455474783@sss.pgh.pa.us> <30320.1455488783@sss.pgh.pa.us> Comments: In-reply-to Robert Haas message dated "Mon, 15 Feb 2016 19:41:28 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <12044.1455653657.0@sss.pgh.pa.us> Date: Tue, 16 Feb 2016 15:15:14 -0500 Message-ID: <12072.1455653714@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: <12044.1455653657.1@sss.pgh.pa.us> Robert Haas writes: > Yeah, you're right. Attached is a draft patch that tries to clean up > that and a bunch of other things that you raised. I reviewed this patch. I don't have any particular comment on the changes in deadlock.c; I haven't studied the code closely enough to know if those are right. I did think that the commentary elsewhere could be improved some more, so attached is a delta patch on top of yours that shows what I suggest. I've not done anything here about removing lockGroupLeaderIdentifier, although I will be happy to send a patch for that unless you know of some reason I missed why it's necessary. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="group-locking-comment-improvements.patch"; charset="us-ascii" Content-ID: <12044.1455653657.2@sss.pgh.pa.us> Content-Description: group-locking-comment-improvements.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/storage/lmgr/README b/src/backend/storage/lmgr/REA= DME index 8eaa91c..5a62c8f 100644 *** a/src/backend/storage/lmgr/README --- b/src/backend/storage/lmgr/README *************** acquire an AccessShareLock while the oth *** 603,609 **** This might seem dangerous and could be in some cases (more on that below)= , but if we didn't do this then parallel query would be extremely prone to self-deadlock. For example, a parallel query against a relation on which= the ! leader had already AccessExclusiveLock would hang, because the workers wo= uld try to lock the same relation and be blocked by the leader; yet the leader can't finish until it receives completion indications from all workers. = An undetected deadlock results. This is far from the only scenario where su= ch a --- 603,609 ---- This might seem dangerous and could be in some cases (more on that below)= , but if we didn't do this then parallel query would be extremely prone to self-deadlock. For example, a parallel query against a relation on which= the ! leader already had AccessExclusiveLock would hang, because the workers wo= uld try to lock the same relation and be blocked by the leader; yet the leader can't finish until it receives completion indications from all workers. = An undetected deadlock results. This is far from the only scenario where su= ch a *************** quickly enough for this interlock to fai *** 664,690 **** =20=20 A PGPROC's lockGroupLeader is NULL for processes not involved in parallel query. When a process wants to cooperate with parallel workers, it become= s a ! lock group leader, which means setting this field back to point to its own PGPROC. When a parallel worker starts up, it points this field at the lea= der, with the above-mentioned interlock. The lockGroupMembers field is only us= ed in ! the leader; it is a list of the workers. The lockGroupLink field is used = to ! link the leader and all workers into the leader's list. All of these fiel= ds ! are protected by the lock manager locks; the lock manager lock that prote= cts ! the lockGroupLeaderIdentifier, lockGroupLeader, and lockGroupMembers fiel= ds in ! a given PGPROC is chosen by taking pgprocno modulo the number of lock man= ager ! partitions. This unusual arrangement has a major advantage: the deadlock ! detector can count on the fact that no lockGroupLeader field can change w= hile ! the deadlock detector is running, because it knows that it holds all the = lock ! manager locks. A PGPROC's lockGroupLink is protected by the lock manager ! partition lock for the group of which it is a part. If it's not part of = any ! group, this field is unused and can only be examined or modified by the ! process that owns the PGPROC. =20=20 We institute a further coding rule that a process cannot join or leave a = lock group while owning any PROCLOCK. Therefore, given a lock manager lock ! sufficient to examine PROCLOCK *proclock, it also safe to examine ! proclock->tag.myProc->lockGroupLeader (but not the other fields mentioned= in ! the previous paragraph). =20=20 User Locks (Advisory Locks) --------------------------- --- 664,689 ---- =20=20 A PGPROC's lockGroupLeader is NULL for processes not involved in parallel query. When a process wants to cooperate with parallel workers, it become= s a ! lock group leader, which means setting this field to point to its own PGPROC. When a parallel worker starts up, it points this field at the lea= der, with the above-mentioned interlock. The lockGroupMembers field is only us= ed in ! the leader; it is a list of the member PGPROCs of the lock group (the lea= der ! and all workers). The lockGroupLink field is the list link for this list. !=20 ! All four of these fields are considered to be protected by a lock manager ! partition lock. The partition lock that protects these fields within a g= iven ! lock group is chosen by taking the leader's pgprocno modulo the number of= lock ! manager partitions. This unusual arrangement has a major advantage: the ! deadlock detector can count on the fact that no lockGroupLeader field can ! change while the deadlock detector is running, because it knows that it h= olds ! all the lock manager locks. Also, holding this single lock allows safe ! manipulation of the lockGroupMembers list for the lock group. =20=20 We institute a further coding rule that a process cannot join or leave a = lock group while owning any PROCLOCK. Therefore, given a lock manager lock ! sufficient to examine a PROCLOCK *proclock, it is also safe to examine ! proclock->tag.myProc->lockGroupLeader (but not the other lock-group-relat= ed ! fields of the PGPROC). =20=20 User Locks (Advisory Locks) --------------------------- diff --git a/src/backend/storage/lmgr/deadlock.c b/src/backend/storage/lmgr= /deadlock.c index d81746d..21f92dd 100644 *** a/src/backend/storage/lmgr/deadlock.c --- b/src/backend/storage/lmgr/deadlock.c *************** *** 40,47 **** * is, it will be the leader rather than any other member of the lock gro= up. * The group leaders act as representatives of the whole group even though * those particular processes need not be waiting at all. There will be = at ! * least one member of the group on the wait queue for the given lock, ma= ybe ! * more. */ typedef struct { --- 40,47 ---- * is, it will be the leader rather than any other member of the lock gro= up. * The group leaders act as representatives of the whole group even though * those particular processes need not be waiting at all. There will be = at ! * least one member of the waiter's lock group on the wait queue for the = given ! * lock, maybe more. */ typedef struct { diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/pro= c.c index cff8c08..deb88c7 100644 *** a/src/backend/storage/lmgr/proc.c --- b/src/backend/storage/lmgr/proc.c *************** ProcKill(int code, Datum arg) *** 826,835 **** leader->lockGroupLeader =3D NULL; if (leader !=3D MyProc) { - procgloballist =3D leader->procgloballist; -=20 /* Leader exited first; return its PGPROC. */ SpinLockAcquire(ProcStructLock); leader->links.next =3D (SHM_QUEUE *) *procgloballist; *procgloballist =3D leader; SpinLockRelease(ProcStructLock); --- 826,834 ---- leader->lockGroupLeader =3D NULL; if (leader !=3D MyProc) { /* Leader exited first; return its PGPROC. */ SpinLockAcquire(ProcStructLock); + procgloballist =3D leader->procgloballist; leader->links.next =3D (SHM_QUEUE *) *procgloballist; *procgloballist =3D leader; SpinLockRelease(ProcStructLock); *************** ProcSleep(LOCALLOCK *locallock, LockMeth *** 1004,1010 **** int i; =20=20 /* ! * If group locking is in use, locks held my members of my locking group * need to be included in myHeldLocks. */ if (leader !=3D NULL) --- 1003,1009 ---- int i; =20=20 /* ! * If group locking is in use, locks held by members of my locking group * need to be included in myHeldLocks. */ if (leader !=3D NULL) *************** BecomeLockGroupMember(PGPROC *leader, in *** 1802,1810 **** /* PID must be valid. */ Assert(pid !=3D 0); =20=20 ! /* Try to join the group. */ leader_lwlock =3D LockHashPartitionLockByProc(leader); LWLockAcquire(leader_lwlock, LW_EXCLUSIVE); if (leader->lockGroupLeaderIdentifier =3D=3D pid) { ok =3D true; --- 1801,1817 ---- /* PID must be valid. */ Assert(pid !=3D 0); =20=20 ! /* ! * Get lock protecting the group fields. Note LockHashPartitionLockByPr= oc ! * accesses leader->pgprocno in a PGPROC that might be free. This is sa= fe ! * because all PGPROCs' pgprocno fields are set during shared memory ! * initialization and never change thereafter; so we will acquire the ! * correct lock even if the leader PGPROC is in process of being recycle= d. ! */ leader_lwlock =3D LockHashPartitionLockByProc(leader); LWLockAcquire(leader_lwlock, LW_EXCLUSIVE); +=20 + /* Try to join the group */ if (leader->lockGroupLeaderIdentifier =3D=3D pid) { ok =3D true; diff --git a/src/include/storage/lock.h b/src/include/storage/lock.h index a3e19e1..2053bfe 100644 *** a/src/include/storage/lock.h --- b/src/include/storage/lock.h *************** typedef enum *** 477,486 **** * by one of the lock hash partition locks. Since the deadlock detector * acquires all such locks anyway, this makes it safe for it to access th= ese * fields without doing anything extra. To avoid contention as much as ! * possible, we map different PGPROCs to different partition locks. */ ! #define LockHashPartitionLockByProc(p) \ ! LockHashPartitionLock((p)->pgprocno) =20=20 /* * function prototypes --- 477,487 ---- * by one of the lock hash partition locks. Since the deadlock detector * acquires all such locks anyway, this makes it safe for it to access th= ese * fields without doing anything extra. To avoid contention as much as ! * possible, we map different PGPROCs to different partition locks. The = lock ! * used for a given lock group is determined by the group leader's pgproc= no. */ ! #define LockHashPartitionLockByProc(leader_pgproc) \ ! LockHashPartitionLock((leader_pgproc)->pgprocno) =20=20 /* * function prototypes diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index 81bddbb..a9405ce 100644 *** a/src/include/storage/proc.h --- b/src/include/storage/proc.h *************** struct PGPROC *** 152,158 **** */ TransactionId procArrayGroupMemberXid; =20=20 ! /* Per-backend LWLock. Protects fields below. */ LWLock backendLock; =20=20 /* Lock manager data, recording fast-path locks taken by this backend. */ --- 152,158 ---- */ TransactionId procArrayGroupMemberXid; =20=20 ! /* Per-backend LWLock. Protects fields below (but not group fields). */ LWLock backendLock; =20=20 /* Lock manager data, recording fast-path locks taken by this backend. */ *************** struct PGPROC *** 163,173 **** * lock */ =20=20 /* ! * Support for lock groups. Use LockHashPartitionLockByProc to get the ! * LWLock protecting these fields. */ int lockGroupLeaderIdentifier; /* MyProcPid, if I'm a leader */ ! PGPROC *lockGroupLeader; /* lock group leader, if I'm a follower */ dlist_head lockGroupMembers; /* list of members, if I'm a leader */ dlist_node lockGroupLink; /* my member link, if I'm a member */ }; --- 163,173 ---- * lock */ =20=20 /* ! * Support for lock groups. Use LockHashPartitionLockByProc on the group ! * leader to get the LWLock protecting these fields. */ int lockGroupLeaderIdentifier; /* MyProcPid, if I'm a leader */ ! PGPROC *lockGroupLeader; /* lock group leader, if I'm a member */ dlist_head lockGroupMembers; /* list of members, if I'm a leader */ dlist_node lockGroupLink; /* my member link, if I'm a member */ }; ------- =_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--