Synchronizing between backends
All the preceding functions are designed to run in a single process/backend as if the other PostgreSQL processes did not exist.
But what if you want to log something to a single file from multiple backends?
Seems easy—just open the file and write what you want. Unfortunately, it is not that easy if you want to do it from multiple parallel processes and you do not overwrite or mix up the data with what other processes write.
To have more control over the writing order between backends, you need to have some kind of inter-process synchronization, and the easiest way to do this in PostgreSQL is to use shared memory and light-weight locks (LWLocks
).
To allocate its own shared memory segment your .so
file needs to be pre-loaded, that is, it should be one of the pre-load libraries given in postgresql.conf
variable shared_preload_libraries
.
In the _PG_init()
function of your module, you ask for the address of a name shared memory segment. If you are the first one asking...