From 3d39f72435c8664ea9ffce084b1cc8297b6070fd Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 7 Nov 2012 21:28:45 +0100 Subject: [PATCH] PostRR_update(): Use rrtimeslice_cmp() to decide how to do the update. That is, added support for overwriting old values and reporting errors if the given timestamp is older than the appropriate timestamp in the database (which is the entry having the same sequence number). --- src/postrr.sql.in | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/postrr.sql.in b/src/postrr.sql.in index 87ef9d3..9f34f22 100644 --- a/src/postrr.sql.in +++ b/src/postrr.sql.in @@ -320,6 +320,7 @@ DECLARE ts_str text; v_str text; update_qry text; + status integer; newts rrtimeslice; new cdata; BEGIN @@ -330,13 +331,13 @@ BEGIN update_qry = 'CData_update(' || vcol || ', ' || v_str || ')'; + -- XXX: handle race conditions + BEGIN - EXECUTE 'UPDATE ' || tbl - || ' SET ' || tscol || ' = ' || ts_str - || ', ' || vcol || ' = ' || update_qry + EXECUTE 'SELECT rrtimeslice_cmp(' || tscol || ', ' || ts_str + || ') AS status FROM ' || tbl || ' WHERE ' || tscol || ' = ' || ts_str - || ' RETURNING ' || tscol || ', ' || vcol - INTO STRICT newts, new; + INTO STRICT status; EXCEPTION WHEN NO_DATA_FOUND THEN @@ -346,9 +347,29 @@ BEGIN || ') RETURNING ' || tscol || ', ' || vcol INTO newts, new; -- use strict again; on exception retry? + RETURN new; WHEN TOO_MANY_ROWS THEN RAISE EXCEPTION '% is not unique in %.%', ts_str, tbl, tscol; END; + + IF status = 0 THEN + -- timestamps match + EXECUTE 'UPDATE ' || tbl + || ' SET ' || vcol || ' = ' || update_qry + || ' WHERE ' || tscol || ' = ' || ts_str + || ' RETURNING ' || tscol || ', ' || vcol + INTO STRICT newts, new; + ELSIF status < 0 THEN + -- given timestamp is newer than in the database + EXECUTE 'UPDATE ' || tbl + || ' SET ' || tscol || ' = ' || ts_str + || ', ' || vcol || ' = ' || v_str + || ' WHERE ' || tscol || ' = ' || ts_str + || ' RETURNING ' || tscol || ', ' || vcol + INTO STRICT newts, new; + ELSE + RAISE EXCEPTION '% is too old in %.%', ts_str, tbl, tscol; + END IF; RETURN new; END; $$; -- 2.30.2