Re: Expose lock group leader pid in pg_stat_activity - Mailing list pgsql-hackers

From Michael Paquier
Subject Re: Expose lock group leader pid in pg_stat_activity
Date
Msg-id 20200316060231.GD2331@paquier.xyz
Whole thread Raw
In response to Re: Expose lock group leader pid in pg_stat_activity  (Justin Pryzby <pryzby@telsasoft.com>)
List pgsql-hackers
On Mon, Mar 16, 2020 at 12:43:41AM -0500, Justin Pryzby wrote:
> I think I see.  Julien's v3 patch did this:
> https://www.postgresql.org/message-id/attachment/106429/pgsa_leader_pid-v3.diff
> +                if (proc->lockGroupLeader)
> +                    values[29] = Int32GetDatum(proc->lockGroupLeader->pid);
>
> ..which is racy because a proc with a leader might die and be replaced by
> another proc without a leader between 1 and 2.
>
> But the code since v4 does:
>
> +                leader = proc->lockGroupLeader;
> +                if (leader)
> +                                       values[29] = Int32GetDatum(leader->pid);
>
> ..which is safe because PROCs are allocated in shared memory, so leader is for
> sure a non-NULL pointer to a PROC.  leader->pid may be read inconsistently,
> which is what the comment says: "no extra lock is being held".

Yes, you have the correct answer here.  As shaped, the code relies on
the state of a PGPROC entry in shared memory.
--
Michael

Attachment

pgsql-hackers by date:

Previous
From: Justin Pryzby
Date:
Subject: Re: Expose lock group leader pid in pg_stat_activity
Next
From: Suraj Kharage
Date:
Subject: Re: backup manifests