rrs-commit: r56 - trunk

decibel at decibel.org decibel at decibel.org
Sun Mar 27 23:03:54 GMT 2005


Author: decibel
Date: Sun Mar 27 23:03:53 2005
New Revision: 56

Modified:
   trunk/rrs_functions.sql
Log:
Refactor update() into update() and update_source_rrs()

Modified: trunk/rrs_functions.sql
==============================================================================
--- trunk/rrs_functions.sql	(original)
+++ trunk/rrs_functions.sql	Sun Mar 27 23:03:53 2005
@@ -13,13 +13,9 @@
 CREATE OR REPLACE FUNCTION "update"() RETURNS integer
     AS '
 DECLARE
-    v_max_end_time rrs.bucket.end_time%TYPE;
-    v_last_end_time rrs.bucket.end_time%TYPE;
-    v_rows int;
     v_total_rows int;
     v_rrs rrs.rrs%ROWTYPE;
     v_source rrs.source%ROWTYPE;
-    v_sql text;
 
     v_start_time rrs.history.start_time%TYPE;
     v_run_time interval;
@@ -48,102 +44,7 @@
         -- Run through all the RRDs
         FOR v_rrs IN SELECT * FROM rrs.rrs ORDER BY coalesce( parent, -1 ), rrs_id
         LOOP
-            v_rows := 0;
-            SELECT max(end_time)
-                INTO v_max_end_time
-                FROM rrs.bucket
-                WHERE rrs_id = v_rrs.rrs_id
-            ;
-
-            IF v_max_end_time IS NOT NULL THEN
-                SELECT INTO v_last_end_time
-                        last_end_time
-                    FROM rrs.source_status
-                    WHERE rrs_id = v_rrs.rrs_id
-                        AND source_id = v_source.source_id
-                ;
-                IF NOT FOUND THEN
-                    v_last_end_time := ''1970-01-01''::timestamptz;
-                END IF;
-
-                IF v_last_end_time = v_max_end_time THEN
-                    RAISE INFO ''Nothing to do for % rrs_id %, skipping...'', v_source.source_name, v_rrs.rrs_id;
-                ELSE
-                    RAISE INFO ''Inserting into % for rrs_id % from % to %'', v_source.source_name, v_rrs.rrs_id, v_last_end_time, v_max_end_time;
-                    IF v_rrs.parent IS NULL THEN
-                        v_sql :=
-                        ''INSERT INTO '' || v_source.insert_table || ''
-                                            ( bucket_id, '' || v_source.group_clause || '', ''
-                                                        || v_source.insert_aggregate_fields || '' )
-                            SELECT a.rrs_bucket_id, '' || v_source.group_clause || ''
-                                        , '' || v_source.primary_aggregate || ''
-                                FROM
-                                    (SELECT b.bucket_id AS rrs_bucket_id, s.*
-                                        FROM rrs.bucket b
-                                            JOIN '' || v_source.source_table || '' s
-                                                ON (
-                                                    b.prev_end_time  < '' || quote_ident(v_source.source_timestamptz_field) || ''
-                                                    AND b.end_time >= '' || quote_ident(v_source.source_timestamptz_field) || ''
-                                                    AND '' || quote_ident(v_source.source_timestamptz_field) || '' <= '' || quote_literal(v_max_end_time) || ''
-                                                    AND '' || quote_ident(v_source.source_timestamptz_field) || '' > '' || quote_literal(v_last_end_time) || ''
-                                                    )
-                                        WHERE b.rrs_id = '' || quote_literal(v_rrs.rrs_id) || ''
-                                            AND b.end_time <= '' || quote_literal(v_max_end_time) || ''
-                                            AND b.end_time > '' || quote_literal(v_last_end_time) || ''
-                                    ) a
-                                GROUP BY rrs_bucket_id, '' || v_source.group_clause || '';''
-                        ;
-                    ELSE
-                        -- Thanks to dealing with rrs.bucket twice, this query is a bit tricky. We want to look at rrs.bucket
-                        -- for the rrs we''re *updating*, so that we know what our ranges are. Then, we want to query the 
-                        -- parent data, and group it by the different ranges
-                        v_sql := 
-                        ''INSERT INTO '' || v_source.insert_table || ''
-                                            ( bucket_id, '' || v_source.group_clause || '', ''
-                                                        || v_source.insert_aggregate_fields || '' )
-                            SELECT a.rrs_bucket_id, '' || v_source.group_clause || ''
-                                        , '' || v_source.rrs_aggregate || ''
-                                FROM 
-                                    -- Wrap this whole thing in a sub-select to avoid field name conflicts
-                                    (
-                                    SELECT b.bucket_id AS rrs_bucket_id, r.*
-                                        FROM '' || v_source.insert_table || '' r
-                                            JOIN rrs.bucket p ON (r.bucket_id = p.bucket_id)
-                                            , rrs.bucket b
-
-                                        -- Get just the appropriate buckets for the RRD we are *updating*
-                                        WHERE b.rrs_id = '' || quote_literal(v_rrs.rrs_id) || ''
-                                            AND b.end_time <= '' || quote_literal(v_max_end_time) || ''
-                                            AND b.end_time > '' || quote_literal(v_last_end_time) || ''
-
-                                        -- Select the parent data but only for the appropriate time slots
-                                            AND p.rrs_id = '' || quote_literal(v_rrs.parent) || ''
-                                            AND p.end_time <= b.end_time
-                                            AND p.end_time > b.prev_end_time
-                                    ) a
-                                GROUP BY rrs_bucket_id, '' || v_source.group_clause || '';''
-                        ;
-                    END IF;
-                    RAISE DEBUG ''Executing query: %'', v_sql;
-                    EXECUTE v_sql;
-
-                    GET DIAGNOSTICS v_rows = ROW_COUNT;
-                    RAISE INFO ''% rows inserted'', v_rows;
-
-                    UPDATE rrs.source_status
-                        SET last_end_time = v_max_end_time
-                        WHERE rrs_id = v_rrs.rrs_id
-                            AND source_id = v_source.source_id
-                    ;
-                    IF NOT FOUND THEN
-                        INSERT INTO rrs.source_status( rrs_id, source_id, last_end_time )
-                            VALUES( v_rrs.rrs_id, v_source.source_id, v_max_end_time )
-                        ;
-                    END IF;
-                END IF;
-            END IF;
-
-            v_total_rows := v_total_rows + v_rows;
+            v_total_rows := v_total_rows + update_source_rrs(v_source.source_id, v_rrs.rrs_id);
             --debug.f(''alert_rrs %s rows added for rrs_id %s'', v_rows, v_rrs.rrs_id);
         END LOOP;
     END LOOP;
