I am attempting to run an "upsert" in postgres like:
INSERT INTO my_table (
id, -- unique key
name,
hash
) VALUES (
'4b544dea-b355-463c-8fba-40c36ac7cb0c',
'example',
'0x0123456789'
) ON CONFLICT (
id
) DO UPDATE SET
name = 'example',
hash = '0x0123456789'
RETURNING
OLD.hash;
I would like to return the previous value of the hash column (the example above is not a valid query due to OLD not being a valid table alias). Importantly, I am trying to find a method that does this in a way that won't cause conflicts under load. Is this even possible? Or is the only solution to do a read-before-write in a transaction?