To me it doesn't look like your snake was in any danger, but rather sought out the tight fit as a way to 'hang on' there. ("Look Ma, no hands!")Glad he's OK.
Remember that in the wild they sometimes smush their way thru things (& yes, sometimes come to harm when they get stuck, like in bird-netting). The way
his mid-body is rippled in your photo also looks like his way of getting traction.
I once had a corn snake (with a nice flat tummy) that was able to slide vertically up a tall glass aquarium & then reach out with at least a third of his body pushing up on the lid, and the ONLY thing keeping him there was the near-suction from his ventral scales on the glass, & the part of his body wedged as best he could into the right angled corner formed by the glass & the screen top. It looked like a magic trick...it shouldn't have been possible, but that's what's so cool about snakes.