How to find if a post with custom_field == X exists?

You can use the WP_Query(); to check that like this:

$my_query = new WP_Query();
$my_query->query(array( 'meta_key' => 'X', 'meta_value' => 'Y'));
if ( $my_query->have_posts() ){

     //it exists

} else {
    //it's not here
}

Hope this helps.