Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function: merge_db(integer, text)
- -- DROP FUNCTION merge_db(integer, text);
- CREATE OR REPLACE FUNCTION merge_db(key integer, data text)
- RETURNS void AS
- $BODY$CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
- $$
- BEGIN
- LOOP
- -- first try to update the key
- UPDATE db SET b = data WHERE a = key;
- IF found THEN
- RETURN;
- END IF;
- -- not there, so try to insert the key
- -- if someone else inserts the same key concurrently,
- -- we could get a unique-key failure
- BEGIN
- INSERT INTO db(a,b) VALUES (key, data);
- RETURN;
- EXCEPTION WHEN unique_violation THEN
- -- do nothing, and loop to try the UPDATE again
- END;
- END LOOP;
- END;
- $$
- LANGUAGE plpgsql;
- $BODY$
- LANGUAGE 'sql' VOLATILE
- COST 100;
- ALTER FUNCTION merge_db(integer, text) OWNER TO postgres;
Add Comment
Please, Sign In to add comment