
PostgreSQL 19 ON CONFLICT DO SELECT: Get-or-Create, Measured
If you build a feature that attaches tags to posts, this requirement always shows up. A user types accessibility. If that tag already exists, use its id. If not, create it and use the new id. Because you get it if it’s there and create it if it isn’t, the pattern is called get-or-create. The usual implementation goes like this: look it up with SELECT, and if there’s no row, INSERT. It works fine when you test it alone. But when two requests try to create the same tag at nearly the same moment, one of them fails with this error. ...