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