+sub _unref_stale ($$$$$) {
+ my ($sync, $docid, $ibx, $xnum, $oidbin) = @_;
+ my $del = $sync->{self}->{oidx}->dbh->prepare_cached(<<'');
+DELETE FROM xref3 WHERE ibx_id = ? AND xnum = ? AND oidbin = ?
+
+ $del->bind_param(1, $ibx->{-ibx_id});
+ $del->bind_param(2, $xnum);
+ $del->bind_param(3, $oidbin, SQL_BLOB);
+ $del->execute;
+ my $xr3 = $sync->{self}->{oidx}->get_xref3($docid, 1);
+ my $idx = $sync->{self}->idx_shard($docid);
+ if (scalar(@$xr3) == 0) { # all gone
+ $sync->{self}->{oidx}->delete_by_num($docid);
+ $sync->{self}->{oidx}->eidxq_del($docid);
+ $idx->ipc_do('xdb_remove', $docid);
+ } else { # enqueue for reindex of remaining messages
+ $idx->ipc_do('remove_eidx_info', $docid, $ibx->eidx_key);
+ $sync->{self}->{oidx}->eidxq_add($docid); # yes, add
+ }
+}
+
+sub _unref_stale_range ($$$) {
+ my ($sync, $ibx, $lt_or_gt) = @_;
+ my $r;
+ my $lim = 10000;
+ do {
+ $r = $sync->{self}->{oidx}->dbh->selectall_arrayref(
+ <<EOS, undef, $ibx->{-ibx_id});
+SELECT docid,xnum,oidbin FROM xref3
+WHERE ibx_id = ? AND xnum $lt_or_gt LIMIT $lim
+EOS
+ return if $sync->{quit};
+ for (@$r) { # hopefully rare, not worth optimizing:
+ my ($docid, $xnum, $oidbin) = @$_;
+ my $hex = unpack('H*', $oidbin);
+ warn("# $xnum:$hex (#$docid): stale\n");
+ _unref_stale($sync, $docid, $ibx, $xnum, $oidbin);
+ }
+ } while (scalar(@$r) == $lim);
+ 1;
+}
+
+sub _reindex_check_ibx ($$$) {