I'd rather consider registering with on_shmem_exit -- as per the insightfull note placed in postgresql.c:4290. (Haven't tried - just thinking)
This point can be too late. Probably it is good enough for releasing external sources, but the query execution engine can be broken in this moment ~ you cannot to run any SQL