A user of the Go driver pgx reported a strange bug (https://github.com/jackc/pgx/issues/1234). After investigating the issue I was able to duplicate it in directly in psql. The problem occurs when a prepared statement has an INSERT ... ON CONFLICT. The statement works the first 5 times and fails on the 6th time with ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification. I would guess this has to do with the planner choosing a generic or custom plan.