4. The total length of all curve segments formed by a moving point on the surface of a regular quadrilateral pyramid $P-ABCD$ with lateral edge length and base edge length both being 4, and at a distance of 3 from the vertex $P$, is $\qquad$ .
4. $6 \pi$.

On the side faces of the square pyramid, the moving point forms 4 arcs with a radius of 3 and a central angle of $\frac{\pi}{3}$, the total length of which is $l_{1}=4 \times \frac{\pi}{3} \times 3=4 \pi$.

It is also known that the height of the square pyramid $h=\sqrt{4^{2}-(2 \sqrt{2})^{2}}=2 \sqrt{2}<3$, so the moving point forms a circle with a radius of $r=\sqrt{3^{2}-(2 \sqrt{2})^{2}}=1$ on the base, whose circumference is $l_{2}=2 \pi$.
Therefore, the total length of all the curve segments formed by the moving point on the surface of the square pyramid is $l_{1}+l_{2}=6 \pi$.