A simple problem from a Postgres newbie:
How do I write a Postgres transaction something like
IF SELECT COUNT(*) FROM myTable WHERE myPK = myValue > 0 THEN
UPDATE myTable ...
ELSE
INSERT INTO myTable...
END
I want this in the hopes that it can solve a bug in my web app that occurs when another insert happens on myTable between my being able to SELECT COUNT(*) from it to decide whether I should update or insert.
There’s a sample plpgsql implementation for this in Postgresql’s documentation: see merge_db
It is specifically meant to support concurrent update-or-insert (often referred to as UPSERT).