Paolo Cravero wrote:
> Pelletier, Robert wrote:
> > Wouldn't the simplest way to handle that kind of load to use the
> > partitionning fonctionnality of MySQL 5.1?
>
> Excellent shot Robert!

That is indeed an excellent suggestion, thanks Robert!

> So probably Mark will mention this possibility in the documentation rather
> than think of how to implement my suggested feature. No problem.

If one adds a field 'partition_tag' into tables maddr, msgs, msgrcpt
and quarantine like this:

ALTER TABLE msgs       ADD partition_tag integer DEFAULT 0;
ALTER TABLE msgrcpt    ADD partition_tag integer DEFAULT 0;
ALTER TABLE quarantine ADD partition_tag integer DEFAULT 0;
ALTER TABLE maddr      ADD partition_tag integer DEFAULT 0;
ALTER TABLE maddr DROP CONSTRAINT maddr_email_key;
ALTER TABLE maddr ADD  CONSTRAINT maddr_email_key UNIQUE (partition_tag,id);

the patch below (to 2.5.2) can put it to good use.


For the time being, putting an assignment like:
  $sql_partition_tag = 35;
(and changing a value every week or so, and restarting amavisd)
will make it possible to drop old table partitions in a breeze.

It would even be possible to compute $sql_partition_tag on the fly,
or use an expression like: EXTRACT(WEEK FROM now())
in SQL clauses.


--- amavisd.orig        Wed Jun 27 12:43:00 2007
+++ amavisd     Wed Aug 29 03:08:07 2007
@@ -237,5 +237,6 @@
       $X_HEADER_TAG $X_HEADER_LINE $notify_xmailer_header
       $remove_existing_x_scanned_headers $remove_existing_spam_headers
-      %sql_clause %local_delivery_aliases $banned_namepath_re
+      %sql_clause $sql_partition_tag
+      %local_delivery_aliases $banned_namepath_re
       $per_recip_whitelist_sender_lookup_tables
       $per_recip_blacklist_sender_lookup_tables
@@ -860,4 +861,19 @@
   );
 
+  # $sql_partition_tag is a user-specified SQL field value in tables
+  # maddr, msgs, msgrcpt and quarantine, inserted into new record. It is
+  # usually an integer, but depending on a schema may be other data type
+  # e.g. a string. May be used to speed up purging of old records by using
+  # partitioned tables (MySQL 5.1+, PostgreSQL 8.1+). A possible usage can
+  # be a week-of-a-year, or some other slowly changing value, allowing to
+  # quickly drop old table partitions without wasting time on deleting
+  # individual records. Mail addresses in table maddr are self-contained
+  # within a partition tag, which means that the same mail address may
+  # appear in more than one maddr partition (using different 'id's), and
+  # that tables msgs and msgrcpt are guaranteed to reference a maddr.id
+  # within their own partition tag. Too fine a granularity of partition
+  # tags wastes space in table maddr by storing unnecessary duplicates.
+  $sql_partition_tag = 0;
+
   # The SQL select clause to fetch per-recipient policy settings.
   # The %k will be replaced by a comma-separated list of query addresses
@@ -896,10 +912,11 @@
     'sel_wblist' => \$sql_select_white_black_list,
     'sel_adr' =>
-      'SELECT id FROM maddr WHERE email=?',
+      'SELECT id FROM maddr WHERE partition_tag=? AND email=?',
     'ins_adr' =>
-      'INSERT INTO maddr (email, domain) VALUES (?,?)',
+      'INSERT INTO maddr (partition_tag, email, domain) VALUES (?,?,?)',
     'ins_msg' =>
-      'INSERT INTO msgs (mail_id, secret_id, am_id, time_num, time_iso, sid,'.
-      ' policy, client_addr, size, host) VALUES (?,?,?,?,?,?,?,?,?,?)',
+      'INSERT INTO msgs (partition_tag, mail_id, secret_id, am_id,'.
+      ' time_num, time_iso, sid, policy, client_addr, size, host)'.
+      ' VALUES (?,?,?,?,?,?,?,?,?,?,?)',
     'upd_msg' =>
       'UPDATE msgs SET content=?, quar_type=?, quar_loc=?, dsn_sent=?,'.
