0

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?

Vlad Keel
  • 372
  • 2
  • 13
  • The backend is not multithreaded, so I don't know what it means that "I found that each query is executed in a separate threads". Can you describe the concrete observation which lead you to this conclusion? – jjanes Mar 19 '23 at 23:13

1 Answers1

0

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.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263