I think it's in the gemora, something like "hameharher bayom ba leyedei ....... balayla".
So it's definitely influenced by what we see and think about during the day.
It can also be a natural consequence of not having another outlet for a while, especially when stopping after having done it frequently. It builds up in the body and has no place to go.
What to do: There are halachos about how to sleep, on the side not on front or back. Also there is a segula to say the first 4 kapitlach of tehillim before going to sleep. In many shuls there is a minhag to do this at the end of Maariv on Yom Kippur night.
Hatzlacha, and KOT!!! (do you know what that means yet?, it's part of the initiation rites here