I might've fixed the root cause of #82. Either way, I cleaned up the DB a little bit with this:
begin;
CREATE TABLE mam_message_backup(
-- Message UID (64 bits)
-- A server-assigned UID that MUST be unique within the archive.
id BIGINT NOT NULL,
user_id INT NOT NULL,
-- FromJID used to form a message without looking into stanza.
-- This value will be send to the client "as is".
from_jid varchar(250) NOT NULL,
-- The remote JID that the stanza is to (for an outgoing message) or from (for an incoming message).
-- This field is for sorting and filtering.
remote_bare_jid varchar(250) NOT NULL,
remote_resource varchar(250) NOT NULL,
-- I - incoming, remote_jid is a value from From.
-- O - outgoing, remote_jid is a value from To.
-- Has no meaning for MUC-rooms.
direction mam_direction NOT NULL,
-- Term-encoded message packet
message bytea NOT NULL,
search_body text,
origin_id varchar,
PRIMARY KEY(user_id, id)
);
WITH t1 AS (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY
user_id, from_jid, remote_bare_jid, remote_resource, direction, search_body
ORDER BY
user_id, from_jid, remote_bare_jid, remote_resource, direction, search_body, id
) AS rn
FROM mam_message
where search_body <> ''
), t2 AS (
SELECT
*
FROM t1
where rn > 1 and direction = 'I'
order by search_body, rn
), t3 AS (
delete from mam_message where id in (select id from t2) returning *
)
insert into mam_message_backup
select * from t3;
commit;
Now the situation's like this:
duo_chat=# select count(*), direction
from mam_message
group by from_jid, remote_bare_jid, direction, search_body
having count(*) > 1
order by direction, count desc;
count | direction
-------+-----------
5 | I
5 | I
4 | I
4 | I
3 | I
3 | I
3 | I
3 | I
3 | I
3 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
2 | I
5 | O
4 | O
4 | O
2 | O
2 | O
2 | O
2 | O
2 | O
2 | O
2 | O
2 | O
2 | O
2 | O
(46 rows)
That query once returned 593 rows.
Anywho, I'm gonna let that cook for a little bit and keep monitoring the situation. At some point I should hopefully be able to give it one of these:
begin;
drop mam_message_backup;
commit;