 SET search_path = cforum, pg_catalog;
 
 --
--- Name: count_threads_delete_forum_trigger(); Type: FUNCTION; Schema: cforum; Owner: -
---
-
-CREATE FUNCTION count_threads_delete_forum_trigger() RETURNS trigger
+-- Name: count_messages_delete_trigger(); Type: FUNCTION; Schema: cforum; Owner: -
+--
+
+CREATE FUNCTION count_messages_delete_trigger() RETURNS trigger
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+  INSERT INTO
+    cforum.counter_table (table_name, difference, group_crit)
+  VALUES
+    ('messages', -1, OLD.forum_id);
+
+  RETURN NULL;
+END;
+$$;
+
+
+--
+-- Name: count_messages_insert_forum_trigger(); Type: FUNCTION; Schema: cforum; Owner: -
+--
+
+CREATE FUNCTION count_messages_insert_forum_trigger() RETURNS trigger
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+  INSERT INTO
+    cforum.counter_table (table_name, group_crit, difference)
+    VALUES ('messages', NEW.forum_id, 0);
+
+  RETURN NULL;
+END;
+$$;
+
+
+--
+-- Name: count_messages_insert_trigger(); Type: FUNCTION; Schema: cforum; Owner: -
+--
+
+CREATE FUNCTION count_messages_insert_trigger() RETURNS trigger
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+  INSERT INTO
+    cforum.counter_table (table_name, difference, group_crit)
+  VALUES
+    ('messages', +1, NEW.forum_id);
+
+  RETURN NULL;
+END;
+$$;
+
+
+--
+-- Name: count_messages_truncate_trigger(); Type: FUNCTION; Schema: cforum; Owner: -
+--
+
+CREATE FUNCTION count_messages_truncate_trigger() RETURNS trigger
     LANGUAGE plpgsql
     AS $$
 BEGIN
   DELETE FROM
     cforum.counter_table
   WHERE
-      table_name = 'threads'
-    AND
-      group_crit = OLD.forum_id;
+    table_name = 'messages';
+
+  INSERT INTO cforum.counter_table (table_name, difference, group_crit)
+    SELECT 'messages', 0, forum_id FROM cforum.forums;
 
   RETURN NULL;
 END;

CREATE FUNCTION count_threads_delete_trigger() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE field_val_v BIGINT;
BEGIN
  INSERT INTO
    cforum.counter_table (table_name, difference, group_crit)
  VALUES
    (TG_TABLE_NAME, -1, OLD.forum_id);

  RETURN NULL;
END;

SET search_path = cforum, pg_catalog;

--
-- Name: messages__count_delete_trigger; Type: TRIGGER; Schema: cforum; Owner: -
--

CREATE TRIGGER messages__count_delete_trigger AFTER DELETE ON messages FOR EACH ROW EXECUTE PROCEDURE count_messages_delete_trigger();


--
-- Name: messages__count_insert_forum_trigger; Type: TRIGGER; Schema: cforum; Owner: -
--

CREATE TRIGGER messages__count_insert_forum_trigger AFTER INSERT ON forums FOR EACH STATEMENT EXECUTE PROCEDURE count_messages_insert_forum_trigger();


--
-- Name: messages__count_insert_trigger; Type: TRIGGER; Schema: cforum; Owner: -
--

CREATE TRIGGER messages__count_insert_trigger AFTER INSERT ON messages FOR EACH ROW EXECUTE PROCEDURE count_messages_insert_trigger();


--
-- Name: messages__count_truncate_trigger; Type: TRIGGER; Schema: cforum; Owner: -
--

CREATE TRIGGER messages__count_truncate_trigger AFTER TRUNCATE ON messages FOR EACH STATEMENT EXECUTE PROCEDURE count_messages_truncate_trigger();


--
