Re: shm_toc_lookup API

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: shm_toc_lookup API
Date: 2017-06-05 18:57:10
Message-ID: 28314.1496689030@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Mon, Jun 5, 2017 at 12:19 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> In practice it probably can't fail even if 64-bit reads aren't atomic,
>> simply because we'll never have enough entries in a shm_toc to make the
>> high-order half ever change. But that just begs the question why the
>> field is declared Size rather than int. I think we should make it the
>> latter.

> Yeah. I think a shm_toc with more than 2^10 entries would probably
> perform badly enough that somebody would rewrite this entire module,
> so we don't really need to worry about having more than 2^31.
> Changing to int (or uint32) seems fine.

Done with uint32.

>> I am also thinking that most of the shm_toc functions need to have the
>> toc pointers declared as "volatile *", but particularly shm_toc_lookup.

(actually, they do already use volatile pointers, except for shm_toc_lookup)

>> That read_barrier call might prevent the hardware from reordering
>> accesses, but I don't think it stops the compiler from doing so.

> If it doesn't prevent both the hardware and the compiler from
> reordering, it's broken. See the comments for pg_read_barrier() in
> atomics.h.

Meh. Without volatile, I think that the compiler would be within its
rights to elide the nentry local variable and re-fetch toc->toc_nentry
each time through the loop. It'd be unlikely to do so, granted, but
I'm not convinced that pg_read_barrier() would prevent that.

However, as long as the write barrier in shm_toc_insert does what it's
supposed to, I think we'd be safe even if that happened. So probably
it's a moot point.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2017-06-05 19:02:49 Re: shm_toc_lookup API
Previous Message Jeff Janes 2017-06-05 18:37:09 postgres_fdw cost estimation defaults and documentation