Guest User

Untitled

a guest
May 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. -- Function: merge_db(integer, text)
  2.  
  3. -- DROP FUNCTION merge_db(integer, text);
  4.  
  5. CREATE OR REPLACE FUNCTION merge_db(key integer, data text)
  6. RETURNS void AS
  7. $BODY$CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
  8. $$
  9. BEGIN
  10. LOOP
  11. -- first try to update the key
  12. UPDATE db SET b = data WHERE a = key;
  13. IF found THEN
  14. RETURN;
  15. END IF;
  16. -- not there, so try to insert the key
  17. -- if someone else inserts the same key concurrently,
  18. -- we could get a unique-key failure
  19. BEGIN
  20. INSERT INTO db(a,b) VALUES (key, data);
  21. RETURN;
  22. EXCEPTION WHEN unique_violation THEN
  23. -- do nothing, and loop to try the UPDATE again
  24. END;
  25. END LOOP;
  26. END;
  27. $$
  28. LANGUAGE plpgsql;
  29. $BODY$
  30. LANGUAGE 'sql' VOLATILE
  31. COST 100;
  32. ALTER FUNCTION merge_db(integer, text) OWNER TO postgres;
Add Comment
Please, Sign In to add comment