Thanks! I've pushed the first two parts, improving the shmem accounting.
I've done a couple additional adjustments before commit, namely: 1) I've renamed hash_get_init_size() to just hash_get_size(). We've not indicated that it's "initial" allocation before, I don't think we need to indicate to do that now. So I've just removed the "shared" part, as that's no longer relevant. 2) I've removed one of the compute_buckets_and_segs() in hash_create, because AFAICS we don't need to recalculate the nsegs - we already have the correct value in the hctl, so I just used that. The fact that we call compute_buckets_and_segs() in multiple places, relying on getting the exact same result seemed a bit fragile to me. With this we now have just two callers (instead of 3), and I don't think we can do better - the first call is in hash_get_size(), and I don't see a convenient way to pass the info init_hash() where we do the 2nd call. 3) In 0002, I've reworked how ProcGlobalShmemSize() calculates the size of the required memory - the earlier patch added PGProcShmemSize(), but the ProcGlobalShmemSize() redid the calculation all over. I changed that to call the new function, and also added a new FastPathLockShmemSize() to do the same for the fast-path lock arrays (which I realized is now allocated as a separate area, so it'll be shown separately). 4) I realized PGProcShmemSize() did the calculation slightly differently from ProcGlobalShmemSize() - by directly multiplying the values, not using mul_size(). I don't recall why we use mul_size(), but it seems like the "usual" way. So I did it that way. I notice we still do the plain multiplication later when setting the ProcGlobal fields. Seems a bit weird, but we always did that - the patch does not really change that. I'll now mark this as committed. I haven't done about the alignment. My conclusion from the discussion was we don't quite need to do that, but if we do I think it's a matter for a separate patch - perhaps something like the 0003. Thanks for the patch, reviews, etc. -- Tomas Vondra