Index: trunk/db.go
===================================================================
--- trunk/db.go	(revision 496)
+++ trunk/db.go	(revision 497)
@@ -110,8 +110,10 @@
 
 type Channel struct {
-	ID       int64
-	Name     string
-	Key      string
-	Detached bool
+	ID   int64
+	Name string
+	Key  string
+
+	Detached              bool
+	DetachedInternalMsgID string
 
 	RelayDetached MessageFilter
@@ -162,4 +164,5 @@
 	key VARCHAR(255),
 	detached INTEGER NOT NULL DEFAULT 0,
+	detached_internal_msgid VARCHAR(255),
 	relay_detached INTEGER NOT NULL DEFAULT 0,
 	reattach_on INTEGER NOT NULL DEFAULT 0,
@@ -246,4 +249,5 @@
 		);
 	`,
+	"ALTER TABLE Channel ADD COLUMN detached_internal_msgid VARCHAR(255)",
 }
 
@@ -547,5 +551,7 @@
 	defer db.lock.RUnlock()
 
-	rows, err := db.db.Query(`SELECT id, name, key, detached, relay_detached, reattach_on, detach_after, detach_on
+	rows, err := db.db.Query(`SELECT
+			id, name, key, detached, detached_internal_msgid,
+			relay_detached, reattach_on, detach_after, detach_on
 		FROM Channel
 		WHERE network = ?`, networkID)
@@ -558,10 +564,11 @@
 	for rows.Next() {
 		var ch Channel
-		var key sql.NullString
+		var key, detachedInternalMsgID sql.NullString
 		var detachAfter int64
-		if err := rows.Scan(&ch.ID, &ch.Name, &key, &ch.Detached, &ch.RelayDetached, &ch.ReattachOn, &detachAfter, &ch.DetachOn); err != nil {
+		if err := rows.Scan(&ch.ID, &ch.Name, &key, &ch.Detached, &detachedInternalMsgID, &ch.RelayDetached, &ch.ReattachOn, &detachAfter, &ch.DetachOn); err != nil {
 			return nil, err
 		}
 		ch.Key = key.String
+		ch.DetachedInternalMsgID = detachedInternalMsgID.String
 		ch.DetachAfter = time.Duration(detachAfter) * time.Second
 		channels = append(channels, ch)
@@ -584,12 +591,12 @@
 	if ch.ID != 0 {
 		_, err = db.db.Exec(`UPDATE Channel
-			SET network = ?, name = ?, key = ?, detached = ?, relay_detached = ?, reattach_on = ?, detach_after = ?, detach_on = ?
+			SET network = ?, name = ?, key = ?, detached = ?, detached_internal_msgid = ?, relay_detached = ?, reattach_on = ?, detach_after = ?, detach_on = ?
 			WHERE id = ?`,
-			networkID, ch.Name, key, ch.Detached, ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn, ch.ID)
+			networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn, ch.ID)
 	} else {
 		var res sql.Result
-		res, err = db.db.Exec(`INSERT INTO Channel(network, name, key, detached, relay_detached, reattach_on, detach_after, detach_on)
+		res, err = db.db.Exec(`INSERT INTO Channel(network, name, key, detached, detached_internal_msgid, relay_detached, reattach_on, detach_after, detach_on)
 			VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
-			networkID, ch.Name, key, ch.Detached, ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn)
+			networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn)
 		if err != nil {
 			return err
Index: trunk/user.go
===================================================================
--- trunk/user.go	(revision 496)
+++ trunk/user.go	(revision 497)
@@ -233,6 +233,17 @@
 		return
 	}
+
+	net.user.logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name)
+
 	ch.Detached = true
-	net.user.logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name)
+
+	if net.user.msgStore != nil {
+		nameCM := net.casemap(ch.Name)
+		lastID, err := net.user.msgStore.LastMsgID(net, nameCM, time.Now())
+		if err != nil {
+			net.user.logger.Printf("failed to get last message ID for channel %q: %v", ch.Name, err)
+		}
+		ch.DetachedInternalMsgID = lastID
+	}
 
 	if net.conn != nil {
@@ -256,6 +267,10 @@
 		return
 	}
+
+	net.user.logger.Printf("network %q: attaching channel %q", net.GetName(), ch.Name)
+
+	detachedMsgID := ch.DetachedInternalMsgID
 	ch.Detached = false
-	net.user.logger.Printf("network %q: attaching channel %q", net.GetName(), ch.Name)
+	ch.DetachedInternalMsgID = ""
 
 	var uch *upstreamChannel
@@ -277,10 +292,7 @@
 		}
 
-		lastDelivered := net.delivered.LoadID(ch.Name, dc.clientName)
-		if lastDelivered == "" {
-			return
-		}
-
-		dc.sendTargetBacklog(net, ch.Name, lastDelivered)
+		if detachedMsgID != "" {
+			dc.sendTargetBacklog(net, ch.Name, detachedMsgID)
+		}
 	})
 }
