Matomo stores the session data (so data like: who is logged in, etc. ) in the matomo_session MySQL table. Each entry has a unique id which previously was 255 characters long. With Matomo 4 this changed to 191 characters and it seems like this shortening of all ids causes one of them to no longer be unique, which causes the error you see.
I think (but am pretty unsure, so don’t do this unless you trust your backups a lot or someone else can confirm this), deleting all data in the matomo_session table should not do any harm apart from causing every user to be logged out immediately.