Occasionally, when I run this script, it will report "Duplicate entry", although I'm checking for that ahead of time as you can see. If I run the script again I don't get the error and everything is fine, usually. Anyone have any thoughts on why I'd occasionally get this strange surprise that would go away upon running the script again?
It's odd that would happen UNLESS your lookup key ($catalogKey) in the first query was changed. Can you check that on your error dump as well? That may help get you started (in the event you are still having this issue as I realize this is from August!)