--- comment_subscribe/comment_subscribe.module.orig	2008-12-27 21:55:24.000000000 +0300
+++ comment_subscribe/comment_subscribe.module	2008-12-27 22:34:08.000000000 +0300
@@ -101,6 +101,14 @@
 function comment_subscribe_nodeapi(&$node, $op, $teaser, $page) {
   
   switch ( $op ) {
+  case 'delete':
+      $sql = "DELETE FROM {z_nodesubscribe} WHERE nid=%d";
+     db_query($sql, $node->nid);
+     break;
+  case 'update':
+      $sql = "DELETE FROM {z_nodesubscribe} WHERE nid=%d";
+     db_query($sql, $node->nid);
+      /* no break here! */
      case 'insert':
       $sql = "INSERT INTO {z_nodesubscribe} (nid, uid, nodesubscribe) 
       values(%d, %d, %d)";    
@@ -275,8 +283,15 @@
 			}
        break;
     case 'delete':
+        if ( $GLOBALS['db_type']=='pgsql')
+            {
+                        $sql = "DELETE FROM {z_commentsubscribe} WHERE cid = %d or parents ~ '%s,' ";
+            }
+        else
+            {
 			$sql = "DELETE FROM {z_commentsubscribe} WHERE cid = %d or 
 			instr(parents, '%s,') ";		
+            }
 			db_query($sql, $comment->cid,$comment->cid);
       break;
   }
@@ -308,12 +323,24 @@
   $commtext= $comment->comment;
   $commsubj= $comment->subject;
   //Get the parents and mailid of a particular comment
+    if ( $GLOBALS['db_type']=='pgsql') 
+        {
+  $parentrslt = db_fetch_object(db_query("SELECT parents, 
+  (case when length(c.mail)<1 then coalesce (u.mail,u.init) else c.mail end) AS mail
+  FROM {z_commentsubscribe} zc 
+  LEFT JOIN {comments} c on zc.cid = c.cid
+  LEFT JOIN {users} u on u.uid = c.uid
+  WHERE zc.cid = %d", $cid));
+        }
+    else
+        {
   $parentrslt = db_fetch_object(db_query("SELECT parents, 
   IF(length(c.mail)<1, ifnull(u.mail,u.init),
   c.mail) mail FROM {z_commentsubscribe} zc 
   LEFT JOIN {comments} c on zc.cid = c.cid
   LEFT JOIN {users} u on u.uid = c.uid
   WHERE zc.cid = %d", $cid));
+        }
   if($parentrslt->parents != NULL) {
   	$arrparents =explode(',',$parentrslt->parents);
     $parents .= $arrparents[0];
@@ -328,7 +355,7 @@
     //or node comments.
     if ( $GLOBALS['db_type']=='pgsql') {
       $result = db_query(
-             "SELECT DISTINCT c.cid,u.init,c.uid,c.name,c.nid, ".
+             "SELECT DISTINCT on (mail) c.cid,u.init,c.uid,c.name,c.nid, ".
              "  (case when length(c.mail)<1 then coalesce (u.mail,u.init) else c.mail end) AS mail ".
              " FROM {comments} c LEFT OUTER JOIN {users} u ON u.uid=c.uid ".
            " LEFT JOIN {z_commentsubscribe} zc on zc.cid=c.cid ".
@@ -410,7 +437,7 @@
     }
     //Sending mail to author of the post, if he subscribed comments to the node
     if ( $GLOBALS['db_type']=='pgsql') {
-      $authorid = db_result(db_query("SELECT  coalesce ( u.mail, u.init )
+      $authorid = db_result(db_query("SELECT  coalesce ( u.mail, u.init ) as
       mail FROM {users} u LEFT JOIN {node} n ON u.uid = n.uid 
       LEFT JOIN {z_nodesubscribe} z ON z.nid = n.nid 
       WHERE n.nid = %d AND z.nodesubscribe=1 AND mail <> '%s' ", $nid, $currmailid));
@@ -534,6 +561,46 @@
 /*
  * Function hook_enable
  */
+
+function comment_subscribe_enable_pgsql() {
+    //If comment_subscribe module is enabled in a live site.
+  //For previous comments an entry in z_commentsubscribe is 
+  //generated using the below code.
+    $level = 1;
+    $ifcondition = "(case when c".$level.".pid <> 0 then (c".$level.".pid::text || ',') else '' end)";
+    $table = "c".$level;
+    $join =null;
+    //First find the level of comments
+    do {
+        if($level != 1) {
+            $prvlevel = $level - 1;
+            $join .= " join comments c".$level." on c".$prvlevel.".pid=c".$level.".cid";
+        }
+        $sql = "select c1.cid, '0,' || ".$ifcondition." as parents from comments c1  ".$join;
+        $result = db_fetch_array(db_query($sql));
+            if($result != NULL ) {
+                $origsql = $sql;
+                $origjoin = $join;
+                $origifcond = $ifcondition;
+            } 
+            else
+                {
+                    // nothing found at first pass, so nothing to add to table
+                    if($level==1) return;
+                }
+            $level++;
+            $table = "c".$level;
+            $ifcondition = " (case when c".$level.".pid <> 0 then (c".$level.".pid:: text || ',') else '' end) || ".$ifcondition;
+        }while($result != NULL );
+        
+        $sql = str_replace('join','left join',$origsql);
+	$sql = str_replace('as parents','as parents,0 as subscribe,0 as subscribenode',$sql);
+        $sql = "insert into z_commentsubscribe (cid, parents, subscribe, subscribenode) ".$sql."
+	left join z_commentsubscribe cs on c1.cid = cs.cid where cs.subscribe is null";
+	db_query($sql);
+}
+
+
 function comment_subscribe_enable() {
   
   $num_rows_result = db_fetch_object(db_query("SELECT count(*) AS num FROM {comments} c"));
@@ -542,6 +609,12 @@
     //If comment_subscribe module is enabled in a live site.
     //For previous comments an entry in z_commentsubscribe is 
     //generated using the below code.
+    if ( $GLOBALS['db_type']=='pgsql')
+        {
+         comment_subscribe_enable_pgsql();
+        }
+    else
+        {
     $level = 1;
     $ifcondition = "if(c". $level .".pid <> 0, concat(c". $level .".pid, ','),'')";
     $table = "c". $level;
@@ -570,7 +643,8 @@
     $sql = "INSERT INTO {z_commentsubscribe} (cid, parents, subscribe, subscribenode) ". $sql ."
     left join {z_commentsubscribe} cs on c1.cid = cs.cid where cs.subscribe is NULL";
     db_query($sql);
+        }
     //Insert previous node informations in to the z_nodesubscribe table.
-    db_query("INSERT INTO z_nodesubscribe (nid, uid) select nid,uid from node");
   }
+  db_query("INSERT INTO z_nodesubscribe (nid, uid) select nid,uid from node");
  }
