It’s the oldest joke in Pirkei Avot.

Why did the rabbis put a fence around the Torah?

They wanted a safer Torah.

Bah dum bum.

But the quest for a safer Torah nearly turned tragic at the chassidic Rabbinical College Yeshiva of Machzikai Hadas in Borough Park this week. A worker was installing a one-ton metal safe, a 8-foot by 3-foot ark designed to secure the yeshiva’s Torah scrolls from theft, when it toppled over onto him.

Luckily, “The hand of God reached down,” the New York Post reported, and the worker “miraculously escaped being crushed to death” and “flattened like a matzo.”

Whether it was a miracle or luck, the door to the secure aron kodesh was open as the safe teetered, and the worker squeezed himself into the opening. He was trapped beneath the fallen aron for 45 minutes, along with a large Torah scroll, until rescue workers came.

“Anyone who this happens to should be really, really hurt . . . but he walked away smiling,” a witness told the Post.