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.89) (envelope-from ) id 1iwQcI-0002ue-VU for pgsql-hackers@arkaria.postgresql.org; Tue, 28 Jan 2020 13:09:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iwQcH-0007w7-PU for pgsql-hackers@arkaria.postgresql.org; Tue, 28 Jan 2020 13:09:17 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iwQcH-0007uw-86 for pgsql-hackers@lists.postgresql.org; Tue, 28 Jan 2020 13:09:17 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iwQcE-0000TS-Gi for pgsql-hackers@lists.postgresql.org; Tue, 28 Jan 2020 13:09:16 +0000 Received: by mail-wm1-x343.google.com with SMTP id f129so2473629wmf.2 for ; Tue, 28 Jan 2020 05:09:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=en/lQ88g6rsUplZZLKj8WxeMZl6BrwUmfGHpyIIackk=; b=ePVp6JeFUT+1yLqRDwKwRG6ezNa/JMG7V1EJCXyCs0vh3NnwCJmq6iI/GAeu002+ZO oXfF9cU3KJ4ETu7wwwe2rrirqi2f7aE6NAPhCCYGkKfspokAt5j1y7jJwmEl0rtWgIY6 HeNL5HpVK+vGpC0uY4dkTY1phMF+FZUiQkHeBQ9/kKHVirHzGKDdzmb6W1enIudaDOBE stHiPmrfVRBGomW/rBV8mncyfHxQMU+y4bS6SxDl9czgVidM+WCqaeV1ed4A81OZsJZl Hq6mO92lAgRXLdo+AT5y/vEWQnXoC3FIiSdmWu3+b5GWbbKyjfO5MlVJqIJ4AX5yiaq+ QEmw== 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:references :mime-version:content-disposition:in-reply-to; bh=en/lQ88g6rsUplZZLKj8WxeMZl6BrwUmfGHpyIIackk=; b=DSj2hxhuXk1rCdMfIMaIz2FdZ9bo4C7xltv6Yv3CI5pEa7vqHUD0wzeE0KyfVJCZ/G jByPnPPCv/w9mCIB3VU7pCX6QjpHQ1NttGHC3ZaxRxh/32FdOyS6cJ/doWCIxmZSj5zc /ytLBWZgLZ5HEM9gD/o6mAlqMjWy6i8gxUJ4kxs0wpSDSz8cWlRipGqzHtTSCiK9vXtk pc8tAswFIuYPtFM0rqxUDwXE20uCxHMEP86Rm/CvYpztW1HPpWaUvqpXOIZ5NjmXLboG y9sbBAUNPfroU4xFCfJgN1ymf97Co+/7d/a2kMcA4863oDH819NII5Q0No/ule2fFisT HuoA== X-Gm-Message-State: APjAAAVQNkmEkiuUL+/WxUYzW4F+OcKG3VfldSjpzjAGF3ME2fPeHh2n M0D4dSBsedp3eLoOIwuyM36HeA== X-Google-Smtp-Source: APXvYqzuws7+fpOj+CHfN4ihVljRObMbsfPb/EpGqi7n4ejadFKNibJEi/1nlHBQrdqZf+Oghz+Oew== X-Received: by 2002:a7b:c318:: with SMTP id k24mr5304723wmj.54.1580216952880; Tue, 28 Jan 2020 05:09:12 -0800 (PST) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id q3sm2910509wmj.38.2020.01.28.05.09.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jan 2020 05:09:12 -0800 (PST) Date: Tue, 28 Jan 2020 14:09:10 +0100 From: Tomas Vondra To: Julien Rouhaud Cc: Michael Paquier , Sergei Kornilov , Guillaume Lelarge , PostgreSQL Hackers Subject: Re: Expose lock group leader pid in pg_stat_activity Message-ID: <20200128130910.anay3mlhaminob6w@development> References: <5153051577359132@sas8-b090c2642e35.qloud-c.yandex.net> <2519941577437281@vla3-985002032993.qloud-c.yandex.net> <20200116072727.GI3117@paquier.xyz> <20200116074912.GA98418@paquier.xyz> <20200118025102.GB230692@paquier.xyz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: 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: >On Sat, Jan 18, 2020 at 3:51 AM Michael Paquier wrote: >> >> On Fri, Jan 17, 2020 at 05:07:55PM +0100, Julien Rouhaud wrote: >> > Oh indeed. But unless we hold some LWLock during the whole function >> > execution, we cannot guarantee a consistent view right? >> >> Yep. That's possible. >> >> > And isn't it already possible to e.g. see a parallel worker in >> > pg_stat_activity while all other queries are shown are idle, if >> > you're unlucky enough? >> >> Yep. That's possible. >> >> > Also, LockHashPartitionLockByProc requires the leader PGPROC, and >> > there's no guarantee that we'll see the leader before any of the >> > workers, so I'm unsure how to implement what you said. Wouldn't it be >> > better to simply fetch the leader PGPROC after acquiring a shared >> > ProcArrayLock, and using that copy to display the pid, after checking >> > that we retrieved a non-null PGPROC? >> >> Another idea would be to check if the current PGPROC entry is a leader >> and print in an int[] the list of PIDs of all the workers while >> holding a shared LWLock to avoid anything to be unregistered. Less >> handy, but a bit more consistent. One problem with doing that is >> that you may have in your list of PIDs some worker processes that are >> already gone when going through all the other backend entries. An >> advantage is that an empty array could mean "I am the leader, but >> nothing has been registered yet to my group lock." (note that the >> leader adds itself to lockGroupMembers). > >So, AFAICT the LockHashPartitionLockByProc is required when >iterating/modifying lockGroupMembers or lockGroupLink, but just >getting the leader pid should be safe. Since we'll never be able to >get a totally consistent view of data here, I'm in favor of avoiding >taking extra locks here. I agree that outputting an array of the pid >would be more consistent for the leader, but will have its own set of >corner cases. It seems to me that a new leader_pid column is easier >to handle at SQL level, so I kept that approach in attached v4. If >you have strong objections to it. I can still change it. I agree a separate "leader_id" column is easier to work with, as it does not require unnesting and so on. As for the consistency, I agree we probably can't make this perfect, as we're fetching and processing the PGPROC records one by one. Fixing that would require acquiring a much stronger lock on PGPROC, and perhaps some other locks. That's pre-existing behavior, of course, it's just not very obvious as we don't have any dependencies between the rows, I think. Adding the leader_id will change, that, of course. But I think it's still mostly OK, even with the possible inconsistency. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services