Show related products by attribute and products that have stock

You were starting on the right track, you do need to look at the _stock_status in the postmeta table but you had a little bit of an error in that you haven't linked in the postmeta table and you had a typo in the line

JOIN {$wpdb->postmeta} terms as as ON pm.meta_key = '_stock_status' AND meta_value = 'instock'

So this line joins in the postmeta table to use

JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = tr.object_id

and then in the where you check the stock status

AND pm.meta_key = '_stock_status' AND meta_value = 'instock'"

Try

$product_ids = $wpdb->get_col( "SELECT DISTINCT tr.object_id
    FROM {$wpdb->prefix}term_relationships as tr
    JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
    JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = tr.object_id
    WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}'
    AND pm.meta_key = '_stock_status' AND meta_value = 'instock'" );

I have tested this and it works for me.