Re: Is custom MemoryContext prohibited? - Mailing list pgsql-hackers

From Kohei KaiGai
Subject Re: Is custom MemoryContext prohibited?
Date
Msg-id CAOP8fzaGHJ3hCJzHbjCUA78FWm+oRcUstgvFVAZwN6-43ZEAbQ@mail.gmail.com
Whole thread Raw
In response to Re: Is custom MemoryContext prohibited?  (Thomas Munro <thomas.munro@gmail.com>)
Responses Re: Is custom MemoryContext prohibited?  (Thomas Munro <thomas.munro@gmail.com>)
List pgsql-hackers
2020年1月29日(水) 4:27 Thomas Munro <thomas.munro@gmail.com>:
>
> On Tue, Jan 28, 2020 at 2:55 PM Kohei KaiGai <kaigai@heterodb.com> wrote:
> > I noticed MemoryContextIsValid() called by various kinds of memory context
> > routines checks its node-tag as follows:
> >
> > #define MemoryContextIsValid(context) \
> >     ((context) != NULL && \
> >      (IsA((context), AllocSetContext) || \
> >       IsA((context), SlabContext) || \
> >       IsA((context), GenerationContext)))
> >
> > It allows only "known" memory context methods, even though the memory context
> > mechanism enables to implement custom memory allocator by extensions.
> > Here is a node tag nobody used: T_MemoryContext.
> > It looks to me T_MemoryContext is a neutral naming for custom memory context,
> > and here is no reason why memory context functions prevents custom methods.
> >
> >
> > https://github.com/heterodb/pg-strom/blob/master/src/shmbuf.c#L1243
> > I recently implemented a custom memory context for shared memory allocation
> > with portable pointers. It shall be used for cache of pre-built gpu
> > binary code and
> > metadata cache of apache arrow files.
> > However, the assertion check above requires extension to set a fake node-tag
> > to avoid backend crash. Right now, it is harmless to set T_AllocSetContext, but
> > feel a bit bad.
>
> FWIW the code in https://commitfest.postgresql.org/26/2325/ ran into
> exactly the same problem while making nearly exactly the same kind of
> thing (namely, a MemoryContext backed by space in the main shm area,
> in this case reusing the dsa.c allocator).
>
Thanks, I had looked at "Shared Memory Context" title on pgsql-hackers a few
months ago, however, missed the thread right now.

The main point of the differences from this approach is portability of pointers
to shared memory chunks. (If I understand correctly)
PG-Strom preserves logical address space, but no physical pages, on startup
time, then maps shared memory segment on the fixed address on demand.
So, pointers are portable to all the backend processes, thus, suitable to build
tree structure on shared memory also.

This article below introduces how our "shared memory context" works.
It is originally written in Japanese, and Google translate may
generate unnatural
English. However, its figures probably explain how it works.

https://translate.google.co.jp/translate?hl=ja&sl=auto&tl=en&u=http%3A%2F%2Fkaigai.hatenablog.com%2Fentry%2F2016%2F06%2F19%2F095127

Best regards,
--
HeteroDB, Inc / The PG-Strom Project
KaiGai Kohei <kaigai@heterodb.com>



pgsql-hackers by date:

Previous
From: Peter Geoghegan
Date:
Subject: Re: Enabling B-Tree deduplication by default
Next
From: Thomas Munro
Date:
Subject: Re: Is custom MemoryContext prohibited?