@@ -907,9 +924,9 @@
       ' WHERE mail_id=?',
     'ins_rcp' =>
-      'INSERT INTO msgrcpt (mail_id, rid,'.
-      ' ds, rs, bl, wl, bspam_level, smtp_resp) VALUES (?,?,?,?,?,?,?,?)',
+      'INSERT INTO msgrcpt (partition_tag, mail_id, rid,'.
+      ' ds, rs, bl, wl, bspam_level, smtp_resp) VALUES (?,?,?,?,?,?,?,?,?)',
     'ins_quar' =>
-      'INSERT INTO quarantine (mail_id, chunk_ind, mail_text)'.
-      ' VALUES (?,?,?)',
+      'INSERT INTO quarantine (partition_tag, mail_id, chunk_ind, mail_text)'.
+      ' VALUES (?,?,?,?)',
     'sel_quar' =>
       'SELECT mail_text FROM quarantine WHERE mail_id=? ORDER BY chunk_ind',
@@ -16058,4 +16075,5 @@
     $naddr =~ s/[^\000-\177]/?/g;  # avoid UTF-8 SQL troubles, only need 7 bits
   }
+  my($partition_tag) = c('sql_partition_tag');
   my($conn_h) = $self->{conn_h}; my($sql_cl_r) = cr('sql_clause');
   my($sel_adr) = $sql_cl_r->{'sel_adr'};
