How to share memory between processes in Postgres FDW

64 Views Asked by At

I'm writing a postgres FDW extension for my custom data storage.

I have a class that provides the API for the storage. initialization of the class is rather costly, and I also need to have access to it outside of the FdwRoutine functions, so I wanted to initialize it once (per foreign table) in the VALIDATOR function, then access it in the FdwRoutine functions and UDFs.

I found that each query is executed in a separate threads, and even the ForeignServer isn't shared between them. I couldn't find any documentation on memory sharing in FDW.

What would be the best approach way to initialize some object when CREATE FOREIGN TABLE is called, and access it in FdwRoutine functions and UDFs?

1

There are 1 best solutions below

0
Laurenz Albe On

Each connection in PostgreSQL is its own process, and you shouldn't try to mess with that architecture. Perhaps you could load your extension with shared_preload_libraries and allocate a shared memory segment in _PG_init at server start time.