I turned on iCloud Photos a few weeks ago. My iPhone shows 'Optimizing Storage' permanently and my Mac says 1,400 photos are still uploading, but the number barely moves. Both devices are on the same Apple ID and Wi-Fi.
Is there a safe way to restart the sync without losing anything?
Do not turn iCloud Photos off and on again until you have a local backup — that can drop items that have not finished uploading. Try this first:
On the Mac, open Photos and leave it running while plugged in with the lid open for a few hours; uploads pause when the Mac sleeps, which is the most common reason the counter freezes. Check in System Settings → Apple ID → iCloud → Photos that 'Optimize Mac Storage' is selected and that you actually have free disk space — iCloud will not upload reliably when the drive is nearly full.
On the iPhone, make sure Low Power Mode is off (it pauses background sync) and that the device is on Wi-Fi and locked with the Photos app closed for a while. If the count still has not moved after 24 hours of those conditions, sign out of iCloud on the Mac only, then sign back in. Your originals stay on the iPhone and the library rebuilds from iCloud. If you ever see a 'missing' warning, stop and contact Apple Support before doing anything else.