I ended up fixing this by preventing the id clash by adding a huge number to the post id column and then exporting the content.
The queries I ran are
; Update the post ids of all attachments. I choose 1000,000 since I already had more than 100,000 posts.
update wp_posts set id = id + 1000000 where post_type="attachment";
; Update the postmeta fields of all attachments
update wp_postmeta set post_id = post_id + 1000000 where meta_key = '_wp_attachment_metadata';
update wp_postmeta set post_id = post_id + 1000000 where meta_key = '_wp_attachment_image_alt';
update wp_postmeta set post_id = post_id + 1000000 where meta_key = '_wp_attached_file';
; Update the featured image ids
update wp_postmeta set meta_value = meta_value + 1000000 where meta_key = '_thumbnail_id'
; You needs this if you are using featured images for terms. Change the meta key to whatever you are using.
update wp_termmeta set meta_value = meta_value + 1000000 where meta_key = 'featured_image_id' and meta_value > 0;