我有這個upsert函數,允許我修改一行的fill_rate列。postgresql中的動態upsert
CREATE FUNCTION upsert_fillrate_alarming(integer, boolean) RETURNS VOID AS '
DECLARE
num ALIAS FOR $1;
dat ALIAS FOR $2;
BEGIN
LOOP
-- First try to update.
UPDATE alarming SET fill_rate = dat WHERE equipid = num;
IF FOUND THEN
RETURN;
END IF;
-- Since its not there we try to insert the key
-- Notice if we had a concurent key insertion we would error
BEGIN
INSERT INTO alarming (equipid, fill_rate) VALUES (num, dat);
RETURN;
EXCEPTION WHEN unique_violation THEN
-- Loop and try the update again
END;
END LOOP;
END;
' LANGUAGE 'plpgsql';
是否有可能修改此函數以獲取列參數?如果有辦法修改函數以獲取列和表格,則需要額外的獎勵分數。
如果兩個事務嘗試在同一時間插入一行,這將不起作用。除非你使用可序列化的事務。 – 2013-07-17 01:31:24
,它不允許postgres針對該功能進行優化。 – baash05 2014-02-05 04:08:49