I am using this function to update a database, but I need it not to check only one key, but two. This is if it only matches one key it's not the same record, then it will insert the row.
This is the example, and my function has the same structure:
CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
-- first try to update the key
UPDATE db SET b = data WHERE a = key;
IF found THEN
-- not there, so try to insert the key
-- if someone else inserts the same key concurrently,
-- we could get a unique-key failure
INSERT INTO db(a,b) VALUES (key, data);
EXCEPTION WHEN unique_violation THEN
-- do nothing, and loop to try the UPDATE again
EDIT: It's solved, adding in the update "and field=field", the other check field.