     '  END IF;'
     '  RETURN NULL;'
     'END; $$ LANGUAGE plpgsql;' ;
-    
+
+  IF NOT trigger_exists
+  THEN
+    EXECUTE
+      'CREATE TRIGGER ermrest_history AFTER INSERT OR UPDATE OR DELETE ON '
+      || quote_ident(sname) || '.' || quote_ident(tname)
+      || ' FOR EACH ROW EXECUTE PROCEDURE _ermrest_history.' || quote_ident('maintain_' || htname) || '();';
+  END IF;
+
+  -- seal off open history tuples if we missed a delete or update
   EXECUTE
-    'CREATE TRIGGER ermrest_history AFTER INSERT OR UPDATE OR DELETE ON '
-    || quote_ident(sname) || '.' || quote_ident(tname)
-    || ' FOR EACH ROW EXECUTE PROCEDURE _ermrest_history.' || quote_ident('maintain_' || htname) || '();';
-    
+    'UPDATE _ermrest_history.' || quote_ident(htname) || ' h'
+    ' SET during = tstzrange(lower(h.during), COALESCE(s."RMT", now()), ''[)'')'
+    ' FROM ('
+        ' SELECT h2."RID", h2.during, s."RMT"'
+	' FROM _ermrest_history.' || quote_ident(htname) || ' h2'
+        ' LEFT OUTER JOIN ' || quote_ident(sname) || '.' || quote_ident(tname) || ' s'
+	'   ON (h2."RID" = s."RID" AND upper(h2.during) IS NULL)'
+	' WHERE lower(h2.during) < s."RMT"'
+    ' ) s'
+    ' WHERE h."RID" = s."RID" AND h.during = s.during;' ;
+
+  -- replicate latest live data if it's not already there
   EXECUTE
-    'INSERT INTO _ermrest_history.' || quote_ident(htname) || '("RID", during, rowdata)'
+    'INSERT INTO _ermrest_history.' || quote_ident(htname) || '("RID", during, "RMB", rowdata)'
     'SELECT'
     '  t."RID",'
     '  tstzrange(t."RMT", NULL, ''[)''),'
+    '  t."RMB",'
     '  (SELECT jsonb_object_agg(' || CASE WHEN sname = '_ermrest' THEN 'j.k' ELSE 'c."RID"::text' END || ', j.v)'
     '   FROM jsonb_each(to_jsonb(t)) j (k, v)'
     '   JOIN _ermrest.known_columns c ON (j.k = c.column_name AND c.table_rid = ' || quote_literal(table_rid) || ')'
     '   WHERE c.column_name NOT IN (''RID'', ''RMT''))'
-    'FROM ' || quote_ident(sname) || '.' || quote_ident(tname) || ' t';
+    ' FROM ' || quote_ident(sname) || '.' || quote_ident(tname) || ' t'
+    ' LEFT OUTER JOIN _ermrest_history.' || quote_ident(htname) || ' h'
+    '  ON (t."RID" = h."RID" AND h.during = tstzrange(t."RMT", NULL, ''[)''))'
+    ' WHERE h."RID" IS NULL;' ;
 END;
 $func$ LANGUAGE plpgsql;
 

  sname text;
  tname text;
  htname text;
BEGIN
  SELECT s.schema_name, t.table_name INTO sname, tname
  FROM _ermrest.known_tables t

      ' FOR EACH STATEMENT EXECUTE PROCEDURE _ermrest.table_change();' ;
  END IF;

  SELECT s.schema_name, t.table_name, it.table_name IS NOT NULL, tg.trigger_name IS NOT NULL
    INTO sname, tname, htable_exists, trigger_exists
  FROM _ermrest.known_tables t
  JOIN _ermrest.known_schemas s ON (t.schema_rid = s."RID")
  LEFT OUTER JOIN information_schema.tables it
    ON (it.table_schema = '_ermrest_history'
        AND it.table_name = CASE WHEN s.schema_name = '_ermrest' THEN t.table_name ELSE 't' || t."RID" END)
  LEFT OUTER JOIN information_schema.triggers tg
    ON (tg.event_object_schema = s.schema_name
        AND tg.event_object_table = t.table_name
	AND tg.trigger_name = 'ermrest_history'
	AND tg.event_manipulation = 'INSERT') -- ignore UPDATE/DELETE that would duplicate table names...
  WHERE t."RID" = $1
  AND t.table_kind = 'r'
  AND s.schema_name NOT IN ('pg_catalog')
  AND (SELECT True FROM _ermrest.known_columns c WHERE c.table_rid = t."RID" AND c.column_name = 'RID')
  AND (SELECT True FROM _ermrest.known_columns c WHERE c.table_rid = t."RID" AND c.column_name = 'RMT')
  AND (SELECT True FROM _ermrest.known_columns c WHERE c.table_rid = t."RID" AND c.column_name = 'RMB')
