Postgres INSERT uniqueness violation (yet another)

Nextcloud version (eg, 20.0.5): 26.0.1
Operating system and version (eg, Ubuntu 20.04): Docker on embedded
Apache or nginx version (eg, Apache 2.4.25): official nextcloud:apache docker image
PHP version (eg, 7.4): 8.1.18
Postgres official docker image 14.8
Redis docker latest

The issue you are facing:
Occasionally for some users, some of the entries in the oc_preferences table have these errors. Even more sometimes they prevent users from logging in.
In this case it prevents login.

The last time this happened with a different configkey (I don’t remember which), there were duplicates and removing them helped the situation. This time there aren’t duplicates.
Deleting the failedLoginAttempts entry for that user does not help the case. Which is weird because INSERT should work if the entry is not present.

I saw many forum posts and Github issues talking about this (with completely different configkeys) and half baked quick fixes. What is the reason this happens? The database settings are as default-nextcloud as it gets because I let Nextcloud create the database on first run.

If you need more specific context to help me debug this, please say so.

Steps to replicate it:

No idea yet.

The output of your Nextcloud log in Admin > Logging:

nextcloud-db_1 | 2023-05-13 20:13:42.673 UTC [8658] DETAIL: Key (userid, appid, configkey)=(CensoredUsername, password_policy, failedLoginAttempts) already exists.
nextcloud-db_1 | 2023-05-13 20:13:42.673 UTC [8658] STATEMENT: INSERT INTO “oc_preferences” (“userid”, “appid”, “configkey”, “configvalue”) VALUES($1, $2, $3, $4)

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

Nothing related I guess.

Nothing related outside of the db logs.