Code

Added support for installing PostRR as an extension.
[postrr.git] / src / postrr.sql.in
index eb197452a5ca6e1e4a771398854efc0d0d625975..34d125773fa4da1cacc7864657899956af5ee41a 100644 (file)
 -- PostRR - PostgreSQL Round-Robin Extension
 --
 
--- suppress messages like 'return type foo is only a shell'
-SET client_min_messages TO WARNING;
-
-BEGIN;
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION postrr" to load this file. \quit
 
 CREATE SCHEMA postrr;
 
@@ -45,6 +43,8 @@ CREATE TABLE postrr.rrtimeslices (
        tsnum integer NOT NULL
 );
 
+SELECT pg_catalog.pg_extension_config_dump('postrr.rrtimeslices', '');
+
 CREATE TABLE postrr.rrarchives (
        rraname text NOT NULL,
        tbl name NOT NULL,
@@ -53,6 +53,8 @@ CREATE TABLE postrr.rrarchives (
        UNIQUE (rraname, tbl, tscol, vcol)
 );
 
+SELECT pg_catalog.pg_extension_config_dump('postrr.rrarchives', '');
+
 CREATE OR REPLACE FUNCTION PostRR_Version()
        RETURNS cstring
        AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'postrr_version'
@@ -342,43 +344,47 @@ BEGIN
 
        -- XXX: handle race conditions
 
-       BEGIN
-               EXECUTE 'SELECT rrtimeslice_cmp(' || tscol || ', ' || ts_str
-                       || ') AS status FROM ' || tbl
-                       || ' WHERE ' || tscol || ' = ' || ts_str
-                       INTO STRICT status;
-
-               EXCEPTION
-                       WHEN NO_DATA_FOUND THEN
-                               EXECUTE 'INSERT INTO ' || tbl
-                                       || ' (' || tscol || ', ' || vcol
-                                       || ') VALUES (' || ts_str || ', ' || v_str
-                                       || ') 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;
+       LOOP
+               BEGIN
+                       -- removing matching (by sequence no) old entries
+                       EXECUTE 'DELETE FROM ' || tbl
+                               || ' WHERE rrtimeslice_cmp(' || tscol || ', ' || ts_str
+                               || ') = -1; '
+                               || 'SELECT rrtimeslice_cmp(' || tscol || ', ' || ts_str
+                               || ') AS status FROM ' || tbl
+                               || ' WHERE ' || tscol || ' = ' || ts_str
+                               INTO STRICT status;
+
+                       EXCEPTION
+                               WHEN NO_DATA_FOUND THEN
+                                       EXECUTE 'INSERT INTO ' || tbl
+                                               || ' (' || tscol || ', ' || vcol
+                                               || ') VALUES (' || ts_str || ', ' || v_str
+                                               || ') 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
+                       -- someone else inserted older data in the meantime
+                       -- => try again
+                       CONTINUE;
+               ELSE
+                       RAISE EXCEPTION '% is too old in %.%', ts_str, tbl, tscol;
+               END IF;
+               EXIT;
+       END LOOP;
        RETURN new;
 END;
 $$;
@@ -404,9 +410,5 @@ BEGIN
 END;
 $$;
 
-COMMIT;
-
-SET client_min_messages TO DEFAULT;
-
 -- vim: set tw=78 sw=4 ts=4 noexpandtab :