diff -ur pdns-2.9.16.orig/modules/gmysqlbackend/gmysqlbackend.cc pdns-2.9.16/modules/gmysqlbackend/gmysqlbackend.cc
--- pdns-2.9.16.orig/modules/gmysqlbackend/gmysqlbackend.cc	Sun Feb  1 13:20:16 2004
+++ pdns-2.9.16/modules/gmysqlbackend/gmysqlbackend.cc	Fri Jun  4 21:42:35 2004
@@ -74,6 +74,7 @@
     declare(suffix,"update-lastcheck-query","", "update domains set last_check=%d where id=%d");
     declare(suffix,"info-all-master-query","", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'");
     declare(suffix,"delete-zone-query","", "delete from records where domain_id=%d");
+    declare(suffix,"zone-serial-query","", "select max(change_date) from records where domain_id=%d");
 
 
   }
diff -ur pdns-2.9.16.orig/modules/gpgsqlbackend/gpgsqlbackend.cc pdns-2.9.16/modules/gpgsqlbackend/gpgsqlbackend.cc
--- pdns-2.9.16.orig/modules/gpgsqlbackend/gpgsqlbackend.cc	Fri Jan 16 14:53:45 2004
+++ pdns-2.9.16/modules/gpgsqlbackend/gpgsqlbackend.cc	Fri Jun  4 21:42:35 2004
@@ -73,6 +73,7 @@
     declare(suffix,"update-lastcheck-query","", "update domains set last_check=%d where id=%d");
     declare(suffix,"info-all-master-query","", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'");
     declare(suffix,"delete-zone-query","", "delete from records where domain_id=%d");
+    declare(suffix,"zone-serial-query","", "select max(change_date) from records where domain_id=%d");
 
 
   }
diff -ur pdns-2.9.16.orig/modules/gsqlitebackend/gsqlitebackend.cc pdns-2.9.16/modules/gsqlitebackend/gsqlitebackend.cc
--- pdns-2.9.16.orig/modules/gsqlitebackend/gsqlitebackend.cc	Thu Jan 29 15:27:42 2004
+++ pdns-2.9.16/modules/gsqlitebackend/gsqlitebackend.cc	Fri Jun  4 21:43:45 2004
@@ -75,6 +75,7 @@
     declare( suffix, "update-lastcheck-query", "", "update domains set last_check=%d where id=%d");
     declare( suffix, "info-all-master-query", "", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'");
     declare( suffix, "delete-zone-query", "", "delete from records where domain_id=%d");
+    declare( suffix, "zone-serial-query", "", "select max(change_date) from records where domain_id=%d");
   }
   
   //! Constructs a new gSQLiteBackend object.
diff -ur pdns-2.9.16.orig/pdns/backends/gsql/gsqlbackend.cc pdns-2.9.16/pdns/backends/gsql/gsqlbackend.cc
--- pdns-2.9.16.orig/pdns/backends/gsql/gsqlbackend.cc	Fri Jan 16 17:18:12 2004
+++ pdns-2.9.16/pdns/backends/gsql/gsqlbackend.cc	Fri Jun  4 21:42:35 2004
@@ -238,6 +238,7 @@
   d_UpdateLastCheckofZoneQuery=getArg("update-lastcheck-query");
   d_InfoOfAllMasterDomainsQuery=getArg("info-all-master-query");
   d_DeleteZoneQuery=getArg("delete-zone-query");
+  d_ZoneSerialQuery=getArg("zone-serial-query");
 }
 
 
@@ -435,3 +436,68 @@
   return true;
 }
 
+// Implementing this as an alternative to my previous tactic, as suggested
+// by Bert. This would be easier if I could just call the superclass method...
+bool GSQLBackend::getSOA(const string &domain, SOAData &sd, DNSPacket *p)
+{
+  this->lookup(QType(QType::SOA),domain,0);
+
+  DNSResourceRecord rr;
+
+  int hits=0;
+
+  while (this->get(rr)) {
+    hits++;
+    DNSPacket::fillSOAData(rr.content, sd);
+    sd.domain_id=rr.domain_id;
+    sd.ttl=rr.ttl;
+  }
+
+  if (!hits)
+    return false;
+
+  if (sd.nameserver.empty())
+    sd.nameserver=arg()["default-soa-name"];
+
+  if(sd.hostmaster.empty())
+    sd.hostmaster="hostmaster."+domain;
+
+  if (!sd.serial) { // try to get the serial number automagically
+    char output[1024];
+
+    // We do this by asking the backend what the max serial number is among
+    // all the records it knows for the domain we're looking at.
+    snprintf(output, sizeof(output)-1, d_ZoneSerialQuery.c_str(), sd.domain_id);
+
+    try {
+      d_db->doQuery(output, d_result);
+    } catch (SSqlException &e) {
+      throw AhuException("Database error trying to query serial for domain_id "+itoa(sd.domain_id)+": "+e.txtReason());
+    }
+
+    if (!d_result.empty())
+      sd.serial = atoi(d_result[0][0].c_str());
+  }
+
+  if (!sd.serial) { // fall back to the other way, if we still have nothing
+    DLOG(L<<Logger::Warning<<"Doing soa serialnumber autocalculation for "<<rr.qname<<endl);
+
+    // we do this by listing the domain and taking the maximum last modified timestamp
+    DNSResourceRecord i;
+    time_t newest=0;
+
+    if(!(this->list(domain, sd.domain_id))) 
+      throw AhuException("Backend error trying to determine magic serial number of zone '"+domain+"'");
+  
+    while(this->get(i)) {
+      if(i.last_modified>newest)
+	newest=i.last_modified;
+    }
+
+    sd.serial=newest; // +arg().asNum("soa-serial-offset");
+    DLOG(L<<"autocalculated soa serialnumber for "<<rr.qname<<" is "<<newest<<endl);
+  }
+
+  sd.db = this;
+  return true;
+}
diff -ur pdns-2.9.16.orig/pdns/backends/gsql/gsqlbackend.hh pdns-2.9.16/pdns/backends/gsql/gsqlbackend.hh
--- pdns-2.9.16.orig/pdns/backends/gsql/gsqlbackend.hh	Sat Aug 23 10:35:35 2003
+++ pdns-2.9.16/pdns/backends/gsql/gsqlbackend.hh	Fri Jun  4 21:42:35 2004
@@ -38,6 +38,7 @@
   void getUpdatedMasters(vector<DomainInfo> *updatedDomains);
   bool getDomainInfo(const string &domain, DomainInfo &di);
   void setNotified(u_int32_t domain_id, u_int32_t serial);
+  bool GSQLBackend::getSOA(const string &domain, SOAData &sd, DNSPacket *p=0);
 private:
   string d_qname;
   QType d_qtype;
@@ -66,5 +67,6 @@
   string d_UpdateLastCheckofZoneQuery;
   string d_InfoOfAllMasterDomainsQuery;
   string d_DeleteZoneQuery;		
+  string d_ZoneSerialQuery;
 
 };
