For every backend that might be running (MaxBackends) we reserve a dsm_handle slot in the addins shared memory. When the new option is turned on, the ExecutorRun hook will produce a plan in whatever format is specified by the auto_explain.log_format, allocate a DSM segment, copy the plan into the segment and finally store the DSM handle into its own slot. No locking is required around this because every backend writes to its slot exclusively, no other backend can be writing into it concurrently. In the ExecutorFinish hook we invalidate the backend's slot by setting the handle to 0 and deallocate the DSM segment.
And this patch adds back the use of table of contents on the DSM. Benefits: magic number validation; communication of the actual plan size.