Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jDhM9-0002pP-DS for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Mar 2020 04:28:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jDhM8-0004fE-69 for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Mar 2020 04:28:00 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1jDhM7-0004f7-TF for pgsql-hackers@lists.postgresql.org; Mon, 16 Mar 2020 04:27:59 +0000 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jDhM4-0007TS-Ta for pgsql-hackers@lists.postgresql.org; Mon, 16 Mar 2020 04:27:59 +0000 Received: by mail-qt1-x842.google.com with SMTP id f17so11780490qtq.6 for ; Sun, 15 Mar 2020 21:27:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :in-reply-to:user-agent; bh=keLFl3Y9VvKdr4No8PF/99Jr+1g7e2Ao468IktIx3Rc=; b=mYVgU7xdCt1OVTYQ4EKjCxEruUeHVhz3TafbfmbnDSSwN71tP20QAH2GUDjTvyzRHF qtu6/MF1jAVQDMhPhzl6diIVC9xTDw6MWrK9SL9JyNfkRluaeTkXsHYLy66vVu2YHEY4 s8lHQSAc+BZ57VynZ92DXJJJvbTiOtvn9lu9CYByMtlENYYRWY5JxhGiae57rd0OzkAi xAL2aysBve+pnHl0qdwJvoghVdoPxvG8RYMtv+GAikmg9Ow80VWOagAHxsa8f5/qICNU +mqSQKJ9gvVFJEIUWwE4oonpz/jMxBXXiCTUaZS04Ps+o8QDuiNACatzLbs6y56SYbPe nRQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:in-reply-to:user-agent; bh=keLFl3Y9VvKdr4No8PF/99Jr+1g7e2Ao468IktIx3Rc=; b=Zx//BDtFDYXkSVzFw6yYJbTpwVfeiXs7If8RM8EDBNpnTfOu8IlQ4bpAzqka5ghrAM sp33tYK/iNR8ph/n3LgooCPjcufmJb+7QSGQhQ4vnCTzHzSD9rjZnBhvQ3LKVVKIk357 QSwu/5ILRHNE+uQbRbzav9Ize7jMdkh5SYubT8DY5pyUVsFeBEcl3w++ypJaAG2n2nmK 6gbkm/iJYnW7qNNUiLqbJL1+XCVhBXm+SubtlEbN4bkFo7FLgGC4GVXJzr7uixr9Wx6T sPZ6L5nbjA2t/iBscwnz57upRg68oPUs4kgkWRMbK+26aeoBiTK+DZNG0rpkYJhnwXsw nYCA== X-Gm-Message-State: ANhLgQ3ubLKYorajGGm+Z+85qO7/Gyl8ImPs1Sk4Lm/WGQFxcgSj5wok scRejevfuv/lh3zRa89Xe7KrWw== X-Google-Smtp-Source: ADFU+vv8IA5ikL/MBuew1AKDi7Qe80Gv+1wij+DbnF9nmJbw8p4a/KIt1q8EYvwkQSoq0uiu8+/cTg== X-Received: by 2002:aed:2625:: with SMTP id z34mr17979848qtc.276.1584332875039; Sun, 15 Mar 2020 21:27:55 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id p65sm3664964qke.42.2020.03.15.21.27.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 Mar 2020 21:27:54 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id EEB31800F98; Sun, 15 Mar 2020 23:27:52 -0500 (CDT) Date: Sun, 15 Mar 2020 23:27:52 -0500 From: Justin Pryzby To: Julien Rouhaud , Michael Paquier Cc: Sergei Kornilov , Guillaume Lelarge , pgsql-hackers@lists.postgresql.org Subject: Re: Expose lock group leader pid in pg_stat_activity Message-ID: <20200316042752.GE26184@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200116074912.GA98418@paquier.xyz> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Jan 28, 2020 at 12:36:41PM +0100, Julien Rouhaud wrote: > So, AFAICT the LockHashPartitionLockByProc is required when > iterating/modifying lockGroupMembers or lockGroupLink, but just > getting the leader pid should be safe. This still seems unsafe: git show -U11 -w --patience b025f32e0b src/backend/utils/adt/pgstatfuncs.c + /* + * If a PGPROC entry was retrieved, display wait events and lock + * group leader information if any. To avoid extra overhead, no + * extra lock is being held, so there is no guarantee of + * consistency across multiple rows. + */ ... + PGPROC *leader; ... + leader = proc->lockGroupLeader; + if (leader) + { # does something guarantee that leader doesn't change ? + values[29] = Int32GetDatum(leader->pid); + nulls[29] = false; } Michael seems to have raised the issue: On Thu, Jan 16, 2020 at 04:49:12PM +0900, Michael Paquier wrote: > And actually, the way you are looking at the leader's PID is visibly > incorrect and inconsistent because the patch takes no shared LWLock on > the leader using LockHashPartitionLockByProc() followed by > LWLockAcquire(), no? That's incorrect because it could be perfectly > possible to crash with this code between the moment you check if > lockGroupLeader is NULL and the moment you look at > lockGroupLeader->pid if a process is being stopped in-between and > removes itself from a lock group in ProcKill(). But I don't see how it was addressed ? I read this: src/backend/storage/lmgr/lock.c: * completely valid. We cannot safely dereference another backend's src/backend/storage/lmgr/lock.c- * lockGroupLeader field without holding all lock partition locks, and src/backend/storage/lmgr/lock.c- * it's not worth that.) I think if you do: |LWLockAcquire(&proc->backendLock, LW_SHARED); ..then it's at least *safe* to access leader->pid, but it may be inconsistent unless you also call LockHashPartitionLockByProc. I wasn't able to produce a crash, so maybe I missed something. -- Justin