A 3D view on the local gravitational instability of cold gas discs in star-forming galaxies at 0 < z < 5