@@ -154,6 +55,131 @@
     --debug.f(''alert_rrs exit'');
     PERFORM rrs.update_lock(v_my_oid, 0);
     RETURN v_total_rows;
+END;
+'
+    LANGUAGE plpgsql
+    SECURITY DEFINER
+;
+
+
+
+
+
+
+
+
+
+CREATE OR REPLACE FUNCTION "update_source_rrs"(rrs.source.source_id%TYPE, rrs.rrs.rrs_id%TYPE) RETURNS integer
+    AS '
+DECLARE
+    v_source_id ALIAS FOR $1;
+    v_rrs_id ALIAS FOR $2;
+
+    v_source rrs.source%ROWTYPE;
+    v_rrs rrs.rrs%ROWTYPE;
+
+    v_max_end_time rrs.bucket.end_time%TYPE;
+    v_last_end_time rrs.bucket.end_time%TYPE;
+    v_rows int := 0;
+    v_sql text;
+BEGIN
+    SELECT INTO v_source * FROM source WHERE source_id = v_source_id;
+    SELECT INTO v_rrs * FROM rrs WHERE rrs_id = v_rrs_id;
+
+    SELECT max(end_time)
+        INTO v_max_end_time
+        FROM rrs.bucket
+        WHERE rrs_id = v_rrs.rrs_id
+    ;
+
+    IF v_max_end_time IS NOT NULL THEN
+        SELECT INTO v_last_end_time
+                last_end_time
+            FROM rrs.source_status
+            WHERE rrs_id = v_rrs.rrs_id
+                AND source_id = v_source.source_id
+        ;
+        IF NOT FOUND THEN
+            v_last_end_time := ''1970-01-01''::timestamptz;
+        END IF;
+
+        IF v_last_end_time = v_max_end_time THEN
+            RAISE INFO ''Nothing to do for % rrs_id %, skipping...'', v_source.source_name, v_rrs.rrs_id;
+        ELSE
+            RAISE INFO ''Inserting into % for rrs_id % from % to %'', v_source.source_name, v_rrs.rrs_id, v_last_end_time, v_max_end_time;
+            IF v_rrs.parent IS NULL THEN
+                v_sql :=
+                ''INSERT INTO '' || v_source.insert_table || ''
+                                    ( bucket_id, '' || v_source.group_clause || '', ''
+                                                || v_source.insert_aggregate_fields || '' )
+                    SELECT a.rrs_bucket_id, '' || v_source.group_clause || ''
+                                , '' || v_source.primary_aggregate || ''
+                        FROM
+                            (SELECT b.bucket_id AS rrs_bucket_id, s.*
+                                FROM rrs.bucket b
+                                    JOIN '' || v_source.source_table || '' s
+                                        ON (
+                                            b.prev_end_time  < '' || quote_ident(v_source.source_timestamptz_field) || ''
+                                            AND b.end_time >= '' || quote_ident(v_source.source_timestamptz_field) || ''
+                                            AND '' || quote_ident(v_source.source_timestamptz_field) || '' <= '' || quote_literal(v_max_end_time) || ''
+                                            AND '' || quote_ident(v_source.source_timestamptz_field) || '' > '' || quote_literal(v_last_end_time) || ''
+                                            )
+                                WHERE b.rrs_id = '' || quote_literal(v_rrs.rrs_id) || ''
+                                    AND b.end_time <= '' || quote_literal(v_max_end_time) || ''
+                                    AND b.end_time > '' || quote_literal(v_last_end_time) || ''
+                            ) a
+                        GROUP BY rrs_bucket_id, '' || v_source.group_clause || '';''
+                ;
+            ELSE
+                -- Thanks to dealing with rrs.bucket twice, this query is a bit tricky. We want to look at rrs.bucket
+                -- for the rrs we''re *updating*, so that we know what our ranges are. Then, we want to query the 
+                -- parent data, and group it by the different ranges
+                v_sql := 
+                ''INSERT INTO '' || v_source.insert_table || ''
+                                    ( bucket_id, '' || v_source.group_clause || '', ''
+                                                || v_source.insert_aggregate_fields || '' )
+                    SELECT a.rrs_bucket_id, '' || v_source.group_clause || ''
+                                , '' || v_source.rrs_aggregate || ''
+                        FROM 
+                            -- Wrap this whole thing in a sub-select to avoid field name conflicts
+                            (
+                            SELECT b.bucket_id AS rrs_bucket_id, r.*
+                                FROM '' || v_source.insert_table || '' r
+                                    JOIN rrs.bucket p ON (r.bucket_id = p.bucket_id)
+                                    , rrs.bucket b
+
+                                -- Get just the appropriate buckets for the RRD we are *updating*
+                                WHERE b.rrs_id = '' || quote_literal(v_rrs.rrs_id) || ''
+                                    AND b.end_time <= '' || quote_literal(v_max_end_time) || ''
+                                    AND b.end_time > '' || quote_literal(v_last_end_time) || ''
+
+                                -- Select the parent data but only for the appropriate time slots
+                                    AND p.rrs_id = '' || quote_literal(v_rrs.parent) || ''
+                                    AND p.end_time <= b.end_time
+                                    AND p.end_time > b.prev_end_time
+                            ) a
+                        GROUP BY rrs_bucket_id, '' || v_source.group_clause || '';''
+                ;
+            END IF;
+            RAISE DEBUG ''Executing query: %'', v_sql;
+            EXECUTE v_sql;
+
+            GET DIAGNOSTICS v_rows = ROW_COUNT;
+            RAISE INFO ''% rows inserted'', v_rows;
+
+            UPDATE rrs.source_status
+                SET last_end_time = v_max_end_time
+                WHERE rrs_id = v_rrs.rrs_id
+                    AND source_id = v_source.source_id
+            ;
+            IF NOT FOUND THEN
+                INSERT INTO rrs.source_status( rrs_id, source_id, last_end_time )
+                    VALUES( v_rrs.rrs_id, v_source.source_id, v_max_end_time )
+                ;
+            END IF;
+        END IF;
+    END IF;
+    RETURN v_rows;
 END;
 '
     LANGUAGE plpgsql


More information about the rrs-commit mailing list