@@ -16066,5 +16084,5 @@
   } else {
     $conn_h->begin_work_nontransaction;  #(re)connect if necessary, autocommit
-    $conn_h->execute($sel_adr,$naddr);
+    $conn_h->execute($sel_adr,$partition_tag,$naddr);
     my($a_ref,$a2_ref);
     if (defined($a_ref=$conn_h->fetchrow_arrayref($sel_adr))) {  # exists?
@@ -16081,5 +16099,5 @@
       $conn_h->begin_work_nontransaction; # (re)connect if not connected
       my($eval_stat);
-      eval { $conn_h->execute($ins_adr,$naddr,$invdomain); 1 }
+      eval { $conn_h->execute($ins_adr,$partition_tag,$naddr,$invdomain); 1 }
         or do { $eval_stat = $@ ne '' ? $@ : "errno=$!" };
       # INSERT may have failed because of race condition with other processes;
@@ -16087,5 +16105,5 @@
       # SELECT after INSERT also avoids the need for a working last_insert_id()
       $conn_h->begin_work_nontransaction; # (re)connect if not connected
-      $conn_h->execute($sel_adr,$naddr);  # try select again
+      $conn_h->execute($sel_adr,$partition_tag,$naddr);  # try select again
       if ( defined($a2_ref=$conn_h->fetchrow_arrayref($sel_adr)) ) {
         $id = $a2_ref->[0]; $conn_h->finish($sel_adr);
@@ -16224,6 +16242,6 @@
       # insert a placeholder message record with sender information
       $conn_h->execute($ins_msg,
-        $msginfo->mail_id, $msginfo->secret_id, $msginfo->log_id,
-        $msginfo->rx_time, $time_iso,
+        c('sql_partition_tag'), $msginfo->mail_id,
+        $msginfo->secret_id, $msginfo->log_id, $msginfo->rx_time, $time_iso,
         untaint($sid), c('policy_bank_path'), untaint($msginfo->client_addr),
         0+untaint($msginfo->msg_size), untaint(substr(c('myhostname'),0,255)));
@@ -16277,5 +16295,5 @@
         $resp =~ s/[^\000-\177]/?/gs;  # just in case, only need 7 bits
         $conn_h->execute($ins_rcp,
-          $mail_id, untaint($rid),
+          c('sql_partition_tag'), $mail_id, untaint($rid),
         # $msginfo->rx_time,
           substr($d,0,1), ' ',
@@ -16391,5 +16409,6 @@
 
 sub open {
-  my($self) = shift; @$self{qw(conn_h clause dbkey mode maxbuf rx_time)} = @_;
+  my($self) = shift;
+  @$self{qw(conn_h clause dbkey mode partition_tag maxbuf rx_time)} = @_;
   my($conn_h) = $self->{conn_h}; $self->{buf} = '';
   $self->{chunk_ind} = $self->{pos} = $self->{bufpos} = $self->{eof} = 0;
@@ -16399,6 +16418,7 @@
   die $@  if $@ =~ /^timed out\b/;  # resignal timeout
   if ($self->{mode} eq 'w') {
-    ll(4) && do_log(4,"Amavis::IO::SQL::open %s drv=%s (%s); key=%s: %s",
-                    $self->{mode}, $driver, $self->{clause}, $self->{dbkey});
+    ll(4) && do_log(4,"Amavis::IO::SQL::open %s drv=%s (%s); key=%s, p_tag=%s",
+                    $self->{mode}, $driver, $self->{clause},
+                    $self->{dbkey}, $self->{partition_tag});
   } else {
     eval { $conn_h->execute($self->{clause}, $self->{dbkey});  1 }
@@ -16548,10 +16568,11 @@
   while (length($self->{buf}) > 0) {
     my($ind) = $self->{chunk_ind} + 1;
-    ll(4) && do_log(4, "sql flush: key: (%s, %d), rx_time=%d, size=%d",
-                 $self->{dbkey}, $ind, $self->{rx_time},
-                 min(length($self->{buf}),$self->{maxbuf}));
+    ll(4) && do_log(4, "sql flush: key: (%s, %d), p_tag=%s, rx_t=%d, size=%d",
+                $self->{dbkey}, $ind, $self->{partition_tag}, $self->{rx_time},
+                min(length($self->{buf}),$self->{maxbuf}));
     eval {
       my($driver) = $conn_h->driver_name;
-      $conn_h->execute($self->{clause}, $self->{dbkey}, $ind,
+      $conn_h->execute($self->{clause},
+                       $self->{partition_tag}, $self->{dbkey}, $ind,
                      # $self->{rx_time},
                        [ untaint(substr($self->{buf},0,$self->{maxbuf})),
@@ -16588,9 +16609,11 @@
     while (length($self->{buf}) >= $self->{maxbuf}) {
       my($ind) = $self->{chunk_ind} + 1;
-      ll(4) && do_log(4, "sql print: key: (%s, %d), size=%d",
-                         $self->{dbkey}, $ind, $self->{maxbuf});
+      ll(4) && do_log(4, "sql print: key: (%s, %d), p_tag=%s, size=%d",
+                         $self->{dbkey}, $ind,
+                         $self->{partition_tag}, $self->{maxbuf});
       eval {
         my($driver) = $conn_h->driver_name;
-        $conn_h->execute($self->{clause}, $self->{dbkey}, $ind,
+        $conn_h->execute($self->{clause},
+                         $self->{partition_tag}, $self->{dbkey}, $ind,
                        # $self->{rx_time},
                          [ untaint(substr($self->{buf},0,$self->{maxbuf})),
@@ -16669,5 +16692,6 @@
       my($mp) = Amavis::IO::SQL->new;
       $mp->open($conn_h, $sql_cl_r->{'ins_quar'}, $msginfo->mail_id, 'w',
-                $sql_quarantine_chunksize_max, $msginfo->rx_time)
+                c('sql_partition_tag'), $sql_quarantine_chunksize_max,
+                $msginfo->rx_time)
         or die "Can't open Amavis::IO::SQL object: $!";
       my($hdr_edits) = $msginfo->header_edits;




Mark

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
AMaViS-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/amavis-user
AMaViS-FAQ:http://www.amavis.org/amavis-faq.php3
AMaViS-HowTos:http://www.amavis.org/howto/

Reply via email to