diff -r 0244f1344b04 -r 821d3ffaaaca pgsql/create_tables-dovecot-1.2.x.pgsql --- a/pgsql/create_tables-dovecot-1.2.x.pgsql Tue Apr 10 00:33:53 2012 +0200 +++ b/pgsql/create_tables-dovecot-1.2.x.pgsql Tue Apr 10 00:50:55 2012 +0200 @@ -599,13 +599,30 @@ RETURN NEXT record; END LOOP; IF NOT FOUND THEN + -- First, we have to check existing mailboxes and + -- return identity if one exists FOR record IN - SELECT recipient, destination - FROM catchall + SELECT recipient, recipient as destination + FROM users WHERE gid = did + AND local_part = localpart + UNION SELECT recipient, recipient as destination + FROM relocated + WHERE gid = did + AND address = localpart LOOP RETURN NEXT record; END LOOP; + -- Only now can we think about catchalls... + IF NOT FOUND THEN + FOR record IN + SELECT recipient, destination + FROM catchall + WHERE gid = did + LOOP + RETURN NEXT record; + END LOOP; + END IF; END IF; RETURN; END;